// Ida Fram
// commons.js
// Requires jquery


$(document).ready(function() {

	// Default string after # in URL
	var pageURLExtension = "yritys,ajankohtaista,asiakkaat";

	// Grap URL
	var pageURL = document.location.toString();
	
	// IF an anchor (#) exists in URL, change pages depending rules
	if (pageURL.match('#')) {
			  
		  var selectedPagesArr = pageURL.split('#')[1]+",";
		  
		  var selectedPages = selectedPagesArr.split(',');

		  // Col1
		  if(jQuery.inArray("yhteystiedot", selectedPages) >= 0) {
				changePage("yritys","hide",true);
				changePage("yhteystiedot","show",true);
				pageURLExtension = pageURLExtension. replace(/yritys/, "yhteystiedot");
		  } else {
				changePage("yritys","show",true);
				changePage("yhteystiedot","hide",true);
		  }
		  
		  // Col2
		  if(jQuery.inArray("uutiskirje", selectedPages) >= 0) {
				changePage("ajankohtaista","hide",true);
				changePage("uutiskirje","show",true);
				pageURLExtension = pageURLExtension.replace(/ajankohtaista/, "uutiskirje");
		  } else {
				changePage("ajankohtaista","show",true);
				changePage("uutiskirje","hide",true);
		  }
		  
		  // Col3
		  if(jQuery.inArray("asiakkaat", selectedPages) >= 0) {
				changePage("tyot","hide",true);
				changePage("asiakkaat","show",true);
				pageURLExtension = pageURLExtension.replace(/tyot/, "asiakkaat");
		  } 
		  else {
				changePage("tyot","show",true);
				changePage("asiakkaat","hide",true);
		  }
		  
		  // click the navigation item corresponding to the anchor
		  //$('.Navi a[href="' + anchor + '"]').click();
  
	}
	else {
		// Col1
		changePage("yritys","show",true);
		changePage("yhteystiedot","hide",true);
			
		// Col2
		changePage("ajankohtaista","show"),true;
		changePage("uutiskirje","hide",true);
		
		// Col3
		//changePage("tyot","show",true);
		//changePage("asiakkaat","hide",true);
		
		changePage("tyot","hide",true);
		changePage("asiakkaat","show",true);
	}


	function linkToggle(linkTo, linkFrom, linkObject) {
		changePage(linkTo,"show");
		changePage(linkFrom,"hide");
		pageURLExtension = pageURLExtension.replace(linkFrom, linkTo);
		linkObject.attr("href","#"+pageURLExtension);
	}
	
	// Col1
	$(".Navi a.yritys, .link_yritys").click(function() {
		linkToggle("yritys", "yhteystiedot", $(this));
		scrollTo($(".ScrollContent1"));
	});

	$(".Navi a.yhteystiedot, .link_yhteystiedot").click(function() {
		linkToggle("yhteystiedot", "yritys", $(this));
		scrollTo($(".ScrollContent1"));
	});
	
	
	// Col2	
	$(".Navi a.ajankohtaista, .link_ajankohtaista").click(function() {
		linkToggle("ajankohtaista", "uutiskirje", $(this));
		scrollTo($(".ScrollContent2"));
	});

	$(".Navi a.uutiskirje, .link_uutiskirje").click(function() {
		linkToggle("uutiskirje", "ajankohtaista", $(this));
		scrollTo($(".ScrollContent2"));
	});
	
	
	// Col3	
	$(".Navi a.tyot, .link_tyot").click(function() {
		linkToggle("tyot", "asiakkaat", $(this));
		scrollTo($(".ScrollContent3"));
	});

	$(".Navi a.asiakkaat, .link_asiakkaat").click(function(e) {
		linkToggle("asiakkaat", "tyot", $(this));
		scrollTo($(".ScrollContent3"));
	});
	
	
	

	function changePage(targetPage, showOrHide, withoutEffect) {

		// Get stuff to arrays
		var naviLinks = $(".Navi").children("div").children("a"); // inactive navitabs
		var naviLinks_active = $(".Navi").children("div").children("span"); // Active navitabs
		var contents = $(".ScrollContents").children("div").children("div").children("div"); // Page contents
		
		if(withoutEffect==true) {
			var effect = "";	
		} else {
			var effect = "fast";
		}

		if(showOrHide == "show") {
			
			// Toggle inactive navitabs
			naviLinks.each(function () {
				if( $(this).attr("class") == targetPage && $(this).is(":visible") ) $(this).hide();
			});
			
			// Toggle active navitabs
			naviLinks_active.each(function () {
				if( $(this).attr("class") == targetPage && $(this).is(":hidden") ) $(this).show();
			});
			
			// Toggle page contents
			contents.each(function () {
				if( $(this).attr("id") == targetPage && $(this).is(":hidden") ) $(this).show(effect);
			});
			
		}
		else if(showOrHide == "hide") {

			// Toggle inactive navitabs
			naviLinks.each(function () {
				if( $(this).attr("class") == targetPage && $(this).is(":hidden") ) $(this).show();
			});
			
			// Toggle active navitabs
			naviLinks_active.each(function () {
				if( $(this).attr("class") == targetPage && $(this).is(":visible") ) $(this).hide();
			});
			
			// Toggle page contents
			contents.each(function () {
				if( $(this).attr("id") == targetPage && $(this).is(":visible") ) $(this).hide(effect);
			});

		}

	}


	var workListLinks = $('.blockWhite,.blockGrey');

	workListLinks.each(function () {

		var extraContent = $(this).children('.blockHiddenContent');
		extraContent.hide();

        $(this).click(function (e) {
			if(extraContent.is(':hidden')) extraContent.show("fast");
			else if(extraContent.is(':visible')) extraContent.hide("fast");

			//e.preventDefault();
		});

	});
   

});


// Facebook sharing
function fbs_click(u,t) { 
	if(u == undefined) {
		u = location.href;  
	}     
	if(t == undefined) {
		t = document.title;
	}
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}


// Scroll
function scrollTo(targetElement) {
	targetElement.animate({scrollTop: 0}, 0);
}

