
function showMainPane(index) {
	$(".homeMainFile").hide();
	$(".homeMainFile:eq(" + index + ")").fadeIn(2000);	

	$(".homeMainContentNavigation a").removeClass("selected");
	$(".homeMainContentNavigation a:eq(" + index + ")").addClass("selected");
}

function rotateMainPane() {
	var index = 0;
	var filesLength = $(".homeMainContentNavigation 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 doSearch() {
	var strSearchFieldLocatorValue = $(".searchFieldWrapper input:hidden").attr("value");
		
	if (strSearchFieldLocatorValue !== "") {
		MarkForm("6","1",strSearchFieldLocatorValue,'');
		window.iSite_SetCacheTrigger(null);
		document.forms[0].submit();
	}
}	

function homeInitialize() {
	//	Short news handler
	$(".inoutdexNews").accordion({
		autoheight: false,
		header: 'h3',
		alwaysOpen: false,
		active: 0
	});
	
	$(".inoutdexNewsContent img").wrap("<div class=inoutdexNewsImage></div>");

	if (!document.getElementById("searchResults") && !document.getElementById("siteMap")) {
		//	Expand Search control
		$(".searchControlContainer").animate({width:"490px"}, 1800, "swing");
		$(".searchControlExpander").animate({marginLeft:"680px"}, 1800, "swing").addClass("searchControlExpanderExpanded");
		
		//	Expand Product control		
		$(".productControlContainer").animate({width:"490px"}, 2000, "swing");
		$(".productControlExpander").animate({marginLeft:"680px"}, 2000, "swing").addClass("productControlExpanderExpanded");
	}

	// Main content handler
	if ($(".homeMainFile").length > 1) {
		$(".homeMainFile").each(function(i){
			var slideIndex = i+1;
			$(".homeMainContentNavigation:first").append("<a href=\"#\" title=\"\">" + slideIndex + "</a>");
		});
	}
	
	//	Files auto rotate
	rotateMainPane();
	
	$(".homeMainContentNavigation a").each(function(i){
		$(this).click(function(e){
			e.preventDefault();
			$(document).stopTime("swapfiles");
			showMainPane(i);
		})
	});
}

function pageInitialize() {
	//	Set UI skin
	$("h2.skin").addClass(topSectionSkin);
	
	//	Mark selected root section
	$("#navigation a").each(function(){
		var oLink = $(this);
		
		if (oLink.attr("href") !== "") {
			if (getSectionId(oLink.attr("href")) == topSectionId) {
				oLink.parent().addClass(topSectionSkin);	
			}
		}
	});

	//	Mark seleced current section
	$(".inoutdexSiteTree a").each(function(){
		var oLink = $(this);
		
		if (oLink.attr("href") !== "") {
			if (getSectionId(oLink.attr("href")) == currentSectionId) {
				oLink.parent().addClass("selected");
				oLink.parent().parents('li').addClass("selected");
			}
		}
	});
	
	//	Build 4-th level navigation
	var oSiteTreeThirdLevel = $(".inoutdexSiteTree ul ul ul");
	var oMainSiteTree = oSiteTreeThirdLevel.clone(); 
	
	oSiteTreeThirdLevel.hide();
	oMainSiteTree.appendTo(".mainSiteTree:first");
	$(".mainSiteTree li").addClass(topSectionSkin);
	
	//	Remove 4-th level selected section link
	$(".mainSiteTree a").each(function(){
		var oLink = $(this);
		
		if (oLink.attr("href") !== "") {
			if (getSectionId(oLink.attr("href")) == currentSectionId) {
				oLink.parent().hide();
			}
		}
	});
	
	//	Back button
	$(".buttonBack").click(function(){
		history.go(-1);							
	});
	
	// Send mail button
	$(".buttonEmail").click(function(){
		var strArticleTitle = $(".articleTitle:first span").text();
		window.location = "mailto:?Subject=" + document.title + " - " + strArticleTitle + "&body=" + document.location;							
	});
	
	// Print button
	$(".buttonPrint").click(function(){
		window.print();							
	});
	
	// Handle article links
	$(".articleLinks li > span").each(function(){
		if ($.trim($(this).text()) != "(, )") $(this).show();					 
	});
}

$(document).ready(function(){
	//	Handle First link in header navigation
	$("#navigation li:first").attr("style", "margin: 0");
	
	//	Search control handler
	$(".searchFieldWrapper input:text").keypress(function(e){
		if (e.which == 13) {
			doSearch();
		}
	});
	
	$("a.search").click(function(){
		doSearch();
	});
	
	$(".searchControlExpander").click(function(){
		if (!($(this).hasClass("searchControlExpanderExpanded"))) {
			$(".searchControlContainer").animate({width:"490px"}, 1000, "swing");
			$(this).animate({marginLeft:"680px"}, 1000, "swing").addClass("searchControlExpanderExpanded");	
		}
		else {
			$(".searchControlContainer").animate({width:"0px"}, 1000, "swing");
			$(this).animate({marginLeft:"197px"}, 1000, "swing").removeClass("searchControlExpanderExpanded");
		}
	});
	
	//	Product control handler
	$(".productControlExpander").click(function(){
		if (!($(this).hasClass("productControlExpanderExpanded"))) {
			$(".productControlContainer").animate({width:"490px"}, 1000, "swing");
			$(this).animate({marginLeft:"680px"}, 1000, "swing").addClass("productControlExpanderExpanded");	
		}
		else {
			$(".productControlContainer").animate({width:"0px"}, 1000, "swing");
			$(this).animate({marginLeft:"197px"}, 1000, "swing").removeClass("productControlExpanderExpanded");
		}
	});
	
	$(".productsWrapper").jCarouselLite({
		btnNext: ".rightSlider",
		btnPrev: ".leftSlider",
		mouseWheel: true,
		//auto: 12000,
		speed: 1200
	});
	
	//	Hide main navigation when search results or tag results is shown
	$("#searchResults, #tagResults").each(function(){
		$(".mainNavigation").hide();								   
	});
	
});