function showMainPane(index) {
	$(".article-highlighted").hide();
	$(".article-highlighted:eq(" + index + ")").fadeIn(2000);	

	$(".home-content-link a").removeClass("selected");
	$(".home-content-link a:eq(" + index + ")").addClass("selected");
}

function rotateMainPane() {
	var index = 0;
	var filesLength = $(".home-content-link a").length;
	
	showMainPane(0);
	
	if (filesLength > 1) {
		$(document).everyTime(10000, "swapfiles", function() {
			index++;
			if (index == filesLength) index = 0;
			
			showMainPane(index);
		});
	}
}


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 handleSearchField() {
	$("input:text.SearchTextInput").each(function(){
		var $search = $(this);
		var strSearchTitle = (searchFieldLabel) ? searchFieldLabel : "";
	
		if ($search.val().length <= 0) {	
			inputBlur();
		}
		
		$search.focus(function(){
			$search.removeClass("text-blur").addClass("text");
			
			if ($search.val() == strSearchTitle) {
				$search.val("");
			}
		});
		
		$search.blur(function(){
			if ($search.val().length <= 0) {
				inputBlur();
			}
		});
		
		function inputBlur(){
			$search.val(strSearchTitle).removeClass("text").addClass("text-blur");	
		}
	});
}

function handleNavigation() {
	//	Mark selected root section
	$("#navigation a[title='" + $.trim(topSectionName) + "']").parents("li").addClass("selected");
	//	Mark selected secpnd level section
	$("#navlist-one a[title='" + $.trim(secondLevelSectionName) + "']").parents("li").addClass("selected");
	//	Mark selected third level section
	$("#page-content-navigation-two a[title='" + $.trim(thirdLevelSectionName) + "']").parents("li").addClass("selected");
}

function handleGalleryArticles() {
	$("div.gallery-article").each(function(){
		var $GalleryArticle = $(this);									
		
		//	Whole article click handler
		$GalleryArticle.click(function(){
			window.location = $GalleryArticle.find("a:first").attr("href");							  
		});
		
		$GalleryArticle.hover(
			function(){
				$GalleryArticle.addClass("gallery-article-hover");
			}, 
			function(){
				$GalleryArticle.removeClass("gallery-article-hover");
			}
		);
	});
}

function homeInitialize() {
	// 	Handle tabs on homepage
	$("#page-content-wrapper").pmTabs();
	
	//	Whole article handler for tab articles on homepage
	$("div.list-box").each(function(){
		var $article = $(this);
		
		$article.click(function(){
			window.location = $(this).find("h1 a:first").attr("href");							  
		});
		
		$article.hover(
			function(){
				$article.addClass("list-box-hover");
			}, 
			function(){
				$article.removeClass("list-box-hover");
			}
		);
	});
	
	//	Hide short articles on homepage when full article is shown
	$(".article-full").each(function(){
		$("div[id^='tabContainer']").hide();								   
	});
	
	// Main content handler
	var $ArticlesHiglighted = $("div.article-highlighted");
	if ($ArticlesHiglighted.length > 1) {
		$ArticlesHiglighted.each(function(i){
			var $ArticleHiglighted = $(this);
			
			var slideIndex = i+1;
			
			//	Whole article click handler
			$ArticleHiglighted.click(function(){
				window.location = $ArticleHiglighted.find("h1 a:first").attr("href");							  
			});
			
			$ArticleHiglighted.hover(
				function(){
					$ArticleHiglighted.addClass("article-highlighted-hover");
				}, 
				function(){
					$ArticleHiglighted.removeClass("article-highlighted-hover");
				}
			);
			
			//	Add pager
			var strSeparator = (i < $ArticlesHiglighted.length-1) ? " | " : "";
			$("div.home-content-link").append("<a href=../scripts/\"#\" title=\"\">" + slideIndex + "</a>" + strSeparator);
		});
	}
	
	//	Files auto rotate
	rotateMainPane();
	
	$(".home-content-link a").each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			$(document).stopTime("swapfiles");
			showMainPane(i);
		})
	});
	
	//	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"));
	}
}


function pageInitialize() {
	//	Set Google Analytics tracker
	$.gaTracker('UA-12370024-1');
	
	//	Search results and site map handler
	if (document.getElementById("search-results") || document.getElementById("site-map")) {
		$("#gallery-wrapper").hide();
	}
	
	//	Handle navigation
	handleNavigation();
	
	//	Handle search field
	handleSearchField();
	
	//	Handle article link to shop
	$(".article-shop-link").each(function(){
		var oLink = $(this);
		
		if (oLink.attr("href").length > 1) oLink.show();
	});
	
	//	Buld article image galleries
	$("div.article").pmArticleGallery({
		thumbImageWidth: 330,
		thumbImageHeight: 218,
		lightboxImageWidth: 500,
		galleryWrapper: "div.article-gallery-wrapper",
		galleryContainer: "div.article-gallery",
		galleryScroller: "div.jcarousel-scroll"
	});
	
	//	Handle gallery articles
	handleGalleryArticles();
	
	$("a.lightbox").lightBox({
		imageLoading: "designTemplates/meblo/images/lightbox-ico-loading.gif",
		imageBtnClose: "designTemplates/meblo/images/lightbox-btn-close.gif",
		imageBtnPrev: "designTemplates/meblo/images/lightbox-btn-prev.gif",
		imageBtnNext: "designTemplates/meblo/images/lightbox-btn-next.gif",
		txtImage: labelGalleryImage,
		txtOf: labelGalleryImageOf																													 
	});
	
	//	Handle forms
	$("div.form-field-text textarea").parent().toggleClass("form-field-textarea");
}