/* CORE */

var ie7;

var currOpenCategory = null;
var categoryOpenCloseTime = 250;
var categoryBusyCount = 0;

$(document).ready(function() { 
	ie7 = ($.browser.msie && Number($.browser.version.substring(0, 3)) < 8);

	initMenu(); 
	if ($("body.home").length) initHomePage();
	else if ($("body.works").length) initWorks();
	else if ($("body.us").length) initUs();
	else if ($("body.reels").length) initReels();
	else if ($("body.namedropping").length) initNameDropping();
	else if ($("body.hype").length) initHype();
	
	adjustForBrowser();
});

/* FANCYBOX */

var fbSSHoldTime = 3000;
var fbSSTimeout;
var fbSSSlideIndex;

var fbSSDirectory;
var fbSSSubdirectory;
var fbSSSource;
var fbSSIndex;

function fancyboxIt(element, directory, subdirectory, source, index) {
	var isVideo = typeof source[index].video_or_slideshow == "string";

	var c = '<div id="fb-wrapper">';
	
	if (isVideo) {
		// video
		
		c += '<div id="fb-content-wrapper"><video id="fb-video" src="assets/' + directory + 'videos/' + subdirectory + source[index].video_or_slideshow + '" width="640" height="360" autoplay="true" controls="controls" /></div>';
		c += '<script type="text/javascript">$("#fb-video").jmeEmbed();</script>';
	} else {
		// slideshow
		
		c += '<div id="fb-content-wrapper"><img src="assets/' + directory + 'slideshow_images/' + subdirectory + source[index].video_or_slideshow[0] + '" width="640" height="360" /></div>';
	
		// preload our slides
		
		for (var i=0; i<source[index].video_or_slideshow.length; i++) {
			var t = new Image();
			t.src = "assets/" + directory + 'slideshow_images/' + subdirectory + source[index].video_or_slideshow[i];
		}
	}
	
	c += '<span class="fb-content-text">';
	
	if (source[index].year) c += '<p><br /><span class="title">YEAR</span> ' + source[index].year + '</p>';
	if (source[index].services) c += '<p><span class="title">SERVICES</span> ' + source[index].services + '</p>';
	if (source[index].shots) c += '<p><span class="title">SHOTS</span> ' + source[index].shots + '</p>';
	if (source[index].description) c += '<br /><br /><p class="description"> ' + source[index].description + '</p>';
	
	c += '</span>';
	
	c += '</div>';
	
	$(element).fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'scrolling'		: 'no',
		'overlayOpacity': '0.8',
		'centerOnScroll': 'true',
		'onStart'		: function() { 
			if (lookMenuOn) lookMenuClicked(); 
			if (!isVideo && source[index].video_or_slideshow.length > 1) fancyboxStartSlideshow(directory, subdirectory, source, index);
		},
		'onClosed'		: fancyboxClosed,
		'content' 		: c	
	});
}

function fancyboxStartSlideshow(directory, subdirectory, source, index) {
	fbSSDirectory = directory;
	fbSSSubdirectory = subdirectory;
	fbSSSource = source;
	fbSSIndex = index;

	fbSSSlideIndex = 0;
	fbSSTimeout = setTimeout(nextFancyboxSlide, fbSSHoldTime);
}

function nextFancyboxSlide() {
	if (++fbSSSlideIndex == fbSSSource[fbSSIndex].video_or_slideshow.length) fbSSSlideIndex = 0;

	$('#fb-content-wrapper').css("background-image", "url('assets/" + fbSSDirectory + 'slideshow_images/' + fbSSSubdirectory + fbSSSource[fbSSIndex].video_or_slideshow[fbSSSlideIndex] + "')");
	$('#fb-content-wrapper img').fadeOut("slow", function() {
		$(this).attr('src', 'assets/' + fbSSDirectory + 'slideshow_images/' + fbSSSubdirectory + fbSSSource[fbSSIndex].video_or_slideshow[fbSSSlideIndex]);
		$(this).fadeIn(0);

		fbSSTimeout = setTimeout(nextFancyboxSlide, fbSSHoldTime);
		$('#fb-content-wrapper').css("background-image", "none");
	});
}


function fancyboxClosed() {
	// cleanup any timeouts
	
	$('#fb-content-wrapper img').clearQueue().stop();
	clearTimeout(fbSSTimeout);
}

/* HOMEPAGE */

var currCombo;
var currQuoteIndex;
var currPromoIndex;

var quoteHoldTime = 7000;
var quoteTimeout;

var promoHoldTime = 5000;
var promoTimeout;

var resetHomePageTime = 3*60*1000; // 3 minutes
var resetTimeout;

function initHomePage(forceRandom) {
	var l = location.href.split("?");

	if (!forceRandom && l.length > 1) {
		currCombo = Number(l[1]) - 1;
	} else {
		var n = currCombo
		
		do { 
			n = Math.floor(Math.random() * homePageContent.length); 
		} while (n == currCombo);
		
		currCombo = n;
	}
	
	// backplate
	
	if ((navigator.userAgent.indexOf("iPhone") > -1) || (navigator.userAgent.indexOf("iPad") > -1)) {
		$('#wrap').css("background-image", "url('assets/home/backplates/" + homePageContent[currCombo].backplate_ios + "')");
	} else {
		$('body.home').css("background-image", "url('assets/home/backplates/" + homePageContent[currCombo].backplate + "')");
	}
	
	// quote
	
	currQuoteIndex = 0;
	
	if (homePageContent[currCombo].quotes) {
		setQuoteHtml(currQuoteIndex);
	 	if (homePageContent[currCombo].quotes.length > 1) quoteTimeout = setTimeout(nextQuote, quoteHoldTime);
	 } else $('#home_quote').html("");
		
	// promo
	
	currPromoIndex = 0;
	
	setPromoHtml(currPromoIndex);
	if (homePageContent[currCombo].promos && homePageContent[currCombo].promos.length > 1) {
		promoTimeout = setTimeout(nextPromo, promoHoldTime);
	
		// preload all our promos
	
		for (var i=1; i<homePageContent[currCombo].promos.length; i++) {
			var t = new Image();
			t.src = 'assets/home/promo_images/' + homePageContent[currCombo].promos[i];
		}
	}
	 
	 // reset
	 
	 resetTimeout = setTimeout(resetHomePage, resetHomePageTime);
}

function resetHomePage() {
	$('#home_quote').clearQueue().stop();
	$('#home_promo img').clearQueue().stop();
	$('#home_promo .promo_wrapper').css("background-image", "none");

	clearTimeout(quoteTimeout);
	clearTimeout(promoTimeout);
	
	initHomePage(true);
}

function setQuoteHtml(index) {
	var q = ""; 
	
	var title = homePageContent[currCombo].quotes[index].title;
	var quote = homePageContent[currCombo].quotes[index].quote;
	var author = homePageContent[currCombo].quotes[index].author;
	
	if (title) q += '<div id="title1" class="quote_title tk-myriad-pro">' + title + '</div>';
	if (quote) q += '<div id="quote1" class="quote_text tk-myriad-pro"">"' + quote + '"</div>';
	if (author) {
		//q += '<div id="author1" class="quote_author tk-myriad-pro">&#8212;' + author;
		//q += '<div class="quote_source">' + homePageContent[currCombo].quotes[index].author_title + '</div></div>';
		q += '<div id="author1" class="quote_author tk-myriad-pro">&#8212;' + author + ', ' + homePageContent[currCombo].quotes[index].author_title + '</div>';
	}
	
	$('#home_quote').html(q);
}

function nextQuote() {
	$('#home_quote').fadeOut("normal", function() {
		if (++currQuoteIndex == homePageContent[currCombo].quotes.length) currQuoteIndex = 0;
		setQuoteHtml(currQuoteIndex);
		
		$('#home_quote').fadeOut(0).fadeIn("normal", function() {
			quoteTimeout = setTimeout(nextQuote, quoteHoldTime);
		});
	});
}

function setPromoHtml(index) {
	var hasVideo = (homePageContent[currCombo].video_or_slideshow != null);
	var p = '';
	
	if (homePageContent[currCombo].promo_title) p += '<div id="home_promo_title">' + homePageContent[currCombo].promo_title + '</div>';
	
	if (homePageContent[currCombo].promos) {	
		if (hasVideo) {
			p += '<div class="promo_wrapper"><a href="#" class="fancybox"><img src="assets/home/promo_images/' + homePageContent[currCombo].promos[index] + '" border="0" alt="" /></a></div>'
		} else {
			p += '<div class="promo_wrapper"><img src="assets/home/promo_images/' + homePageContent[currCombo].promos[index] + '" border="0" alt="" /></div>'
		}
	}
	
	$('#home_promo').html(p);
	
	if (hasVideo) {
		// fancybox
		
		$("#home_promo a.fancybox").each(function() {
			fancyboxIt(this, "home/", "", homePageContent, currCombo);
		});
	}
}

function nextPromo() {
	// set bgd to what we're fading in
	
	if (++currPromoIndex == homePageContent[currCombo].promos.length) currPromoIndex = 0;
	
	$('#home_promo .promo_wrapper').css("background-image", "url('assets/home/promo_images/" + homePageContent[currCombo].promos[currPromoIndex] + "')");
	$('#home_promo img').fadeOut("slow", function() {
		setPromoHtml(currPromoIndex);		
		promoTimeout = setTimeout(nextPromo, promoHoldTime);
		$('#home_promo .promo_wrapper').css("background-image", "none");
	});
}

/* WORKS */

function initWorks() {
	var h = '';
	
	for (var i in worksContent) {
		var c = '<div class="works_category_wrapper">';
		c += '<div class="works_category">';
		c += '<div class="works_category_title"><img class="works_category_arrow" src="assets/_global/arrow_right.png" border="0" width="12" height="12" /> ' + worksContent[i].name + '</div>';
	
		// carousel content
		
		c += '<div class="works_carousel">';

		c += '<div class="works_arrow works_arrow_left"><img src="assets/_global/arrow_works_prev.png" border="0" width="17" height="30" /></div>';		
		if (ie7) {
			c += '<div class="works_jcarousel_wrapper" style="visibility: hidden">';
		} else {
			c += '<div>';
		}
		
		c += '<ul class="jcarousel-skin-works">';
								
		// we add the thumbs below

		c += '</ul></div>'
		c += '<div class="works_arrow works_arrow_right"><img src="assets/_global/arrow_works_next.png" border="0" width="17" height="30" /></div>';
		c += '</div>';

		c += '</div>';
		c += '</div>';

		h += c;
	}
	
	$('#works_wrapper').html(h);
	
	// set up the actual carousel
	
	$('#works_wrapper .jcarousel-skin-works').each(function(index) {
		$(this).jcarousel({
			size: worksContent[index].works.length,
			itemLoadCallback: { onBeforeAnimation: worksCarousel_itemLoadCallback },
  		    initCallback: worksCarousel_initCallback,
			
	        itemFirstInCallback: { onBeforeAnimation: worksCarousel_itemFirstInCallback },
    	    itemLastInCallback: { onBeforeAnimation: worksCarousel_itemLastInCallback },

			buttonNextHTML: null,
			buttonPrevHTML: null,
			
			scroll: 5,
			
			index: index
		});
    });
		
	// bind our open/close, hide our arrows if not needed

	$('#works_wrapper .works_category_wrapper').each(function(index) {		
		var element = this;
		
		// store our index here as well
		
		this.index = index;
		$(this).find(".works_category_title").bind('click', function() { worksCategoryClicked(element) })
		
		// arrows
		
		if (worksContent[index].works.length <= 5) {
			$(this).find(".works_arrow").css("visibility", "hidden");
		} else {
			// otherwise just hide the left arrow
			
			$(this).find(".works_arrow_left").css("visibility", "hidden");
		}
	});	
	
	// default open (workCarousel_initCallback will have been called)
	
	 worksCategoryClicked($('#works_wrapper .works_category_wrapper:first')[0], true, true);
}

function worksCarousel_itemLoadCallback(carousel, state) {	
	// bring in all our items

	for (var i = carousel.first; i <= worksContent[carousel.options.index].works.length /*carousel.last*/; i++) {
		if (carousel.has(i)) {
			continue;
		}

        if (i > worksContent[carousel.options.index].works.length) {
            break;
        }

		var h = '<a href="#" class="fancybox"><img src="assets/works/thumbnail_images/' + worksContent[carousel.options.index].directory + '/' + worksContent[carousel.options.index].works[i - 1].thumbnail + '" border="0" width="103" height="152" /></a>';
		carousel.add(i, h);
	}
	
	// explicitly fancybox
	worksCarouselFancyboxIt(carousel.options.index);
}

function worksCarousel_initCallback(carousel, state) {
    $('#works_wrapper .works_arrow_left').eq(carousel.options.index).bind('click', function() {
		// if we are open, close
		if (lookMenuOn) lookMenuClicked();

        carousel.prev();
        return false;
    });

    $('#works_wrapper .works_arrow_right').eq(carousel.options.index).bind('click', function() {
		// if we are open, close
		if (lookMenuOn) lookMenuClicked();

        carousel.next();
        return false;
    });
}

function worksCarousel_itemFirstInCallback(carousel, item, index, state) {
	if (index > 1) {
		// left arrow is visible
		
		$('#works_wrapper .works_arrow_left').eq(carousel.options.index).css("visibility", "visible");
	} else {
		// is not
		
		$('#works_wrapper .works_arrow_left').eq(carousel.options.index).css("visibility", "hidden");
	} 	
}

function worksCarousel_itemLastInCallback(carousel, item, index, state) {
	if (index < worksContent[carousel.options.index].works.length) {
		// right arrow is visible
		
		$('#works_wrapper .works_arrow_right').eq(carousel.options.index).css("visibility", "visible");
	} else {
		// is not
		
		$('#works_wrapper .works_arrow_right').eq(carousel.options.index).css("visibility", "hidden");
	} 
}

function worksCategoryClicked(element, force, instant) {
	if (categoryBusyCount > 0 && !force) return;

	// if our menu is open, close
	if (lookMenuOn) lookMenuClicked();

	categoryBusyCount++;

	if (currOpenCategory == element) {
		// we are clicking to close

		// if ie7, hide first
		$(element).find(".works_jcarousel_wrapper").css("visibility", "hidden");
	
		$(element).find("img.works_category_arrow").attr({src: "assets/_global/arrow_right.png"});
		$(element).animate({ height: '-=200' }, categoryOpenCloseTime, function() {
			categoryBusyCount--;
		});
		
		currOpenCategory = null;
	} else {
		// if we have a current open category, close it
		
		var delay = 0;
		
		if (currOpenCategory != null) {
			worksCategoryClicked(currOpenCategory, true);
			
			delay = categoryOpenCloseTime + 50;
		}
		
		// open the new category
	
		$(element).find("img.works_category_arrow").attr({src: "assets/_global/arrow_down.png"});
		
		if (instant) {
			$(element).animate({ height: '+=200' }, 0, function() {
							categoryBusyCount--;
					   
							// if ie7, show when done
							
							$(element).find(".works_jcarousel_wrapper").css("visibility", "visible");
					   });
		} else {
			$(element).delay(delay).
					   animate({ height: '+=200' }, categoryOpenCloseTime, function() {
							categoryBusyCount--;
					   
							// if ie7, show when done
							
							$(element).find(".works_jcarousel_wrapper").css("visibility", "visible");
					   });
		}							
							
		currOpenCategory = element;		
	}
}

function worksCarouselFancyboxIt(index) {
	$('#works_wrapper .jcarousel-skin-works').eq(index).each(function() {
		$(this).find("a.fancybox").each(function(wIndex) {
			fancyboxIt(this, "works/", worksContent[index].directory + "/", worksContent[index].works, wIndex);
		});
	});
}

/* US */

function initUs() {
	var h = '';
	
	for (var i in usContent) {	
		var s = '<div class="us_category_wrapper">';
		s += '<div class="us_category">';
		s += '<div class="us_category_title"><img class="us_category_arrow" src="assets/_global/arrow_right.png" border="0" width="12" height="12" /> ' + usContent[i].name + '</div>';

		if (usContent[i].submenus.length == 1) {
			// only one submenu so a special case
			
			s += '<div class="us_box_noscroll flexcroll">';
			s += '<div class="us_image_block_vertical">';
			
			for (var j=0; j<usContent[i].submenus[0].image_count; j++) {
				s += '<img src="assets/us/' + usContent[i].directory + '/img' + (j + 1) + '.jpg" class="us_image_vertical" />';
			}
			
			s += '</div>';

		} else {
			// content
			
			s += '<div class="us_box">';
			s += '<div class="us_submenu">';
			
			for (var j in usContent[i].submenus) {
				if (j == 0) s += '<a href="#" class="us_submenuitem sub_on">' + usContent[i].submenus[j].name + '</a>';
				else s += '<a href="#" class="us_submenuitem sub">' + usContent[i].submenus[j].name + '</a>';
				
				if (j < usContent[i].submenus.length - 1) s += "<br />";
			}
			
			s += '</div>';
		}			
			
		// current active content -- init later
		
		s += '<div class="us_content">';
		s += '</div>';

		s += '</div>';
		
		s += '</div>';
		s += '</div>';
		
		h += s;
	}
	
	$('#us_wrapper').html(h);
	
	// set up active content
	
	$('#us_wrapper .us_content').each(function(index) {
		var c;

		if (ie7) $(this).css("visibility", "hidden");

		if (usContent[index].submenus.length == 1) {
			// only one submenu so a special case

			c = '<div class="us_text_block_noscroll flexcroll"><div class="us_text_block_child">';
			c += usContent[index].submenus[0].content;
			c += '</div>';
		} else {
			c = '<div class="us_image_block">';
			
			for (var i=0; i<usContent[index].submenus[0].image_count; i++) {
				c += '<img src="assets/us/' + usContent[index].directory + '/' + usContent[index].submenus[0].directory + '/img' + (i + 1) + '.jpg" class="us_image" />';
			}
			
			c += '</div>';
			c += '<div class="us_text_block flexcroll"><div class="us_text_block_child">';
			c += usContent[index].submenus[0].content;
			c += '</div></div>';
		}
		
		$(this).html(c);
	});
	
	// bind our open/close

	$('#us_wrapper .us_category_wrapper').each(function(index) {		
		var element = this;
		
		// store our index here as well
		
		this.index = index;
		$(this).find(".us_category_title").bind('click', function() { usCategoryClicked(element) })
		
		// bind our submenus

		if (usContent[index].submenus.length > 1) {
			$(this).find(".us_submenuitem").each(function(sindex) {
				
				$(this).bind('click', function() {
					usSubmenuClicked(element, sindex);
				});
			});
		}
	});	

	// default open
	
	usCategoryClicked($('#us_wrapper .us_category_wrapper:first')[0], true, true);
}

function usCategoryClicked(element, force, instant) {
	if (categoryBusyCount > 0 && !force) return;

	// if our menu is open, close
	if (lookMenuOn) lookMenuClicked();

	categoryBusyCount++;

	if (currOpenCategory == element) {
		// we are clicking to close

		// if ie7, hide first
		if (ie7) $(element).find(".us_content").css("visibility", "hidden");

		$(element).find("img.us_category_arrow").attr({src: "assets/_global/arrow_right.png"});
		$(element).animate({ height: '-=410' }, categoryOpenCloseTime, function() {
			categoryBusyCount--;
		});
		
		currOpenCategory = null;
	} else {
		// if we have a current open category, close it
		
		var delay = 0;
		
		if (currOpenCategory != null) {
			usCategoryClicked(currOpenCategory, true);
			
			delay = categoryOpenCloseTime + 50;
		}
		
		// open the new category
	
		$(element).find("img.us_category_arrow").attr({src: "assets/_global/arrow_down.png"});

		if (instant) {
			$(element).animate({ height: '+=410' }, 0, function() {
							categoryBusyCount--;
					   
							// if ie7, show when done
							
							if (ie7) $(element).find(".us_content").css("visibility", "visible");
					   });
		} else {
			$(element).delay(delay).
					   animate({ height: '+=410' }, categoryOpenCloseTime, function() {
							categoryBusyCount--;
					   
							// if ie7, show when done
							
							if (ie7) $(element).find(".us_content").css("visibility", "visible");
					   });
		}
							
		currOpenCategory = element;
	}
}

function usSubmenuClicked(element, sindex) {
	$(element).find(".us_text_block_child").html(usContent[element.index].submenus[sindex].content);
	$(element).find(".us_submenuitem").attr("class", "us_submenuitem sub").eq(sindex).attr("class", "us_submenuitem sub_on");
	
	// update our images
	
	var c = "";
			
	for (var i=0; i<usContent[element.index].submenus[sindex].image_count; i++) {
		c += '<img src="assets/us/' + usContent[element.index].directory + '/' + usContent[element.index].submenus[sindex].directory + '/img' + (i + 1) + '.jpg" class="us_image" />';
	}
	
	$(element).find(".us_image_block").html(c);
	
	// update our scrollbars
	
	fleXenv.updateScrollBars();
	$(element).find(".us_text_block").each(function() {
		if (this.fleXcroll) this.fleXcroll.setScrollPos(false, 0);
	});
	$(element).find(".us_text_block_noscroll").each(function() {
		if (this.fleXcroll) this.fleXcroll.setScrollPos(false, 0);
	});
}

/* REELS */

function initReels() {
	var h = '';
	
	for (var i in reelsContent) {	
		var s = '<div class="reel">';
		s += '<a href="#" class="fancybox">';
		s += '<img src="assets/reels/thumbnail_images/' + reelsContent[i].thumbnail + '" class="reel_img" />';
		s += '<div class="reel_title">' + reelsContent[i].name + '</div>';
		s += '</a></div>';
		
		h += s;
	}
	
	$('#reels_wrapper').html(h);

	// fancybox	
	
	$('#reels_wrapper a.fancybox').each(function(index) {
		fancyboxIt(this, "reels/", "", reelsContent, index);
	});
}

/* NAME DROPPING */

function initNameDropping() {
	var h = '';
	
	for (var i in nameDroppingContent) {	
		var s = '<div class="namedropping_category_outer_wrapper">';
		s += '<div class="namedropping_category_wrapper">';
		s += '<div class="namedropping_category">';
		s += '<div class="namedropping_category_title"><img class="namedropping_category_arrow" src="assets/_global/arrow_right.png" border="0" width="12" height="12" /> ' + nameDroppingContent[i].name + '</div>';
		s += '</div>';
		s += '</div>';

		s += '<div class="namedropping_box">';
		
		var tnc = nameDroppingContent[i].names.length;
		var hnc = Math.ceil(tnc / 2); // first column (may be longer than the second by 1)
		
		s += '<div class="namedropping_col">';
		for (var j=0; j<hnc; j++) {
			s += nameDroppingContent[i].names[j];
			if (j != hnc - 1) s += "<br />";
		}
		s += '</div>';

		s += '<div class="namedropping_col">';
		for (var j=hnc; j<tnc; j++) {
			s += nameDroppingContent[i].names[j];
			if (j != tnc - 1) s += "<br />";
		}
		s += '</div>';
		
		s += '</div>';
		s += '</div>';
		
		h += s;
	}
	
	$('#namedropping_wrapper').html(h);
	
	
	// bind our open/close

	$('#namedropping_wrapper .namedropping_category_outer_wrapper').each(function(index) {		
		var element = $(this);
		
		$(this).find(".namedropping_category_title").bind('click', function() { nameDroppingCategoryClicked(element) })
		
	});

	// default open
	
	nameDroppingCategoryClicked($('#namedropping_wrapper .namedropping_category_outer_wrapper:first')[0], true, true);
}

function nameDroppingCategoryClicked(element, force, instant) {
	if (categoryBusyCount > 0 && !force) return;

	// if our menu is open, close
	if (lookMenuOn) lookMenuClicked();

	categoryBusyCount++;

	if (currOpenCategory == element) {
		// we are clicking to close

		$(element).find("img.namedropping_category_arrow").attr({src: "assets/_global/arrow_right.png"});

		$(element).find(".namedropping_box").slideUp(categoryOpenCloseTime, function() {
			categoryBusyCount--;
		});
		
		currOpenCategory = null;
	} else {
		// if we have a current open category, close it
		
		var delay = 0;
		
		if (currOpenCategory != null) {
			nameDroppingCategoryClicked(currOpenCategory, true);
			
			delay = categoryOpenCloseTime + 50;
		}
		
		// open the new category
	
		$(element).find("img.namedropping_category_arrow").attr({src: "assets/_global/arrow_down.png"});

		if (instant) {
			$(element).find(".namedropping_box").slideDown(0, function() {
							categoryBusyCount--;
					   });
		} else {
			$(element).find(".namedropping_box").delay(delay).
					   slideDown(categoryOpenCloseTime, function() {
							categoryBusyCount--;
					   });
		}
							
		currOpenCategory = element;
	}
}


/* HYPE */

function initHype() {
	var h = '';
	
	for (var i in hypeContent) {	
		var s = '<div class="hype_category_outer_wrapper">';
		s += '<div class="hype_category_wrapper">';
		s += '<div class="hype_category">';
		s += '<div class="hype_category_title"><img class="hype_category_arrow" src="assets/_global/arrow_right.png" border="0" width="12" height="12" /> ' + hypeContent[i].name + '</div>';
		s += '</div>';
		s += '</div>';

		s += '<div class="hype_box">';

		if (hypeContent[i].type == "testimonials") s += '<div class="hype_submenu_scroll flexcroll">';
		else s += '<div class="hype_submenu_scroll">';
		
		for (var j in hypeContent[i].submenus) {
			if (j == 0) s += '<a href="#" class="hype_submenuitem sub_on">' + hypeContent[i].submenus[j].name + '</a>';
			else s += '<a href="#" class="hype_submenuitem sub">' + hypeContent[i].submenus[j].name + '</a>';
			
			if (j < hypeContent[i].submenus.length - 1) s += "<br />";
		}
		
		s += '</div>';
		
		s += '<div class="hype_content">';
		if (hypeContent[i].type == "testimonials") s += '<div class="hype_text_block">';
		else s += '<div class="hype_text_block flexcroll">';
		
		s += '<div class="hype_text_block_child">';
		s += '</div>';	
		s += '</div>';
		
		s += '</div>';
		
					
		s += '</div>';

		s += '</div>';
		
		h += s;
	}
	
	$('#hype_wrapper').html(h);
	
	
	$('#hype_wrapper .hype_box').each(function(index) {
		if (ie7) $(this).css("visibility", "hidden");
	});

	// bind our open/close

	$('#hype_wrapper .hype_category_outer_wrapper').each(function(index) {		
		var element = this;
		
		// store our index here as well
		
		this.index = index;
		$(this).find(".hype_category_title").bind('click', function() { hypeCategoryClicked(element) })
		
		// bind our submenus

		hypeSetSubmenu(this, index, 0);

		$(this).find(".hype_submenuitem").each(function(sindex) {
			$(this).bind('click', function() {
				hypeSubmenuClicked(element, sindex);
			});
		});
	});	

	// default open
	
	hypeCategoryClicked($('#hype_wrapper .hype_category_outer_wrapper:first')[0], true, true);
}

function hypeCategoryClicked(element, force, instant) {
	if (categoryBusyCount > 0 && !force) return;

	// if our menu is open, close
	if (lookMenuOn) lookMenuClicked();

	categoryBusyCount++;

	if (currOpenCategory == element) {
		// we are clicking to close

		// if ie7, hide first
		if (ie7) $(element).find(".hype_box").css("visibility", "hidden");

		$(element).find("img.hype_category_arrow").attr({src: "assets/_global/arrow_right.png"});
		$(element).animate({ height: '-=420' }, categoryOpenCloseTime, function() {
			categoryBusyCount--;
		});
		
		currOpenCategory = null;
	} else {
		// if we have a current open category, close it
		
		var delay = 0;
		
		if (currOpenCategory != null) {
			hypeCategoryClicked(currOpenCategory, true);
			
			delay = categoryOpenCloseTime + 50;
		}
		
		// open the new category
	
		$(element).find("img.hype_category_arrow").attr({src: "assets/_global/arrow_down.png"});

		if (instant) {
			$(element).animate({ height: '+=420' }, 0, function() {
							categoryBusyCount--;
					   
							// if ie7, show when done
							
							if (ie7) $(element).find(".hype_box").css("visibility", "visible");
					   });
		} else {
			$(element).delay(delay).
					   animate({ height: '+=420' }, categoryOpenCloseTime, function() {
							categoryBusyCount--;
					   
							// if ie7, show when done
							
							if (ie7) $(element).find(".hype_box").css("visibility", "visible");
					   });
		}
							
		currOpenCategory = element;
	}
}

function hypeSetSubmenu(element, index, sindex) {
	var c = '';

	if (hypeContent[index].type == "testimonials") {
		// throw in our testimonials
		
		for (var i in hypeContent[index].submenus[sindex].testimonials) {
			c += '<div class="hype_testimonial">';
			c += hypeContent[index].submenus[sindex].testimonials[i].content;
			
			c += '<div class="hype_author">';
			c += hypeContent[index].submenus[sindex].testimonials[i].author;
			c += '</div>';
			
			c += '</div>';
		}

		$(element).find(".hype_text_block_child").html(c);
	} else {
		// load into a fake div, then push into our block
		
		var div = document.createElement('div');
	
		$(div).load("assets/hype/content/" +  hypeContent[index].submenus[sindex].content_file, function () {		
			// the only way to get this to work is a reset
			
			var c = '<div class="hype_text_block flexcroll">';
		
			c += '<div class="hype_text_block_child">';
			c += $(div).html();
			c += '</div>';	
			c += '</div>';
			
			$(element).find(".hype_content").html(c);
			fleXenv.fleXcrollMain($(element).find(".hype_text_block")[0]);
		});
	}
}

function hypeSubmenuClicked(element, sindex) {
	hypeSetSubmenu($(element), element.index, sindex);
	$(element).find(".hype_submenuitem").attr("class", "hype_submenuitem sub").eq(sindex).attr("class", "hype_submenuitem sub_on");	
}

/* MENU */

var lookMenuOn = false;
var lookMenuReady = false;

var lookMenuLoadedThumbs = 0;
var lookMenuTotalThumbs;

var waitBounce = 1000;
var bouncePause = 0;

var menuOpenDist;
var menuHeight;
var menuTabHeight;

var baseMenuHeight;

function initMenu() {
	lookMenuTotalThumbs = lookMenuContent.length;
	
	
	// populate our thumbnails, fancybox them
	
	var h = '<div id="look_wrapper">';
		
	for (var i=0; i<lookMenuTotalThumbs; i++) {
		var t = '<div class="thumb">'; 
		t += '<div class="look_icon"><a href="#" class="fancybox">';
		t += '<img src="assets/looktab/thumbnail_images/' + lookMenuContent[i].thumbnail +'" border="0" alt="" />';
		t += '</a></div>';
		
		t += '<div class="look_title">' + lookMenuContent[i].title + '</div>';
		t += '</div>';
		
		h += t;
	}
	
	//h += '<div id="look_tab"><img src="assets/_global/look_tab.png" width="140" height="28" border="0" alt="look" /></div>';
	h += '<div id="look_tab"><img src="assets/_global/spacer.gif" width="140" height="28" border="0" alt="look" /></div>';
	h += '</div>';
		
	$("#look_menu").html(h);
			
	$("#look_tab").bind('click', lookMenuClicked);

	$("#look_menu .look_icon a.fancybox").each(function(index) {
		fancyboxIt(this, "looktab/", "", lookMenuContent, index);
	});
	
	// bind our load handler
	
	$("#look_menu .look_icon img").bind('load', loadedMenuThumb);
	
	// work out the proper height of the menu based on the number of thumbs

	menuTabHeight = 28; // height of just the tab (closed)
	baseMenuHeight = $("#look_menu").height(); // the height of the open nav
	
	var iconH = $("#look_menu .look_icon").outerHeight(true);
	var titleH = $("#look_menu .look_title").outerHeight(true);
	var otherH = baseMenuHeight - ((iconH + titleH) * 4); // css defaults to 4 thumbs
	
	menuHeight = (iconH + titleH) * lookMenuTotalThumbs + otherH;

	$("#look_menu").css("height", menuHeight + "px");
	$("#look_menu").css("top", menuTabHeight - menuHeight + "px");

	$("#look_menu").css("background-position", "0 " + (menuHeight - baseMenuHeight) + "px"); 
		
	menuOpenDist = menuHeight - menuTabHeight;
		
	// if we are not on the home page, don't bounce the menu
	
	if (!$("body.home").length) {
		lookMenuReady = true;
	}
}

function loadedMenuThumb() {
	if (++lookMenuLoadedThumbs == lookMenuTotalThumbs) {
		if ($("body.home").length) {
			// if we are on the home page, bounce the menu
			
			$("#look_menu").delay(waitBounce).
							animate({ top: '+=100' }, 350).
							delay(bouncePause).
							animate({ top: '-=100' }, 1150, "easeOutBounce", function() {
								lookMenuReady = true;
							});
		}
	}
}

function lookMenuClicked() { 
	if (!lookMenuReady) return;
	
	lookMenuReady = false;
	
   	if (!lookMenuOn) {
   		$("#look_menu").animate({ top: '+=' + menuOpenDist }, "normal", function() {
			lookMenuReady = true;
   		});   		
   	} else {
   		$("#look_menu").animate({ top: '-=' + menuOpenDist }, "normal", function() {
			lookMenuReady = true;
   		});
   	}
   	
   	lookMenuOn = !lookMenuOn;
}



/* Shari JS */

function adjustForBrowser() {
	if (navigator.platform.indexOf("Win32") > -1) {
		/* Windows - adjust look tab position */
  		lookTabObj = document.getElementById("look_tab");
		lookTabObj.style.marginTop = "16px";
		if (navigator.userAgent.indexOf("MSIE 7.0") > -1) {
	      	/* IE 7 - adjust icons inside open look menu */
			lookObj = document.getElementById("look_wrapper");
			lookObj.style.marginLeft = "0px";
		}
	} else if (navigator.platform.indexOf("Mac") > -1) {
		/* Mac - adjust nav padding */
	   navObj = document.getElementById("nav");
	   navObj.style.paddingLeft = "14px";
	   if (navigator.userAgent.indexOf("Firefox") > -1) {
 			/* Firefox on Mac - adjust look tab position */
  	   		lookTabObj = document.getElementById("look_tab");
	   		lookTabObj.style.marginTop = "-5px";
	   }
	}
	
	
}

/*************************************************************/
