// site jquery
$(document).ready(function(){

	//===========================================
	// CONSTANTS - ADJUST TO LAYOUT
	var slideWidth = 930;
	var slideTime = 800;	// ms
	var topMargin = 25;
	var wrapperWidth = 930;
	var wrapperHeight = 659;
	var contentHeight = 490;
	
	var docPages = [
		{title:"[IN]PLACE Products | Home",											pageId: "1100",	parentId: "1100"},
		{title:"[IN]PLACE Products | System",										pageId: "1105",	parentId: "1100"},
		{title:"[IN]PLACE Products | File Folders",									pageId: "1110",	parentId: "1100"},
		{title:"[IN]PLACE Products | Hanging Folders",								pageId: "1115",	parentId: "1100"},
		{title:"[IN]PLACE Products | Expanding Folders",							pageId: "1120",	parentId: "1100"},
		{title:"[IN]PLACE Products | File Jackets & Specialty Folders",				pageId: "1125",	parentId: "1100"},
		{title:"[IN]PLACE Products | End Tab Filing",								pageId: "1130",	parentId: "1100"},
		{title:"[IN]PLACE Products | Filing Accessories",							pageId: "1135",	parentId: "1100"},
		{title:"[IN]PLACE Products | Notebooks, Clipboards, & Sheet Protectors",	pageId: "1140",	parentId: "1100"}
	];
	
	// if set to true, the slide animation will start after the start page animation instead of at the same time
	var slideAfterStartPage = false;
	var defaultSubNavId = "inplace_system_sub_nav";
	var subNavIdSuffix = "_sub_nav";
	var startpageSectionImgPrefix = "startpage_section_img_";
	
	//===========================================
	// STATE VARIABLES - PLEASE DON'T EDIT
	var startPageShowing = true;
	var startPageButtonClicked = true;
	var slideNext = 0;
	var slideTotal = $(".section").length;
	
	// set current slide
	var activeSubNavId;
	var locationHash = window.location.hash.replace(subNavIdSuffix,"");
	
	// preload these images in the background
	var preloadImagesQueue = [
		"images/header_active.jpg",
		"images/inplace_system_sub_nav_active.png",
		"images/file_folders_sub_nav_active.png",
		"images/hanging_folders_sub_nav_active.png",
		"images/expanding_folders_sub_nav_active.png",
		"images/file_jackets_sub_nav_active.png",
		"images/end_tab_sub_nav_active.png",
		"images/filing_accessories_sub_nav_active.png",
		"images/notebooks_sub_nav_active.png",
	null];
	
	
	//===========================================
	// INIT
	setHeaderTopMargin();
	setContentWidth();
	setStartPage( true );
	preloadImages();
	
	
	
	//===========================================
	// RESIZE
	$(window).resize(function(){
	 	setHeaderTopMargin();
		setContentWidth();
	});
	
	//===========================================
	// HEADER
	$('#header').hover(function () {
		// roll-over
		if(!startPageShowing){
			$(this).css('backgroundImage','url("images/header_active.jpg")');
		}
		
		// roll-out
 	}, function () {
		$(this).css('backgroundImage','url("images/header.jpg")');
		
 	});

	$('#header').click(function () {
		//setStartPage( !startPageShowing );
		setStartPage( true );
		$(this).css('backgroundImage','url("images/header.jpg")');
	});
	
	
	//===========================================
	// STARTPAGE
	$('.startpage_section_box_img, .startpage_right_img').hover(function () {
		// roll-over
		$(this).attr('src',"images/startpage/"+this.id+"_active.jpg");
		
		// roll-out
 	}, function () {
		$(this).attr('src',"images/startpage/"+this.id+".jpg");
		
 	});

	$('.startpage_section_box_img').click(function () {
		$(this).attr('src',"images/startpage/"+this.id+".jpg");
		//goToSection( this.id.replace( startpageSectionImgPrefix, ""), 0 );
		var subNavId = this.id.replace( startpageSectionImgPrefix, "") + subNavIdSuffix;
		startPageButtonClicked = true;
		$("#"+subNavId).click();
	});
	
	$('.startpage_right_img').click(function () {
		$(this).attr('src',"images/startpage/"+this.id+".jpg");
		window.location.href = "http://www.inplacesystem.com/";
	});
	
	
	//===========================================
	// SUB NAV (TABS)
	// generic rollovers for nav
	$('div.button').hover(function () {
		if (this.id != activeSubNavId || startPageShowing) {
			$(this).css('backgroundImage','url("images/' + this.id + '_active.png")');
		};
 	}, function () {
 		if (this.id != activeSubNavId || startPageShowing) {
			$(this).css('backgroundImage','url("images/' + this.id + '.png")');
		};
 	});
	
	$('div.sub_nav.button').click(function () {
		if( !startPageShowing || startPageButtonClicked ){
			goToSection( this.id, $(this).parent().parent().children().index($(this).parent()) );
			startPageButtonClicked = true;
		}
	});
	
	
	//===========================================
	// NAVIGATION
	// sets current tab and section and slides frame
	function goToSection( newActiveNavId, slideNextIndex ){
		if(activeSubNavId=="" || newActiveNavId != activeSubNavId) {
			slideNext = slideNextIndex;
			setActiveSubNav( newActiveNavId );
			setLocationHash( newActiveNavId, slideNext+1 );
		}
		
		if(startPageShowing){
			if( !slideAfterStartPage ){
				slide();
			}
			setStartPage( false );
		} else {
			slide();
		}
	}
	
	function setStartPage( visible ) {
		startPageShowing = Boolean( visible );
		
		if( !startPageShowing && (activeSubNavId==null || activeSubNavId.length==0)){
			activeSubNavId = defaultSubNavId;
		}
		setSubNavStatus( activeSubNavId, !startPageShowing );
		
		if( startPageShowing ){
			$("#header").css("cursor","default");
			$("div.sub_nav.button").css("cursor","default");
			setLocationHash( "", 0 );
		} else {
			$("#header").css("cursor","pointer");
			$("#header").css("cursor","hand");
			$("div.sub_nav.button").css("cursor","pointer");
			$("div.sub_nav.button").css("cursor","hand");
		}
		
		var startPageDestination = startPageShowing ? 0 : -parseInt( $("#startpage").css("height") );
		$("#startpage").animate({marginTop:startPageDestination + "px" }, slideTime, "easeInOutCubic", onStartPageAnimationDone);
		
		//var subNavDestination = startPageShowing ? -parseInt( $("#sub_nav").css("height") ) : 0;
		var subNavDestination = startPageShowing ? -65 : 0;
		$("#sub_nav").animate({marginTop:subNavDestination + "px" }, slideTime, "easeInOutCubic");
		$("#footer").animate({bottom: Number(4 - subNavDestination) + "px" }, slideTime, "easeInOutCubic");
	};
	
	
	function setActiveSubNav( newActiveNavId ){ 
		if(activeSubNavId && activeSubNavId.length>0){
			deactivateSubNav( activeSubNavId );
		}
		activateSubNav( newActiveNavId );
		activeSubNavId = newActiveNavId;
	}
	
	function activateSubNav( subNavId ){
		setSubNavStatus( subNavId, true );
	}
	
	function deactivateSubNav( subNavId ){
		var newId = setSubNavStatus( subNavId, false );
		$('div#'+subNavId).removeAttr('id').attr('id', newId);
	}
	
	
	function setLocationHash( hash, index ){
		if( hash && hash.length > 0 ){
			hash = hash.replace(subNavIdSuffix, "");
			hash = hash.replace("_","-");
		}
		
		window.location.hash = "#" + hash;
		document.title = docPages[index].title;
		cmSetProduction();
		cmCreatePageviewTag(docPages[index].pageId, docPages[index].parentId, null, null);
	}
	
	
	//===========================================
	// ANIMATION / DISPLAY STATES
	function onStartPageAnimationDone(){
		// slide to target section if start page was hidden
		if(!startPageShowing && slideAfterStartPage){
			slide();
		}
	}
	
	function setSubNavStatus( subNavId, active ){
		if(subNavId == null || subNavId.length==0){
			return null;
		}
		
		// remove or add '_active' from id if necessary
		var newId = subNavId;
		if( active ){
			if( subNavId.indexOf("_active")==-1 ){
				newId += "_active";
			}
		} else {
			newId = newId.replace("_active","");
		}
		
		$('div#'+subNavId).css('backgroundImage','url("images/' + newId + '.png")');
		return newId;
	}
	
	
	function slide() {
		var slideDestination = -slideWidth * (slideNext);
		
		// jump directyl to section if coming from start page
		if( startPageShowing ){
			$("#content").css('marginLeft', slideDestination + "px");
			
		// slide to section if already inside content
		} else {
			$("#content").animate({marginLeft:slideDestination + "px" },slideTime,"easeInOutCubic");
		}
	};
	
	
	//===========================================
	// WINDOW RESIZING
	function setHeaderTopMargin() {
		var headerTopMargin = $(window).height()-wrapperHeight;

		if (headerTopMargin < topMargin && headerTopMargin > 0) {
			$('#wrapper_content').css('margin-top', headerTopMargin + 'px');
			$('#wrapper').css('height', (wrapperHeight + headerTopMargin) + 'px');
		} else if (headerTopMargin < 0) {
			$('#wrapper_content').css('margin-top', '0px');
			$('#wrapper').css('height',  wrapperHeight + 'px');
		} else {
			$('#wrapper_content').css('margin-top', topMargin + 'px');
			$('#wrapper').css('height',  (wrapperHeight + topMargin) + 'px');
		}	
	};

	function setContentWidth() {
		var contentWidth = $(window).width();
		
		if (contentWidth < wrapperWidth) {
			contentWidth = wrapperWidth;
			$('#wrapper_content').css('margin-left', '0');
			$('#wrapper_content').css('margin-right', '0');
			$('#wrapper').css('width', contentWidth + 'px');
			$('#footer').css('width', contentWidth + 'px');
		} else {
			var contentMargin = (contentWidth - wrapperWidth) / 2;
			$('#wrapper_content').css('margin-left', contentMargin + 'px');
			$('#wrapper_content').css('margin-right', contentMargin + 'px');
			$('#wrapper').css('width', contentWidth + 'px');
			$('#footer').css('width', contentWidth + 'px');
		}
	};
	
	
	//===========================================
	// PRELOADING
	function preloadImages(){
		for( var i=0; i<preloadImagesQueue.length; i++ ){
			if( preloadImagesQueue[i] ){
				var img = new Image();
				img.src = preloadImagesQueue[i];
			}
		}
	}
	
	
	//===========================================
	// check for deeplink
	if( locationHash && locationHash.length>1 ){
		// click the navigation item corresponding to the anchor
		if( locationHash.indexOf( subNavIdSuffix ) == -1 ){
			locationHash += subNavIdSuffix;
		}
		
		locationHash = locationHash.replace("-","_");
		
		$(locationHash).click();
	}
});