/*
 * hrefID jQuery extention
 */
$.fn.extend({ hrefId: function() { return $(this).attr('href').substr($(this).attr('href').indexOf('#')); } });

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			},
			compactLabels : function(){
				$('.search-form label').each(function(index, item){
					var label = $(this),
						input = $('#'+ label.attr('for'));
					input.focus(function(){ 	//fetch related field, on focus hide label if field is empty
						label.hide();
					}).blur(function(){			//on blur show label if field is empty			
						if (input.val() === '') {
							label.show();
						}
					});
										
					window.setTimeout(function(){ //hide label if browser autofills the field
						if (input.val() !== '') {
							label.hide();
						}
					},50);
				});				
			}
		},
		fixes : {
			blog : function(){
				// no comments/trackbacks + alternative
				var $comments = $('div.comments-a');
				$comments.each(function(){
					if($(this).find('div.comment').length == 0){
						var fixed = $(this).html().replace('</h2>','</h2><p class="empty">') + '</p>';
						$(this).html(fixed);
					} else {
						$(this).find('div.comment:odd').addClass('alt');
					}
				});
				
				// show/hide comments/trackbacks
				var $links = $('div.post-a p.info a.comments, div.post-a p.info a.trackbacks');
				$links.click(function(){
					$($(this).hrefId()).toggle();
					if($(this).is('.comments')) $($(this).hrefId()).next('div.add-comment-a:first').toggle();
					return false;
				});
				
				// single post (show trackbacks and comments)
				if($('div.post-a').length == 1){
					$('div.comments-a, div.add-comment-a').show();
				}
			},
			separators : function(){
				$('ul[class*=cols]').each(function(){
					var matches = /cols([0-9]+)/i.exec($(this).attr('class'))
					var row = matches[1];
					$(this).find('li').each(function(i){
						if((i+1) % row == 0) $(this).after('<li class="separate"><a href="#top">Back to top</a></li>');
					});
				});
			},
			pagination : function(){
				$('.paging-a span.pagination').contents().each(function(){
					if (this.nodeType === 3 && /[0-9]+/.test(this.textContent)) {
						$(this).replaceWith(' <strong>'+$.trim(this.textContent)+'</strong> ')
					}
				});
			},
			emptyLists : function(){
				$('.categories-a ul li.catalogueItemNotFound').each(function(){
					$(this).parent().remove();
				});
				
				//remove product list if we are on the main catalog page
				if ($('.categories-a ul li.catalogueItem').length > 0 && $('.products-a ul li.productItemNotFound').length > 0) {
					$('.products-a').remove();
				} 
			},
			productDetails : function(){
				var l = window.location+'';
				if (l.indexOf('_catalog_') > 0) {
					$('#content .aside').remove();
					$('#content .breadcrumbs').css("text-align","center");
					$('#content .wrapper-a').css("text-align","center");
					$('#content .primary').css("margin","0 auto");
					$('#content .primary').css("float","none");
					$('#content .primary').css("display","block");
					return;
				}
				if (l.indexOf('_product_') === -1) {
					return;
				}
				$('#content .aside').remove();
				$('#content .primary').removeClass('primary');
				$('.paging-a').remove();
				$('h2.categoryName').remove();
				$('.categories-a').remove();
				$('.productList').replaceWith($('.productList div.product'));
				
				var relatedProducts = $('.product .description .productTable');
				if (relatedProducts.length > 0) {
					var featuredProducts = $('#content > .related ul');
					featuredProducts.prev('h2').html('Related products');
					
					featuredProducts.find('li').remove();
					relatedProducts.find('td.productItem').each(function(index, item){
						var li = $('<li class="productItem" />');
						li.attr('id',$(item).attr('id'));
						li.html($(item).html());
						featuredProducts.append(li);
					});
					
					relatedProducts.remove();
				}
			},
			cart : function() {
				if ( $("#content .system-error-message").length ) {
					/* ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
					**temporary fix to get around not being able to edit the home page**
					*****  ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** */
					var orig = window.location+'';
					if (typeof(console.log) != 'undefined') { console.log(orig); }
					orig = orig.replace( '75749','75731' ); 
					orig = orig.replace( '75750','75731' ); 
					orig = orig.replace( '75746','75731' ); 
					if (orig !== window.location+'') { window.location = orig; }
				}
				 if ($('#catCartSummary .cartSummaryItem').html() != 'Shopping cart is empty.') {
					var summary = $('#catCartSummary .cartSummaryItem').text();
					var arr = summary.split(" ");	
					$('#user-options .cart a').text(arr[0] + ' item(s)').addClass('items');
				 }	
				 
				 if ($('input#Amount').val() === '0.00') {
                         $('#PaymentMethodType_9').attr('checked','checked');
                         $('.paymentContainer').css('display','none'); 
						 //alert('zero action');
 				 }

			},
			footerNavTweak : function(){
				$(".footer-heading").each(function(){
					var headerLink = $(this).find("a:first").css("cursor","default");
					$(this).find("a:first").hover(function(){
  						$(this).css("text-decoration","none");
					},function(){
  						// none
					});
					
				});
			},
			activeNav : function(navCssId,activeClass){				
				$(navCssId + ' li a').each(function(){
					var path = document.location.href; 
					var current_href = this.getAttribute('href',2);
					if (path.split(".com")[1] == current_href) {
					  $(this).addClass(activeClass);
					  $(this).parents("li").addClass(activeClass);
					  
					  	$(this).parents("li").hover(function(){
							$(this).addClass(activeClass);
						  }, 
						  function () {
							$(this).addClass(activeClass);
						});
					}
				});	
			},
			
			shippingOptionChange : function(){
				$("#shippingSpan").text("Select Shipping Option");
				},
				
			productNameTweak : function(){
				
				$("#catProdTd_1057645 span").find("a").html("Ultrashine<br />Moisture Shampoo").css("text-decoration","none");
				$("#catProdTd_1057642 span").find("a").html("UltraShine<br />Moisture Conditioner").css("text-decoration","none");
				$("#catProdTd_1057643 span").find("a").html("ULTIMATE<br />Styling Lotion").css("text-decoration","none");
				$("#catProdTd_1057644 span").find("a").html("TotalControl<br />Styling Creme").css("text-decoration","none");
				$("#catProdTd_1057646 span").find("a").html("SuperFinish<br />Polishing Balm").css("text-decoration","none");

				
				},
			
			prodNavStick : function(){
				
				if (document.location.href.indexOf("_catalog_") != -1){     
					$("#nav_14623 li:nth-child(3)").addClass("active");
				}
				
				},
				
			
			stripFeatured : function(){
				
				if (document.location.href.indexOf("_catalog_") != -1){     
					$(".related").hide();
				}
			},
			
			
			billingShipping : function(){
				
			$("#f-shipping-same").bind("change", function(e){
                        
                         var n = $("#f-shipping-same:checked").length
                              if(n == 0){
                                   $("#ShippingAddress").val('');
                                   $("#ShippingCity").val('');
                                   $("#ShippingState").val('');
                                   $("#ShippingZip").val('');
                                   $("#ShippingPhone").val('');
								   $("#ShippingAttention").val('');
                              }else{
                                   $("#ShippingAddress").val($("#BillingAddress").val());
                                   $("#ShippingCity").val($("#BillingCity").val());
                                   $("#ShippingState").val($("#BillingState").val());
                                   $("#ShippingZip").val($("#BillingZip").val());
                                   $("#ShippingPhone").val($("#BillingPhone").val());
								   $("#ShippingAttention").val($("#FirstName").val() + " " +$("#LastName").val() );
                              }
                   
                    });
                    

			
				
			},
			
			
			
			ditchLeftNav : function(){
		
				if (document.location.href.indexOf("LostPasswordProcess") != -1 || document.location.href.indexOf("login") != -1 || document.location.href.indexOf("LogOutProcess") != -1 || document.location.href.indexOf("about-us") != -1 || document.location.href.indexOf("OrderRetrievev2") != -1 || document.location.href.indexOf("orderretrievev2") != -1 || document.location.href.indexOf("FormProcessv2") != -1 || document.location.href.indexOf("contact") != -1){    
					$(".aside").css("display","none");
					$("#content .primary").css("float","none").css("width","auto");
				}
		
			}
				
			
			
			
		} // end fixes 
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.utils.compactLabels();
	Engine.fixes.blog();
	Engine.fixes.separators();
	Engine.fixes.pagination();
	Engine.fixes.emptyLists();
	Engine.fixes.productDetails();
	Engine.fixes.cart();
	Engine.fixes.activeNav("#nav_14623","active");	
	Engine.fixes.activeNav("#nav_525552","active");
	Engine.fixes.activeNav(".catalogueitemdump","active");
	Engine.fixes.footerNavTweak();
	Engine.fixes.stripFeatured();
	Engine.fixes.shippingOptionChange();
	Engine.fixes.productNameTweak();
	Engine.fixes.prodNavStick();
	Engine.fixes.ditchLeftNav();
	Engine.fixes.billingShipping();
});
