﻿jQuery(document).ready(function() {
 	

	// Iframeen avoin hakulomake työpaikkoihin
 	jQuery("#iframe_hakulomake").html("<iframe src=http://www.are.fi/hakulomake/avoinhae.aspx width='750' height='4300' style='border: 0px;'>Selaimesi ei tue iframeja.</iframe>");
 	
 	// Ingressin piilotus, mikäli se on tyhjä
 	if ( jQuery.trim(jQuery("#are_description div").text()) == '' ) {
 		jQuery("#are_description").css("display","none");
 	}
 	
 	// Apuotsikon piilotus, mikäli se on tyhjä
 	if ( jQuery.trim(jQuery("#apuotsikko").text()) == '' ) {
 		jQuery("#apuotsikko").css("display","none");
 	}

 	
 	// Palvelu-sivun alatabien rullaukset
 	jQuery("#palvelut #WebPartWPQ1 span").live('click', function() {
	 	jQuery(window).scrollTo("#are_wrapper2");
 	});
 	
 	// Are navigaatiosta valitun elementin jälkeisen elementin reunan poisto ja viimeisen viivan...
 	jQuery("img[src$='globalnav-separator.png']:last").remove();
 	jQuery(".sgNavHeaderActive").parents("td:first").next().next().find("td").css({
 		borderLeft: "none",
 		paddingLeft: "1px"
 	});
	jQuery("table.globalNavItemActive").parent().prev().prev().css("visibility","hidden");
	jQuery("table.globalNavItemActive").parent().next().next().css("visibility","hidden");
	
	// Third navi hide separator from left side of selected
	
	jQuery(".sgNavHeaderActive.third").parent("td").prev().prev().children("table:first").css("borderRight", "none");
	
	// Navigation mouseover fix for IE
	/*jQuery("table.globalNavItem").mouseover(function () {
		jQuery(this).css("background", "url('/Style%20Library/images/menu_tabs_active.gif') no-repeat right");
	});
	jQuery("table.globalNavItem:not(.globalNavItemActive)").mouseout(function () {
		jQuery(this).css("background", "none");
	});*/
 
 	// calculate map position
	if (jQuery("#areMap").length == 1) {;
		document.mapTopPosition = jQuery("#areMapContainer").offset().top;
		placeMap(document.mapTopPosition);
		jQuery(window).resize(function() {
      		placeMap(document.mapTopPosition);
  		});
		jQuery(window).scroll(function() {
      		placeMap(document.mapTopPosition);
  		});
  	}
 	
	function placeMap (amount) {

 		if (jQuery(window).scrollTop() >= amount)
			jQuery("#areMap").css("top", 0);
 		else
			jQuery("#areMap").css("top", amount - jQuery(window).scrollTop() + "px");
	}

	// hide submenu, if empty
	if (jQuery("#are_submenu").children().length == 0) {
		jQuery("#are_submenu").hide();
		jQuery("body").css("backgroundImage","url('/style%20library/images/header_frontpage.gif')");
	}
 
	initRightMenu();
 
 	// Putkistouudistuksen yhteystietolomake
 	jQuery("#ota-yhteytta-lomake").html("<iframe src= http://www.are.fi/FI/putkistouudistus/Lists/OtaYhteytta/CustomNewForm.aspx width='650' height='300' scrolling='no' style='border: 0px;'>Selaimesi ei tue iframeja.</iframe>");

 
 	// Putkistouudistuksen videot
	jQuery("#putkienpinnoitus_swf").append('<object height="450" width="600"><param value="opaque" name="wmode"><param value="true" name="allowfullscreen"><param value="always" name="allowscriptaccess"><param value="http://www.are.fi/FI/putkistouudistus/Documents/are_saneeraus_5.swf?width=800&amp;height=600" name="movie"><embed height="450" width="600" wmode="opaque" allowscriptaccess="always" allowfullscreen="true" type="application/x-shockwave-flash" src="http://www.are.fi/FI/putkistouudistus/Documents/are_saneeraus_5.swf?width=800&amp;height=600"></object>');
	jQuery("#viemariensukitus_swf").append('<object height="450" width="600"><param value="opaque" name="wmode"><param value="true" name="allowfullscreen"><param value="always" name="allowscriptaccess"><param value="http://www.are.fi/FI/putkistouudistus/Documents/Are_Sukitus.swf?width=800&amp;height=600" name="movie"><embed height="450" width="600" wmode="opaque" allowscriptaccess="always" allowfullscreen="true" type="application/x-shockwave-flash" src="http://www.are.fi/FI/putkistouudistus/Documents/Are_Sukitus.swf?width=800&amp;height=600"></object>');

	// handle the tab navigation
				jQuery.expr[':'].contains = function(a,i,m){
				    return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
				};
				
				(function (jQuery) {
				    jQuery.extend({      
				        getQueryString: function (name) {           
				            function parseParams() {
				                var params = {},
				                    e,
				                    a = /\+/g,  // Regex for replacing addition symbol with a space
				                    r = /([^&=]+)=?([^&]*)/g,
				                    d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
				                    q = window.location.search.substring(1);
				
				                while (e = r.exec(q))
				                    params[d(e[1])] = d(e[2]);
				
				                return params;
				            }
				
				            if (!this.queryStringParams)
				                this.queryStringParams = parseParams(); 
				
				            return this.queryStringParams[name];
				        }
				    });
				})(jQuery);
				
				jQuery(document).ready(function(){
				
				// fetch tab and anchor
				
				var tab = jQuery.getQueryString('tab');
				var anchor = jQuery.getQueryString('a')
				window.intervalCount = 0;
				window.intervalMax = 30;
				
				// check if tab exists
				if(jQuery(".sgInactive, .sgActive").has("a:contains('" + tab + "')").length == 1) {
					// open tab
						jQuery(".sgInactive a:contains('" + tab + "')").click();
				
					// attempt to scroll to the anchor
					if (anchor) {
						window.interval = window.setInterval('scrollToAnchor("' + anchor + '")', 100);
					}
				
					}
				
				});
				if (jQuery.browser.msie) {
					if (parseInt(jQuery.browser.version) < 7) {
						jQuery("body").children().hide();
						jQuery("body").removeAttr("style");
						jQuery("body").addClass("clearscreen").append("<a href='http://www.mozilla.org/firefox'><img border='0' src='/Style%20Library/Images/obsolete.gif' alt='Your browser is obsolete' /></a>");
					}
					else
						forceReDraw();
				}
				var polku = window.location.pathname;
				var fi = polku.split("/");
				if(fi[1].toLowerCase() == "en"&&jQuery("#tyopaikatflash").length){
					jQuery("#frontpageflash").append('<object width="310" height="280"><param name="wmode" value="transparent"><param name="FlashVars" value="pathToXML=/EN/flash/karuselli.xml"><param name="movie" value="/FI/flash/Are.fi_karuselli.swf"><embed src="/FI/flash/Are.fi_karuselli.swf" width="310" height="280" FlashVars="pathToXML=/EN/flash/karuselli.xml"></embed></object>');
					var so = new SWFObject("/EN/flash/tyopaikat.swf", "tyopaikatflash", "200", "256", "7", "#fff");
					so.addParam("quality", "high");
					so.addParam("FlashVars", "typeid=season&link=/EN/recruitment/Pages/default.aspx");
					so.addParam("wmode", "transparent");
					so.write("tyopaikatflash");
				}
				else if(fi[1].toLowerCase() == "fi"&&jQuery("#tyopaikatflash").length){
					jQuery("#frontpageflash").append('<object width="310" height="280"><param name="wmode" value="transparent"><param name="FlashVars" value="pathToXML=/FI/flash/karuselli.xml"><param name="movie" value="/FI/flash/Are.fi_karuselli.swf"><embed src="/FI/flash/Are.fi_karuselli.swf" width="310" height="280" FlashVars="pathToXML=/FI/flash/karuselli.xml"></embed></object>');
					var so = new SWFObject("/FI/flash/tyopaikat.swf", "tyopaikatflash", "200", "256", "7", "#fff");
					so.addParam("quality", "high");
					so.addParam("FlashVars", "typeid=season&link=/FI/tyopaikat/Sivut/default.aspx");
					so.addParam("wmode", "transparent");
					so.write("tyopaikatflash");
				}
				
	// Snoobi-toiminto tabeihin
	jQuery(".sgInactive a").click(function () {
		var currenturl = new String(document.location);
		snoobi.trackPageView(currenturl.substring(currenturl.lastIndexOf("/"), currenturl.length - 5) + "_tab_" + jQuery(this).attr("href").substring(1));
	});
	
	// hide content from labels, show at frontpage "Ajankohtaista" or "News"
	if (new String(document.location).toLowerCase().indexOf("/fi/") != -1){
		jQuery(".sgHideFromFI").remove();
		jQuery("#etusivun-uutiset").text("Ajankohtaista");
	}
	if (new String(document.location).toLowerCase().indexOf("/en/") != -1){
		jQuery(".sgHideFromEN").remove();
		jQuery("#etusivun-uutiset").text("News");	
	}
	
	// Translate search functinality into English
	if (new String(document.location).toLowerCase().indexOf("/en/") != -1) {
		jQuery(".box1>a").text("Contact Us").attr("href", "/EN/aboutare/businesslocations/Pages/default.aspx");
		jQuery("#ctl00_SearchBox").find("img").replaceWith("<img style=\"BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px\" onmouseover=\"this.src='/Style%20Library/Images/search.gif'\" title=\"Go Search\" onmouseout=\"this.src='/Style%20Library/Images/search.gif'\" alt=\"Go Search\" src=\"/Style%20Library/Images/search.gif\">");
	}
	jQuery("#are_searcharea").show();
	
	// handle frontpage "kohdeHaku"
	function kohdeHaku() {
	  	jQuery("form:last").submit();
	}

//
// Etusivun remonttihaun piilotus osoitteesta riippuen
//
	var polku = window.location.pathname;
	if (polku.toLowerCase().indexOf("fi/sivut/etusivu.aspx") != -1) {
		jQuery("#bottom").css("display", "block");
		jQuery("#auki").show();
	}


//
// Piilotetaan rekrylomakkeen asteriskit dropdownien päältä
//


});

// show selected page on right hand menu
function initRightMenu () {
	jQuery(".rightMenuItem").filter(function () { 
		return jQuery.trim(jQuery(this).text()) == jQuery.trim(jQuery(".pitkaotsikko:first").text())
	}).addClass("currentItem").find("a").removeAttr("href");
}

function scrollToAnchor (anchor) {
	if (jQuery("a[name='" + anchor + "']").length != 0) {
			jQuery.scrollTo('a[name="' + anchor + '"]');
			window.clearInterval(interval);
	}
	else {
		window.intervalCount++;
		if (window.intervalCount > intervalMax)
			window.clearInterval(interval);
	}
}

function forceReDraw() {
	jQuery("#are_wrapper").hide();
	jQuery("#are_wrapper").show();
}
