var currUrl = '';
var wrapClass = '';

function sizeUp() {
	/* BG IMAGE */
	var imgLoc = $('#background img').attr('src');
	var img = new Image();
	var imgHolder = '#background';
	
	
	$(img).load(function() {
	/* var offset = $('.copybox').offset(); */
		var winHeight = $(window).height();
	//	if (winHeight < (offset.top + 53)) { winHeight = offset.top + 53; }
		
		if (winHeight < 560) { winHeight = 560; }
		var width = img.width;
		var height = img.height;
		
		var ratio = winHeight / height;	
		width = Math.floor(ratio * width) + 'px';
		
		$(img).css({'height' : winHeight, 'width' : width});
		$('#background').css('height', winHeight);
		$(imgHolder).children('img').remove();
		$(imgHolder).prepend(img);
	/*
	*/
	}).attr('src', imgLoc);
	
	
	/* COPY TRAIL */
	/*
	var offset = $('#copyright').offset();
	var copyWidth = $('#copyright').width();
	var winWidth = $(window).width();
	var tWidth = winWidth - (offset.left + copyWidth) + 20;
	$('.trail').css('right', '0px').width(tWidth);
	*/
}

/* LOAD NEW BACKGROUND IMAGE */
function loadNewBg(delayTime) {
	var currImg = $('#background img').attr('src');
	var newImg = currImg;
	while (currImg == newImg) {
		newImg = randImg();
	}
	var imgHolder = '#loading-bg';
	var img = new Image();
	$(img).load(function() {
		var offset = $('.copybox').offset();
		var winHeight = $(window).height();
		if (winHeight < (offset.top + 53)) { winHeight = offset.top + 53; }
		
		if (winHeight < 560) {
			winHeight = 560; 
		}
		var width = img.width;
		var height = img.height;
		
		var ratio = winHeight / height;	
		width = Math.floor(ratio * width) + 'px';
		$(img).css({'height' : winHeight, 'width' : width}).fadeOut();
		$(imgHolder).css('height', winHeight);
		$(imgHolder).children('img').remove();
		var lClass = '';
		if (imgLoc.indexOf('lineup') > -1) { lClass = 'lineup'; }
		$(imgHolder).removeClass().addClass(lClass);
		$(imgHolder).fadeOut(0).delay(delayTime).prepend(img).fadeIn(1200, function() {
			$('#background').css('height', winHeight);
			$('#background').children('img').remove();
			$('#background').attr('class', lClass).prepend(img);
			$(imgHolder).removeClass().children('img').remove();
			$(imgHolder).fadeOut(0);
		});
	}).attr('src', newImg);
}

/* RANDOM BG GENERATOR */
var bgs=new Array("Content/core/pages/backgrounds/home.jpg","Content/core/pages/backgrounds/blanket.jpg","Content/core/pages/backgrounds/lineup.jpg");
function randImg() {
	randno = Math.floor ( Math.random() * bgs.length );
	return(imgLoc = bgs[randno]);
}

function updateContent(pageUrl) {
	$(this).parent().addClass('current');
	var contentWidth = ($('#contentbox').children('div:first').width() + 20 )+ 'px';
	$('#contentload').css({ 'width' : contentWidth, 'right' : '-' + contentWidth });
	$.ajax({
		url: pageUrl,
		type: "GET",
		dataType: "html",
		success: function(pageHtml) {
		var contentHtml = '';
		$(pageHtml).find('#contentbox').each(function() {
			wrapClass = $(this).parent().attr('class');
			contentHtml += $(this).html();
			
		});
		var buzzHtml='';
		$(pageHtml).find('#buzz-gallery-nav').each(function() {
			buzzHtml += $(this).html();											
		});
		if (contentHtml.length > 20) {
			$('#wrapper').attr('class', wrapClass);
			$('#contentbox').attr('class', 'on').fadeIn(200, function() { 
				$(this).children('div:first').html(contentHtml);										
				$('#buzz-gallery-nav').html(buzzHtml);
				Cufon.refresh('h2');
			});
		} else {
			$('#contentbox').attr('class', 'off').fadeOut(200, function() {
				$(this).children('div:first').html('');														
			});
		}
		
		},
		error:function (xhr, ajaxOptions, thrownError, xml){
		alert(xhr.status);
		alert(thrownError);
		}
	});
	currUrl = pageUrl;
}
function changeContent (pageUrl) {
	$.ajax({
		url: pageUrl,
		type: "GET",
		dataType: "html",
		success: function(pageHtml) {
			var contentHtml = '';
			$(pageHtml).find('#contentbox').each(function() {
				wrapClass = $(this).parent().attr('class');
				contentHtml += $(this).html();
			});
			
			$('#wrapper').attr('class', wrapClass);
			if (contentHtml.length > 100) {
				$('#contentbox').attr('class', 'on').fadeIn(200, function() { 
					$('#buzz-gallery-nav').html('');
					$(this).children('div:first').html(contentHtml).fadeIn(0, function() {
						var collectionFlag = pageUrl.match(/collection/i);
						var buzzFlag = pageUrl.match(/buzz/i);
						var eventFlag = pageUrl.match(/event/i);
						var pressFlag = pageUrl.match(/press/i);
						if (collectionFlag) {
							collectionReady();
						} else if (buzzFlag) {
							buzzReady();
						} else if (eventFlag) {
							eventReady();
						} else if (pressFlag) {
							pressReady();
						}
					});								
					//Cufon.refresh('h2');
					if (pageUrl.indexOf("contact") > -1) {
						$("span input").each(function(i) {
							var beginStyle = $(this).attr("style");
							$(this).attr("style", "color: #fff;" + beginStyle).val($(this).attr("title"));
							$(this).bind("focus", function(e) {
								if ($(this).val() == $(this).attr("title")) {
									$(this).val("");
									$(this).attr("style", beginStyle);
								}
							});
							$(this).bind("blur", function(e) {
								if ($(this).val() == "") {
									$(this).attr("style", "color: #fff;" + beginStyle).val($(this).attr("title"));
								}
							});
						});
					}
				});
			} else {
				$('#contentbox').attr('class', 'off').fadeOut(200, function() {
					$(this).children('div:first').html('');														
				});
			}
		},
		error:function (xhr, ajaxOptions, thrownError, xml){
			alert(xhr.status);
			alert(thrownError);
		}
	});
	currUrl = pageUrl;
}
$(document).ready(function() {			   					   
	$('#pagination a').live('click', function() {
		$('#pagination a.current').removeClass('current');
		$(this).addClass('current');
		var imgSrc = $(this).attr('href');	
		var thisTxt = $(this).text();
		$('.collection-img').attr("src", imgSrc).attr('usemap', '#Collection' + thisTxt);
		return false;
    });
	sizeUp();
	$("#navlist li a").mouseover(function() {
		$(this).addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover');
	});
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {

	} else {
		$('#navlist a').live('click', function() {
			var parLiID = $(this).closest('li').attr('id');
			var thisHref = $(this).attr('href');
			var contentURL = thisHref.match(/contentid/i);
			if (contentURL) { contentURL = true; }
			var winLoc = window.location.href;
			var winLocTst = winLoc.match(/contact/i);
			if ((parLiID != 'nav-contact') && (contentURL) && (!winLocTst)) {
				var pageUrl = $(this).attr('href');
				
				$('#navlist li a.active').removeClass('active');
				$(this).addClass('active');
				changeContent(pageUrl);
				loadNewBg(100);
				return false;
			}
		});
		$('#contentbox a').live('click', function() {
			if (!$(this).hasClass('gallery')) {
				var thisHref = $(this).attr('href');
				var contentURL = thisHref.match(/contentid/i);
				if (contentURL) { contentURL = true; }
				var winLoc = window.location.href;
				var winLocTst = winLoc.match(/contact/i);
				if ((contentURL) && (!winLocTst)) {
					var pageUrl = $(this).attr('href');
					changeContent(pageUrl);
					return false;
				}
			}
		});
	}
});


$(window).resize(function() {
	sizeUp();
	var winHeight = $(window).height();
	$('#background img').css('height', winHeight);
});

/* COLLECTIONS PAGE */
var imgHolder = '#feature-image';
var featWidth = 314;
var featHeight = 249;
var loadedImages = new Array();
var collectLeft = 0;
var collectTop = 0;
function placeFeature(coords, imgLoc) {
	var offset = $('#collection-image').offset();
	collectLeft = parseInt(offset.left);
	collectTop = parseInt(offset.top);
	featHeight = $('#collection-feature').height();
	featWidth = $('#collection-feature').width();
	var coordArray = coords.split(",");
	var x1 = parseInt(coordArray[0]);
	var y = parseInt(coordArray[1]);
	var x2 = parseInt(coordArray[2]);
	var xPos = parseInt((x1+x2) / 2) - parseInt(.5 * featWidth) + collectLeft;
	var yPos = parseInt(y - featHeight + collectTop);
	if (yPos < 0) { yPos = 0; }
	loadImage(imgLoc, '#feature-image');
	if($.browser.msie) { $('#collection-feature').css({ 'top' : yPos, 'left' : xPos, 'visibility' : 'visible' }).show(); }
	else { $('#collection-feature').css({ 'top' : yPos, 'left' : xPos, 'visibility' : 'visible' }).fadeIn(300); }
}

function loadImage(imgLoc, imgHolder) {
	$('#loadingImg').attr('id', 'loadedImg');
	/*if (jQuery.inArray(imgLoc, loadedImages) < 0) {*/
	if ($.inArray(imgLoc, loadedImages) < 0) {
		$(imgHolder).html('').fadeIn(0, function() {
			var img = new Image();
			$(img).load(function() {
				$(img).hide();
				$(imgHolder).prepend(img);
				$(img).attr('id', 'loadingImg').fadeIn('slow', function() { $('#loadedImg').remove(); });
				loadedImages.push(imgLoc);
			}).error(function() {
				
			}).attr('src', imgLoc);
		});
	} else {
		$(imgHolder).prepend('<img id="loadingImg" src="' + imgLoc + '" style="display:none;" />').fadeIn(0, function() { 
			$('#loadingImg').fadeIn(200, function() {
				$('#loadedImg').remove(); 
			});
		});
	}
}
collectReady=false;
function collectionReady() {
	if (!collectReady) {
		$('#content').prepend('<div id="collection-feature"><div id="feature-image"></div></div>');
		var loadedImages = new Array();
		var collectLeft = 0;
		var collectTop = 0;
		/* var featHeight = 0;
		var featWidth = 0; */
		var hideCollection = '';
		var imgHolder = '#feature-image';
		var offset = $('#collection-image').offset();
		collectLeft = parseInt(offset.left);
		collectTop = parseInt(offset.top);
		/* featHeight = $('#collection-feature').height();
		featWidth = $('#collection-feature').width(); */
		$('.collection area').live('mouseenter', function() {
			clearTimeout(hideCollection);
			var coords = $(this).attr('coords');
			var imgLoc = $(this).attr('href');
			placeFeature(coords, imgLoc);
		});
		$('.collection area').live('mouseleave', function() {
			if($.browser.msie) { hideCollection = setTimeout("$('#collection-feature').hide()",300); }
			else { hideCollection = setTimeout("$('#collection-feature').fadeOut()",300); }	
		});
		$('.collection area').live('click', function() { return false; });
		
		$(window).resize(function() {
			var offset = $('#collection-image').offset();
			collectLeft = parseInt(offset.left);
			collectTop = parseInt(offset.top);
			featHeight = $('#collection-feature').height();
			featWidth = $('#collection-feature').width();
		});
		collectReady=true;
	}
}
/* PRESS PAGE */

function pressReady() {
	easySlide();
	$("a[rel=press-group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
}

