/*----------------------------------------------------------------*/
/* document ready */
/*----------------------------------------------------------------*/

	$(document).ready(function(){

	  // set font for menu
		//Cufon.replace('#menu_inside ul a',{hover: true} );

	  // splash screen
		var splash = $.cookie('splash');	
		if (splash == null){

			//$("#splash").show();
			$("#splash").click(function(){
				$("#splash").fadeOut();
				splash = $.cookie('splash',"1",{path: '/',expires: 30});
			})
		}

	  // nice scrolling photos
		$('#large_photo_itself').cycle({ 
			fx:     'fade'
		});	

	  // switch news items
		newsTimeout = setTimeout('setTimeNews()', 2000);
		
	  // init star rating
		rating_done = false;
		setWidth = 0;
		starWidth = (105 / 5);

		$(".rating").mouseenter(function(){
			if (!rating_done){		
				$(".rating .over").show();
			}
		});

		$(".rating").mouseleave(function(){
			if (!rating_done){
				$(".rating .over").hide();
			}
		});

		$(".rating").bind('mousemove',function(e){ 
			current_x = e.pageX; 
			offset = $(".rating").offset();
			setWidth = current_x - offset.left;				
			setWidth = Math.ceil(setWidth / starWidth) * starWidth;
			$(this).find(".over").width(setWidth);
		});

		$(".rating").click(function(){
			id = $(".current_id").attr("id");
			type = $("#page_type").val();
			setRating = $(this).find(".over").width() / starWidth;
			$(".rating .over").hide();		
			url = base_url+"?action=set_rating&id="+id+"&rating="+setRating+"&type="+type;
			$.get(url,send_rating_done);
			rating_done = true;

			$(".rating").unbind('mousemove');
			$(".rating").unbind('click');		

		});		

		$(".news a").mouseover(function(){
			id = $(this).attr("id");
			$("#"+id+" .yellow").show();
			$("#"+id+" .border").hide();
		});

		$(".news a").mouseout(function(){
			if ($(this).hasClass("i_am_pic")){			
				id = $(this).attr("id");
				$("#"+id+" .yellow").hide();
				$("#"+id+" .border").show();
			}
		});
	})

	function send_rating_done(data){
		setWidth = starWidth * data;
		$(".rating .rate").width(setWidth);
	}

/*----------------------------------------------------------------*/
/* fade the news or vids on the homepage */
/*----------------------------------------------------------------*/

	function setTimeNews(){
		setTimeout('setRows(0,"news")', 100);
		setTimeout('setRows(1,"news")', 2300);
		setTimeout('setRows(2,"news")', 1000);
	}

	function setTimeVideo(){
		setTimeout('setRows(0,"video")', 100);
		setTimeout('setRows(1,"video")', 2300);
		setTimeout('setRows(2,"video")', 1000);
	}
  
	function setRows(row,t_class){
		
	 // first init pic class is done by php
		$("."+t_class+"_row_"+row+" a.pic .border, ."+t_class+"_row_"+row+" a.i_am_pic .border").fadeOut(1000);
		$("."+t_class+"_row_"+row+" a.pic .yellow, ."+t_class+"_row_"+row+" a.i_am_pic .yellow").fadeIn(1000);

		$("."+t_class+"_row_"+row+" .i_am_pic").addClass("i_was_pic");
		$("."+t_class+"_row_"+row+" .i_am_pic").removeClass("i_am_pic");

		rand =Math.floor(Math.random()*2);

		j = 0;
		$("."+t_class+"_row_"+row+" a:not(.i_was_pic)").each(function(){

			if (j == rand){				
				$(this).addClass("i_am_pic");
				$(this).find(".border").fadeIn();
				$(this).find(".yellow").fadeOut();
			}
			j++;
		});

		$("."+t_class+"_row_"+row+" .i_was_pic").removeClass("i_was_pic");
		
		miliSec = (rand * 500) + 2500;
		setTimeout('setRows('+row+',"'+t_class+'")', miliSec);
	}

/*----------------------------------------------------------------*/
/* cool accordion menu :-) */
/*----------------------------------------------------------------*/

	function menu_showChildren(depth,id){
		doit = true;
		
		if ($("#menu_"+id).css("display") == "block"){
			doit = false;
		}

		if (depth == 0){
			$("#menu ul ul").slideUp();
		}
		if (depth == 1){
			$("#menu ul ul ul").slideUp();
		}
		if (depth == 2){
			$("#menu ul ul ul ul").slideUp();
		}

		if (doit){	
			$("#menu_"+id).slideDown();
		}

		$("body").focus();
	}

/*----------------------------------------------------------------*/
/* print stuff */
/*----------------------------------------------------------------*/

	function print_page(){

		print_me = "";
		
	  // grab all the div's classified with .print and drop them in the print div	  
		$('.print').each(function(index) {
			print_me += $(this).html();
		});

		$("#print").html(print_me);

		window.print()

	}

/*----------------------------------------------------------------*/
/* send a friend */
/*----------------------------------------------------------------*/

	function saf(){
		$(".comment_form").slideUp();
		$(".saf_hide").slideUp();
		$(".saf_form").slideDown();
	}

	function saf_hide(){
		$(".saf_hide").slideDown();
		$(".saf_form").slideUp();
	}

	function send_saf(){
		$("#saf_form").submit();
	}

/*----------------------------------------------------------------*/
/* Social media options */
/*----------------------------------------------------------------*/

	function shareThis(network){

		video_url = $("#social_video_url").val();
		video_title = $("#social_video_title").val();

		if (typeof video_url != "undefined" && video_url != ""){
			item_url = video_url;
		}else{
			item_url = document.location.href;
		}

		if (typeof video_title != "undefined"){
			item_title = video_title;
		}else{
			item_title = "Deze interessante info vond ik op ikzithier.nl ";
		}	

		item_url = encodeURIComponent(item_url);
		item_title = encodeURIComponent(item_title);
		

		if (network == "facebook"){
			url = "http://www.facebook.com/sharer.php?u="+item_url+"&t="+item_title;
		}

		if (network == "twitter"){			
			url = "http://twitter.com/home?status="+item_title+escape(": ")+item_url;
		}
		
		if (network == "delicious"){
			url = "http://delicious.com/save?v=5&noui&jump=close&url="+item_url+"&title="+item_title;
		}

		if (network == "linkedin"){
			url = "http://www.linkedin.com/shareArticle?mini=true&ro=true&url="+item_url+"&title="+item_title+"&summary=&source=http://www.ikzithier.nl&armin=armin";
		}

		if (network == "hyves"){
			url = "http://www.hyves.nl/profilemanage/add/tips/?name=home&text="+item_title+item_url;			
		}
		
		window.open(url, 'new_win', 'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,resizable=1,width=700,height=500');
		
	}

/*----------------------------------------------------------------*/
/* Comment section */
/*----------------------------------------------------------------*/

	function comment(){
		$(".saf_form").slideUp();
		$(".comment_hide").slideUp();
		$(".comment_form").slideDown();
	}

	function comment_hide(){
		$(".comment_hide").slideDown();
		$(".comment_form").slideUp();
	}

	function send_comment(){
		$("#comment_form").submit();
	}

/*----------------------------------------------------------------*/
/* video functions */
/*----------------------------------------------------------------*/
	
	vids_max = 7;
	vids_current_page = 0;

	function update_video_list_buttons(){
		
		vids_allow_nav = true;

		if (vids_current_page == 0){
			$("#video_list_last").addClass("in_active");
		}else{
			$("#video_list_last").removeClass("in_active");
		}

		if (Math.floor(vids_total / vids_max) == vids_current_page){
			$("#video_list_next").addClass("in_active");
		}else{
			$("#video_list_next").removeClass("in_active");
		}

		if (vids_total < vids_max){
			vids_allow_nav = false;
			$("#video_list_next").addClass("in_active");
			$("#video_list_last").addClass("in_active");
		}
	}

	function video_list_next(){
		if ($("#video_list_next").hasClass("in_active") == false){
			video_list(+1);
		}
	}
	function video_list_last(){
		if ($("#video_list_last").hasClass("in_active") == false){
			video_list(-1);
		}
	}

	function video_list(dir){

		if (vids_allow_nav){
			
			vids_current_page += dir;

			url = "?action=get_video_list&page="+vids_current_page;

			$.get(url,video_list_done);
		}				
	}

	function video_list_done(data){
		$("#video_list_inside").html(data);
		update_video_list_buttons();
	}

	function showVideo(id,code,title,name){

		$("#social_video_url").val(base_url+lang_code+"/video/"+id);
		$(".video_title").html(title);
		$(".video_name").html(name);
		
		
		$(".hide_on_playing").slideUp();
		$("#video_do_main").hide();
		$("#video_do").show();
		$("#video_options").show();


		$(".video_id").val(id);

		obj = '<object width="642" height="'+video_height+'"><param name="movie" value="http://www.youtube.com/v/'+code+'?rel=0&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+code+'?rel=0&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="642" height="'+video_height+'"></embed></object>';
		$("#large_video_itself").html(obj);		
		$("#video_vote").slideUp();
		$("#video_saf").slideUp();
		$("#large_video").slideDown();
		$("#message").slideUp();
	}

	function video_vote(){
		$("#video_vote").slideDown();
		$("#video_saf").slideUp();
		$("#large_video").slideUp();
		$("#message").slideUp();
	}
	function video_vote_hide(){		
		$("#video_vote").slideUp();
		$("#large_video").slideDown();
		$("#message").slideUp();
	}

	function video_saf(send_type){

		if (send_type == "main"){
			$(".title_main_video_saf").show();
			$(".title_video_saf").hide();
		}else{
			$(".title_main_video_saf").hide();
			$(".title_video_saf").show();		
		}

		$("#video_vote").slideUp();
		$("#video_saf").slideDown();
		$("#large_video").slideUp();		
		$("#message").slideUp();

	}

	function video_saf_hide(){
		$("#video_saf").slideUp();
		$("#large_video").slideDown();
		$("#message").slideUp();
	}

/*----------------------------------------------------------------*/
/* social functions */
/*----------------------------------------------------------------*/

	function social_show(){
		$.get("?action=social_step_1",social_show_do);
	}

	function social_hide(){
		$("#social_overlay").hide();
	}

	function social_show_do(data){

		$("#social_overlay").html(data);
		$("#social_overlay").show();
		
		allow_hide = true;
		$("#social_form_div").mouseover(function(){
			allow_hide = false;
		})
		$("#social_form_div").mouseout(function(){
			allow_hide = true;
		})
		$("#social_form_div").click(function(){
			allow_hide = false;
		})
		$("#social_overlay").click(function(){
			if (allow_hide){			
				$(this).hide();
			}
		})
				
		$("#social_form").validate({
			messages: {
				party : "Kies een netwerk.",
				username : "Vul je gebruikersnaam in.",
				password : "Vul een wachtwoord in."
			},
			errorPlacement: function(error, element) {
				if (element.attr("name") != "product_id"){
					error.insertAfter(element);
				}
			},
			submitHandler: function(form) {
				// do other stuff for a valid form
				//form.submit();
				var obj = new Object();
				obj.party = $("#party").val();
				obj.username = $("#username").val();
				obj.password = $("#password").val();
				$.post("?action=social_step_2",obj,social_get_contacts_done);
				$("#social_form_itself").slideUp();
				$("#social_form_result").slideDown();
				$("#social_form_result").html('<div class="wait"></div>');

				return false;
			},
		  // add special highlighting for inputs with background div
			highlight: function(element, errorClass, validClass) {
				$(element).addClass(errorClass).removeClass(validClass);
				$(element).parent().addClass(errorClass).removeClass(validClass);
			},
			unhighlight: function(element, errorClass, validClass) {
				$(element).removeClass(errorClass).addClass(validClass);
				$(element).parent().removeClass(errorClass).addClass(validClass);											
			}
				
		});	
	}

	function send_social_form(){
		$("#social_form").submit();
	}

	function social_get_contacts_done(data){

		if (data == "error_get_contacts"){
			$("#social_alert").html("Kan de contact lijst niet ophalen. Probeert u het nog eens.");
			$("#social_alert").show();
			$("#social_form_itself").slideDown();
			$("#social_form_result").slideUp();
		}else if (data == "error_no_valid_login"){
			$("#social_alert").html("Geen geldige login gegevens.");
			$("#social_alert").show();
			$("#social_form_itself").slideDown();			
			$("#social_form_result").slideUp();
		}else{
			$("#social_form_result").html(data)
		}
	}

	function social_add_contacts(){
		var contact_arr = new Array();
		$("input[@name='users[]']:checked").each(function() {contact_arr.push($(this).val());});
		contact_str = contact_arr.join(",");
		$("#to_email").val(contact_str);
		$("#to_email").keyup();
		social_hide();		
	}

