function getSectionId(str) {
    var myregexp = /sec=(\d{1,4})/;
    var match = myregexp.exec(str);
    
    if (match != null) {
        result = match[1];
    } else {
        result = "";
    }

    return result;
}

function popup(url) {
	params  = "width=" + screen.width + ", height=" + screen.height + ", scrollbars=1, address=1, toolbar=1, top=0, left=0, fullscreen=yes";
	
	$window = window.open(url,'popup', params);
	
	if (window.focus) {
		newwin.focus()
	}
	
	return false;
}


function doSearch() {
	var strSearchFieldLocatorValue = $("#search input[name$='SearchFormLocator']").val();
		
	if (strSearchFieldLocatorValue !== "") {
		MarkForm("6", "1", strSearchFieldLocatorValue,"");
		window.iSite_SetCacheTrigger(null);
		document.forms[0].submit();
	}
}	

function submitNewsletter() {
	MarkForm("5", newsletterFormId, "", newsletterRegionElementId);
	window.iSite_SetCacheTrigger(null);
	document.forms[0].submit();
}	

var t;

function handleNavigation() {
	var themes; 
	
	//	Layout for english viersion is, a bit diferent from croation version
	if ($("#language-picker a:first").text() == "English") {
		themes = new Array("greysteel","redrose", "orangefruit", "jellowgold", "greenleaf");
	} 
	else {
		themes = new Array("redrose-small", "orangefruit", "jellowgold", "greenleaf");
		
		$("#navigation").css("margin-left", "125px");
		$(".footer-navigation").css("margin-left", "60px");
		$(".footer-navigation-items").css("max-width", "190px");
	}
	
	$(".navigation-item").each(function(i){
			var navItem = $(this);
			
			//	Set themes for navigation items
			navItem.addClass(themes[i]);
			
			//	By default navigation items are hidden
			navItem.show();
			
			//	Show / hide white arrow
			navItem.mouseover(function(event){
				clearTimeout(t);
				hideNavItems() ;
				
				if (navItem.hasClass("selected")) {
					navItem.removeClass("selected");
					navItem.attr("rel", "selected");
				}									   
				
				navItem.css("height", "85px");
				navItem.css("z-index", "1000");
				navItem.find("span").addClass("marked");
				navItem.find(".menu-container").show();
			});
			
			navItem.mouseout(function(event){
				t = setTimeout(function() {
					var eventTargetObject = $(event.relatedTarget);
									
					if (!eventTargetObject.hasClass(themes[i]) && !eventTargetObject.parents().hasClass(themes[i])) {
							navItem.find("span").removeClass("marked");
							navItem.css("height", "30px");
							navItem.css("z-index", "0");
							navItem.find(".menu-container").hide();
						
							if (navItem.attr("rel") == "selected") {
								navItem.addClass("selected");
							}		
							
					}
				}, 1000);
			});		
	});
	
	function hideNavItems() {
		$(".navigation-item").each(function(i){
			var navItem = $(this);	
			
			navItem.find("span").removeClass("marked");
			navItem.css("height", "30px");
			navItem.css("z-index", "0");	
			navItem.find(".menu-container").hide();
		
			if (navItem.attr("rel") == "selected") {
				navItem.addClass("selected");
			}		
		});
	}
};

function setVideoPlayer(video, img, autoplay) {		
	var oSwf = new SWFObject("designtemplates/perpetuum/include/media/player.swf","player","248","190","9","#ffffff");
	var autostart = "";
	var image = "";	
	var flashvars = "";
		
	if(autoplay){
		autostart = "&autostart=true";
	}
	
	if (img != "") {
		image = "&image=" + img;
	}
	
	flashvars = "&file=" + video + autostart + image + "&backcolor=666666&frontcolor=ffffff&screencolor=EEEEEE";
	
	oSwf.addParam("allowfullscreen", "true");
	oSwf.addParam("allowscriptaccess", "always");
	oSwf.addParam("quality", "high"); 
	oSwf.addParam("wmode", "transparent"); 
	oSwf.addParam("salign", "t");
	oSwf.addParam("flashvars", flashvars);
	
	oSwf.addVariable("type", "video");
	
	oSwf.write("video-player");
}

function pageInitialize() {
	//	Hide content if main region is not empty
	if (document.getElementById("non-editable")) {
		if ($("#non-editable").html().length > 10) {
			//($("#page-content-container").remove());
			$("#non-editable").show();
		}
	}
	
	//	Set themes to main navigation
	handleNavigation();
	
	//	Mark selected root section
	$("#navigation span").each(function(){
		var oSpan = $(this);
		if (oSpan.html() != "" && topSectionName != "") {
			if (oSpan.html() == topSectionName) {
				oSpan.parent().addClass("selected");
			}
		}
	});
	
	//	Mark destination page tab for seleced current section
	if ($("#destination-page-navigation ul").length > 0) {
		var oRootTab = $("<li>");
		var oRootLink = $("<a>");
		
		oRootLink.text(destinationRootTabName);
		oRootLink.attr("href", "default.aspx?sec=" + secondLevelSectionId);
		oRootLink.appendTo(oRootTab);
		
		oRootTab.prependTo($("#destination-page-navigation ul"));
	}
	
	
	if (currentSectionId != secondLevelSectionId) {
		$("#destination-page-navigation a").each(function(){
			var oLink = $(this);
			
			if (oLink.attr("href") !== "") {
				if (oLink.text() == currentSectionName) {
					oLink.parent().addClass("selected");
					oLink.parent().parents("li").addClass("selected");
				}
				/*
				if (getSectionId(oLink.attr("href")) == currentSectionId) {
					oLink.parent().addClass("selected");
					oLink.parent().parents("li").addClass("selected");
				}
				*/
			}
		});
	}
	else {
		$("#destination-page-navigation a:first").each(function(){
			var oLink = $(this);
			oLink.parent().addClass("selected");
			oLink.parent().parents("li").addClass("selected");
		});
	}
	
	//	Search control handler
	$("#search input[name$='SearchDefaultSectionID']").val(searchDefaultSectionId);
	
	$("#search input:text").keypress(function(e){
		if (e.which == 13) {
			doSearch();
		}
	});
	
	$("#search a").click(function(){
		doSearch();
	});
	
	//	Newsletter form handler
	if (document.getElementById("newsletter")) {
		$("#newsletter input:text").keypress(function(e){
			if (e.which == 13) {
				submitNewsletter();
			}
		});
		
		$("#newsletter a").click(function(event){
			submitNewsletter();
			event.preventDefault();
		});
	}
	
	//	Survey form handler
	if (document.getElementById("survey")) {
		if ($.cookie("userUnknownform" + surveyFormId) != "true") {
			$(".survey-buttons .button-send").show();
		}
	}
	
	//	Survey results handler
	if (document.getElementById("survey-results")) {
		$(".survey-container").replaceWith($("#survey-results"));
	}
	
	//	Video in side bar handler
	$("#side-bar .article-links a:first").each(function() {
		//	First article link is video
		$("#side-bar .article-links:first").before("<div id='video-player'><a href='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash'>Download Adobe Flash Player</a></div>");
		
		//	If exist second article link is video image 
		var img = $("#side-bar .article-links a")[1];
		
		setVideoPlayer(this, img, false);									   
	});
	
	//	Handle blog links in the side bar on first page
	$("div.link-blog").each(function(){
		var $this = $(this);
		var $img = $this.find("img:first");
		var $link = $this.find("a:first");
		var $description = $this.find("span.blog-description");
		
		$link.html($img.attr("alt")).attr("target", "_blank");
		$("<div class='blog-image'>").append($img).prependTo($this);
		$this.append($description);
		/*
		$this.click(function(event){
			event.preventDefault();
			popup($link.attr("href"));					 
		});
		*/
	});

	//	Handle forms
	$("div.form-field-text textarea").parent().removeClass("form-field-text").addClass("form-field-textarea");


	//	Set Google Analytics tracker
	$.gaTracker('UA-220380-7');
	
	//	Monitoring functionality
	lpAddMonitorTag();
}