$(function(){
	var flash_support = navigator.mimeTypes["application/x-shockwave-flash"] ? true : false;
	//DROP-DOWN MENU
	$('#top-nav li')
	.hover(function(){
		$(this).find('>ul').stop(true,false).attr('style','display:none').fadeIn();
	},function(){
		$(this).find('>ul').stop(true,false).fadeOut();
	})
	.find('ul').css({display:'none'});
	//TEXT INPUT AUTOFILL
	$('.autofill input.text').each(function(){
		$(this).attr('o_val',$(this).val())
		.bind('focus',function(){
			if ($(this).val() == $(this).attr('o_val')) $(this).val('').removeClass('empty');
		})
		.bind('blur',function(){
			if ($(this).val() == '') $(this).val($(this).attr('o_val')).addClass('empty');
		});
	});
	//SEARCH RADIO BUTTONS
	$('#form-search input.url').click(function(){
		var str_url = $(this).attr('value');
		$(this).closest('form').attr('action',str_url);
	});
	$('#form-search input.url:first').attr('checked','checked');
	//Form Actions (Spam Protect)
	$('#frm-signup').attr('action','/form-post/email-signup.asp');
	//FIND EMAIL AND PRINT LINKS
	$('#col-01 .actions').each(function(){
		var $a = $(this).find('a');
		$a.first().removeClass('popup').addClass('email');
		$a.last().addClass('print');
	});
	//POPUP LINKS
	$('a.popup').click(function(){
		window.open (this.href,'winpopup','width=480,height=460');
		return false;
	});
	//EMAIL AND PRINT LINK BEHAVIOUR
	$('a.email').click(function(){
		mail_str = "mailto:?subject=Check out this website";
		mail_str += "&body=I thought you might be interested in this website: ";
		mail_str += location.href;
		location.href = mail_str;
		return false;
	});
	$('a.print').click(function(){ window.print(); return false; });
	
	//IMAGE SCALING (Scale to Fit Bounding Box)
	$('.p_thumb img').each($img_fit([148,111]));
	$('.product-images .thumb img').each($img_fit([66,48]));
	$('#product-image img').each($img_fit([350,270]));
	
	//PRODUCT PREVIEW IMAGE(S)
	var $thumbs = $('.product-images .thumb');
	$thumbs.each(function(){
		var $a = $(this), $lg = $a.closest('.prod_det_left').find('#product-image');
		var str_src = $a.attr('href')
			, img = $a.find('img').get(0)
			, img_tmp = new Image();
		img_tmp.onload = function() {
			var wh = fn_img_fit([img_tmp.width,img_tmp.height],[350,270]);
			$a.click(function(){
				$lg.html('')
					.append($('<a/>').attr({'class':'MagicMagnifyPlus',href:str_src}).append($('<img/>').css({'width':wh[0] + 'px','height':wh[1] + 'px'}).attr({width:wh[0],height:wh[1],src:str_src})));
				if (window.MagicThumb_Refresh && !window.ie_ver) {
					setTimeout(MagicThumb_Refresh,50);
					//MagicThumb_Refresh();
				}
				return false;
			});
		};
		img_tmp.src = str_src;
	});
	
	//PAGE PEEL INIT
	if (!fn_isdev()) $.getScript('/pagepeel/peel-4.js');
	//PNG FIX FOR IE6
	if (window.ie_ver == 6)
	$('img').each(function(){
		var img = this, src = String(img.src);
		if (src.match(/\.png$/i)) fn_png_fix(img);
	});
});

function $img_fit(arr_wh) {
	return function(){
		var img = this, img_tmp = new Image();
		$(img_tmp).load(function(){
			var old_dim = [this.width,this.height];
			var wh = fn_img_fit(old_dim,arr_wh);
			$(img).css({'width':wh[0] + 'px','height':wh[1] + 'px'});
		});
		img_tmp.src = img.src;
	};
}

function fn_img_fit(wh_full,wh_fit) {
	if (wh_full[0] <= wh_fit[0] && wh_full[1] <= wh_fit[1]) {
		return wh_full;
	}
	var ar_full = wh_full[0] / wh_full[1], ar_fit = wh_fit[0] / wh_fit[1];
	if (ar_full > ar_fit) {
		return [wh_fit[0],Math.floor(wh_fit[0] / ar_full)];
	} else {
		return [Math.floor(wh_fit[1] * ar_full),wh_fit[1]];
	}
}

function fn_isdev() {
	return (window.location.host == 'cleanerswarehouse.local')
}

function fn_png_fix(i1) {
	var i2 = new Image();
	i2.onload = function(){
		var int_w = i2.width, int_h = i2.height;
		i1.onload = function(){
			this.onload = null;
			$(this).css({'width':int_w + 'px','height':int_h + 'px','display':'block','filter':"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + i2.src + "', sizingMethod='scale')"});
		};
		i1.src = '/img/blank.gif';
	};
	i2.src = i1.src;
}

