$(document).ready(function(){
	build_bg_height();
	$(window).resize(function() {build_bg_height();});
});
$(window).load(function(){ // initiate other scripts -> window.load to wait for rounded corners first.

	$('html').removeClass('loading');
	$("ul.menu li").hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover'); }
	);
	$(".ship").hover(
		function(){
			$(this).children(".photo").stop().animate({ opacity: 0.2});
			//$(this).children("table").stop().animate({ opacity: 1});
			$(this).children("table").fadeIn();
		},
		function() {
			$(this).children(".photo").stop().animate({ opacity: 1});
			//$(this).children("table").stop().animate({ opacity: 0});
			$(this).children("table").fadeOut();
		}
	);
	$(".ship").click(function(){ document.location = $(this).parent().attr("href"); });
	
	$("#mi_"+ $("body").attr('id')).addClass("active");
	build_bg_height();
});

function preload(arr, element) {
	var count	= arr.length;
	var temp	= $('<img />')[0];
	temp.src=arr[0];
	$(temp).load(function(){
		$(temp).css('display','none');
		$(element).prepend(temp);
		if(count-1>0){
			arr.shift();
			preload(arr, element);
		}else{
			$(element +" .ajaxload").remove();
			$(element).cycle({fx:"fade",slideResize:0,fit:0,prev:".prevSlide",next:".nextSlide",timeout: 6000});
		}
	});
}

function preloadhome(arr, element) {
	var count	= arr.length;
//	console.log(count);
	var temp	= $('<img />')[0];
	temp.src	= arr[0]+ "?" + new Date().getTime();
//	console.log(arr[0]);
	$(temp).load(function(){
//		console.log(count +' loaded');
		$(temp).css('display','none');
		$(element).prepend(temp);
		if(count-1>0){
			arr.shift();
//			console.log(count +' next->');
			preloadhome(arr, element);
		}else{
//			console.log('go');
			build_bg_height();
			$(element +" .ajaxload").remove();
			$(element).cycle({
				fx			: "fade",
				slideResize	: 0,
				fit			: 1,
				timeout		: 6000,
				after		: function(currSlideElement, nextSlideElement) {
					$.post(base + 'ajax/set_bg', {bgimg : $(nextSlideElement).attr('src')}, function(data)
					{
						//console.log(data);
					}); 
				}
			});
			$(element).width('');
		}
	});
}

function build_bg_height() {
	var window_width	=	$(window).width();
	var window_height	= 	$(window).height();
	var ratio_window	=	window_width / window_height;
	//console.log("Window height: "+ window_height);console.log("Window width: "+ window_width);console.log("Window ratio w/h: "+ ratio_window);
	if(window_width < 960) { window_width = 960; }
	//console.log($("#background-image img"));
	$("#background-image img").each(function() {
		/* console.log("---BEGIN---");console.log("orig img height: "+ $(this).height());console.log("orig img width: "+ $(this).width()); */
		var ratio_img	=	$(this).width() / $(this).height();
		var new_width	=	window_width;
		var diff		=	1+((new_width - $(this).width())/$(this).width());
		var new_height	=	Math.ceil($(this).height()*diff);
		
		if(ratio_window < ratio_img) {
			var ratio_img	=	$(this).width() / $(this).height();
			var new_height	=	window_height;
			var diff		=	1+((new_height - $(this).height())/$(this).height());
			var new_width	=	Math.ceil($(this).width()*diff);

		}
		/*console.log("DIFF :"+ diff);console.log("IMG ratio w/h: "+ ratio_img);console.log("New height: "+ new_height);console.log("New width: "+ new_width);console.log("New ratio w/h: "+ new_width/new_height);*/
		$(this).width(new_width);
		$(this).height(new_height);
		//var ratio_img = $(this).height() / $(this).width(); console.log("Ratio h/w: "+ ratio_img); $(this).height($(this).height()*ratio_img); console.log("---END---");
	});
}
