(function($){	
	$.fn.fullscreenr = function(options) {
		if(options.height === undefined) alert('Please supply the background image height, default values will now be used. These may be very inaccurate.');
		if(options.width === undefined) alert('Please supply the background image width, default values will now be used. These may be very inaccurate.');
		if(options.bgID === undefined) alert('Please supply the background image ID, default #bgimg will now be used.');
		var defaults = { width: 1280,  height: 1024, bgID: 'bgimg' };
		var options = $.extend({}, defaults, options); 
		$(document).ready(function() { $(options.bgID).fullscreenrResizer(options);	});
		$(window).bind("resize", function() { $(options.bgID).fullscreenrResizer(options); });		
		return this; 		
	};	
	$.fn.fullscreenrResizer = function(options) {
		// Set bg size
		var ratio = options.height / options.width;	
		// Get browser window size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		// Scale the image
		if ((browserheight/browserwidth) > ratio){
		    $(this).height(browserheight);
		    $(this).width(browserheight / ratio);
		} else {
		    $(this).width(browserwidth);
		    $(this).height(browserwidth * ratio);
		}
		// Center the image
		$(this).css('left', (browserwidth - $(this).width())/2);
		$(this).css('top', (browserheight - $(this).height())/2);
		return this; 		
	};
})(jQuery);

$(document).ready(function(){
	var theWidth = $(document).width();
	$(".container .wrap").css({"width":theWidth+"px"});
	
	var windowHeight = $(window).height();
	$(".FullHeight").height(windowHeight-31);
	var newMarginTop = "-"+((windowHeight-175)/2)+"px";
	$("#tickerNav").css({"position":"fixed","top":"50%","margin-top":"-100px"})
	
	$(window).resize(function(){
		var newMarginTop = "-"+((windowHeight-250)/2)+"px";
		$("#tickerNav").css({"position":"fixed","top":"50%","margin-top":"-100px"})
	})
	
	$(".gridContent").hover(function(){
		$(this).addClass("hoverGrid");
	}, function(){
		$(this).removeClass("hoverGrid");
	})

	$("#bgimg").parent().append("<div id=\"bgFader\"></div>");
	var newWidth = $(document).width();
	$("#bgFader").css({"width":(newWidth-530)+"px"});

	$("#textArea").hover(function(){
		$("#textArea").fadeTo(500,"1");
		$("#bgimg").fadeTo(500,".4");
	}, function(){

	})

	$("#bgFader").hover(function(){
		$("#textArea").fadeTo(300,".5");
		$("#bgimg").fadeTo(300,"1");
	}, function(){
	})
	
	$("#tickerNav .TickerLinkToPage a").each(function(){
		var theURL = $(this).attr("href");
		if(theURL.indexOf("sneezemag.com") == -1){
			$(this).parent().parent().find(".TickerPercentChanged").append("<img src=\"/cms/wp-content/themes/sneezemag/art/leaveSite.png\" class=\"externalIt\" />");
		}
	})

	$("#textArea").fadeTo(500,".5");

	$(".buyit").hover(function(){
		$(this).addClass("buyitHover");
	}, function(){
		$(this).removeClass("buyitHover");
	})

	$(window).resize(function() {
		var theWidth = $(document).width();
		$(".container .wrap").css({"width":theWidth+"px"});
		var windowHeight = $(window).height();
		$(".FullHeight").height(windowHeight);
		var newWidth = $(document).width();
		$("#bgFader").css({"width":(newWidth-530)+"px"});
		checkImgHeight();
	});
	
	$(".setBackground").each(function(){
		var imageURL = $(this).attr("href");
		$(this).attr("href","javascript:;");
		$(this).attr("rel",imageURL);
		setTimeout("runPreloader('"+imageURL+"');",3000);
	})
	
	$(".setBackground").click(function(){
		var imageURL = $(this).attr("rel");
		var captionToShow = $(this).parent().attr("rel");
		//loadBackDrop(imageURL);
		$(".footerExtra").hide();
		$("#extraCredits").show();
		$(".extraCaption").hide();
		$("#extraCaption"+captionToShow).show();
		$("#backgroundImageLarge img").attr("src",imageURL);
	})

	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
	
})

function runPreloader(whatImage){
	$.preLoadImages(whatImage);
}
function setBGds(whatImage){
	$("body").prepend("<div id=\"backgroundImageLarge\"><img id=\"backgroundImageLargeIMG\" src=\""+whatImage+"\" border=\"0\" /><\/div>");
	var FullscreenrOptions = {  width: 1024, height: 683, bgID: '#backgroundImageLargeIMG' };
	$.fn.fullscreenr(FullscreenrOptions);
}

function checkImgHeight(){
	var theHeight = $(document).height();
	if($("#backgroundImageLarge img").height() < theHeight){
		//$("#backgroundImageLarge img").css({"width":"auto","height":theHeight});
	}
}



