/*
$(window).load(function() {
	if(_site.page.main == "news" && _site.page.sub != "")
	{
		$(".scroll-pane").jScrollPane();
	}
});
*/
$(document).ready(function(){
	
	_site = site;
	_site.init();
	
	setInterval("checkURL()", 250);
	
	
	

	/********************************************************************************************************
	*
	*	-- Music controls --
	*
	*********************************************************************************************************/
	
	$("#links > ul > li > a").click(function(e){
		if($(this).attr("href") == "#")
		{
			e.preventDefault();
			//$("#music_player").pause();
			
			if($("#music_player").attr("class") == "playing")
			{
				document.getElementById('music_player').pause();
				$("#music_player_controls > a")
				.empty()
				.append('<img src="assets/images/music_play.png" alt="Blog" /><span>Play</span>');
				
				$("#music_player").attr("class", "pause");
			}
			else
			{
				document.getElementById('music_player').play();
				$("#music_player_controls > a")
				.empty()
				.append('<img src="assets/images/music_pause.png" alt="Blog" /><span>Pause</span>');
				
				$("#music_player").attr("class", "playing");
			}
		}	
	});
	
	/********************************************************************************************************
	*
	*	-- Main menu triggers --
	*
	*********************************************************************************************************/
	
	/*
    *	Clicking a menu link
    ------------------------------------------------------------*/
	$(".menu_item_hover > a").click(function(e){
		e.preventDefault();

		var page = $(this).attr("href");

		if(page == "home")
		{
			$("#content").empty();
			_site.show_background();
			_site.page.full_path = "";
			window.location.hash = "";
		}
		else
		{
			if(_site.page.full_path == page)
			{
				$("#content").empty();
				_site.show_background();
				_site.page.full_path = "";
				window.location.hash = "";
			}
			else
			{
				_site.load_page(page);
				_site.hide_background();
			}
		}
	});
	
	/*
    *	Mouse over on a menu link
    ------------------------------------------------------------*/
    $(".menu_item_hover").mouseover(function(){
    
    	if(!$.browser.msie)
    	{
    		$(this).children("ul").css("display", "block");
    	}
    	
    	// Sytle button until #sub_menu mouseout
		$(this).children("a")
		.css("background", "#fff")
		.css("color", "#f70")
		.css("border", "1px solid #bfbfbf")
		.css("cursor", "pointer")
		.css("font-weight", "bold")
		.css("box-shadow", "1px 1px 1px #bfbfbf")
		.css("-webkit-box-shadow", "1px 1px 1px #bfbfbf")
		.css("-moz-box-shadow", "1px 1px 1px #bfbfbf");
		
		$("#menu").css("z-index", 800);
		
		var sub_menu_id = $(this).parent().attr("id");
		
		if(sub_menu_id == "for_candidates" || sub_menu_id == "for_companies" || sub_menu_id == "vacatures")
		{
			$(this).children("a")
			.css("-moz-border-radius-topleft", "0px")
			.css("-moz-border-radius-topright", "0px")
			.css("-webkit-border-top-left-radius", "0px")
			.css("-webkit-border-top-right-radius", "0px");
		}
    });
    
    /*
    *	Mouse out on a menu link
    ------------------------------------------------------------*/
    $(".menu_item_hover").mouseout(function(){
    	$(this).children("ul").css("display", "none");
    	
    	// Remove button style
		$(".menu_item_hover > a")
		.css("background", "#d7d7d7")
		.css("color", "#5d5adc")
		.css("border", "")
		.css("cursor", "pointer")
		.css("font-weight", "normal")
		.css("box-shadow", "")
		.css("-webkit-box-shadow", "")
		.css("-moz-box-shadow", "")
		.css("-moz-border-radius-topleft", "10px")
		.css("-moz-border-radius-topright", "10px")
		.css("-webkit-border-top-left-radius", "10px")
		.css("-webkit-border-top-right-radius", "10px");
		
		$("#menu").css("z-index", 500);
    });
    
    
	/********************************************************************************************************
	*
	*	-- Sub menu triggers --
	*
	*********************************************************************************************************/
	
	/*
    *	Mouse click sub-menu item
    ------------------------------------------------------------*/
	$(".menu_item_hover > ul > li > a").click(function(e) {
		e.preventDefault();
		
		_site.load_page($(this).attr("href"));
		_site.hide_background();
	});
	
	
    /********************************************************************************************************
	*
	*	-- Content menu triggers --
	*
	*********************************************************************************************************/
	
	/*
    *	Mouse click content-menu item
    ------------------------------------------------------------*/
	$(".content_menu").live("click", function(e) {
		e.preventDefault();

		$(".menu > a").removeClass("selected");
		$(this).addClass("selected");
		
		// IE7
		if($.browser.msie && $.browser.version == "7.0")
		{
			var request_url = $(this).attr("href").replace("http://www.sres.nl/", "");
		}
		else
		{
			var request_url = $(this).attr("href");
		}
		
		_site.set_page_vars(request_url);
		_site.load_sub_page("."+_site.page.main+"_content > .jScrollPaneContainer .scroll-pane");
		_site.hide_background();
	});
	
	/*
    *	Vacatures category click
    ------------------------------------------------------------*/
	$(".vacancy_category").live("click", function(e) {
		e.preventDefault();

		$(".menu > a").removeClass("selected");
		$(this).addClass("selected");
		
		// IE7
		if($.browser.msie && $.browser.version == "7.0")
		{
			var request_url = $(this).attr("href").replace("http://www.sres.nl/", "");
		}
		else
		{
			var request_url = $(this).attr("href");
		}
		
		_site.set_page_vars(request_url);
		_site.load_sub_page(".vacature_content > .jScrollPaneContainer > .scroll-pane");
		_site.hide_background();
	});
	
	/*
    *	Vacatures item click
    ------------------------------------------------------------*/
	$(".vacancy_item").live("click", function(e) {
		e.preventDefault();
		
		//$(".menu > a").removeClass("selected");
		//$(this).addClass("selected");
		
		// IE7
		if($.browser.msie && $.browser.version == "7.0")
		{
			var request_url = $(this).attr("href").replace("http://www.sres.nl/", "");
		}
		else
		{
			var request_url = $(this).attr("href");
		}
		
		_site.set_page_vars(request_url);
		_site.load_sub_page(".vacature_content > .jScrollPaneContainer > .scroll-pane");
		_site.hide_background();
	});
	
	/*
    *	News item click
    ------------------------------------------------------------*/
	$(".news_item").live("click", function(e) {
		e.preventDefault();

		$(".menu > a").removeClass("selected");
		$(this).addClass("selected");
		
		// IE7
		if($.browser.msie && $.browser.version == "7.0")
		{
			var request_url = $(this).attr("href").replace("http://www.sres.nl/", "");
		}
		else
		{
			var request_url = $(this).attr("href");
		}
		
		_site.set_page_vars(request_url);
		_site.load_sub_page(".news_content > .jScrollPaneContainer > .scroll-pane");
	});
	
	/*
    *	News item hover scroll-pane solution
    ------------------------------------------------------------*/
    $("#content").mouseover(function(){
    	if(_site.page.main == "news")
    	{
    		//$(".scroll-pane").jScrollPane(); laggar som fan
    	}
    });
	
	
	/********************************************************************************************************
	*
	*	-- For companies > Contact - Triggers --
	*
	*********************************************************************************************************/
	
	/*
    *	Contact form submit
    ------------------------------------------------------------*/
	$("#contact_form").live("submit", function(e) {
		e.preventDefault(); //Override default link
		
		// Remove error messages
		$(".required").css("visibility", "hidden");
		$(".success").css("visibility", "hidden");
		
		// Set form as valid
		var valid = 1;
		
		// Name required
		if($("#contact_name").val() == "")
		{
			$("#name").css("visibility", "visible");
			var valid = 0;
		}
		
		// Email valid
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var email = $("#contact_email").val();
		   	
		if(reg.test(email) == false) 
		{
			$("#email").empty().append("Invalid");
			$("#email").css("visibility", "visible");
			var valid = 0;
		}
		
		// Email required
		if($("#contact_email").val() == "")
		{
			$("#email").empty().append("Required");
			$("#email").css("visibility", "visible");
			var valid = 0;
		}
		
		// Subject required
		if($("#contact_subject").val() == "")
		{
			$("#subject").css("visibility", "visible");
			var valid = 0;
		}
		
		// Message required
		if($("#contact_message").val() == "")
		{
			$("#message").css("visibility", "visible");
			var valid = 0;
		}

			
		if(valid == 1)
		{
			submit_contact_form();
			
			$("input[type=text]").val("");
			$("textarea").val("");
			$(".success").css("visibility", "visible");
		}
	});
	
	
	/********************************************************************************************************
	*
	*	-- For candidates > Upload CV - Triggers --
	*
	*********************************************************************************************************/
	
	/*
    *	Upload-cv form submit
    ------------------------------------------------------------*/
    $("#uploadcv_form").live("submit", function(e) {
		e.preventDefault(); //Override default link
		
		// Remove error messages
		$(".required").css("visibility", "hidden");
		$(".success").css("visibility", "hidden");
		
		// Set form as valid
		var valid = 1;
		
		// Name required
		if($("#uploadcv_name").val() == "")
		{
			$("#name").css("visibility", "visible");
			var valid = 0;
		}
		
		// Email valid
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var email = $("#uploadcv_email").val();
		   	
		if(reg.test(email) == false) 
		{
			$("#email").empty().append("Invalid");
			$("#email").css("visibility", "visible");
			var valid = 0;
		}
		
		// Email required
		if($("#uploadcv_email").val() == "")
		{
			$("#email").empty().append("Required");
			$("#email").css("visibility", "visible");
			var valid = 0;
		}
		
		if(valid == 1)
		{
			submit_uploadcv_form();
			
			//$("input[type=text]").val("");
			//$("textarea").val("");
			$("span.applying_for").css("display", "none");
			$("label").css("display", "none");
			$("input").css("display", "none");
			$("textarea").css("display", "none");
			$("#uploaded_file").empty();
			$("label.success").css("display", "block");
			$(".success").css("visibility", "visible");
		}
	});
	
	
	/********************************************************************************************************
	*
	*	-- Vacatures > Apply now --
	*
	*********************************************************************************************************/
	
	/*
    *	Vacatures: Apply now / upload cv
    ------------------------------------------------------------*/
    $("#apply_now_button").live("click", function(e){
		e.preventDefault();

		$("#temp_data").val($(this).attr("href"));
		_site.load_page("for_candidates/upload_cv");
    });
    
    
    /********************************************************************************************************
	*
	*	-- News --
	*
	*********************************************************************************************************/
	
	/*
    *	Pop-up image
    ------------------------------------------------------------*/
    $(".news_image").live("click", function(){
    
    	// News image
    	var img_path = $(this).attr("src");
    	
    	// Screen vars
    	var screen_width = $(document).width();
    	var screen_height = $(document).height();
    	
    	$("#image_display").empty().append('<img class="the_image" src="'+img_path+'" />');
    	
    	// Image vars
    	var img_width = $(".the_image").width();
    	var img_height = $(".the_image").height();
    	
    	// Calculate stuff
    	if(img_width > screen_width || img_height > screen_height)
    	{
    		var img_height = screen_height - 50;
    		
    		$(".the_image").css("height", img_height+"px");
    		
    		var img_width = $(".the_image").width();
    		var img_top = 25;
    		var img_left = (screen_width - img_width) / 2;
    	}
    	else
    	{
    		var img_top = (screen_height - img_height) / 2;
    		var img_left = (screen_width - img_width) / 2;
    	}

		// Display dimmer
		$("#dimmer")
		.css("width", screen_width+"px")
		.css("height", screen_height+"px")
		.css("top", "0px");
		
		// Display image
    	$(".the_image").css("top", img_top+"px").css("left", img_left+"px");
    	
    });
    
    /*
    *	Close image
    ------------------------------------------------------------*/
    $("#image_display").click(function(){
    	// Image vars
    	var img_width = $(".the_image").width();
    	var img_height = $(".the_image").height();
    	
    	/*
    	$('.the_image').animate({
    		top : "55px",
  		}, 100, function() {
    		$('.the_image').animate({
	    		top : "-"+(img_height + 200)+"px"
	  		}, 700, function() {
	  			// Reset image
	  			$(".the_image").css("top", "0px");
	  			$(".the_image").css("left", "0px");
	  			$("#image_display").empty();
	  			  			
	  			// Remove dim
	    		$("#dimmer").css("top", "-10000px");
	  		});
  		});*/
    });
    
    /*
    *	News > template > Contact company button
    ------------------------------------------------------------*/
    $(".contact_us_button").live("click", function(e){
		e.preventDefault();

		$("#temp_data").val($(this).children("a").attr("href"));
		_site.load_page("for_companies/contact");
    });
    
    
    /********************************************************************************************************
	*
	*	-- Share buttons --
	*
	*********************************************************************************************************/
	
	// Twitter
	$(".retweet_button").live("click", function(e){
		e.preventDefault();
		
		var url = $(this).children("a").attr("href");
		
		window.open(url, "Share", "status = 1, height = 400, width = 565, resizable = 1" );
	});
	
	// Facebook
	$(".fb_share_button").live("click", function(e){
		e.preventDefault();
		
		var url = $(this).children("a").attr("href");
		
		window.open(url, "Share", "status = 1, height = 450, width = 600, resizable = 1" );
	});
	
	// LinkedIn
	$(".linkedin_share_button").live("click", function(e){
		e.preventDefault();
		
		var url = $(this).children("a").attr("href");
		
		window.open(url, "Share", "status = 1, height = 450, width = 600, resizable = 1" );
	});
	
	
	/*
	*	Check FLash-support
	------------------------------------------------------------*/
	if(navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"])
	{
		// flash
	}
	else 
	{
		// no flash
		$("body").css("background", "url(assets/images/f_bg.png)");
	}
});

/*
*	Check if the url has changed (back-button)
------------------------------------------------------------*/
function checkURL()
{
	if(window.location.hash.replace("#", "") != _site.page.full_path && _site.page.is_loading == "false")
	{
		if(window.location.hash.replace("#", "").length == 0)
		{
			$("#content").empty();
			_site.show_background();
			_site.page.full_path = "";
			window.location.hash = "";
		}
		else
		{
			_site.load_page(window.location.hash.replace("#", ""));
			_site.hide_background();
		}
	}
}







