var onloadEvents = new Array();
function addToOnload(event){
	onloadEvents.push(event);
	window.onload = function() { 
		for (var x = 0; x < onloadEvents.length; x ++) {
			eval(onloadEvents[x]);
		}
	}
}


var Look = {
	originalShowcaseHeight : 0,
	eventResizing: false,
	collapseTopNavigation : function () {
		$('top-navigation').morph({
			'height':'20px'
		});
		$('top-navigation').setStyles({
			'z-index': '4'
		});
	},
	collapsePhotographyNavigationBottom : function () {
		$('photography-navigation').morph({
			'height':'40px',
			'top':'732px'
		});
		$('photography-navigation').setStyles({
			'z-index': '4',
			'top':''
		});
	},
	collapseStylistNavigationBottom : function () {
		$('stylist-navigation').morph({
			'height':'20px',
			'top':'769px'
		});
		$('stylist-navigation').setStyles({
			'z-index': '4',
			'top':''
		});
	},
	collapsePhotographyNavigationTop : function () {
		$('photography-navigation').morph({
			'height':'40px',
			'top':'235px'
		});
		$('photography-navigation').setStyles({
			'z-index': '4'
		});
	},
	collapseStylistNavigationTop : function () {
		$('stylist-navigation').morph({
			'height':'20px',
			'top':'270px'
		});
		$('stylist-navigation').setStyles({
			'z-index': '4'
		});
	},
	collapseHairNavigationBottom : function () {
		$('hair-navigation').morph({
			'height':'20px',
			'top':'786px'
		});
		$('hair-navigation').setStyles({
			'z-index': '4',
			'top':''
		});
	},
	posMenuElement : function (elementId) {
		return $(elementId).getPosition().y + 17;
	},
	clickOnMenuLink : function () {
		$('menu-link').setStyle('display','none');
		$('top-navigation').setStyles({
			'display':'block'
		});
		$('hair-navigation').setStyles({
			'display':'block'
		});
		$('stylist-navigation').setStyles({
			'display':'block'
		});
		$('photography-navigation').setStyles({
			'display':'block'
		});
		return false;
	},
	clickOnTopLink : function () {
		if ($('top-navigation').getSize().y>21) {
			window.setTimeout('window.location.href="/"', 200);
			return true;
		}
		Look.collapsePhotographyNavigationBottom();
		Look.collapseStylistNavigationBottom();
		Look.collapseHairNavigationBottom();
		$('top-navigation').morph({
			'height':'500px'
		});
		$('top-navigation').setStyles({
			'z-index': '2'
		});
		return false;
	},
	clickOnPhotographyLink : function() {
		if ($('photography-navigation').getSize().y>40) {
			return false;
		}
		Look.collapseTopNavigation();
		Look.collapseStylistNavigationBottom();
		Look.collapseHairNavigationBottom();
		$('photography-navigation').morph({
			'height':'550px',
			'top':'235px'
		});
		$('photography-navigation').setStyles({
			'z-index': '2'
		});
		return false;
	},
	clickOnStylistLink : function () {
		if ($('stylist-navigation').getSize().y>21) {
			return false;
		}
		Look.collapseTopNavigation();
		Look.collapsePhotographyNavigationTop();
		Look.collapseHairNavigationBottom();
		$('stylist-navigation').morph({
			'height':'500px',
			'top':'270px'
		});
		$('stylist-navigation').setStyles({
			'z-index': '2'
		});
		return false;
	},
	clickOnHairLink : function () {
		if ($('hair-navigation').getSize().y>21) {
			return false;
		}
		Look.collapseTopNavigation();
		Look.collapsePhotographyNavigationTop();
		Look.collapseStylistNavigationTop();
		$('hair-navigation').morph({
			'height':'500px',
			'top':'287px'
		});
		$('hair-navigation').setStyles({
			'z-index': '2'
		});
		return false;
	},
	/**
	 * Fix position and attach search onchange event
	 */
	init : function() {
		$$('#category-search select').each(function(s){
			s.addEvent('change',function(e){
				window.location.href = this.value;
			});
		});
		$('top-navigation').set(
			'morph',{
				duration: 1000,
				transition: 'sine:out'
			}
		);
		$('photography-navigation').set(
			'morph',{
				duration: 1000,
				transition: 'sine:out'
			}
		);
		$('stylist-navigation').set(
			'morph',{
				duration: 1000,
				transition: 'sine:out'
			}
		);
		$('hair-navigation').set(
			'morph',{
				duration: 1000,
				transition: 'sine:out'
			}
		);

		//clickOnMenuLink();
		
		$('menu-link').addEvent('click', function(e) {
			new Event(e).stop();
			return Look.clickOnMenuLink();
		});

		$('top-link').addEvent('click', function(e) {
			return Look.clickOnTopLink();
		});
		$('photography-link').addEvent('click', function(e) {
			new Event(e).stop();
			return Look.clickOnPhotographyLink();
		});
		$('stylist-link').addEvent('click', function(e) {
			new Event(e).stop();
			return Look.clickOnStylistLink();
		});
		$('hair-link').addEvent('click', function(e) {
			new Event(e).stop();
			return Look.clickOnHairLink();
		});
		
		var flash = $('flash');
		//IE 6 fix. Animate box repositioning.
		if (Browser.Engine.trident4) {
			Look.showcase = $('showcase-links');
			if (!Look.showcase) return;
			
			if (!flash) {
				$('content').setStyle('padding','45px 20px 130px 0');
				Look.showcase.setStyle('position','absolute');
			} else {
				$('content').setStyle('padding','20px 10px 0 0');
				Look.showcase.setStyle('position','absolute');
			}
			
			//find footer position. Make sure it  doesnt go below it
			Look.footer = $('footer');
			Look.setShowcasePosition(true);
			window.addEvent('scroll', function(){
				clearTimeout(window.reorderTimeout);
				window.reorderTimeout = setTimeout('Look.setShowcasePosition()', 200);
			});
			window.addEvent('resize',function(){
				clearTimeout(window.reorderTimeout);
				window.reorderTimeout = setTimeout('Look.setShowcasePosition()', 200);
			});
			$('menu-search').setStyles({
				'top': ($('latest-news').getPosition().y-15) + 'px',
				'position': 'absolute'
			});
		} else {
			Look.nav = $('look-nav');
			Look.setNavPosition();
			
			window.addEvent('resize',function(){
				Look.setNavPosition();
			});
		}
		
		if (flash) {
			Look.prepareFlash(true);
			//setInterval(Look.prepareFlash, 200);
			window.addEvent('resize',function(){
				Look.prepareFlash(false);
			});
		}
	},
	prepareFlash: function(start) {
		var flash = $('flash');
		
		var offsetX = 0;
		var offsetY = 0;
		
		//Ratio = 1.7 
		var ratio = 1.6;
		//Get window size and available space
		var windowSize = window.getSize();
		
		var maxX = windowSize.x - 173;
		var maxY = windowSize.y - 210 + 25;
		
		//IE6
		if (Browser.Engine.trident4) {
			flash.setStyle('position','absolute');
			
			var scroll = window.getScroll();
			var offset = scroll.y + windowSize.y - 160;
			//Get footer position
			var footerPos = $('footer').getCoordinates();
			if (offset > (footerPos.top - 160)) {
				offset = footerPos.top - 160;
			}
			maxY = offset;
			maxX = maxX + 8;
		}
		
		//Try to use up all the height first
		var yt = Math.round(maxY * ratio) < maxX ? maxY  : false;
		if (!yt) {
			xt = maxX;
			yt = Math.round(xt/ratio);
		} else {
			xt = Math.round(yt*ratio);
		}
		if (Look.xt == xt && Look.yt == yt) {
			return;
		}
		
		Look.xt = xt;
		Look.yt = yt;
		flash.setStyles({'height':yt, 'width': xt });
		
		//IE6 Position the swf at the bottom of the page.
		if (Browser.Engine.trident4) {
			if (footerPos.bottom > windowSize.y ) {
				flash.setStyle('top', windowSize.y - yt - 145 );
			} else {
				flash.setStyle('top', footerPos.bottom - yt - 165);	
			}
			$('showcase-links').setStyles({
				'width' : xt + 10
			});
		} else {
			$('latest-news').setStyles({
				'width' : xt
			});
		}
		
		var width = '100%';
		var height = '100%';
		var area = 'flash-movie';
		/*var flashvars = {xmlPath : 'http://lookproduction.com/func/Look.getXml/' , xmlName : 'talent.xml'};*/
		var flashvars = {xmlPath : '/xml/' , xmlName : 'look.xml'};
		if (start) {
			swfobject.embedSWF("/media/Look.swf", area, width, height, "9.0.0","/media/expressInstall.swf", flashvars, {bgcolor: '#222222' });	
		}
	},
	/**
	 * Set nav position and showcase links width. (showcase links should be the same width as the photo area)
	 */
	setNavPosition: function() {		
		var photo = $('photo');		
		var size = window.getSize();
		var flash = $('flash');
		
		if (!flash) {
			var sL = $('showcase-links');
			var sC = $('showcase');
			if (sL) {
				//160 , 13
				var showcaseWidth = size.x - ((size.x - 185) % 170) - 7;
				var showcaseLinksWidthTmp = showcaseWidth;
				
				if(photo != null) {
					var photoWidthWithBuffer = parseInt(photo.get('width'))+188;
				} else {
					var photoWidthWithBuffer = 0;
				}
				
				if(photoWidthWithBuffer > showcaseLinksWidthTmp) {
					showcaseLinksWidthTmp = photoWidthWithBuffer;
				}
				
				if(showcaseLinksWidthTmp > size.x) {
					showcaseLinksWidthTmp = size.x;
				}
				
				sL.setStyle('width',showcaseLinksWidthTmp);
				var content = $('content');
				if (content && !sC) {
					content.setStyle('width',showcaseWidth - 188);
					content.setStyle('padding-bottom',175);
				}
			}
			var imageHeader = $('header_0');
			if (imageHeader) {
				var maxWidth = showcaseWidth - imageHeader.getSize().x - 190;
				var tl = $('top-links');
				if (tl) 
					tl.setStyle('width',maxWidth);
			}
		}
		
		if (size.y > 740) {
			Look.nav.setStyle('position' ,'fixed');
		} else {
			Look.nav.setStyle('position' ,'absolute');
		}
		
		Look.setPhotoFrame();
	},
	setShowcasePosition : function(start) {
		/*
		currentFooterPosition = Look.footer.getCoordinates().top;
		windowHeight = window.getSize().y;
		menuSearchHeight = $('menu-search').getSize().y;
		if (menuSearchHeight>600) {
			menuSearchHeight = 600;
		}
		searchHeight = windowHeight-50-currentFooterPosition + menuSearchHeight;
		$('menu-search').setStyle('height', searchHeight + 'px');
		*/
		var scroll = window.getScroll();
		var size = window.getSize();
		var offset = scroll.y + size.y - 140;
		
		var showcaseWidth = size.x - 165;
		
		//Get footer position
		var footerPos = Look.footer.getCoordinates();
		if (offset > (footerPos.top - 140)) {
			offset = footerPos.top - 140;
		}

		var flash = $('flash');
		if (flash) {
			showcaseWidth = flash.getStyle('width') + 10;
		} else {
			showcaseWidth = size.x - 165 - ((size.x - 165) % 170);	
			var sC = $('showcase');
			var imageHeader = $('header_0');
			if (imageHeader) {
				var maxWidth = showcaseWidth - imageHeader.getSize().x;
				var tl = $('top-links');
				if (tl) 
					tl.setStyle('width',maxWidth);
			}
			
			if (content && !sC) {
				content.setStyle('width',maxWidth + 215);
			}
			
		}
		
		var photo = $('photo');
		var showcaseLinksWidthTmp = showcaseWidth;
		
		if(photo != null) {
			var photoWidthWithBuffer = parseInt(photo.get('width'))+188;
		} else {
			var photoWidthWithBuffer = 0;
		}
		
		if(photoWidthWithBuffer > showcaseLinksWidthTmp) {
			showcaseLinksWidthTmp = photoWidthWithBuffer;
		}
				
		if(showcaseLinksWidthTmp > size.x-188) {
			showcaseLinksWidthTmp = size.x-188;
		}
		
		Look.showcase.getFirst().setStyle('padding','0 10px 0 0');
		Look.showcase.setStyles({
			left		: 165,
			padding		: '0 0 0 0',
			margin		: 0,
			width 		: showcaseLinksWidthTmp
		});
		
		if (Browser.Engine.trident4) {
			offset = offset-25;
			
			if(Look.originalShowcaseHeight == 0) {
				// set the height bigger just once
				Look.showcase.setStyle('height', parseInt(Look.showcase.getStyle('height'))+25);
				Look.originalShowcaseHeight = parseInt(Look.showcase.getStyle('height'))+25;
			}
		}
		
		Look.showcase.setStyle('top',offset+50);
		
		/** the tween seemed to only work in ie6 and also looked bad
		
		if (start == true) {
			Look.showcase.setStyle('top',offset);
		} else {
			Look.showcase.set('tween',{'duration':250});	
			Look.showcase.tween('top', offset);
		}
		*/
		
		Look.setPhotoFrame();
	},
	
	fixImagePosition : function(type)
	{
		if (type == 'news') {
			var imgList = $$('#news img');
		} else {
			var imgList = $$('#showcase img');
		}
		
		if (imgList.length == 0) return;
		
		imgList.each(function(img){
			if (img.height > 20) {
				var padding = (160 - img.height)/2;
				if (padding > 2) {
					img.setStyle('padding-top',padding+'px');
				}
			} else {
				img.addEvent('load',function(){
					var padding = (160 - img.height)/2;
					if (padding > 2){
						this.setStyle('padding-top',padding+'px');	
					}
				});	
			}
			
		});
		
	},
	/**
	 * Photo Frame
	 */
	setPhotoFrame : function(x, y) {
		
		try {
			var pframe = $('photo-frame');
			var photo = $('photo');
			var sL = $('showcase-links');
			
			//determine what the height of the photo frame should be
			var windowSize = window.getSize();
			var cheight = windowSize.y - 170 - 40;
			
			if(cheight < photo.get('height')) {
				cheight = photo.get('height');
			}
			
			if (Browser.Engine.trident4) {
				pframe.setStyles({
					'width' : parseInt(sL.getStyle('width'))-10
				});
			} else {
				pframe.setStyles({
					'width' : parseInt(sL.getStyle('width'))-188
				});
			}
			
			if(photo.get('height') < 600) {
				// photo is less than 600, we need to make the frame bigger and give the photo padding
				pframe.setStyles({
					'background' : '#222222',
					'height' : cheight
				});
				
				var paddingTop = Math.round((cheight - photo.get('height'))/ 2);
				photo.setStyles({
					'padding' : paddingTop+'px 0 0 0'
				});
			} else {
				// photo is full size, need to give same size to frame and no padding
				pframe.setStyles({
					'background' : '#222222',
					'height' : cheight
				});
				
				var paddingTop = 0;
				photo.setStyles({
					'padding' : paddingTop+'px 0 0 0'
				});
			}
		} catch (error) {
			
		}
//		Look.setPhotoFrame.delay(700);
	}
}


var HeaderReplacement = {
	//List of images
	list : [],
		
	add : function(cnt, val){
		HeaderReplacement.list[cnt] = val;	
		var header = document.getElementById(val.id);	
		if (!header){
			return 0;
		}
		var bits = header.id.split('_');
		var param = HeaderReplacement.list[Number(bits[1])];
		if(!param) return 1;
		header.className = header.className+' image-heading';
		var bg = param.url;
		
		var bgString = "url('"+bg+"') no-repeat 0 0";
		header.style.background = "url('"+bg+"') no-repeat 0 0";
		header.style.height = param.imgHeight+'px';
		header.style.width = param.imgWidth+'px';
		return 1;
	}
}


var Loader = {
	display : function(e) {
		
		var obj = $(e);
		var size = obj.getScrollSize();
		//var e = $('large-image').setStyle('opacity','.1');
		//Generate a prefix id for our feedback animation containers.
		var loading = new Element('div',{
			'id' : e+'-loader',
			'styles' : {
				'width' : (size.x - 12)+'px',
				'height': (size.y - 10)+'px',
				'margin': '0 6px',
				'position': 'absolute',
				'opacity' : '.5',
				'z-index' :	'1000',
				'filter'  : 'alpha(opacity=100)',
				'background' : 'url(/css/images/spinner.gif) no-repeat 50% 50%'	
			}
		});
		
		loading.inject(obj,'top');				
	},
	
	clear : function(e) {
		if ($(e+'-loader')) {
			$(e+'-loader').dispose();
		}
	}
	
}



