// JavaScript Document
var hideCartInt;
var hide13int;
jQuery(document).ready(function(){

	//******** HIDE SHIPPING IF OVER A POUND - COST IS $1337 *********//
	setTO13();
	//var amount = jQuery('#s_method_tablerate_bestway').next().children('span.price').text();
	//if ( amount.length > 5 ) {
		//jQuery('#s_method_tablerate_bestway').parentsUntil('dl').hide().prev().hide();
	//}
	//******** END HIDE SHIPPING *********//
	
	
	//********HOME PAGE FEATURED PRODUCT*********//
	var featureImgCount=0;
	jQuery('#feature ul#featured-images li').each(function(){
		jQuery(this).addClass(''+featureImgCount).hide();
		featureImgCount++;
	});
	jQuery('#feature ul#featured-images li').first().addClass('selected').show();
	jQuery('#featured-title').html(jQuery('#feature ul#featured-images li.selected img').attr('alt'));
	var featureLinkCount=0;
	jQuery('#feature #featured-right ul#featured-list li a').each(function(){
		jQuery(this).addClass(''+featureLinkCount);
		featureLinkCount++;
	});
	jQuery('#feature #featured-right ul#featured-list li a').first().addClass('selected');
	jQuery('#feature #featured-right ul#featured-list li a').mouseover(function(){
		if(jQuery(this).hasClass('selected')==false){
			jQuery('#feature #featured-right ul#featured-list li a.selected').removeClass('selected');
			jQuery('#feature ul#featured-images li.selected').removeClass('selected').hide();
		
			jQuery('#feature ul#featured-images li.'+jQuery(this).attr('class')).addClass('selected').show();			
			jQuery(this).addClass('selected');
			
			jQuery('#featured-title').html(jQuery('#feature ul#featured-images li.selected img').attr('alt'));			
		}
	});
	//********HOME PAGE FEATURED PRODUCT*********//
	
	//******** CART ROLLOVER *********//
	jQuery('a.top-link-cart').hover(function(){
		showCart();
		makeCartHovers();
	},function(){
		setTimeOut();
	});	
	function makeCartHovers(){
		jQuery('.col-main div.block.block-cart').hover(function(){
			showCart();
		},function(){
			setTimeOut();
		});
	};
	function showCart(){
		clearTimeOut();
		if(jQuery('body').hasClass('checkout-onepage-index')){
			
		}else{
		jQuery('.col-main div.block-cart').css({'display':'block'});
		}
	}
	function hideCart(){
		clearTimeOut();
		jQuery('.col-main div.block-cart').css({'display':'none'});
	}
	function setTimeOut(){hideCartInt=window.setTimeout(hideCart,1500)};
	function clearTimeOut(){window.clearTimeout(hideCartInt)};
	//******** CART ROLLOVER *********//
	
	//******** CONSTRUCT TABS ********//
	if(jQuery('.product-collateral')){
		//HIDE OBSOLETE H2S
		jQuery('.product-collateral h2').each(function(){if(jQuery(this).html()=='Additional Information'||jQuery(this).html()=='Details'){jQuery(this).css({'display':'none'});}});
	
		//CREATE TABS DIV
		jQuery('.product-collateral').append('<div class="tab-content" id="tabs"/>');
		
		//CREATE TAB MENU
		jQuery('.product-collateral #tabs').append('<ul id="jq-tabs"></ul>');
		
		//OVERVIEW TAB
		if(jQuery('.product-collateral .std').html()){
			jQuery('ul#jq-tabs').append('<li><a class="tab" href="#tab-overview">Overview</a></li>');
			jQuery('.product-collateral #tabs').append('<div class="tab-content" id="tab-overview"><h2>Overview</h2>'+jQuery('.product-collateral .std').html()+'</div>');
			jQuery('.product-collateral .std').first().html('');
		}
		//WHATS IN THE BOX TAB
		if(jQuery('.product-collateral #what-n-box')){
			if(jQuery('.product-collateral #what-n-box ul').children('li').length>0){
				jQuery('ul#jq-tabs').append('<li><a class="tab" href="#tab-whats-in-the-box">'+jQuery('.product-collateral #what-n-box').find('h2').first().html()+'</a></li>');
				jQuery('.product-collateral #tabs').append('<div class="tab-content" id="tab-whats-in-the-box">'+jQuery('.product-collateral #what-n-box').html()+'</div>');
			}
			jQuery('.product-collateral #what-n-box').first().html('');
			jQuery('.product-collateral #what-n-box').css({'display':'none'});
		}
		
		//ALL UPSELLS
		jQuery('.product-collateral div.tab-category').each(function(){
			if(jQuery(this).find('div.content-spizzy').find('ul.upsell-list').children('li').length > 0){
				var bb_id = jQuery(this).find('h4 a').html().toLowerCase();
				bb_id = bb_id.replace(/\s/g,'-');
				jQuery('ul#jq-tabs').append('<li><a class="tab" href="#tab-'+bb_id+'">'+jQuery(this).find('h4 a').html()+'</a></li>');
				jQuery('.product-collateral #tabs').append('<div class="tab-content" id="tab-'+bb_id+'">'+jQuery(this).find('div.content-spizzy').first().html()+'</div>');
				jQuery(this).find('div.content-spizzy').first().html('');
			}
			jQuery(this).css({'display':'none'});
		});
		
		//FIND SPECIAL ATTRIBUTES
		if(jQuery('.product-collateral table#product-attribute-specs-table').html()!=null){
			jQuery('.product-collateral table#product-attribute-specs-table>tbody>tr').each(function(){
				//var bb_id = jQuery(this).find('th').html().toLowerCase();
				//console.log(jQuery(this).find('th').first().html());
				var bb_id = '';
				bb_id += jQuery(this).find('th').first().html();
				bb_id = bb_id.toLowerCase();
				bb_id = bb_id.replace(/\s/g,'-');
				if(bb_id!='sample-images'){
					jQuery('ul#jq-tabs').append('<li><a class="tab" href="#tab-'+bb_id+'">'+jQuery(this).find('th').first().html()+'</a></li>');
					jQuery(this).find('td').first().prepend('<h2>'+jQuery(this).find('th').first().html()+'</h2>');
					jQuery('.product-collateral #tabs').append('<div class="tab-content" id="tab-'+bb_id+'">'+jQuery(this).find('td').first().html()+'</div>');
					jQuery(this).find('td').first().html('');
				}else{
					//jQuery('ul#jq-tabs').append('<li><a class="tab" href="#tab-'+bb_id+'">'+jQuery(this).find('th').html()+'</a></li>');
					jQuery('ul#jq-tabs li').first().after('<li><a class="tab" href="#tab-'+bb_id+'">'+jQuery(this).find('th').first().html()+'</a></li>');
					jQuery(this).find('td').first().prepend('<h2>'+jQuery(this).find('th').first().html()+'</h2>');
					//jQuery('.product-collateral #tabs').append('<div class="tab-content" id="tab-'+bb_id+'">'+jQuery(this).find('td').html()+'</div>');
					jQuery('.product-collateral #tabs div').first().after('<div class="tab-content" id="tab-'+bb_id+'">'+jQuery(this).find('td').first().html()+'</div>');
					jQuery(this).find('td').first().html('');
				}
			});
		}
		
		//REVIEWS TAB
		if(jQuery('.product-collateral .box-reviews').html()){
			jQuery('ul#jq-tabs').append('<li><a class="tab" href="#tab-reviews">Reviews</a></li>');
			jQuery('.product-collateral #tabs').append('<div class="tab-content" id="tab-reviews">'+jQuery('.product-collateral .box-reviews').html()+'</div>');
			jQuery('.product-collateral .box-reviews').first().html('')
			//jQuery('.product-collateral .box-reviews').first().html('');
		}
		
		//var tabs = jQuery('#tabs').tabs();
		
	}
	
	//******** CONSTRUCT TABS ********//
	
	//******** MAKE GRID LOOK RIGHT *********//
	jQuery('ul.upsell-list').each(function(){
		jQuery(this).find('li').each(function(index){
			if((index)%5==0){
				jQuery(this).addClass('new-line');
			}
		});
	});
	
	jQuery('ul.cross-sell-list').each(function(){
		jQuery(this).find('li').each(function(index){
			if((index)%5==0){
				jQuery(this).addClass('new-line');
			}
		});
	});
	//******** MAKE GRID LOOK RIGHT *********//
	
	
	
	//******** QUICK VEIW FOR ALL ********//
	jQuery('.products-grid li div.product-image-wrapper').each(function(){
		jQuery(this).hover(
			function(){
				jQuery(this).children('a.quick-view').css({'display':'block'});
			},
			function(){
				jQuery(this).children('a.quick-view').css({'display':'none'});
			}
		);	
	});
	//******** QUICK VEIW FOR ALL ********//
	
	
	//******** 360 VIEWER ********//
	if(jQuery('#explore-prod').html()!=null){
		jQuery('#explore-prod').css({'display':'block'});
		jQuery('#explore-link').click(function(){
			if(jQuery('.product-view #view360').html()==null){
				var urlenc = jQuery('.product-view .product-name h1').html();
				jQuery('.product-view').prepend('<div id="view360"></div>');
				jQuery('#view360').slideUp(100).append('<a href="#product_addtocart_form" id="close360">CLOSE</a><div id="iframeholder" style="height:540px;width:1000px;clear:both;"><div id="iframecontent"><iframe src="/360/index.php?p='+urlenc+'" style="border:0;margin:0;padding:0;" height="540" width="1000" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></div></div>');
				jQuery('#view360 #iframecontent').fadeOut(100);
				jQuery('#view360 #close360').click(function(){
					jQuery('#view360 #iframecontent').fadeOut(500,function(){jQuery('.product-view #view360').slideUp(1500);});
					jQuery('.product-view .product-essential').delay(500).slideDown(1500);
					return false;
				});
				jQuery('.product-view #view360').delay(100).slideDown(1500,function(){jQuery('#view360 #iframecontent').fadeIn(500);});
				jQuery('.product-view .product-essential').delay(100).slideUp(1500);
			}else{
				jQuery('.product-view #view360').slideDown(1500,function(){jQuery('#view360 #iframecontent').fadeIn(500);});
				jQuery('.product-view .product-essential').slideUp(1500);
			}
			return false;
		});
		
		jQuery('div.product-image a.highslide').attr('onclick','null').removeClass('highslide').addClass('noslide').click(function(){
			if(jQuery('.product-view #view360').html()==null){
				jQuery('.product-view').prepend('<div id="view360"></div>');
				var urlenc = jQuery('.product-view .product-name h1').html();
				jQuery('#view360').slideUp(100).append('<a href="#product_addtocart_form" id="close360">CLOSE</a><div id="iframeholder" style="height:540px;width:1000px;clear:both;"><div id="iframecontent"><iframe src="/360/index.php?p='+urlenc+'" style="border:0;margin:0;padding:0;" height="540" width="1000" scrolling="no" frameborder="0" marginwidth="0" marginheight="0"></iframe></div></div>');
				jQuery('#view360 #iframecontent').fadeOut(100);
				jQuery('#view360 #close360').click(function(){
					jQuery('#view360 #iframecontent').fadeOut(500,function(){jQuery('.product-view #view360').slideUp(1500);});
					jQuery('.product-view .product-essential').delay(500).slideDown(1500);
					return false;
				});
				jQuery('.product-view #view360').delay(100).slideDown(1500,function(){jQuery('#view360 #iframecontent').fadeIn(500);});
				jQuery('.product-view .product-essential').delay(100).slideUp(1500);
			}else{
				jQuery('.product-view #view360').slideDown(1500,function(){jQuery('#view360 #iframecontent').fadeIn(500);});
				jQuery('.product-view .product-essential').slideUp(1500);
			}
			return false;
		});
	}
	//******** 360 VIEWER ********//
	
	
	//******** DOCUMENT RESIZE FOR SEARCH RESULTS *********//
	jQuery(window).resize(function() {
		if(jQuery('#myContainer').html()!=null){
			var rightValRez = (jQuery(document).width()/2)-(jQuery('.wrapper .header-container div.header').width()/2);
			jQuery('#myContainer').css({'right':rightValRez+'px'});
		}
	});
	//******** DOCUMENT RESIZE FOR SEARCH RESULTS *********//
	
	//******** GCHAT *********//
	jQuery("#questions-callout>div").append('<div id="new-gtalk"></div>');
	if(jQuery("#questions-callout>div>a.a").html()!=null){
		jQuery("#questions-callout>div div#new-gtalk").append(jQuery("#questions-callout>div>a.a"));
		jQuery("#questions-callout>div div#new-gtalk a.a").html('<img src="/skin/frontend/default/custombrackets/images/chat-live.gif" border="0" alt="Have Questions? Chat live with Custom Brackets" />');
	}else{
		jQuery("#questions-callout>div div#new-gtalk").html('<a href="/contact-us"><img src="/skin/frontend/default/custombrackets/images/chat-unavail.gif" border="0" alt="Have Questions? Email Us" /></a>');
	}
	jQuery("#questions-callout>div>a.a").css({'display':'none'});
	//******** GCHAT *********//
	
	//******** GCHAT SIDEBAR  *********//
	if(jQuery("#google-sidebar").html()!=null){
		jQuery("#google-sidebar").append('<div id="new-gtalk"></div>');
		if(jQuery("#google-sidebar>div>a.a").html()!=null){
			jQuery("#google-sidebar>div#new-gtalk").append(jQuery("#google-sidebar>div>a.a"));
			jQuery("#google-sidebar>div#new-gtalk a.a").html('<img src="/skin/frontend/default/custombrackets/images/sidebar-chat.jpg" border="0" alt="Have Questions? Chat live with Custom Brackets" />');
		}else{
			jQuery("#google-sidebar>div#new-gtalk").html('<a href="/contacts"><img src="/skin/frontend/default/custombrackets/images/sidebar-emailus.jpg" border="0" alt="Have Questions? Email Us" /></a>');
		}
		jQuery("#google-sidebar>div:first").css({'display':'none'});
	}
	//******** GCHAT SIDEBAR *********//
	
	
	if(jQuery('a.product-image').html()!=null){
		//there is an item link on the page
		/*jQuery('a.product-image').parents('li').click(function(){
			if(jQuery(this).find('a.quick-view').html()!=null){
				window.location.href = jQuery(this).find('a.quick-view').attr('href');
			}else{
				window.location.href = jQuery(this).find('a.product-image').attr('href');
			}
		});*/
	}
	
	// GIMBAL COMPARE CODE
	hideCompareOthers();
	var compareDefault = true;
	jQuery('tr.bottom td a').click(function () { 
		var divid = jQuery(this).attr('href'); 
		jQuery('#gimbal-compare').hide();
		jQuery(divid).show();
    });
	jQuery('td.first-column a').click(function () { 
		var divid = jQuery(this).attr('href'); 
		hideCompareOthers();
		jQuery('#gimbal-compare').show();
    });

	//************ HIDE EMPTY GRIDS ************//
	jQuery('ul.upsell-list').each(function(){
		if(jQuery(this).html()==''){
			jQuery(this).css({'display':'none'}).prev().css({'display':'none'});
		}
	});	
	//************ HIDE EMPTY GRIDS ************//
	
	//************ ALT VIEWS PRODUCT ************//
	jQuery('.product-img-box .more-views').appendTo('.product-shop');
	//************ ALT VIEWS PRODUCT ************//

	jQuery(".fancybox").fancybox({'titleShow':false});
	
	//************ ZIP SEARCH DEALERS ************//
	if(jQuery('.dealer-list').html()!=null){
		if(jQuery('ul.countries li.active a').html()=='United States'){
			jQuery('head').append('<style type="text/css">#us-map-container object, #us-map-container embed{visibility: visible !important;	}</style>');
			var oVal = 'Zipcode';
			var oMiVal = '50';
			jQuery('#us-map-container').css({'position':'relative'}).append('<div id="zipsearchform" style="position:absolute;top:0;right:0;z-index:5;"><div id=""></div><form action="javascript: searchForZip()" method=""><label for="mizipsearch">Search within</label><input tabindex="1" id="mizipsearch" name="mizipsearch" type="text" value="50" class="input-text" /><span id="mitext">miles of</span><input tabindex="2" id="zipsearch" type="text" name="zipsearch" value="'+oVal+'" class="input-text" /><button tabindex="3" type="submit" title="Search" class="button"><span><span>Search</span></span></button></form><br/><div id="errorResponse" style="color:#B83746;text-align:center;display:block;"></div></div>');
			jQuery('#us-map-container object#us-map').css({'z-index':'1','visibility':'visible !important'});
			jQuery('#zipsearch').focus(function(){
				if(jQuery(this).val()==oVal){
					jQuery(this).val('');
				}
			}).blur(function(){
				if(jQuery(this).val()=='' || jQuery(this).val()==' '){
					jQuery(this).val(oVal);	
				}
			});
			jQuery('#mizipsearch').focus(function(){
				if(jQuery(this).val()==oMiVal){
					jQuery(this).val('');
				}
			}).blur(function(){
				if(jQuery(this).val()=='' || jQuery(this).val()==' '){
					jQuery(this).val(oMiVal);	
				}
			});
		}
	}
	//************ ZIP SEARCH DEALERS ************//
	
	//************ HIDE OUR PRICE WHEN YOUR PRICE ************//
	if(jQuery('.product-view .product-essential .product-shop .tier-prices').html()!=null){
		jQuery('.product-view .product-essential .product-shop .price-box').css({'display':'none'});
	}
	if(jQuery('ul.products-grid').html()!=null){
		jQuery('ul.products-grid li').each(function(){
			if(jQuery(this).find('a.minimal-price-link').html()!=null){
				jQuery(this).find('a .price-box').css({'display':'none'});
			}
		});
	}
	//************ HIDE OUR PRICE WHEN YOUR PRICE ************//
	
	//************ OVERRIDE REVIEWS LINKS ************//
	if(jQuery('.product-info .rating-holder .rating-links')){
		jQuery('.product-info .rating-holder .rating-links a').attr('href','#review-form').click(function() {
			tabs.tabs('select', getReviewTab());
			//return false;
		}).first().attr('href','#tab-reviews');
	}
	
	//************ OVERRIDE REVIEWS LINKS ************//
	
});
function getReviewTab(){
	var _id = null;
	
	jQuery('#tabs div.tab-content').each(function(index){
		if(_id==null){
			if(jQuery(this).attr('id') == 'tab-reviews'){
				_id = index;
			}
		}
	});
	return _id;
}
function hideCompareOthers() {
	jQuery('#wimberley-jobu,#kirk-jobu,#wimberley').hide();
}

function beginAddingToCart() {
	jQuery.blockUI({ message: '<br/><img src="/skin/frontend/default/custombrackets/images/cb-addingtocart.gif" alt="adding items to cart..." /><br/>&nbsp;' });	
}
function doneAddingToCart() {
	jQuery.unblockUI();
}
function setTO13(){hide13int = window.setInterval(checkFor13,200);}
function clearTO13(){window.clearInterval(hide13int);}
function checkFor13(){
	if(jQuery('#s_method_tablerate_bestway').next().children('span.price').html()!=null && jQuery('#s_method_tablerate_bestway').css('display')!='none'){
		var amount = jQuery('#s_method_tablerate_bestway').next().children('span.price').html();
		if ( amount.length >5 || amount == '$1,337.00') {
			jQuery('#s_method_tablerate_bestway').css({'display':'none'}).parentsUntil('dl').css({'display':'none'}).prev().css({'display':'none'});
		}else{
		}
	}
	// adding a check to removeother shipping options when matrix option is available
	var shippingMethods = [];
	var matrix = false;
	jQuery('.sp-methods li input').each(function() {
		shippingMethods = jQuery(this).attr('id').split('_');
		if ( shippingMethods[2] == 'productmatrix' ) {
			window.matrix = true;
		}
		if ( window.matrix && shippingMethods[2] != 'productmatrix' && shippingMethods[0] == 's' ) {
				jQuery(this).parent().parent().parent().hide().prev().hide();
		}
	});  
}
var hasLetter;
function searchForZip(){
	var zip = jQuery('#zipsearch').val();
	var radius = jQuery('#mizipsearch').val();
	radius = parseInt(radius);
	if(zip.length == 5){
		var charcount = zip.toString();
		charcount = charcount.split('');
		hasLetter = 0;
		var letter = ['f'];
		for(var i=0;i<charcount.length;i++){
			if(isNaN(parseInt(charcount[i]))){
				hasLetter++;	
			}
		}
		if(hasLetter==0){
			window.location.href = '?zipcode='+parseInt(zip,10)+'&radius='+radius;
		}
	}else{
		jQuery('#errorResponse').slideUp(1);
		jQuery('#errorResponse').html('Please input a valid 5 digit Zipcode.').slideDown(500);
		setTimeout("jQuery('#errorResponse').slideUp(500);",3000);
	}
}
