/**
 * @author dave
 */

jQuery(document).ready(function($){
	(function() {
		
		function BluntControl() { 
			var $Win;
			var $Root; 
			var $Media,$MediaWrap;
			var mediaType;
			
			var fadeTime=500;
			var delayTime=300;
			
			var img,$PN;

			$(document).ready(_init);
			
			function _init(){
				alignr($('#Nav'),$('#NavArea'), {'v' : 'top', 'h' : 'center', 'toff' :10, 'loff' : 0, 'vUnit' : 'px', 'hUnit' : 'px', 'pos' : 'absolute'} );
				$Win = ("onorientationchange" in window) ? $(document) : $(window); 
				
				$Root = $('#Content');
				
				
				//console.log('Root : '+$Root.width());
				$MediaWrap = $('#MediaWrap');
				$Media = $('#Media');
				
				$PN= $MediaWrap.find('#PostNav');

				
				mediaType = $Media.attr('class');
				
				$MediaWrap.css('opacity',0);
				//$Media.css('filter', 'alpha(opacity=0)');
				
				
				loadBackPlate();
				
				if(mediaType == 'Thumbs' || mediaType == 'HThumbs')
					enableThumbEvents();
					
				$(window).resize(handleResize);
			}
			
			function loadBackPlate() { 
				var src = $('div#BG p img ').attr('src');
				$('div#BG p ').remove();
				$.backstretch(src,{ speed : 1000 }, function(){  backPlateOnScreen(); });
			}
			
			function backPlateOnScreen() { 
				loadMedia();
			}
			
			function loadMedia() { 
				switch(mediaType) { 
					case 'Thumbs'  :
					case 'HThumbs' :
						layoutMedia();
						revealMedia();
						break;
					case 'Image':
						loadImage(); 
						break;
					case 'Video':
						loadVideo();
						revealMedia();
						break;
				}
			}
			
			function revealMedia() { 
				setTimeout(function() { $('#FG').css('visibility','visible'); $MediaWrap.fadeTo(1000,1); },100);
			}
			
			function loadImage() { 
				iSrc = $Media.find('img').attr('src');
				$Media.find('#Image').remove();
				
				

				img = new Image();

				$(img).load(function(){
					$Media.append(img);
					layoutMedia();
					revealMedia();
					preloadImages();				
				}).attr('src', iSrc);
			
			}
			
			function loadVideo() { 
				//console.log('load video : '+mediaType);
				$Media.css('opacity',1);
				layoutMedia(); 
			}
			
			function layoutMedia() { 
				//console.log('render media : '+mediaType);
				switch (mediaType) {
					case 'Thumbs' : // we are dealing with a thumbnail page
						layoutThumbs();
						break;
					case 'HThumbs' :
						layoutHomeThumbs();
						break;
					case 'Image' : // Its a photo
						layoutImage(); 
						break; 
					case 'Video' : // Its a video  
						layoutVideo(); 
						break; 	
				}
				
				alignr($('#Nav'),$('#NavArea'), {'v' : 'top', 'h' : 'center', 'toff' :10, 'loff' : 0, 'vUnit' : 'px', 'hUnit' : 'px', 'pos' : 'absolute'} );
				
			}
			
			function layoutThumbs() { 
				var thumbsWidth = $('.Thumbs').width();	
				var tw,th, ntw;
				
				//Scale Thumbs
				tw=(thumbsWidth*25)/100;
					
				tw=tw-30;
				th=tw;
				
				$('.Thumb').css({
						'margin' : '5px',
						'width' : tw + 'px',
						'height': tw + 'px'
				});
						
				ntw = (tw*4)+120;
				var ttw = $('.Thumb').width();
				ntw = (ttw*4)+40;
				$('.Thumbs').css('width',ntw);
				
				alignr($Media, $Root,{'v' : 'top', 'h' : 'center', 'toff' :($Root.height()*8)/100, 'loff' : 0, 'vUnit' : 'px', 'hUnit' : 'px', 'pos' : 'absolute'});
			}
			
			function layoutHomeThumbs() {
				
				//alert('HomeThumbs');
				var thumbsWidth = $('.HThumbs').width();	
				var tw,th, ntw;
				
				//Scale Thumbs
				tw=(thumbsWidth*20)/100;
					
				tw=tw-50;
				th=tw;
				
				$('.Thumb').css({
						'margin' : '5px',
						'width' : tw + 'px',
						'height': tw + 'px'
				});
						
				ntw = (tw*5)+120;
				var ttw = $('.Thumb').width();
				ntw = (ttw*5)+60;
				$('.HThumbs').css('width',ntw);
				
				alignr($Media, $Root,{'v' : 'top', 'h' : 'center', 'toff' :($Root.height()*8)/100, 'loff' : 0, 'vUnit' : 'px', 'hUnit' : 'px', 'pos' : 'absolute'});
			}
			
			function layoutImage() { 
				img.style.visibility = 'hidden';
				scaleImage(img, $Root, 73);
				img.style.visibility = 'visible';

				
				alignr($Media, $Root,{'v' : 'top', 'h' : 'center', 'toff' :($Root.height()*8)/100, 'loff' : 0, 'vUnit' : 'px', 'hUnit' : 'px', 'pos' : 'absolute'} );
				alignPostNav();
			}
			
			function layoutVideo() { 
				//console.log('renderVideo');
				$Media.css({ 'width' :'760px', 'height' : '468px'});
				//divScaler($Media, $Root, 75);
				alignr($Media, $Root,{'v' : 'top', 'h' : 'center', 'toff' :($Root.height()*8)/100, 'loff' : 0, 'vUnit' : 'px', 'hUnit' : 'px', 'pos' : 'absolute'});
				alignPostNav();
			}
			
			function alignPostNav() { 
				var mw,mh,ml, mt;
				var pw,ph;
				var nl, nt;; 
				
				mw = $Media.width(); 
				mh = $Media.height();
				
				ml = $Media.position().left;
				mt = $Media.position().top;
				
				pw = $PN.width();
				ph = $PN.height(); 
				
				nl = (ml+(mw/2) - (pw/2));
				nt = (mt+mh)+20;
				
				$PN.css( { 'top' : nt+'px', 'left' : nl+'px', 'position' : 'absolute'});
			}
			
			function enableThumbEvents() { 
				$('.Thumb').find('.Title').css('opacity',0);
					$('.Thumb').hover(function(){
						$(this).find('img').fadeTo('slow', .6);
						$(this).find('.Title').fadeTo('slow', 1);
					}, function(){
						$(this).find('img').fadeTo('slow', 1);
						$(this).find('.Title').fadeTo('slow', 0);
				});
			}
			
			function align($t,$b,vm,hm) { 
				// $t ->  align target, b -> boundary ref, vm -> vertical align mode, hm ->  horizontal align mode.
				
				var tw,th,bw,bh;
				var top, left;
				
				tw=$t.width();
				th=$t.height();
				
				bw=$b.width();
				bh=$b.height();
				
				
				(vm == undefined) ? top = ((bh/2) - (th/2)) +'px' : top = vm;
				(hm == undefined) ? left = ((bw/2) - (tw/2))+ 'px' : left = hm;
				
				$t.css({ 'position' : 'absolute', 'top' : top, 'left' : left});
			}
			
			function scaleImage(img,$b,p) { 
				// $t ->  scale target (should be an image), b -> boundary ref, p -> percentage of b that t will be scaled to.		
				var tw,th,bw,bh;
				var maxw,maxh,nw,nh,r;
								
				//tw=img.width;
				//th=img.height
				
				th=img.clientHeight;
				tw=img.clientWidth;

				bw=$b.width();
				bh=$b.height();
				
				maxw = Math.round((bw*p)/100);
				maxh = Math.round((bh*p)/100);
						
				r = Math.min( maxw/tw,maxh/th);
						
				nw = tw*r; 
				nh = th*r;
				
				//bdb('tw : '+tw+' th : '+th+' bw : '+bw+' bh : '+bh);
				$(img).css( { 'width' : nw+'px', 'height' : nh+'px'} );
			}
			
			function alignImage($t,$b,vm,hm) { 
				// $t ->  align target, b -> boundary ref, vm -> vertical align mode, hm ->  horizontal align mode.			
				var tw,th,bw,bh;
				var top, left;
				
				tw=$t.width()+hm;
				th=$t.height();
				
				bw=$b.width();
				bh=$b.height();
				
				(vm == undefined) ? top = ((bh/2) - (th/2)) +'px' : top = vm;
				(hm == undefined) ? left = ((bw/2) - (tw/2))+ 'px' : left = ((bw/2) - (tw/2))+ 'px';
				
				$t.css({ 'position' : 'absolute', 'top' : top, 'left' : left});
			} 
			
			function alignTo($t,$b,vm,hm) { 
				// $t ->  align target, b -> boundary ref, vm -> vertical align mode, hm ->  horizontal align mode.
				
				var tw,th,bw,bh,bl,bt;
				var top, left;
				
				tw=$t.width();
				th=$t.height();
				
				bw=$b.width()+40;
				bh=$b.height();
				
				bl=Math.floor($b.offset().left);
				bt=Math.floor($b.offset().top);

				
				(vm == undefined) ? top = (bh/2) - (th/2) : top = (bt+bh)+vm;
				(hm == undefined) ? left = ((bw/2) - (tw/2))+bl : left = hm;

				
				$t.css({ 'position' : 'absolute', 'top' : top, 'left' : left});
			}
			
			function handleResize() { 
				switch(mediaType) { 
					case 'Thumbs' : 
						$('.Thumbs').css('width',$Root.width()/2);
						break;
					case 'HThumbs' : 
						$('.HThumbs').css('width','75%');
						break;
				}
				
				layoutMedia();
			}
			
			function alignr(a, t,opt) { 
				var aw,ah,at,al;
				var tw,th, tt, tl;
				var top,left,vCoord, hCoord;
				
				var o = {'v' : 'center', 'h' : 'center', 'toff' : 0, 'loff' : 0, 'vUnit' : 'px', 'hUnit' : 'px', 'pos' :'absolute'};
				(opt) ? o = opt : o = o; 
				
				aw = a.width();
				ah = a.height();
				
				tw = t.width(); 
				th = t.height();
				
				tt = t.offset().top; 
				tl = t.offset().left;
				
				switch(o.v) { 
					case 'center' : 
						top = (th/2) - (ah/2);	
						vCoord = 'top';
						
						break; 
					case 'top' :
						top = tt + o.toff;
						vCoord = o.v;
						break;
					case 'bottom' :	
						//top = o.toff;
						top = (tt+th)+o.toff;
						vCoord = o.v;
						break;			
				}
				
				top = top+o.vUnit;
				(o.v == 'bottom') ? a.css('top',top)  : a.css('top',top);
				
				
				
				switch(o.h) { 
					case 'center' : 
						left = (tw/2) - (aw/2);	
						hCoord = 'left';
						break; 
					case 'left' :
						left =  o.loff;
						hCoord = o.h;
						break;
					case 'right' :	
						left = o.loff;
						hCoord = o.h;
						break;			
				}
				
				
				left = left+o.hUnit;
				
				(o.v == 'right') ? a.css('right',left)  : a.css('left',left);
				
				//console.log(' align '+a.attr('id')+' -> '+t.attr('id')+' aw : '+aw+' ah : '+ah);
				//console.log(' align '+a.attr('id')+' -> '+t.attr('id')+' top : '+top+' left : '+left);	

				a.css('position', 'absolute');
			}
			
			
			function preloadImages() { 
				var $P, pSrc,nSrc;
				var nImg;
				
				$P = $('#Preload li');
				
				nSrc=$($P[0]).html();
				pSrc=$($P[1]).html();
				
				nImg = new Image(); 
				
				$(nImg).load(function () { 
					//alert( 'image preloaded'); 
					pImg = new Image(); 
					$(pImg).attr('src',pSrc);
				}).attr('src', nSrc);
				
				
			}
	}
	
	//Kick it all off
	BluntControl();
})();


});

function bdb(m) { 
	var $dbo = $('#BluntDebug')[0];
	
	m='<p>'+m+'</p>';
	
	var dbHtml= '<div id="BluntDebug" style="position:absolute; z-index:1000; top:0; right:0; width:300px;"></div>'
	
	if($dbo == undefined ) 
		$('body').append(dbHtml);

	$dbo = $('#BluntDebug');
	
	$dbo.append(m);	
}









