


var menuOpen = false;
var parts = document.location.href.split('/');
var page_path = "/" + parts[3] + "/" + parts[4];

$(function() {

	// Headlines
	var headlines_busy = 0;
	var headlines_clicked = 0;
	var headlines_over = 0;
	
	$("#headlines").hover(
		function() {
			headlines_over = 1;
		},
		function() {
			headlines_over = 0;
		}
	);
		
	$("#headlines-select a").click( function() {
		headlines_clicked = 1;
		if( !headlines_busy ) {
			if( $(this).parent().hasClass("current") ) {
					// Track
				pageTracker._trackEvent('Headlines', 'DoubleClick', page_path );
				return true;
			}
			headlines_busy = 1;
				// Track
			pageTracker._trackEvent('Headlines', 'Switch', page_path );
			$("#headlines-select .current").removeClass("current");
			$(this).parent().addClass("current");
			var index = $(this).attr('id').split("-")[1];
			$("#headline-details-"+index+" .headline-description").show();
			$("#headline-details-"+index+" .headline-description").fadeTo( 100, 0 );
			$("#headlines-details .current .headline-description").fadeTo( 200, 0 );
			$("#headlines-details .current").fadeOut(500, function() {
				$("#headlines-details .current").removeClass("current");
				 	$("#headline-details-"+index).fadeIn(500, function() {
					$("#headline-details-"+index).addClass("current");
					$("#headline-details-"+index+" .headline-description").fadeTo(200, 0.85, function() {
						headlines_busy = 0;
					});
				});
			});
		}
		return false;
	});

	var automate_headlines = function() {

		if( headlines_clicked || headlines_busy ) {
			return;
		}
		if( headlines_over ) {
			setTimeout( pause_headlines, 4000 );
			return;
		}
		headlines_busy = 1;
		var index = $("#headlines-details .current").attr('id').split("-")[2];
		new_index = ( index == 3 ) ? 0 : ( parseInt( index ) + 1 );
		$("#headline-btn-"+index).removeClass( "current" );
		$("#headline-btn-"+new_index).addClass( "current" );
		$("#headline-details-"+new_index+" .headline-description").show();
		$("#headline-details-"+new_index+" .headline-description").fadeTo( 100, 0 );
		$("#headlines-details .current .headline-description").fadeTo( 200, 0 );
		$("#headlines-details .current").fadeOut(500, function() {
			$("#headlines-details .current").removeClass("current");
		 	$("#headline-details-"+new_index).fadeIn(500, function() {
				$("#headline-details-"+new_index).addClass("current");
				$("#headline-details-"+new_index+" .headline-description").fadeTo(200, 0.85, function() {
					headlines_busy = 0;
				});
			});
		});
		setTimeout( automate_headlines, 8000 );
	};
	
	// Start automating headline effects
	if( document.getElementById("headlines-details") ) {
		setTimeout( automate_headlines, 8000 );
	}
	
	var pause_headlines = function() {
		if( headlines_over ) {
			setTimeout( pause_headlines, 4000 );
		}
		else {
			setTimeout( automate_headlines, 1000 );
		}
	};
	
	
	// Page Tools
	$("#saf a").click( function() {
		$("#sendafriend").fadeIn(300, function() {
			$("#sendafriend form").show();
		});
	});
	$("#saf-cancel").click( function() {
		$("#sendafriend").fadeOut(300, function() {
			$("#sendafriend form").hide();
			return false;
		});
	});
	
	var emailRegexp = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	$("#sendafriend form").submit( function() {

		var post_id = $("#post-id").val();
		var sender = $("#saf-sender").val();
		var receiver = $("#saf-receiver").val();
		
		if( !post_id || !receiver || !sender || !emailRegexp.test( receiver ) ) {
			$("#saf-msg").html("<span>Wel graag alle gegevens goed invullen...</span>");
			return false;
		}
		else {
			$("#sendafriend").addClass("loading");
			$("#saf-send").attr("disabled", "disabled");
			$.ajax({
				type: "POST",
				url: "/sendafriend.php",
				data: "post-id=" + $("#post-id").val() + "&sender=" + $("#saf-sender").val() + "&receiver=" + $("#saf-receiver").val(),
				success: function() {
					$("#saf-msg").html("<strong>Bericht verstuurd!</strong>");
					$("#sendafriend").removeClass("loading");
					pageTracker._trackEvent('Page tools', 'Send a Friend', page_path );
					setTimeout( function() {
						$("#sendafriend").fadeOut(300, function() {
							$("#saf-sender").attr("value","");
							$("#saf-receiver").attr("value","");
							$("#sendafriend form").hide();
							$("#saf-send").attr("disabled", "");
							$("#saf-msg").html("Verstuur dit bericht via email:");
							return false;
						});
					}, 1000 ); 
				}
		 	});
			return false;
		}
	});
	
	var videos_headlines_busy = 0;
	
	$("#video-forward").click( function() {
		if( videos_headlines_busy ) {
			return false;
		}
		else {
			videos_headlines_busy = 1;
			var left = parseInt( $("#video-headlines ul").css("margin-left") );
			if( left > -1510 ) {
				var newLeft = left - 302;
				$("#video-headlines ul").animate({ 
	   	     marginLeft: newLeft + "px"
				}, 300, "", function() {
					videos_headlines_busy = 0;
				});
			}
			else {
				videos_headlines_busy = 0;
			}
		}
	});
	
	$("#video-backward").click( function() {
		if( videos_headlines_busy ) {
			return false;
		}
		else {
			videos_headlines_busy = 1;
			var left = parseInt( $("#video-headlines ul").css("margin-left") );
			if( left < 0 ) {
				var newLeft = left + 302;
				$("#video-headlines ul").animate({ 
	   	     marginLeft: newLeft + "px"
				}, 300, "", function() {
					videos_headlines_busy = 0;
				});
			}
			else {
				videos_headlines_busy = 0;
			}
		}
	});
	
	/*
	$("#updates-film li").mouseover( function() {
		$(this).addClass("over");
		var parts = $(this).attr("id").split("-");
		var id = parseInt( parts[1] );
		$("#updates-film-img").attr("src", "http://www.xi-online.nl/wp-content/images/m/"+id+"_m.jpg");
	});
	$("#updates-film").mouseout( function() {
		var parts = $("#updates-film li:first").attr("id").split("-");
		var id = parseInt( parts[1] );
		$("#updates-film-img").attr("src", "http://www.xi-online.nl/wp-content/images/m/"+id+"_m.jpg");
	});
	$("#updates-tv li").mouseover( function() {
		$(this).addClass("over");
		var parts = $(this).attr("id").split("-");
		var id = parseInt( parts[1] );
		$("#updates-tv-img").attr("src", "http://www.xi-online.nl/wp-content/images/m/"+id+"_m.jpg");
	});
	$("#updates-tv").mouseout( function() {
		var parts = $("#updates-tv li:first").attr("id").split("-");
		var id = parseInt( parts[1] );
		$("#updates-tv-img").attr("src", "http://www.xi-online.nl/wp-content/images/m/"+id+"_m.jpg");
	});
	$("#updates-interactief li").mouseover( function() {
		$(this).addClass("over");
		var parts = $(this).attr("id").split("-");
		var id = parseInt( parts[1] );
		$("#updates-interactief-img").attr("src", "http://www.xi-online.nl/wp-content/images/m/"+id+"_m.jpg");
	});
	$("#updates-interactief").mouseout( function() {
		var parts = $("#updates-interactief li:first").attr("id").split("-");
		var id = parseInt( parts[1] );
		$("#updates-interactief-img").attr("src", "http://www.xi-online.nl/wp-content/images/m/"+id+"_m.jpg");
	}); */
	
	$("#updates li").mouseout( function() {
		$(this).removeClass("over");
	});
	
	$("#related-posts a").click( function() {
		var parts = $(this).attr('href').split('/');
		var related_post_path = "/" + parts[3] + "/" + parts[4];
		return pageTracker._trackEvent('Page tools', 'Related Post', page_path );
	});
	$("#print a").click( function() {
		return pageTracker._trackEvent('Page tools', 'Print', page_path );
	});
	$("#bookmark a").click( function() {
		return pageTracker._trackEvent('Page tools', 'Bookmark', page_path );
	});
	$("#newsletter-btn").click( function() {
		// Track
		return pageTracker._trackEvent('Recent Videos', 'Image', $(this).attr('title') );
	});
	
});


	var XI_video_busy = false;

	function XI_view_video( youtube_id ) {

		if( !XI_video_busy ){
			XI_video_busy = 1;
		}
		else {
			return false;
		}
		$("#video-btn").hide();
		$("#article-image img").fadeOut(500, function() {
			$("#article-info").hide();
			$("#article-image").css( "border", "dotted 1px #ccc" );
			$("#article-image").animate({
					width: '640px',
					height: '385px',
					marginBottom: '40px' 
				}, 600, "linear", function() {
					$("#article-image").css( "border", "none" );
					$("#video").append('<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/'+youtube_id+'&autoplay=1&fs=1&re=0&egm=0&showsearch=0&color1=0xf1f1f1&color2=0xf1f1f1&hd=1&iv_load_policy=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="hd" value="1"></param><param name="iv_load_policy" value="3"></param></param><embed src="http://www.youtube.com/v/'+youtube_id+'&autoplay=1&hd=1&&fs=1&re=0&egm=0&showsearch=0&color1=0xf1f1f1&color2=0xf1f1f1&iv_load_policy=3" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" autoplay="true" hd="1" iv_load_policy="3" allowfullscreen="true" width="640" height="385"></embed></object>');
						// Track event
					pageTracker._trackEvent('Videos', 'Play', page_path );
					$("#video-close").html('<a href="#">Sluit video</a>');
					$("#video-close").show();
					$("#article-image").css( "marginBottom", "8px" );
					$("#video-close a").click( function() {
						return XI_close_video( youtube_id );
					});
					XI_video_busy = 0;
			});
		});
		return false;
	}

	function XI_close_video( youtube_id ) {

		if( !XI_video_busy ){
			XI_video_busy = 1;
		}
		else {
			return false;
		}
		$("#video").empty();

		$("#video-close").hide();
		$("#article-image").css( "marginBottom", "28px" );
		$("#article-image").animate({
				width: '480px',
				height: '260px',
				marginBottom: '12px'
			}, 400, "linear", function() {
				$("#article-info").show();
				$("#article-image").css("border", "none");
				$("#article-image img").fadeIn(500, function() {
					$("#video-btn").fadeIn(400);
				});
					// Track event
				pageTracker._trackEvent('Videos', 'Close', page_path );
				XI_video_busy = 0;	

		});
		return false;
	}

	function XI_hoverRate(val) {
		var bp = -1 * val * 17;
		$("#whwt-rate ul").css("background-position", "0 " + bp + "px");
	}

	function XI_clearRate() {
		if( $("#whwt-rating").val() ) {
			var bp = -1 *  $("#whwt-rating").val() * 17;
			$("#whwt-rate ul").css("background-position", "0 " + bp + "px");			
		}
		else {
			$("#whwt-rate ul").css("background-position", "0 -300px");
		}
		return false;
	}

	function XI_resetRate() {
		$("#whwt-rating").val('');
		XI_clearRate();
		$("#whwt-reset").hide();
		return false;
	}

	function XI_setRate(val) {
		$("#whwt-rating").val(val);
		$("#whwt-reset").show();
		return false;
	}

	function XI_toggleTabs(view) {
		if( view == 2 ) {
			$("#tab1").removeClass('current');
			$("#tab2").addClass('current');
			$("#recent-items").show();
			$("#most-read").hide();
		}
		else {
			$("#tab1").addClass('current');
			$("#tab2").removeClass('current');
			$("#recent-items").hide();
			$("#most-read").show();
		}
		return false;
	}

	function XI_checkTextLength() {
		var val = $("#whwt-text").val();
		var num = 300 - val.length;
		$("#whwt-num").text(num);
		if( num < 0 ) {
			$("#whwt-num").addClass('error');
		}
		else {
			$("#whwt-num").removeClass('error');
		}	
	}

	function XI_check_whwt() {
		if( $("#whwt-name").val() == 'Naam' || $("#whwt-name").val() == '' ) {
			$("#whwt-name").css("border-color", "#f00");
			return false;
		}
		if( $("#whwt-text").val() == 'Mening' || $("#whwt-text").val() == '' ) {
			return false;
		}
	}