//image preload needs to be before buildsite()
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
	 jQuery("<img>").attr("src", arguments[i]);
  }
}

//cookie vars
var cookieTxt = $.cookie('last')
var thisCookie = unescape(cookieTxt)
var cookieOptions = {path: '/', expires: 365}
var date = new Date();
var cookieValue = (date.setTime(date.getTime()))
//session vars
var sessionName;
var sessionOptions = {path: '/', expires: ''}

//Path Vars
var hasPath = false
var path = new Array()
var currentPage = new Array()
//session Home page
var homePage = "About EBF";
//Array Vars
var tier1pgs = new Array()
var tier2pgs = new Array()
var tier3pgs = new Array ()

//build array
buildSite()


//Snippet vars
// find snip master container
var snip;
//individual container
var snipCan;
//keep track of all snips
var ct4Snips;
//keep track of which snip is gbeing reduced
var currentSnip = null;
//set interval to reduce snippets opacity
var snipTime;

//Start Runing the javascript
$(document).ready(function(){
//get cookie value 
var cookieName = escape (page);
//prep page for those w/ javascript
//remove menu ul
$(".menu ul").remove()
//hide snippettes
$(".snippets").hide();
//empty snippetts
$(".snippets div").html("")

//write cookie and create user click path
if (!isHome){
	createPath()
		if ((section != "Contact Us")&&(section != "footer")){
		$.cookie("last", cookieName, cookieOptions);
		}
	}
//unless it is the home page
else {
	$(".content-home-bkg").css({"height":"310px"})
	setHomePage()}

//create equal guuter rules
$("div.row").each(function(){
	var ht4Gut = ($(this).height()-20)
	$(this).children("div.colgutter").css({"height": ht4Gut+"px", "margin-top" : 20+"px"})						   
	})

//create navigation
buildNav()
//load path into snippets
buildSnip()
//fix layout for MSIE

jQuery.each(jQuery.browser, function(i) {
	
   if(($.browser.msie)&&($.browser.version < 8.0)){
		 $("div.nav").css({"width": "172px"})
		 $("div.menu").css({"width": "172px"})
         $("div.content").css({"margin-top":"-20px"}).children("h1").css({"margin-top":"20px"});
		 $("h1.sniptitle").css({"line-height":"1em", "font-size" : "14px", "color" : "#fff"})
		 //$("div.snipoverlay > span").css({"overflow": "visible"})
		 $("div.nav").css({"margin-top":"-20px"});
	  	 $("div.snippets").css({"margin-top":"-20px"});
		 $("div.main").css({"overflow": "visible"})
		 //$(".sniptop").css({"padding-top":-3})
		}
   });


})

//home page discovery
function setHomePage(){
var foundlast = false;
var bannerImg;
var bannerAlt;
var content;

//if this is a first time visit use...
if (!$.cookie('last')){
	page = "About EBF"
	section = "Home"
	$(".content-home").load("cms/about-text.html .invisible div"); 
	}
//loop through cookies find last
// find match if last is last item in array go to first else increment  

//for returning visits find the last page

else{
	if (!$.cookie('session')){
		
		for (i=0; i<tier1pgs.length; i++){
		//if it is a top tier page...
		if (tier1pgs[i].title  == thisCookie){
			
			if (i >= 4){
			buildHomePg (tier1pgs[1].title,"", tier1pgs[1].title,
						tier1pgs[1].bodyText, tier1pgs[1].bannerHomeImage,
						tier1pgs[1].alt, tier1pgs[1].sliceImage, 
						tier1pgs[1].base)
			homePage = tier1pgs[1].title;
			sessionName =  escape (tier1pgs[1].title)
				}
			else {
			var hPg2show = (i+1)
			buildHomePg (tier1pgs[hPg2show].title,"", tier1pgs[hPg2show].title,
						tier1pgs[hPg2show].bodyText, tier1pgs[hPg2show].bannerHomeImage,
						tier1pgs[hPg2show].alt, tier1pgs[hPg2show].sliceImage, 
						tier1pgs[hPg2show].base)
			homePage = tier1pgs[hPg2show].title;
			sessionName =  escape (tier1pgs[hPg2show].title)
				}
			foundlast = true;	
			}
		}
	
	$.cookie("session",  sessionName , sessionOptions);	
	}
	else{
		for (j=0; j<tier1pgs.length; j++){
			homePage = thisCookie = unescape($.cookie('session'))
			if (tier1pgs[j].title  == homePage){
				buildHomePg (tier1pgs[j].title,"", tier1pgs[j].title,
						tier1pgs[j].bodyText, tier1pgs[j].bannerHomeImage,
						tier1pgs[j].alt, tier1pgs[j].sliceImage, 
						tier1pgs[j].base)
				
				}
			}
		
		}
	
}
	createPath()
		var ht;
	//adjust tab width, text container and set pg colours
	setTimeout("adjHomeTxt()", 1000)
}

function adjHomeTxt(){
	ht = (110+$("#home-txt").children("blockquote").height())
	ht = (ht + $("#home-txt").children("h3").height())
	if (ht <= 300){
		$("#home-txt").animate({height:ht}, 500)
		$(".content-home-bkg").animate({height:ht}, 500)
		$(".adiv").animate({top:ht+1}, 500)
	}
	var tWdth = ($(".atab").width())
	var dif = (385-tWdth)
	$(".acoltwo").css({"width":tWdth+10+"px"})
	$(".acolone").animate({width:dif-11},500)
	setColours()
}

//homepage build
function buildHomePg (pg, subsection, section, content, bImg, alt, sImg, base){
page = pg;
subSection = subsection;
section = section; 
$(".content-home").load(content+" .invisible div", function (){
		setColours()
		}); 
$(".banner-home").children("img").attr({"alt":alt})
}


//temp function to set page colours
//put into css use addClass
function setColours(){
var cMod = "about"
if ((page == "About EBF")||(section == "footer")){
	var cMod = "about"
	//alert ("about")
	}
else if ((page == "Contact Us")||(page == "Thank You")){
	var cMod = "contact"	
	//alert ("contact")
	}	
else {cMod = (page.toLowerCase())}


$("div.content > h1").addClass("h_"+cMod)
$("h2").addClass("h_"+cMod)
$("h3").addClass("h_"+cMod)
$("h4").addClass("h_"+cMod)
$(".sniptop").addClass("sniptop_"+cMod)
$(".snipcan").addClass("snipcan_"+cMod)
}

//build promotional snippets
// using path to determine what is shown -- largly unused
function buildSnip(){
snip = $(".snippets")
snipCan = $(".snipcan")
var html =""
ct4Snips = path.length;
//add class to css addClass to divs
var hMod = (Math.floor($(".content").height()/ct4Snips))
if (hMod > 136){hMod = 136;}
if (section == "Home"){
	if (ct4Snips == 4){
		hMod = 78; 
	}
	snip.css({"height" : "354px"})
}
for (i=0; i< path.length; i++){
	html +=("<div class='snipcan'>")
   	html +=("<div class='snipoverlay'><span class='sniptop'><img src='images/sm_cube.gif' alt='"+path[i].title+"' style='margin-top:1px;'/><a href='"+path[i].page+"'><h1 class='sniptitle'>"+path[i].title+"</h1></a></span>")
	html +=("</div>")
	html +=("<div class='snippethover'><a href='"+path[i].page+"'><img src='"+path[i].snippetHov+"' alt='"+path[i].alt+"' /></a></div>")
	html +=("<div class='snippet'><a href='"+path[i].page+"'><img src='"+path[i].snippetImg+"' alt='"+path[i].alt+"' /></a></div></div>")    
	}
snip.html(html)
//remove css and addClass
$(".snipcan").css({"height": hMod+"px", "overflow": "hidden", "display" : "none"})
$(".snippet").css({"height": hMod+"px", "overflow": "hidden"})
$(".snippethover").css({"height": hMod+"px", "overflow": "hidden"})

snip.show()
snip.children("div.snipcan").each( function(i){
	$(this).show(250*i)				
	});
reduceSnip()
//for creating static content copy alert past for inc_snippet/-home
//alert (html)
}



//Fade Snips back and bind hover
function reduceSnip(){
if (currentSnip == -1){
	clearInterval(snipTime)
	}
else if (currentSnip == null){
	currentSnip = ct4Snips;
	snipTime = (setInterval("reduceSnip()",500))
	}
else {
	// bind snippet hover  
	$(".snippethover").eq(currentSnip).hover(function(i){
		$(this).fadeTo(100,0.1);
		},function(){
		$(this).fadeTo(1000,0.8)
			}).fadeIn(1000, function(){
				$(this).fadeTo(500,0.8)
				});
	currentSnip--	
	}
}




//used to create user click path, largly unused, but maybe...
function createPath(){
var ct4P = 0;
var  ct4Tp = 0;
var hasKids = false;
//if page is top teir and not Home
if (subSection ==""){
for (var i=0; i<tier1pgs.length; i++){
		if (tier1pgs[i].title == page){
			for (var j=0; j < tier2pgs.length; j++){
				if (tier1pgs[i].path != "utility"){
					if (tier2pgs[j].path == tier1pgs[i].subPath){
						path[ct4P] = new tier2pg (tier2pgs[j].path,
						tier2pgs[j].pagePath, tier2pgs[j].title,
						tier2pgs[j].subTitle, tier2pgs[j].mainColor,
						tier2pgs[j].accentColor, tier2pgs[j].page,
						tier2pgs[j].base, tier2pgs[j].childEnd,
						tier2pgs[j].ct4Child);
						ct4P++;
						hasKids = true;
						}
				}
				} 
			}
			
			//if there are no pages in path find sibs
			if (!hasKids){
					
					if ((tier1pgs[i].path != "utility")&&(tier1pgs[i].title != page)&&(tier1pgs[i].path != "Home")){
					path[ct4Tp] = new tier1pg (	tier1pgs[i].path, 
					tier1pgs[i].subPath, tier1pgs[i].title, tier1pgs[i].subTitle,
					tier1pgs[i].mainColor, tier1pgs[i].accentColor,
					tier1pgs[i].page, tier1pgs[i].base,
					tier1pgs[i].childEnd, tier1pgs[i].ct4Child );
				ct4Tp++
				}
			}
		}
	}
	
//if the current page is a subsection
	else if (subSection == page){
		
		if ((!hasKids)||(path.length <= 2))
			for (k=0; k < tier2pgs.length; k++)
				if ((tier2pgs[k].path == section)&&(tier2pgs[k].path != "Contact Us")&&(tier2pgs[k].title != page)){
						path[ct4P] = new tier2pg (tier2pgs[k].path,
						tier2pgs[k].pagePath, tier2pgs[k].title,
						tier2pgs[k].subTitle, tier2pgs[k].mainColor,
						tier2pgs[k].accentColor, tier2pgs[k].page,
						tier2pgs[k].base, tier2pgs[k].childEnd,
						tier2pgs[k].ct4Child);
					ct4P++
				}
		}	
	
path.reverse();
buildSnip()
}





//Begin Nav
//expand nav built to loop through a three layer nav site currently only uses 2
function setNav(){
//create expand and collaps
$(".menu img.expand").each(function(i){
	$(this).click(function(){
		if ($(this).next().css("display") == "none"){
		$(this).next().fadeIn("slow", function(){
			if ($(".nav").height() > $(".content").height()){
			$(".nav").addClass("border-right")
			$(".content").addClass("no-border-left")
			}								   
		   })
		$(this).attr({"src": "images/i_less.gif", "alt" : "Collapse list"})
		}
		else{
		$(this).next().fadeOut("slow", function(){
			if ($(".nav").height() < $(".content").height()){
			$(".nav").removeClass("border-right")
			$(".content").removeClass("no-border-left")
			}						})
			$(this).attr({"src": "images/i_more.gif", "alt" : "Expand list"})
		}
	});
});
//set diplay of nav based upon current page

if (section == "footer"){
	//if a footer link do nothing may be used later
	}
else{
	$(".menu li").each(function(i){
	//match section addclass show child UL  only used on thank you page
	if ($(this).children("a").html() == section){
		$(this).children("a").addClass("active")
		$(this).addClass("active")
		if (section != "Contact Us"){
			$(this).children("ul").fadeIn("slow")
			$(this).children("img.expand").attr({"src": "images/i_less.gif", "alt" : "Collapse list"})
			}
		}
	//match subsection addclass show child UL (page) not used currently
	if ($(this).children("a").html() == subSection){
		$(this).children("a").addClass("active")
		$(this).addClass("active")//#23493d
		$(this).children("ul").fadeIn(500)
		$(this).children("img.expand").attr({"src": "images/i_less.gif", "alt" : "Collapse list"})
		}
	//match page addclass
	if ($(this).children("a").html() == page){
		if (section == "Home"){ 
			$(this).children("a").addClass("selected").css({"color":"#777"})
			}
		else{
			$(this).children("a").addClass("selected")
			//remove and place in CSS
			$(this).addClass("selected")
			}
		}						
	});
	}
}

//build nav list
// built for 3 layers of navigation currently only uses 2
function buildNav(){
var nav = $(".menu")
var menu = ("<ul>");
for (i=0; i <  tier1pgs.length; i++){
	menu +=("<li><a href="+tier1pgs[i].page+" >")
	menu +=(tier1pgs[i].title+"</a>")
		if ((tier1pgs[i].ct4Child > 0)&&( tier1pgs[i].title != "Contact Us")){
		menu +=("<img src='images/i_more.gif' class='expand' alt='Expand list' />");
		menu +=("<ul>")
			for (j=tier1pgs[i].childStart; j < tier1pgs[i].childEnd; j++){
			menu +=("<li><a href='"+tier2pgs[j].page+"' >")
			menu +=(tier2pgs[j].title+"</a>")
				if (tier2pgs[j].ct4Child > 0){ 
				menu +=("<img src='images/i_more.gif' class='expand' alt='Expand list' />");  // put expand Icon here...
				menu +=("<ul>")
					for (k=tier2pgs[j].childStart; k < tier2pgs[j].childEnd; k++){
					menu +=("<li><a href='"+tier3pgs[k].page+"'>")
					menu +=(tier3pgs[k].title+"</a></li>")
					}
				menu +=("</ul>")	
				}
			menu +=("</li>")
			}
		menu +=("</ul>")
		}
	menu +=("</li>")
	}
menu +=("</ul>")
nav.append(menu)
$(".menu ul li > ul").hide()
setNav()
//use to build static nav copy and past from alert into inc_menu
//alert (menu)
}
//End Nav





//Begin Array
function tier1pg (path, subpath, title, subtitle, color1, color2, page, image, total, ct){
	this.path = path;
	this.subPath = subpath;
	this.title = title;
	this.subTitle = subtitle;
	this.mainColor = color1;
	this.accentColor = color2;
	this.page = page;
	this.base = image;
	this.childEnd = total;
	this.ct4Child = ct;	
		this.childStart = (total-ct);
		this.snippetImg = ("images/snip_"+image+".jpg");
		this.snippetHov = ("images/snip_"+image+"_hover.jpg");
		this.bannerImage = ("images/head__banner_"+image+".jpg");
		this.headImg = ("images/head_logo_"+image+".jpg");
		this.alt = (title+" "+subtitle);
		this.bodyText = ("cms/"+image+"-text.html");
	$.preloadImages(this.snippetHov, this.snippetImg, this.bannerImage, this.sliceImage, this.headImg);
	return this;
}


//currently only used by Thank you
function tier2pg (path, pagepath, title, subtitle, color1,color2, page, image, total, ct){
	this.path = path;
	this.pagePath = pagepath;
	this.title = title;
	this.subTitle = subtitle;
	this.mainColor = color1;
	this.accentColor = color2;
	this.page = page;
	this.base = image;
	this.childEnd = total;
	this.ct4Child = ct;
		this.snippetImg = ("images/snip_"+image+".jpg");
		this.snippetHov = ("images/snip_"+image+"_hover.jpg");
		this.bannerImage = ("images/head__banner_"+image+".jpg");
		this.headImg = ("images/head_logo_"+image+".jpg");
		this.alt = (title+" "+subtitle);
		this.bodyText = ("cms/"+image+"-text.html");
	$.preloadImages(this.snippetHov, this.snippetImg, this.bannerImage, this.sliceImage, this.headImg);
	return this;
}

//not used
function tier3pg (parentpath, path, title, subtitle, color1, color2, page, image){
	this.parentPath = parentpath;
	this.path = path;
	this.title = title;
	this.subTitle = subtitle;
	this.mainColor = color1;
	this.accentColor = color2;
	this.page = page;
	this.base = image;
		this.snippetImg = ("images/snip_"+image+".jpg");
		this.snippetHov = ("images/snip_"+image+"_hover.jpg");
		this.bannerImage = ("images/head__banner_"+image+".jpg");
		this.headImg = ("images/head_logo_"+image+".jpg");
		this.alt = (title+" "+subtitle);
		this.bodyText = ("cms/"+image+"-text.html");
	$.preloadImages(this.snippetHov, this.snippetImg, this.bannerImage, this.sliceImage, this.headImg);
return this;
}

function buildSite(){
i4sec = 0;	//index for sections
i4sSec = 0;	//index for subsections
i4pg = 0;	//index for pages

sSecCt = 0; //subsection ct 4 section
pgCt = 0;	//page ct 4 section
tier1pgs[i4sec++] = new tier1pg ("Home","","Home","Welcome to the EBF &amp; Associates, L.P. Web Site","#333","#333","index.html","index",i4sSec,sSecCt);
tier1pgs[i4sec++] = new tier1pg ("Corporate","About EBF","About EBF","Founded in 1988","#795f6a","795f6a","about.html","about",i4sSec,sSecCt);
tier1pgs[i4sec++] = new tier1pg ("Process","Philosophy","Philosophy","Opportunistic:<br />Identify, Analyze and Profit ","#495138","#495138","philosophy.html","philosophy",i4sSec,sSecCt);
sSecCt=0; //reset for next subsection
tier1pgs[i4sec++] = new tier1pg ("Themes","Themes","Themes","Value, Downside Protection, Avoid Leverage","#62878f","#62878f","themes.html","themes",i4sSec,sSecCt);
sSecCt=0; //reset for next subsection

tier1pgs[i4sec++] = new tier1pg ("Corporate","Team","Team","Experience","#7f6f2b","#7f6f2b","team.html","team",i4sSec,sSecCt); 

sSecCt=0;//restart subsection 4 section
tier2pgs[i4sSec++] = new tier2pg ("Contact Us","","Thank You","We appreciate your Interest in EBF &amp; Associates","#357360","#013827","thank_you.html","thank_you");sSecCt++;
tier1pgs[i4sec++] = new tier1pg("utility","Contact Us","Contact Us","","#04355d","#04355d","contact.html","contact",i4sSec,sSecCt);

}
// End Array
