$(function() {  
	String.prototype.trim = function () {
		return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}       
	//IE 6 transparency fixes
	if ($.browser.msie && $.browser.version.substr(0,1)<7)
		DD_belatedPNG.fix(".IE6transparency, .featureHovers");
	
	//text replacment
	Cufon.replace(['h1', 'h2', 'h3', 'h5', '.head_text'], { fontFamily: 'Trajan' });	
	Cufon.replace(['h4','.ad_boxes p','.home_boxes p'], { fontFamily: 'Cochin' });//cursive font					
	if ($.browser.msie) 
		$('#secondary_nav, #main_nav').toggleClass("boldMe");//this is being done beacuse IE8 cant handle the hovering... dies
	else 
		Cufon.replace(['#main_nav li a','#secondary_nav a','a.button'], {
			hover: true
		}, { fontFamily: 'Trajan' } );						
	
	/*sliding down nav
	setTimeout("slidingNav ()",1500);
	*/
	
	//setting container height for the floating out ad boxes
	var hight_collector = 0;
	$('#container_ad_box .ad_boxes').each(function(){
		$(this).css('top',hight_collector+'px');
		hight_collector += $(this).height()*1 + pxStripper($(this).css('padding-top'))*1 + pxStripper($(this).css('padding-bottom'))*1 + pxStripper($(this).css('margin-bottom'))*1 + pxStripper($(this).css('margin-top'))*1;
	});
	$('#container_ad_box').height(hight_collector);
	/*subnav product page movment
	$('#secondary_nav li a').click(function () {
		$('#container_content_right div.options').slideUp();
		$('#container_content_right div#'+$(this).attr('lang')).slideDown();
		//$(this).parent('li').siblings("li").children('a').removeClass('active');
		$('#secondary_nav li a').removeClass('active');
		$(this).addClass('active');
		return false;
	});*/
	
	$("a[rel=group], a[rel=group0], a[rel=group1], a[rel=group2], a[rel=group3], a[rel=group4], a[rel=group5]").fancybox({
		'titleShow'			: false,					   	
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic'
	});		
	
	//CONTACT US FORM page.php?ref=Contact+Us
	if ( $('#contactSubmit').length ) {//this is shared by 2 pages but gives errors on pages that dont have the container presnet
		$('.contactUsErrorOverlay').width($('#contactUs').width()).height($('#contactUs').height());
		$('#contactSubmit').click(function () {
			$('.fadeContainer').fadeTo("slow", 0.33, function () {
				$('.contactUsErrorOverlay').removeClass('hideMe');
				var allClear = true;
				var inputCollector = new Object;
				var warrantyNumExist = false;
				/* if( typeof window.warrantyNumbers == 'function' ) {
					var warrantyNumbersD = warrantyNumbers();
					for ( i=0; i<=warrantyNumbersD.length; i++ ) {
						if ( warrantyNumbersD[i] == $('#warranty_number-prefix').val()+$('#warranty_number').val() ) {
							warrantyNumExist = true;
						}
					}
				} */
				$('#contactUs input, #contactUs select, #contactUs textarea').each(function(){
					if ( 	$(this).attr('lang').substring(0,3) == 'req' && $(this).val().trim() == "" ||
						//	warrantyNumExist && $(this).attr('id') == 'warranty_number' ||
							$(this).attr('lang') == 'req_num' && isNaN($(this).val()) ||
							$(this).attr('lang') == 'req_email' && badEmail( $(this).val() ) ||
							$(this).attr('lang') == 'req_pc' && badPC( $(this).val() ) ||
							$(this).val() == 'None' ) {

						allClear = false;
						$('.contactUsErrorOverlay').removeClass('loaderImg');

						if ( warrantyNumExist )
							$('.contactUsErrorOverlay tr td').html('<strong>The warranty registration number you are trying to enter has already been registered.<br />If you feel this is in error please <a href="page.php?ref=Contact+Us">contact us</a></strong><br /><br /><button id="err_signup">Back</button>');
						else if ( $(this).attr('lang') == 'req_num' && $(this).val().trim() != '' && typeof($(this).val()) != 'undefined' )
							$('.contactUsErrorOverlay tr td').html('<strong>Numeric values only, please verify entry</strong><br /><br /><button id="err_signup">Back</button>');
						else if ( $(this).attr('lang') == 'req_pc' && badPC( $(this).val().trim() ) )
							$('.contactUsErrorOverlay tr td').html('<strong>Please enter a valid postal code</strong><br /><br /><button id="err_signup">Back</button>');
						else
							$('.contactUsErrorOverlay tr td').html('<strong>Please fill in all the required fields</strong><br /><br /><button id="err_signup">Back</button>');

						$(this).addClass('redBorder');
						$("#err_signup").bind("click", function(){
							$('.fadeContainer').fadeTo("normal", 1);
							$('.contactUsErrorOverlay').addClass('hideMe loaderImg');
							$('.contactUsErrorOverlay tr td').html('');
							return false;
						});
						return false;//no need to have the script keep running if we got to this point
					}
					if ($(this).attr('lang') == 'req_pc')
					  inputCollector[$(this).attr('id')] = $(this).val().toUpperCase().trim();
					else
					  inputCollector[$(this).attr('id')] = $(this).val().trim();
				});
				if (allClear) {
					$.post("lib/php/contactUs.php", inputCollector,
						function(data){
							$('.contactUsErrorOverlay').removeClass('loaderImg');
							$('.contactUsErrorOverlay tr td').html('<strong>'+data.success+'</strong>');
						},
					"json");
				}

			});
			return false;
		});
		$('#contactUs input, #contactUs select, #contactUs textarea').click(function () {
			$(this).removeClass('redBorder');
		});
		$('#ct_country, #country').change(function() {
			if ($(this).val() == 'Canada') {
				$('.countrySpecific label .Canada').removeClass('hideMe');
				$('.countrySpecific label .US').addClass('hideMe');
				$('.countrySpecific').removeClass('hideMe');
			} else {//this is the US
				$('.countrySpecific label .Canada').addClass('hideMe');
				$('.countrySpecific label .US').removeClass('hideMe');
				$('.countrySpecific').removeClass('hideMe');
			}
		}).change();
	}
	
	var clickDifferent = function() {
		if ( $(this).siblings('ul').attr('lang') != "" ) {
			$(this).siblings('ul').slideUp('normal').removeAttr('lang');
		} else {
			$('.expand ul[lang="expanded"]').slideUp('normal').removeAttr('lang');
			$(this).siblings('ul').attr('lang','expanded');			
			$(this).siblings('ul').slideDown('slow');
		}
		return false;
	};
	$('.expand a.xclick').bind('click', clickDifferent);	

	// initialize home impage rotation
	if ( $('#imgContainer').length ) {
		$('#imgContainer').cycle({
			fx: 'fade',
			timeout: 20000,
			random: 1,
			pause: 1
		});	
	}
	
	//makes the entire ad box clickable
	if ( $('.ad_boxes').length ) {
		$('.ad_boxes').click(function () { 
			window.location = $(this).children('div').children('a.button').attr('href')
		});
	}
	if ( $('.home_boxes').length ) {
		$('.home_boxes').click(function () { 
			window.location = $(this).children('a.button').attr('href')
		});
	}	
	
});
/*
function slidingNav () {
	$('#secondary_nav').removeClass('hideMeVis').animate({ paddingTop: "206px", marginBottom: "0px" }, { duration: 1500, queue: false });			
} 
*/

function pxStripper (value) {
	return value.substring(0,value.length-2);
}
	function badPC(pc) {
	   var reg = /^[ABCEGHJ-NPRSTVXYabceghj-nprstvxy]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Zabceghj-nprstv-z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Zabceghj-nprstv-z]{1}[0-9]{1}$/;
	   if (!reg.test(pc)) {
	       return true;
	   }
	   return false;
	}
function badEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if( !reg.test(email) ) {
	  return true;
   }
   return false;
}	
