// JavaScript Document
//main.js - javascript fixes for frn
$(function(){
	//load navigation, VERY important
	$('#leftNav').load('leftNav/navigation.html', startMenu);
	
	//start up featured slideshow on homepage
	$('#featured').jshowoff({
		controls:false,
		links:false,
		speed:10000
	});
	
	//important messages
/*	$.facebox({
		div:'#shippingError',
		loadingImage : '../images/loading.gif',
        closeImage   : '../images/closelabel.png'
	});*/
	
	//if special sale price date has passed, or if there is no special price, only show the list price
	//fix is for all browsers
	$(".special").each(function(index, element){
		var current = element;
		var specialContents = $(current).text(),
		isEmpty = specialContents.substr(0, 7);
		if(isEmpty != "Sale: $"){
			$(current).hide();
			$(current).siblings('.list').css({'text-decoration': 'none', 'font-weight': 'bold', 'font-size':'14px !important'});
		}
	});
	$(".container table span").not(".special").css('font-weight', 'normal');
	$(".container select").each(function(){
		$(this).css({'float':'left !important'});
	});
	
	//remove "Select" from the attribute labels
	//fix is for all browsers
	$('.attributes > div > table > tbody > tr > td:first-child').each(function(i){
		var text = $(this).text();
		var replacement = text.substr(7);
		$(this).html(replacement);
	});
	
	//fix image size in list view
	//all browsers but ie
	$('div.item.container.itemDesc > div.left > a > img').load(function(){
		var img = $(this);
		img.each(function(){
			var width = img.width();
			var height = 125;
			var ratio = width/height;
			//resize image if its too big
			if(width > 150){
				var newWidth = 150;
				var newHeight = newWidth/ratio
				img.attr({
					width: newWidth,
					height: newHeight
				});
				img.css({'width':newWidth+'px','height':newHeight+'px'});
				img.width(newWidth);
				img.height(newHeight);
			}
			if(width < 150){
				var newMargin = (150-width)+71;
				img.parents('div.left').siblings('div.options').css({'margin-left' : newMargin+'px !important'});
			}
		});
	});
	
	//fix image size on detail pages
	//all browsers but ie
	$('#image > img').load(function(){
		var img = $(this);
		var width = img.width();
		var height = 175;
		var ratio = width/height;
		//resize the image if it's too big
		if(width > 222){
			var newWidth = 222;
			var newHeight = newWidth/ratio;
			img.css({
				'width': newWidth+'px',
				'height': newHeight+'px'
			});
		}
		if(width < 222){
			var newMargin = (222-width)+30;
			//img.parents('#image').siblings('div#itemInfo').css({'left':newMargin+'px'});
		}
	});
	
	//image fix for when detail page reloads on attribute select
	$('#image img').each(function(){
		var img = $(this);
		var width = img.width();
		var height = 175;
		var ratio = width/height;
		if(width > 222){
			var newWidth = 222;
			var newHeight = newWidth/ratio;
			img.attr({width:newWidth, height:newHeight});
		}
	});
	
	//fix attribute/custom option select boxes
	$('#itemInfo select').uniform();
	$('div.options select').uniform();
	
	//if there aren't any attributes, move the qty box up
	if($('.attributes').find('table').length = 0){
		$('.attributes p').css({'margin-top':'-20px'});
	}
	
	//fix width of tables in description
	$('div.desc table').each(function(){
		$table = $(this);
		if($table.width() > 550){
			$(this).attr('width', '550');
			$(this).css({'width':'550px'});
		}
		if($table.attr('width') > 550){
			$(this).attr('width', '550');
		}
	});
	
	//if table in tabs
	$('#tabs table').each(function(){
		$table = $(this);
		if($table.attr('width') >= 500){
			$(this).removeAttr('width');
			$(this).css({'width':'100%'});
		}
	});
	
	//zebra stripes
	$('#tabs table tr:even').not(':first-child').css({'background-color':'#fff'});
	$('#tabs table tr:odd').css({'background-color':'#dbdbbe'});
	
	//format more tables
	$("#rope tbody tr:last").css("border-bottom","1px solid #000");
	$("#rope tbody tr td:first-child").css("border-right","1px solid #000");
	$("#rope tbody  tr td").css("text-align","left");
	$("#rope tbody tr th:first-child").css("border-right", "1px solid #000");
	$(".table tbody tr td").not(".table tbody tr td:last").css("border-right", "1px solid #000");
	$(".table tbody tr th").not(".table tbody tr th:last").css('border-right', 'solid 1px #000');
	
	//ie specific fixes
	if($.browser.msie){
		//rounded corners
		$('div.featured').each(function(){
			PIE.attach(this);
		});
		
		//fix select widths
		$('div.attributes select').css({'width':'auto'});
		
		//list view in ie
		$('div.item.container.itemDesc > div.left > a > img').load(function(){
			var img = $(this);
			img.each(function(){
				var width = img.width();
				var height = 125;
				var ratio = width/height;
				if(width > 150){
					var newWidth = 150;
					var newHeight = newWidth/ratio;
					img.attr({width:newWidth, height:newHeight});
					img.css({'width':newWidth+'px', 'height':newHeight+'px'});
					img.width(newWidth);
					img.height(newHeight);
				}
				if(width < 150){
					var newMargin = (150-width)+71;
					img.parents('div.left').siblings('div.options').css({'margin-left':newMargin+'px !important'});
				}
			});
		});
		
		//detail view in ie
		$('#image img').load(function(){
			var img = $(this);
			var width = img.width();
			var height = 175;
			var ratio = width/height;
			if(width > 222){
				var newWidth = 222;
				var newHeight = newWidth/ratio;
				img.attr({width:newWidth, height:newHeight});
			}
		});
		
		//fix cart button in ie6
		if(parseInt($.browser.version) == 6){
			$('div.options div.pricing div.cart input').css({
				'position':'absolute',
				'top':'0',
				'left':'25px'
			});
			$('div.item.container.itemDesc').append("<div class='clear'>&nbsp;</div>");
		}
	}
	
	//link clicks
	//respirator tips
	$('a[rel*=facebox]').facebox({
		loadingImage : '../images/loading.gif',
        closeImage   : '../images/closelabel.png'
	});
});
