var hoverTime;

function returnImages() {
	var hLeaves = $("#headerTop .leaves");
	var mainImage = $(".mainImage");
	var fLeaves = $("#footer .leaves");
	hLeaves.hide().css("background-position", "0 0").show();
	mainImage.fadeOut(200, function() {
		$(this).css("background-position", "0 0")
	}).fadeIn(200);
	fLeaves.hide().css("background-position", "0 0").show();
}

function showHover(currentContainer) {
	clearTimeout(hoverTime);
	var fState = currentContainer.find(".firstState");
	var sState = currentContainer.find(".secondState");
	var hLeaves = $("#headerTop .leaves");
	var mainImage = $(".mainImage");
	var fLeaves = $("#footer .leaves");
	currentContainer.find(".title").css("background-position", "0 -46px");
	var this_id = currentContainer.attr("id");
	switch (this_id) {
		case 'c1' :
			hLeaves.hide().css("background-position", "0 -408px").show();
			mainImage.fadeOut(100, function() {
				$(this).css("background-position", "0 -280px")
			}).fadeIn(100);
			fLeaves.hide().css("background-position", "0 -121px").show();
			break;
		case 'c2' :
			hLeaves.hide().css("background-position", "0 -136px").show();
			mainImage.fadeOut(100, function() {
				$(this).css("background-position", "0 -840px")
			}).fadeIn(100);
			fLeaves.hide().css("background-position", "0 -242px").show();
			break;
		case 'c3' :
			hLeaves.hide().css("background-position", "0 -272px").show();
			mainImage.fadeOut(100, function() {
				$(this).css("background-position", "0 -560px")
			}).fadeIn(100);
			fLeaves.hide().css("background-position", "0 -363px").show();
			break;
		default :
			hLeaves.hide().css("background-position", "0 0").show();
			mainImage.fadeOut(100, function() {
				$(this).css("background-position", "0 0")
			}).fadeIn(100);
			fLeaves.hide().css("background-position", "0 0").show();
			break;
	}
	fState.hide();
	currentContainer.find(".content").css("background-position", "0 -96px").css("height", "348px");
	sState.show();
}
function hideHover(currentContainer) {
	var fState = currentContainer.find(".firstState");
	var sState = currentContainer.find(".secondState");
	var hLeaves = $("#headerTop .leaves");
	var mainImage = $(".mainImage");
	var fLeaves = $("#footer .leaves");
	currentContainer.find(".title").css("background-position", "0 0");
	sState.hide();
	currentContainer.find(".content").css("background-position", "0 0").css("height", "85px");
	fState.show();
	hoverTime = setTimeout("returnImages()", 300);
}

$(document).ready(function() {
	var sState = $(".secondState");
	sState.hide();
	
	var bottomMenuItems = $("#bottomMenu").find("li").not(".first, .second, .last");
	var zIndex = bottomMenuItems.length;
	var offset = 6;
	var offsetStep = offset;
	$("#bottomMenu").find("li.second").css("z-index", zIndex + 1);
	bottomMenuItems.each(function() {
		$(this).css("left", "-" + offset + "px").css("z-index", zIndex)
		offset += offsetStep;
		zIndex -= 1;
	});
	bottomMenuItems.hover(function() {
		$(this).addClass("li-hover");
	}, function() {
		$(this).removeClass("li-hover");
	});
	
	var extSections = $(".extendingSections").find(".oneSection").not(".fourthSection").find(".container");
	extSections.hover(function() {
		showHover($(this));
	}, function() {
		hideHover($(this));
	});
	
	$("#productsMenu .content, .bgTitle").not(".firstbgTitle, .secondTitle, .thirdbgTitle").corner("7px");
	$(".firstbgTitle").corner("tl bl 7px");
	$(".thirdbgTitle").corner("tr br 7px");
	$(".prodContainer").parent().css("background-color", "#dedede").css("padding", "1px").corner("5px").children(".prodContainer").css("background-color", "#fff").corner("5px");
	$(".picOfTheWeek").parent().css("background-color", "#dedede").css("padding", "1px").corner("5px").children(".picOfTheWeek").css("background-color", "#fff").corner("5px");
	$(".servicesSection").parent().css("background-color", "#dedede").css("padding", "1px").corner("5px").children(".servicesSection").css("background-color", "#fff").corner("5px");
	
	var prodMenu = $("#productsMenu");
	prodMenu.find(".content").children().find("ul").css("display","none");
	var selectedLink = prodMenu.find(".selected");
	selectedLink.parent().addClass("selectedLI");
	var openedParentLists = selectedLink.parents("ul");
	var openedChildList = selectedLink.parent().children("ul");
	var openedLists = $(".topLink").next("ul");
	openedParentLists.show();
	openedChildList.show();
	openedLists.show();
	
	$(".menuShower").click(function() {
		var childrenList = $(this).next();
		var otherLists = $(this).not(".topLink").parent().siblings().find("ul");
		if(childrenList.is(":hidden")) {
			otherLists.slideUp(200);
			childrenList.slideDown(200);
		}
		else {
			childrenList.slideUp(200);
		}
		return false;
	});
	
	$(".infoLinks a").click(function() {
		$(this).siblings().removeClass("current");
		$(this).addClass("current");
		var this_ID = $(this).attr("id");
		var descSection = $("#" + this_ID + "s");
		descSection.siblings().slideUp(200);
		descSection.slideDown(200);
		return false;
	});
	
	hs.graphicsDir = '/js/highslide/graphics/';
	hs.align = 'center';
	hs.transitions = ['expand', 'crossfade'];
	hs.outlineType = 'rounded-white';
	hs.fadeInOut = true;
	//hs.dimmingOpacity = 0.75;

	// Add the controlbar
	if (hs.addSlideshow) hs.addSlideshow({
		//slideshowGroup: 'group1',
		interval: 3000,
		repeat: false,
		useControls: true,
		fixedControls: false,
		overlayOptions: {
			opacity: 1,
			position: 'top right',
			hideOnMouseOut: false
		}
	});
	
	var currContentHeight = 0;
	var maxContentHeight = 0;
	var currTitleHeight = 0;
	var maxTitleHeight = 0;
	var oneRowLevel = $(".oneRowLeveler");
	oneRowLevel.each(function() {
		var titleLeveler = $(this).find(".titleLeveler");
		//console.log(titleLeveler);
		titleLeveler.each(function() {
			currTitleHeight = $(this).height();
			if (currTitleHeight > maxTitleHeight) {
				maxTitleHeight = currTitleHeight;
			}
		});
		titleLeveler.height(maxTitleHeight);
		//console.log(maxTitleHeight);
		currTitleHeight = 0;
		maxTitleHeight = 0;
		
		var contentLeveler = $(this).find(".contentLeveler");
		//console.log(contentLeveler);
		contentLeveler.each(function() {
			currContentHeight = $(this).height();
			if (currContentHeight > maxContentHeight) {
				maxContentHeight = currContentHeight;
			}
		});
		contentLeveler.height(maxContentHeight);
		//console.log(maxContentHeight);
		currContentHeight = 0;
		maxContentHeight = 0;
	});
});