$(document).ready(function()
{
	window.overDD = false;
	window.overMI = false;
	window.overDD2 = false;
	
	$(".specialsItem .image img").resizeImg({
		maxWidth: 230,
		maxHeight: 154
	});

	/*$(".specialsItem .image img").each(function()
	{
		ran = Math.random();
		$(this).attr("id", ran);
		maxWidth = 230;
		maxHeight = 154;
		
		img = document.getElementById(ran);
		
		alert(img);
		
		w = img.width;
		h = img.height;
		
		alert(wi);
		
		if(maxWidth < w)
		{
			ratio = maxWidth/w;
			w = w*ratio;
			h = h*ratio;	
		}
		
		if(maxHeight < h)
		{
			ratio = maxHeight/h;
			h = h*ratio;
			w = w*ratio;			
		}
		
		//$(this).css("width", w + "px");
		//$(this).css("height", h + "px");		
	});*/
	
	$(".hasSub").live("click", function()
	{
		subName = $(this).attr("sub");
		sub = $("#" + subName);
		if($(this).hasClass("activated"))
		{
			$(this).removeClass("activated");
			sub.css("height", "1px");
		}
		else
		{
			$(this).addClass("activated");
			sub.css("height", "auto");
		}
	});
	
	window.checkDD = function()
	{
		if(!window.overDD && !window.overMI && !window.overDD2)
		{
			$("#dropDown").css("visibility", "hidden");
			$("#dropDown2").css("visibility", "hidden");
		}
	}
	
	$(".menuItem").hover(function()
	{
		window.overMI = true;
		dd = $(this).attr("dd");
		if(dd != null && dd != "")
		{
			ddHTML = $("#" + dd).html();
			//god damn IE
			document.getElementById("dropDown").innerHTML = ddHTML;
			offset = $(this).offset();
			hght = $(this).height();
			wdth = $(this).width();
			
			$("#dropDown").css("left", offset.left + "px");
			$("#dropDown").css("top", offset.top + hght + "px");
			$("#dropDown").css("width", wdth + "px");
			$("#dropDown").css("visibility", "visible");
		}
		else
		{
			$("#dropDown").css("visibility", "hidden");
		}
	}, function()
	{
		window.overMI = false;
		setTimeout("checkDD()", 10);
	});
	
	$("#dropDown").hover(function()
	{
		window.overDD = true;
	}, function()
	{
		window.overDD = false
		setTimeout("checkDD()", 10);
	});
	
	$(".ddItem").live("mouseover", function()
	{
		sub = $(this).attr("sub");
		wdth = $(this).parent().width();
		hght = $(this).height();
		
		if(sub != "" && sub != null)
		{
			offset = $(this).offset();
			subHTML = $("#" + sub).html();
			//GO AWAY IE!!!
			document.getElementById("dropDown2").innerHTML = subHTML;
			
			$("#dropDown2").css("top", offset.top + "px");
			$("#dropDown2").css("left", offset.left + wdth + "px");
			$("#dropDown2").css("width", wdth + "px");
			$("#dropDown2").css("visibility", "visible");			
		}
	});
	
	$("#dropDown2").hover(function()
	{
		window.overDD2 = true;
	}, function()
	{
		window.overDD2 = false;
		$(this).css("visibility", "hidden");
		setTimeout("checkDD()", 10);
	});
});


(function($) {
    $.fn.resizeImg = function(options) {
 
        var settings = $.extend({
            scale: 1,
            maxWidth: null,
			maxHeight: null
        }, options);
        
        $(this).one('load', function() {
 
        //return this.each(function() {
			
			if(this.tagName.toLowerCase() != "img") {
				// Only images can be resized
				return $(this);
			} 

			var width = this.naturalWidth;
			var height = this.naturalHeight;
			if(!width || !height) {
				// Ooops you are an IE user, let's fix it.
				var img = document.createElement('img');
				img.src = this.src;
				
				width = img.width;
				height = img.height;
			}
			
			if(settings.scale != 1) {
				width = width*settings.scale;
				height = height*settings.scale;
			}
			
			var pWidth = 1;
			if(settings.maxWidth != null) {
				pWidth = width/settings.maxWidth;
			}
			var pHeight = 1;
			if(settings.maxHeight != null) {
				pHeight = height/settings.maxHeight;
			}
			var reduce = 1;
			
			if(pWidth < pHeight) {
				reduce = pHeight;
			} else {
				reduce = pWidth;
			}
			
			if(reduce < 1) {
				reduce = 1;
			}
			
			var newWidth = width/reduce;
			var newHeight = height/reduce;
			
			return $(this)
				.attr("width", newWidth)
				.attr("height", newHeight);
			
        })
        .each(function(){
			if (this.complete)
			{
				$(this).trigger("load");
			}
		});
    }
})(jQuery);
