//to be copied to the site wide area of the amazon store
//<script type="text/javascript" src="/images/jquery_1.3.2.min.js"></script>
//<script type="text/javascript" src="/images/jquery.validate.min.js"></script>
//<script type="text/javascript" src="/images/amazon_csw.js"></script>


$(document).ready(function(){	
	preFormatting();
	setupFooterRightColumn();		
	adjustColumnsHeight();
	
	validateNewsletterForm();
	openLinkExtWindow();
});

function openLinkExtWindow(){
	$('a[rel=external]').click(function(){
		window.open($(this).attr('href'));
		return false; 
	});
}//end function

function validateNewsletterForm(){
	$("#newsletterForm").validate({
	  rules: {
		email1: {
		  required: true,
		  email: true
		},
		email2: {
		  equalTo: "#email1"
		}
	  }
	});
}//end function

function preFormatting(){
	//force all table to have cellspacing of 0, as IE doesn't respect border collapse
	$('table').attr({ 'cellSpacing' : '0' });
	
	//hide the spacer gif between the three main column in the home page
	$('.content > tbody > tr > td > img').hide();
	
	//hide the amazon account tr which is still shown as a black gap in IE
	$('table.logo').parents('tr').hide();

	//order id top menu to have some space below the top nav
	if($('meta[name=PageType]').attr('content') == 'OrderStatus'){
		$('td.fg1').parents('tr').css('height','20px');
	}
}//end function

function setupFooterRightColumn(){
	//remove both the left and right widgets from showing up on the checkout page
	if($('meta[name=PageType]').attr('content') == 'orderConfirmation'){
		return;
	}
		
	//setup the privacy policy page so that the text doesn't get too cramped.
	if($('meta[name=PageType]').attr('content') == 'privacyPolicy'){
		$('td#line2').next().css('width','0%');
	}
	
	//adding the td right column if it doesn't exists for product and category page
	if ($('td.rightnav-col').length < 1){
		$('table.content > tbody > tr:last').append('<td class="rightnav-col" valign="top" width="155"><div id="cswhometopright"></div></td>');
	}
	
	//move the third site widget to become the right column
	var tempHtml = $('#left-nav-3_s').html();
	$('#left-nav-3_s').html(""); //CLOSED THIS LINE FOR DEBUGGING
	$('.rightnav-col').html(tempHtml);
	
	//move the fourth site widget to become the footer below middle div
	if($('meta[name=PageType]').attr('content') == 'Homepage' || $('#body-1').length > 0){
		//for the home page
		tempHtml = $('#body-1').html() + $('#left-nav-4_s').html();
		$('#body-1').html(tempHtml);
	}
	else if($('#productsection').length > 0){
		//for radio, speaker, home theater page
		$('div#productpage').append("<br/><br/>"+$('#left-nav-4_s').html());		
	}
	//custom added meta tag under each page's Page Properties
	else if($('meta[name=PageType]').attr('content') == 'Category'){
		//for category page
		$('div#content-3').append("<br/><br/>"+$('#left-nav-4_s').html());

		//set the width for the breadcrumb
		$('table#page-title').parent('td').parent('tr').parent('tbody').parent('table').css({				
			'margin-left':'15px',
			'width':'95%'
		});
	}
	else if($('meta[name=PageType]').attr('content') == 'Product'){
		//for product page
		$('div#content-2').append("<br/><br/>"+$('#left-nav-4_s').html());				
					
		//hide the amazon freemusic image as it breaks the width of the center column
		$("img[src='http://images.americas.creative.com/images/inline/products/amazonfreemusic_logo.jpg']").hide();
		
		//set the width for the breadcrumb
		$('table > tr > td:contains(table#page-title)').css({
			'margin-left':'15px',
			'width':'95%'
		});
	
		//set the table width for the content to have some left padding
		$('tr.breadcrumbtrail').parent('tbody').parent('table').css({
			'margin-left':'15px',
			'width':'95%'
		});
	}
	else{
		//catch all for the about, cs page prepared by li zhi
		$('div#content-1').append("<br/><br/>"+$('#left-nav-4_s').html());
	}
	
	//clear the temp html else it might cause the layout to be misaligned
	$('#left-nav-4_s').html(""); //CLOSED THIS LINE FOR DEBUGGING		
}//end function

function adjustColumnsHeight(){
	//grab the main table height
	// for right column it should be equal to this value
	// for the left column need to minus out all the top part and adjust the bottom div to the balance.
	var newHeight = 0;
	newHeight = $('table.main').height();
	var newLeftBottomHeight = 0;
	//set the right column height
	$('div#cswhometopright').height(newHeight);
	
	newLeftBottomHeight = newHeight - $('#left-nav-1_s').height();
	
	//removed the category and product page as the best seller widgets were removed.
	if(( ($('#body-1').length > 0) && ($('meta[name=PageType]').attr('content') != 'Homepage') ) 
	|| $('meta[name=PageType]').attr('content') == 'Search' || $('meta[name=PageType]').attr('content') == 'orderConfirmation' )
	{	
		newLeftBottomHeight = newLeftBottomHeight - $('#left-nav-2_s').height();
		
		//alert(newLeftBottomHeight);
		$('#left-nav-6').height(newLeftBottomHeight);			
	}
	else{
		$('#left-nav-2_s').height(newLeftBottomHeight);
	}
}//end function