/*
 * 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(){
				$('div[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('_product_') === -1 && l.indexOf('product.html') === -1) {
                         return;
                    }
                         */


if(typeof(productDetailRewrite) !== 'undefined') {

                    $('#content .aside').remove();
                    $('#content .primary').removeClass('primary');
                    $('.paging-a').remove();
                    $('h2.categoryName').remove();
                    $('.categories-a').remove();

var id = $(".productList li:first").attr("id");

                    $('.productList').replaceWith($('.productList div.product'));

$("div.product").attr("id",id);
$("div.product").addClass("productItem");


             
                    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();
                    }

} // end if

               },

			cart : function() {
				 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');
				 }			
			},
			activeNav : function(el,activeClass){				
				$(el).find('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);
					}
				});	
			},
			checkout : function(){
				// -----------------------------------------------------
				// This will copy over the shipping address value to the 
				// billing address.  Make sure the checkbox Id is "SameAsShipping"
				// -----------------------------------------------------	
				$("#f-shipping-same").bind("change", function(e){
					if($(this).checked){
						// unchecked
					}else{
						// checked
						$("#f-shipping-address-1").val($("#f-billing-address-1").val());
						$("#f-shipping-address-2").val($("#f-billing-address-2").val());
						$("#f-shipping-city").val($("#f-billing-city").val());
						$("#f-shipping-state").val($("#f-billing-state").val());
						$("#f-shipping-zip").val($("#f-billing-zip").val());
						$("#f-shipping-phone").val($("#f-billing-phone").val());
					}
				});	

				// ------------------------------------------------------
				// This sets an html element with id of #order-summary-value 
				// with the $500.00, so the $ plus the amount.  This allows
				// you to style that container and merely hide the amount
				// input form element.
				//-------------------------------------------------------
				$('#order-summary-value').text($('input#Amount').val());
			
				// ------------------------------------------------------
				// This will prevent Credit card information from being 
				// collected if the purchase amount is zero.  It means 
				// that either a discount code or gift voucher was used.
				// Make sure to wrap the entire credit card area with a 
				// container id of #credit-card-information
				//-------------------------------------------------------
				if ($('input#Amount').val() === '0.00') {
					$('#PaymentMethodType_9').attr('checked','checked');
					$('#credit-card-information').css('display','none');	
				}
			},
			what_is_this : function(){
				$("#what_link").click(function(){
					$("#what-is-this").slideToggle("fast");
				});
			},
			loginCheck : function(){
				if ($('#lchk').text() === '1') {
					$('li.my-account').attr('href','/my-account');
					$('#user-options p.login a').html('Log out').attr('href','/LogOutProcess.aspx');
				}
			},
			collectionNav : function(collectionUrl){
				var nav = $('#content .aside .catalogueitemdump');
				nav.prepend('<li><a href="'+collectionUrl+'">The Collection</a></li>');
				
				Engine.fixes.activeNav(nav, 'active');
			},
loginPeepShow : function(){
			
				$("#btnLostPass").click(function(){
					$("#lostPass").slideToggle();
					return false;
				});
			
				
				$("#btnRegister").click(function(){
					$("#registerForm").slideToggle();
					return false;
				});
			
			
			} // login peep
		}
	};

	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_391926","active");	
    Engine.fixes.activeNav("#nav_391929","active");
	Engine.fixes.activeNav("#nav_391942","active");
	Engine.fixes.collectionNav('_catalog_44675/The_Collection');
	Engine.fixes.checkout();
	// Engine.fixes.what_is_this();
	Engine.fixes.loginCheck();	
Engine.fixes.loginPeepShow();
});

function popUp(URL) {day = new Date();id = day.getTime();eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=700,left = 660,top = 300');");}


