$(window).load(function(){
	showcase.create();
});

showcase = {
	create: function(){
		$('.showcase').attr('direction', 'right');
		$('.showcase > div').wrapInner('<div></div>');

		var width = 0;

		for(var i=0; i<$('.showcase > div > div').children().length; i++){
			width += $('.showcase > div > div').children().eq(i).outerWidth();
		}

		$('.showcase > div > div').css({ 'width':width+'px' });
		
		showcase.start();

	},
	
	start: function(){
		switch($('.showcase').attr('direction')){
			case 'left':
				out = 0;
				var distance = $('.showcase > div > div').width();
			break;
			default:
				out = $('.showcase > div > div').width()-760;
				var distance = out - $('.showcase > div > div').scrollLeft();
			break;
		}
		
		$(".showcase > div").animate({ scrollLeft: out+'px' }, distance*10, "linear", function(){
			if($('.showcase').attr('direction') == 'left') $('.showcase').attr('direction', 'right');
			else $('.showcase').attr('direction', 'left');
			showcase.stop();
			setTimeout("showcase.start()", 20);
		});
	},
	
	stop: function(){
		$('.showcase').stop();
	}
}
/*$(window).load(function(){
	loadPage();
	$('.shade, .view').fadeOut(2000);
});

$(document).ready(function(){
	// Section determines if there is a #value or not, if so it loads that page, if not, and the page
	// isn't default.asp it will forward to the url minus the file name with #value, so
	// http://www.site.com/showcase.asp becomes http://www.site.com/#showcase

	if(location.hash){
		$('body').attr("page", dehash(location.hash));
		
		page = $('body').attr('page');
		if(page == 'home') page = 'default';
		
		$.get(page+'.asp', { 'js':'true' }, function(data){
				$('body > div > div > div').html(data);
				
				loadPage();
		}, "html");
				
	}else if(location.href.indexOf('.asp') > 0){
		url = location.href.substring(0, location.href.lastIndexOf('/')+1);
		page = location.href.substring(location.href.lastIndexOf('/')+1, location.href.lastIndexOf('.'));
		if(page == 'default') page = 'home';
		window.location.href = 	url+'#'+page;
	}
  	
  	// Sets the menu up to allow it to fade in and out, without JS the menu will highlight normally
  
	$('body > div > ul > li > img').css({ 'opacity':'0' });  
	
	$('body > div > ul > li').hover(function(){
		$(this).find('img').fadeTo(0, 0);
		$(this).find('img').fadeTo(300, 0.5);
		
		return false;
	}, function(){
		$(this).css({ 'display':'block' });
		$(this).find('img').fadeTo(300, 0);
		
		return false;
	});
	
	// Deals with clicking main navigation
	// Takes the file name without extension and sets that as <body page="VALUE"> for easy reference
	// Then runs PAG.asp?js=true which is a content only page
	// Then fades out the content, adds the new data and fades back in
	// The <body page="VALUE"> is then dehashed() and the page is relocated to #VALUE
	// loadPage() runs the necessary JS for the new content
	
	$('body > div > ul > li').find("a").click(function() {
		
		$('body').attr("page", '#'+$(this).attr('href').replace('.asp', ''));
		
		$.get($(this).attr("href"), { 'js':'true' }, function(data){
			
			$('body > div > div > div').animate({ 'opacity':'0' }, 500, function(){
																				 
				$(this).html(data);
				$(this).css({ 'opacity':'0' });
				$(this).animate({ 'opacity':'1' });
				
				page = dehash($('body').attr('page'));
				if(page == 'default') page = 'home';
				
				window.location.hash = page;
				
				loadPage();
			});
			
		}, "html");
		
		return false;
	 });

});



loadPage = function(){

	page = dehash($('body').attr('page'));
	if(page == 'default') page = 'home';
			
	
	if(page == 'showcase' || page == 'about-us' || 'about-you'){
		log('<br/>Page: '+page);
		
		// For each image in the new showcase div check to see if it is loaded ("complete")
		// If not, after 100ms reload loadPage() and try again, if a link is broken it still counts as complete
		// and the js can pick up it's width later on, once finished it runs showcase.create();
		
		for(var i=1; i<$('body > div > div > div').find('img').length; i++){
			me = $('body > div > div > div').find('img').eq(i);
			if(!$(me).attr("complete")){
				log('&nbsp;!&nbsp;'+$(me).attr("src")+'&nbsp;'+i);
				setTimeout("loadPage()", 100);
				return false;
			}else{
				log('&nbsp;&nbsp;&nbsp;'+$(me).attr("src"));
			}
		}
		log('Complete<br/>');
		showcase.create();
	}
	
	// Fades out the logo on the home page, shows it on the rest
	
	if(page == 'home'){
		$('.logo').fadeOut(200);
	}else{
		$('.logo').fadeIn(200);
	}
};


showcase = {
	create: function(){
		$('.arrow').css({ 'visibility':'visible', 'opacity':'0.5' });
		$('.showcase').css({ 'overflow':'hidden' });
		$('.showcase').wrapInner('<div></div>');
		$('.showcase').attr('direction', 'right');
		
		var width = 0;
		log(width+'MY WIDTH');
		for(var i=0; i<$('.showcase > div').children().length; i++){
			width += $('.showcase > div').children().eq(i).outerWidth();
			log(width+' :'+i);
		}
		
		width += ($('.showcase > div').children().length*4);
		log(width+'MY WIDTH');

		$('.showcase > div').css({ 'width':width+'px' });
				
		$('.arrow').hover(function(){
			if($(this).hasClass("left")) dir = 'left';
			else dir = 'right';
			$('.showcase').attr('direction', dir);
			showcase.move(dir);
			
			$(this).animate({ 'opacity':'1' }, 300); return false;
		}, function(){
			showcase.stop();
			$(this).animate({ 'opacity':'0.5' }, 300); return false;
		});
		
		$('body > div > div').hover(function(){
			showcase.stop();
		}, function(){
			showcase.start();
		});
		
		showcase.start();
		var images = [];
		
		for(var i=0; i<$('.showcase > div').find("a").length; i++){
			log("link: "+$('.showcase > div').find("a").eq(i).attr("href"));
			images[i] = new Image();
			images[i].src = $('.showcase > div').find("a").eq(i).attr("href");
			images[i].onload=function(){
				log($(this).attr("src")+' :complete');
			};
		}
		
		$('.showcase > div').find("a").click(function(){
			me = this;
			out = "<div><img src=\""+$(me).attr('href')+"\" alt=\""+$(me).attr('title')+"\" />";
			if($(me).attr('title')){
				out += "<p>"+$(me).attr('title')+"</p>";
			}
			jQuery.facebox(out+"</div>");

			return false;
		});
		
	},
	
	move: function(dir){
		switch(dir){
			case 'left':
				out = 0;
				var distance = $('.showcase').scrollLeft() - out;
			break;
			case 'right':
				out = $('.showcase > div').width()-760;
				var distance = out - $('.showcase').scrollLeft();
			break;
		}
		
		log('AR: '+$('.showcase').attr('direction')+': '+out);
		log(distance);
		showcase.stop();
		$(".showcase:not(:animated)").animate({ scrollLeft: out+'px' }, distance*5, "linear");
	},
	
	start: function(){
		switch($('.showcase').attr('direction')){
			case 'left':
				out = 0;
				var distance = $('.showcase').scrollLeft() - out;
			break;
			default:
				out = $('.showcase > div').width()-760;
				var distance = out - $('.showcase').scrollLeft();
			break;
		}
		
		log('SC: '+$('.showcase').attr('direction')+': '+out);
		log('Distance: '+distance);
		log('Scroll: '+$('.showcase').scrollLeft());
		
		$(".showcase:not(:animated)").animate({ scrollLeft: out+'px' }, distance*100, "linear", function(){
			if($('.showcase').attr('direction') == 'left') $('.showcase').attr('direction', 'right');
			else $('.showcase').attr('direction', 'left');
			showcase.stop();
			setTimeout("showcase.start()", 300);
		});
	},
	
	stop: function(){
		$('.showcase').stop();
	}
}

debug = false;

$(document).ready(function(){
	if(!debug) return false;
	$('#console').css({ 'left':'-200px', 'display':'block' });
	
	$('#console').hover(function(){
		$(this).animate({ 'left':'0px' });
	}, function(){
		$(this).animate({ 'left':$(this).width()-$(this).width()-$(this).width()+20 });
	});
});

log = function(me){
	if(debug)
	$('#console > div').prepend('<br/>'+me);
}

dehash = function(me){
	if(me == undefined) return false;
	return(me.substring(me.indexOf('#')+1, me.length));
}*/