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 newsletterSubscriptionResponse() { 
	document.location = "http://www.perpetuum.hr/newsletter-prijava";
}

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();
	}
}	

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 homePageInitialize() {
	$("#twitter").getTwitter({
		userName: "perpetuumzg",
		numTweets: 5,
		loaderText: "Tweets...",
		slideIn: true,
		slideDuration: 750,
		showHeading: false,
		headingText: "Perpetuum Tweets",
		showProfileLink: false,
		showTimestamp: false
	});
}

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");
				}
			}
		});
	}
	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 player functionalities
	$("#video-player a")
		.prepend("<div class='player-wrapper'></div>")
		.click(function(event) {
			event.preventDefault();
			
			$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'centerOnScroll': 'true',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	});
	
	//	Handle forms
	$("div.form-field-text textarea").parent().removeClass("form-field-text").addClass("form-field-textarea");
	
	//	Set Google Analytics tracker
	$.gaTracker('UA-220380-7');
}
