var blnAjax;
var blnNextKey;
var pleasementId;

function initIdeeen(){
	$('#frmGreen').submit(handleSubmit);
	$('#frmBlue').submit(handleSubmit);

	blnAjax	= false;
	blnNextKey = false;

	$('input.search').keyup(handleKeyUp);

	$('input.search').focus(function(){
		if($(this).val() == $(this).attr('defaultValue')) {
			$(this).val('');
		}
	});

	$('input.search').blur(function(){
		if($(this).val() == '') {
			$(this).val($(this).attr('defaultValue'));
		}
	});

	activateMediaItems();
	activateTextItems();
	var v = $('#greenBoxFlashContent2').html();
	if(v != ''){
		showGreenBox(v);
	}

	var t = $('#blueBoxFlashContent2').html();
	if(t != ''){
		showBlueBox(t);
	}

	$('#popupbg').click(closePopup);
	$('#gclose').click(closePopup);
	$('#bclose').click(closePopup);

}

function activateMediaItems(){

	$('.video-box a.handler').each(function(i){
		$(this).removeAttr('target');
		$(this).attr('href', 'javascript://');
		$(this).click(doGreenBoxClick);
	});
}

function activateTextItems(){

	$('.comments-box a.handler').each(function(i){
		$(this).removeAttr('target');
		$(this).attr('href', 'javascript://');
		$(this).click(function(){
			//alert($(this).attr('id').replace('key_',''));
			
			showBlueBox($(this).attr('id').replace('key_',''), true);
			doTabsBox('blue', $('#bdoorsturen'));
		});

	});
}

function handleSubmit(){
	return false;
}


function handleKeyUp(){
	//if(!blnAjax && $(this).val() != '' && $(this).val() != $(this).attr('defaultValue')){
	if($(this).val() != $(this).attr('defaultValue')){
		blnAjax = true;
		pleasementId = $(this.form).attr('id');
		$('#' + pleasementId + 'Paginate').slideUp(250); //css({display:'none'});
		$(this).attr('defaultValue', $(this).val());
		//alert($(this.form).attr('action'));
		$.post($(this.form).attr('action') + 'js/', $(this.form).serializeArray( ), handlePostCallback, "html");
	}else{
		blnNextKey = true;
	}
}

function handlePostCallback(msg){
	blnAjax = false;
	$('#' + pleasementId + 'Data').html(msg);

	if(pleasementId == 'frmGreen'){
		activateMediaItems();
		activateMediaStemmen();
	}
	if(pleasementId == 'frmBlue'){
		activateTextItems();
		activateTextStemmen();
	}

	if(blnNextKey){
		blnNextKey = false;
		handleKeyUp();
	}
}

function showPopup(blnAnimate, flashvars, flashparams, blnBlue){
	//alert($(this).attr('id'));
	//blnAnimate = false;
	
	$('#popupbg').css({opacity:0, display:'block'}).height($(document).height());
	if(blnAnimate){
		$('#popupbg').animate({opacity:0.75}, 250);
	}else{
		$('#popupbg').css({opacity:0.75});
	}/**/
	$('#popup').css({display:'block', top:($(window).height() - 410) /2, left:($(document).width() - $('#popup').width()) /2 })
	/*opacity:0, 
	if(blnAnimate){
		$('#popup').animate({opacity:1}, 250);
	}else{
		$('#popup').css({opacity:1});
	}*/
	if(blnBlue){
		swfobject.embedSWF("/swf/reader.swf", "blueBoxFlashContent2", "480", "282", "9.0.0", "", flashvars, flashparams, "");
	}else{
		swfobject.embedSWF("/swf/player.swf", "greenBoxFlashContent2", "480", "330", "9.0.0", "", flashvars, flashparams, "") 
	}
}

function doBlueBoxClick(){
	showBlueBox($(this).attr('id').replace('key_',''), true);
}
function doGreenBoxClick(){
	showGreenBox($(this).attr('id').replace('key_',''), true);
}
function showGreenBox(inkey, blnAnimate){
	var flashvars = {
		key: inkey
	};
	if(!blnAnimate){
		blnAnimate = false;
	}else{
		blnAnimate = true;
		//registerGreen(inkey); deze word door de flash aangeroepen
	}
	$('.green-box').css({display: 'block'});
	$('#gbekijken').trigger('click');
	$('#greenBoxFlash2').html('<div id="greenBoxFlashContent2"></div>');

    var flashparams = {
		allowScriptAccess: "always",
		wMode: "transparent"
	}
	
	showPopup(blnAnimate, flashvars, flashparams, false);
}

function showBlueBox(inkey, blnAnimate){
	var flashvars = {
		key: inkey
	};
	if(!blnAnimate){
		blnAnimate = false;
	}else{
		blnAnimate = true;
		registerBlue(inkey);
	}
	$('.blue-box').css({display: 'block'});
	$('#bbekijken').trigger('click');
	$('#blueBoxFlash2').html('<div id="blueBoxFlashContent2"></div>');

    var flashparams = {
		allowScriptAccess: "always",
		wMode: "transparent"
	}

	//swfobject.embedSWF("/swf/reader.swf", "blueBoxFlashContent2", "480", "282", "9.0.0", "", flashvars, flashparams, "");
	showPopup(blnAnimate, flashvars, flashparams, true);
}

function closePopup(){
	$('#popupbg').animate({opacity:0}, 250, function(){
		$('#popupbg').css({opacity:0, display:'none'});
	});
/**/
	//$('#popup').animate({opacity:0}, 250, function(){
		$('#popup').css({display:'none'});//opacity:0, 
		$('.blue-box').css({display: 'none'});
		$('.green-box').css({display: 'none'});
	//});
}

$(document).ready(initIdeeen);