﻿var currViewTxt = "currently viewing", assetList="", firstload=true, fL=true, extraAtrs="";
var mmList="", extList="", btnGoList="", currentState="", addHighLight="", currIdx;
var agent = navigator.userAgent.toLowerCase();
var iphone =  (agent.indexOf("iphone")!= -1 || agent.indexOf("ipad") != -1);

$(document).ready(function() {
	
	
	
	
	if (iphone){
		$("body").addClass("iphone");
	}
	if ( ($.browser.msie && $.browser.version>7) || !$.browser.msie) {
		$(window).resize(updateLayout);
		$(window).resize(adjustFooterHeight);
		$("#contentWrapper").css("visibility","hidden");
		$("section.mmNavWrapper").css("top", -70);
		
		
		
		$("section.mmNavWrapper").css("visibility", "hidden");
		$("section.mmNavWrapper nav").css("visibility", "hidden");
		$("section#utilNavWrapper").css("top", -40);
		$("section#uniNavWrapper").css("top", -91);
		$("#logo").css("top", -100);
		$("section#utilNavWrapper, section#uniNavWrapper").css("visibility","hidden");
		
		
		if ( !$("body").hasClass("insidePage") ){
			
			$("div#footerWrapper").css("top",100);
		}
		
		
		if ( iphone ){
			//alert('');
			//$("div#footerWrapper").css("top",3000);
		}
		
	}

	$("<div class='expander'>&nbsp;</div>").insertBefore(".sf-menu li li a");
	$(".sf-menu li ul").addClass("sub");
	
	
	$('ul.sf-menu li li').hover(
		function() { $(this).addClass("highlight"); $(this).find('.expander').animate( {"height": "39", "opacity": 1},200) },
		function() { $(this).removeClass("highlight"); $(this).find('.expander').animate(  {"height": "0","opacity": .2},200 ) }
	);


	$("ul.sf-menu").superfish();


	
	//no child selector support for IE...so we have to add some classes w/JS
if ( $.browser.msie ) {

	$("#uniNavWrapper nav ul:eq(0)").children("li").each(function(i){

		
		$(this).attr("id", "uniNav_" + parseInt($(this).index()+1) );
		
		if ( ($(this).index() == 2) || ($(this).index() == 3) ) {
			$(this).addClass("right");
		}
		
	});
}
		

		$("#uniNavWrapper nav ul li a").click(function(e){
			
			
			if( $(this).attr("href").indexOf('//') != -1 ){
				
				location.href=$(this).attr("href");
				
			}else if ( $(this).attr("href") != "/en/" && !$.browser.msie){
				e.preventDefault();
				
				$.address.value($(this).attr('href'));
				
				
				$.address.title("Avant - "+$(this).text());
				
				$("#uniNavWrapper nav ul li").removeClass("current");
				
				
				if ( $(this).parents("ul").attr("class") == "sub" ){
				
					//console.log($(this).parents);
				
					$(this).parent("li").addClass("current");
					
				}
				
				//trackAnchor = $(this).parent("li").index();
			//	console.log(trackAnchor);
			}
			return;
		});
	//$("section.modTypeBio .bioImg img:eq(0)").addClass("on");
	
	if ($("section.modTypeBio .bioImg img").size() > 1){
		 setInterval(slideShow, 3000);
	}
	
	
});

$(window).load(function() {	
	
	if ( ($.browser.msie && $.browser.version>7) || !$.browser.msie  ) {
		startAnim();
		updateLayout();
	}
	
	if (!iphone){	
		
		//console.log($("body").attr("class"));
		
		adjustFooterHeight();
	}	
	
	
	
	
	if ($(".mmNavWrapper nav").length > 0){
		loadMM($(".mmNavWrapper nav").attr("id"));
	}
	
	
});


function startAnim(){
	
	$("section#utilNavWrapper, section#uniNavWrapper").css("visibility","visible");
	
	//UTIL NAV 	
	$("section#utilNavWrapper").animate(
		{top: '+=40'}, //animating
		{
			duration: 500, //how fast
			easing: 'easeOutBack',  //type of easing
			complete: function() { //callback
				//UNI NAV
				$("section#uniNavWrapper").animate(
					{top: '+=105'}, //animating
					{
						duration: 500, //how fast
						//easing: 'easeOutBounce',  //type of easing
						complete: function() { //callback
							//logo
							$("#logo").animate(
								{top: '+=105'}, //animating
								{
									duration: 500, //how fast
									easing: 'easeOutCubic',  //type of easing
									complete: function() { //callback
										
										showModules();
										
									}
							
								}
							);
							
						}
				
					}
				);
				
			}
	
		}
	);
		
		

	
	$("div#footerWrapper").css("visibility", "visible");
	if ( !$("body").hasClass("insidePage") ){	

		
		
		//footer
		$("#footerWrapper").animate(
			{top: '-=165'}, //animating
			{
				duration: 500, //how fast
				easing: 'easeOutBack',  //type of easing
				complete: function() { //callback
					
					
					
				}
		
			}
		);
		
		
		
	}


}

function showModules(){
	
	$("div#contentWrapper").css("visibility","visible");
	
	
	$("section.module").each(function(index) {		
		$(this).hide().fadeIn(250, function(){
			
			if ( $(this).children("section.mmNavWrapper").get(0) ){
				$("section.mmNavWrapper, section.mmNavWrapper nav").css("visibility", "visible");
				

				//mm nav animation
				$("section.mmNavWrapper").animate(
					{top: '0'}, //animating
					{
						duration: 450, //how fast
						easing: 'easeOutBack',  //type of easing
						complete: function() { //callback
							
							adjustFooterHeight();
							
						}
				
					}
				);
				
				
				
			}
		});
	});
}

function loadMM(categoryName){

	/*the json feed ;)*/
	$.getJSON("/feed.json?asset={\"category.name\":\""+categoryName+"\"}", function(json){
		
		$.each(json.response.asset.object_list, function(i,json){
			
			
			if ( this.asset.type==1 ){
				extraAtrs = " class=\"video\"";
			}else if ( this.asset.type==2 ){
				extraAtrs = " class=\"swf\"";
			}else{
				extraAtrs="";
			}
			
			
			
			assetList +="<li>";
			assetList +="	<a"+extraAtrs+" title=\""+this.asset.name+"\" href=\""+this.asset.link+"\">";
			assetList +="		<span><img width=\"127\" height=\"61\" alt=\""+this.asset.description+"\" src=\""+this.asset.file+"\"></span>";
			assetList +="		<span class=\"shdw\">&nbsp;</span>";
			assetList +="		<span class=\"outline\"></span>";
			assetList +="	</a>";
			assetList +="</li>";
			vidLink="";	

		
		});  
		$("section.mmNavWrapper nav ul").html(assetList);	
		$("section.mmNavWrapper nav").jCarouselLite({
			btnNext: "button.next",
			btnPrev: "button.back",
			scroll:1,
			visible:4,
			speed:500,
			easing:"easeOutSine",
			circular:false
		});
		loadNavControls();
		assetList="";
	});
	
}



function updateActiveState(selectedIdx){
	$("#toolTipWrapper").hide();
	$("section.mmNavWrapper nav ul li a span.outline p").remove();
	$("section.mmNavWrapper nav ul li").removeClass("active");
	$("section.mmNavWrapper nav ul li").removeClass("highlight");
	$("section.mmNavWrapper nav ul li a").find("span.shdw").show();
	theIdx=$(selectedIdx).parent("ul").children().index(selectedIdx);
	$("section.mmNavWrapper nav ul li:eq("+theIdx+")").addClass("active");
	$("section.mmNavWrapper nav ul li:eq("+theIdx+") a span.outline").append("<p>"+currViewTxt+"</p>");
	$("section.mmNavWrapper nav ul li:eq("+theIdx+") a span.shdw").hide();
	
	updateMainAsset(theIdx);
}

function updateMainAsset(theIdx){
	

	$(".mmMainAssetWrapper .mainAsset").html("");
	var newAsset = $("section.mmNavWrapper nav ul li:eq("+theIdx+") a").attr("href");

	if ($("section.mmNavWrapper nav ul li:eq("+theIdx+") a").hasClass("video")){

		if (iphone){
			
			var altVid =  '<object width="100%" height="100%">';
				altVid += ' <param name="movie" value="'+newAsset+'&autoplay=1&fs=1&rel=0&color1=0x006699&color2=0x54abd6" />';
				altVid += ' <param name ="wmode" value="transparent" />';
				altVid += ' <embed src="'+newAsset+'&autoplay=1&fs=1&rel=0&color1=0x006699&color2=0x54abd6"';
				altVid += ' type="application/x-shockwave-flash" wmode="transparent" width="100%" height="100%">';
				altVid += '</object>';
			
			
			$(".mmMainAssetWrapper .mainAsset").html(altVid);
		}else{
			$(".mmMainAssetWrapper .mainAsset").flashembed({
				src: newAsset+"&autoplay=1&fs=1&rel=0&color1=0x006699&color2=0x54abd6",
				wmode: 'transparent'
			});
			$(".mmMainAssetWrapper .mainAsset").append("<img class=\"blockLink\" src=\"/media/img/blank.gif\" height=\"100%\" width=\"100%\" />");
		}
		
	}else if($("section.mmNavWrapper nav ul li:eq("+theIdx+") a").hasClass("swf")){
		
		$(".mmMainAssetWrapper .mainAsset").flashembed({
			src: newAsset,
			wmode: 'transparent'
		});
	}else{
		$(".mainAsset").hide().append("<img src=\""+newAsset+"\" alt=\"\" height=\"100%\" width=\"100%\" />").fadeIn(350);
	}

}



function updateLayout(){
	$("#toolTipWrapper").hide();
	
	if(!$(".modTypeBio").get(0)){	
		if ( $(window).width() > 1280 ){
			$("body").addClass("large")
		}else{
			$("body").removeClass("large")
		}
	}
}

function adjustFooterHeight(){
		$("#footerWrapper .pad").css("height", ( $(window).height() - $("#mainWrapper").height()+100 ))
}
function updateToolTip(hoveredThumb){
	
	$("#toolTipWrapper h4").html($(hoveredThumb).children("a").attr("title"));
	$("#toolTipWrapper p").html($(hoveredThumb).find("img").attr("alt"));
	var newH = $("#toolTipWrapper").height()+15; //height of tooltipWrapper

	var thumbPos = $(hoveredThumb).offset();
	$("#toolTipWrapper").css("left", thumbPos.left-25);
	$("#toolTipWrapper").css("top", thumbPos.top-newH);

	
}


$.fn.superfish.defaults = { 
    hoverClass:    'sfHover',          
    pathClass:     'overideThisToUse', 
    pathLevels:    1,                  
    delay:         100,                
    animation:     { height : 'show', opacity : 'show'},   
    speed:         100,
    autoArrows:    false,               
    dropShadows:   true,               
    disableHI:     false,              
    onInit:        function(){},       
    onBeforeShow:  function(){},       
    onShow:        function(){},       
    onHide:        function(){}        
};

function loadNavControls(){
	
	//updateMainAsset(0);
	
	if ( $.browser.msie && $.browser.version<8 ) {
		$("section.mmNavWrapper nav").css("visibility","visible")
	}
	
	updateActiveState("section.mmNavWrapper nav ul li:eq(0)");
	$("button.back").addClass("disabledB");
	$('#section.mmNavWrapper nav ul li a span').blur(function() {
	  $("#toolTipWrapper").hide();
	});
	
	$("#toolTipWrapper").mouseenter(function() {
	$(this).css("display", "block");
	$("nav.thumbs ul li:eq("+currIdx+")").addClass("highlight");
		
		
	}).mouseleave(function(){
		$(this).css("display", "none");
		$("nav.thumbs ul li:eq("+currIdx+")").removeClass("highlight");
		//$(this).removeClass( "highlight");
    });

	

	$("section.mmNavWrapper nav ul li").hoverIntent(function() {
		
		
		
		if ( !$.browser.msie ) {
			$(this).children("a").find("span.shdw").fadeOut(350);
		}else{
			$(this).children("a").find("span.shdw").hide();
		}
		updateToolTip(this);
		$(this).addClass("highlight");
		$("#toolTipWrapper").fadeIn(350);
	}, function(){
		

		$(this).removeClass( "highlight");
		if (!$(this).hasClass("active")){
			if ( !$.browser.msie ) {
				$(this).children("a").find("span.shdw").fadeIn(350);
			}else{
				$(this).children("a").find("span.shdw").show();
			}
		}
		
		$("#toolTipWrapper").hide();
		
    });
	
	
	
	
	
	
	$("section.mmNavWrapper button").mouseenter(function() {
		
		$("#toolTipWrapper").hide();
		if ($(this).hasClass("back")){
			$(this).addClass( "highlightB");
		}else{
			$(this).addClass( "highlightN");
		}
	}).mouseleave(function(){
		if ($(this).hasClass("back")){
			$(this).removeClass( "highlightB");
		}else{
			$(this).removeClass( "highlightN");
		}
    });
	
	
	$("section.mmNavWrapper nav ul li").click(function(e){		
		e.preventDefault();			
		if($(this).hasClass("active")){
			return false;
		}			
		updateActiveState(this);
	});
	$("section.mmNavWrapper nav ul li a").click(function(e){	
		//alert('');
		e.preventDefault();
	});
	
}


function startUnload(theURL){
	
	//console.log(theURL);
	if (theURL == "/" || theURL == "/en/"){ //if it's home page
		if (!firstload){ // if not the first load
			location.href=theURL; //change the location to HOMEPAGEURL
		}else{
			firstload=false; //stop
			return false
		}
	}
	//home page to inside page animation
	if ( !$("body").hasClass("insidePage") ){
		
		
		$("#uniNavWrapper nav li ul").hide();
		$("#contentWrapper").animate(
			{opacity: "hide", height: 0, top: 100}, //what we're animating
			{
				duration: 500, //how fast
				easing: 'easeOutCubic',  //type of easing
				complete: function() { //callback
					
				}
		
			}
		);
		
		
		
		$("#footerWrapper .pad").hide();
		$("#footerWrapper").css("backgroundColor", "white");
		$("#footerWrapper").animate(
			{top: -17, height:900}, //what we're animating
			{
				duration: 500, //how fast
				easing: 'easeOutQuad',  //type of easing
				complete: function() { //callback
					
					$("body").addClass("insidePage").fadeIn("slow");
					$("#footerWrapper").hide();
					
					loadPage(theURL);
					location.hash=theURL;
					$("#adminLinks a.editPage").attr("href", "/admin/edit_page"+theURL);
				}
		
			}
		);
		//insidePg to insidePg animation
	}else{
		$("#uniNavWrapper nav li ul").hide();
		$("#contentWrapper").animate(
			{opacity: "hide", height: "100%"}, //what we're animating
			{
				duration: 250, //how fast
				easing: 'easeOutQuad',  //type of easing
				complete: function() { //callback
					loadPage(theURL);
					location.hash=theURL;
					$("#adminLinks a.editPage").attr("href", "/admin/edit_page"+theURL);
					
				}
		
			}
		);
	}
	
}

function loadPage(theURL){
$("#footerWrapper").hide();
	$('#contentWrapper').load(theURL+' #content, #breadCrumb', function() {
	
		$(this).children("#breadCrumb").hide();
	
		$("#breadCrumb").html(    $(this).children("#breadCrumb").html() );
		

		if ($(".mmNavWrapper nav").length > 0){
			loadMM($(".mmNavWrapper nav").attr("id"));
			$(".mmNavWrapper nav").css("visibility", "visible");
		}

		$("#contentWrapper").animate(
			{opacity: "show", height: "100%"}, //what we're animating
			{
				duration: 500, //how fast
				//easing: 'easeOutBounce',  //type of easing
				complete: function() { //callback
					$("#footerWrapper").css("top", 0);
					$("#footerWrapper .pad").css("height", "auto");
					$("#footerWrapper, #footerWrapper .pad").fadeIn(250);
					$("#footerWrapper").css("height", 40);
					adjustFooterHeight();
				}

			}
		);
	  
	  
	});
	
}



function updateTitle(newTitle){
	
	var str= newTitle.toLowerCase();
	var Rx= /\b([a-z]+)\b/ig;
	str= str.replace(Rx,function(w){
		return w.charAt(0).toUpperCase()+w.substring(1);
	});
	document.title="Avant - "+ str;
	
	
}

$.address.change(function(event) {
	if (fL){
		
		
		$("#uniNavWrapper nav li li").each(function(){
			if ( $(this).children("a").attr("href") == event.value ){
				$(this).addClass("current");
			}
		})
		
		
		fL=false;
	}
	startUnload(event.value);
});


