var eraSelected;
var isIE = navigator.appName.indexOf("Microsoft") != -1;
var isIE6 = navigator.appVersion.indexOf("MSIE 6.0") != -1;
var aUrl = document.location.href.split("index.cfm");
var baseUrl = aUrl[0];

$(document).ready(function(){
	
	// Right menu for E-Cards
	if ( $("#ecardcontent").length > 0 ) {
		
		if ( $("#ecardcontent").children().size() == 0 ){
			// fill the div with the first menu article
			//$("#main #ecardcontent").html( $("#itembanner .item .hidden").html() );
			//$("#itembanner .item a span:first").addClass('current');
			//redirect page to first URL
			if(typeof( window[ 'adminroot' ] ) == "undefined"){
				top.location=$("#itembanner .item a").attr("href");
			}
		}
	
	// Agenda right menu
	}else if ( $("#agendacalendar").length > 0 ){
	
		// fill the div with the first menu article(if there is one)
		if($(".calendar td .hidden").length > 0){
			$("#main #feature").html( $(".calendar td .hidden").html() );
			$(".calendar td a:first").addClass('current');
			
			// Show article onclick
			$(".calendar td a").mouseover(function(){
				$(".calendar td a").removeClass('current');
				$("#main #feature").html( $(this).next('div').html() );
				$(this).addClass('current');
			});
		}
	
	// Other right menu's'
	}else if ( $("#feature").length > 0 ){
		$(".close").click(function () {
			$("#overlay").hide();
			$("#OverlayContent").hide();
		});
		  
		$("#overlay").click(function () {
		    $("#overlay").hide();
		    $("#OverlayContent").hide();
		});
	
		// fill the div with the first menu article (if there is one)
		if($("#itembanner .item .hidden").length > 0){
			$( 'a.iframe' ).each( function() {
				var url = $(this).attr('href');
				$(this).attr( 'href', '#' );
				$(this).attr( 'ref', url );
			} );
			
			$("#main #feature").html( $("#itembanner .item .hidden").html() );
			$("#itembanner .item a span:first").addClass('current');
			if ( $("a.iframe").length > 0 ){
				//$("a.iframe").fancybox({ 'hideOnContentClick': false, 'frameWidth': 1000, 'frameHeight': 680 });
				$("a.iframe").click(function () {
				    $("#overlay").show().css("height",$(window).height());
					$("#overlay").css("width",$(window).width());
					$(".anno_iframe").attr( 'src', $(this).attr('ref') );
				    $("#OverlayContent").show();
				});
			}
			
			// Show article onclick
			$("#itembanner .item a").mouseover(function(){
				$("#itembanner .item a span").removeClass('current');
				$("#main #feature").html( $(this).next('div').html() );
				$(this).children("span:first").addClass('current');
				if ( $("a.iframe").length > 0 ){
					//$("a.iframe").fancybox({ 'hideOnContentClick': false, 'frameWidth': 1000, 'frameHeight': 680 });
					$("a.iframe").click(function () {
					    $("#overlay").show().css("height",$(window).height());
						$("#overlay").css("width",$(window).width());
						$(".anno_iframe").attr( 'src', $(this).attr('ref') );
					    $("#OverlayContent").show();
					});
				}
			});
		}
	
	// Other right menus
	}else{
	
		if($("#itembanner .item .hidden").length > 0){
			// fill the div with the first menu article(if there is one)
			$("#main #feature").html( $("#itembanner .item .hidden").html() );
			$("#itembanner .item a span:first").addClass('current');
			if ( $("a.iframe").length > 0 ){
				$("a.iframe").fancybox({ 'hideOnContentClick': false, 'frameWidth': 1000, 'frameHeight': 680 });
			}
			
			// Show article onclick
			$("#itembanner .item a").mouseover(function(){
				$("#itembanner .item a span").removeClass('current');
				$("#main #feature").html( $(this).next('div').html() );
				$(this).children("span:first").addClass('current');
				if ( $("a.iframe").length > 0 ){
					$("a.iframe").fancybox({ 'hideOnContentClick': false, 'frameWidth': 1000, 'frameHeight': 680 });
				}
			});
		}
	}
	
	$("#ecardForm #ecard_preview").click(function(){
		$("#ecardForm #action").val("preview");
		$("#ecardForm").submit();
	});
	
	$("#ecardForm").validate({
		//set the rules for the field names
		rules: {
			name: { required: true, minlength: 2 },
			email: { required: true, email: true },
			friendname: { required: true, minlength: 2 },
			friendemail: { required: true, email: true }
		},
		//set messages to appear inline
		messages: {
			name: "Vul je naam in.",
			email: "Vul je e-mailadres in.",
			friendname: "Vul de naam van een vriend in.",
			friendemail: "Vul het e-mailadres van een vriend in."
		}
	});
	
	if ( $("#reactionForm").length > 0 ) {
		$("#reactionForm #newHash").click(function(){
			$.get(baseUrl + "/newHash.cfm?pageid=" + $(this).attr('name'),function(txt){
				aStrings = txt.split("^");
				$("#reactionForm #checker").val(aStrings[1]);
				$("#reactionForm #secureImg").attr("src",baseUrl + "/secure_image.cfm?objectid=" + aStrings[0]);
			});
		});
		
		$("#reactionForm #reactionSubmit").click(function(){
			if($("#reactionForm #address").val().toLowerCase()==$("#reactionForm #checker").val().toLowerCase()){
				$("#reactionForm").submit();
			}else{
				$("#reactionForm #controle").show();
			}
		});
		
		$("#reactionForm").validate({
			//set the rules for the field names
			rules: {
				label: { required: true, minlength: 2 },
				email: { required: true, email: true },
				reactie: { required: true, minlength: 2 }
			},
			//set messages to appear inline
			messages: {
				label: "Vul je naam in.",
				email: "Vul je e-mailadres in.",
				reactie: "Vul een reactie in."
			}
		});
	}
	
	$("#friendForm").validate({
		//set the rules for the field names
		rules: {
			name: { required: true, minlength: 2 },
			email: { required: true, email: true },
			friendname: { required: true, minlength: 2 },
			friendemail: { required: true, email: true }
		},
		//set messages to appear inline
		messages: {
			name: "Vul je naam in.",
			email: "Vul je e-mailadres in.",
			friendname: "Vul de naam van een vriend in.",
			friendemail: "Vul het e-mailadres van een vriend in."
		}
	});
	
	if ( $("#newyearForm").length > 0 ) {
	
		$("#newyearForm #newHash").click(function(){
			$.get(baseUrl + "/newHash.cfm?pageid=" + $(this).attr('name'),function(txt){
				aStrings = txt.split("^");
				$("#newyearForm #checker").val(aStrings[1]);
				$("#newyearForm #secureImg").attr("src",baseUrl + "/secure_image.cfm?objectid=" + aStrings[0]);
			});
		});
		
		$("#newyearForm #description").keyup(function(){
			$("#newyearForm #description_error").hide();
			inhoud = $("#newyearForm #description").val();
			aContent = inhoud.split(' ');
			if (aContent instanceof Array && aContent.length > 149){
				// Remove new character
				$("#newyearForm #description_error").show();
			}
		});
		$("#newyearForm #description").change(function(){
			$("#newyearForm #description_error").hide();
			inhoud = $("#newyearForm #description").val();
			aContent = inhoud.split(' ');
			if (aContent instanceof Array && aContent.length > 149){
				// Remove new character
				$("#newyearForm #description_error").show();
			}
		});
		
		$("#newyearForm #newyearSubmit").click(function(){
			//
			var submitForm = true;
			var fileError = false;
			var filename = $("#newyearForm #photo").val().toLowerCase();
			var dot = filename.lastIndexOf(".");
			var extension = filename.substr(dot,filename.length);
			var extensionList = '.jpg,.jpeg,.png,.gif';
			
			$("#newyearForm #photo_error").hide();
			$("#newyearForm #controle").hide();
			$("#newyearForm #description_error").hide();
			
			if(filename.length == 0) fileError = true;
			if(dot == -1) fileError = true;
			if(extension.length == 0) fileError = true;
			if(extensionList.lastIndexOf(extension) == -1) fileError = true;
			if(fileError){
				submitForm = false;
				$("#newyearForm #photo_error").show();
			}
			// Check captcha
			if($("#newyearForm #address").val().toLowerCase()!=$("#newyearForm #checker").val().toLowerCase()){
				submitForm = false;
				$("#newyearForm #controle").show();
			}
			
			// Check description
			inhoud = $("#newyearForm #description").val();
			aContent = inhoud.split(' ');
			if (aContent instanceof Array && aContent.length > 149){
				// Remove new character
				submitForm = false;
				$("#newyearForm #description_error").show();
			}
			
			if(submitForm) $("#newyearForm").submit();
		});
		
		$("#newyearForm").validate({
			//set the rules for the field names
			rules: {
				name: { required: true, minlength: 2 },
				email: { required: true, email: true },
				description: { required: true, minlength: 2 },
				photo: { required: true },
				voorwaarden: { required: true }
			},
			//set messages to appear inline
			messages: {
				name: "Vul je naam in.",
				email: "Vul je e-mailadres in.",
				description: "Vul een beschrijving in.",
				photo: "Kies een foto om te uploaden.",
				voorwaarden: "Je bent nog niet akkoord met de voorwaarden."
			}
		});
	}
	
	function resizeBox(){
		$("#fancy_outer").css("width",$("#fancy_outer #fancy_content .fotoBox .image img").width()+20);
		$("#fancy_outer").css("height",$("#fancy_outer #fancy_content .fotoBox").height()+20);
		// center foto
		var left = $(window).width() - $("#fancy_outer").width();
		var top = $(window).height() - $("#fancy_outer").height();
		$("#fancy_outer").css("left",left / 2);
		$("#fancy_outer").css("top",top / 2);
	}
	
	$(".nieuwjaarsactie a.inline").fancybox({
		'zoomSpeedIn': 0, 
		'zoomSpeedOut':	0,
		'zoomSpeedChange': 0,
		'overlayShow': true,
		'imageScale': true,
		'callbackOnShow': resizeBox
	});
	
	$("#fancy_right_ico").click(function(){
		$("#fancy_outer").hide();
	});
	$("#fancy_left_ico").click(function(){
		$("#fancy_outer").hide();
	});
	
	
});

function timelineDDChange( el ) {
	if (el.value != eraSelected) {
		getFlashMovie("flashtimelinecontent").changeTimeline(el.value);
		eraSelected = el.value;
	}
}

function changeTimelineHeight( state ) {
	switch( state ) {
		case 0:
			$('#timelineflash').animate({ height: '70px' }, 500);
			break;
		case 1:
			$('#timelineflash').animate({ height: '275px' }, 500);
			break;
	}
}

function getFlashMovie(movieName){
	var IE = navigator.appName.indexOf("Microsoft") != -1;
	return (IE) ? window[movieName] : document[movieName];
}

function popitup(url) {
	newwindow=window.open(url,'expositie','height=680,width=1000,status=0,resizable=0,toolbar=0,location=0,scrollbars=0');
	if (window.focus) {newwindow.focus()}
	return false;
}
