/**
 * Global scripts.
 *
 * @author  Jerico Veloso <jerico@madwellnyc.com>
 * @version SVN: $Id: scripts.js 231 2012-02-08 16:56:57Z jerico $
 */

$(document).ready(function(){

	$('#customerLogin br, #cartItems br').remove();

// homepage

	if ($('body.home').length == 1) {
	}

// category page

	if ($('body.page-product_list').length == 1) {
		var productGrid = $('#productGrid'),
			breadcrumb = $('h1', productGrid);

		// transform breadcrumb
		var breadCrumbLinks = $('a', breadcrumb), isFirst = true;
		breadcrumb.empty();
		breadCrumbLinks.each(function(i){
			if (i > 0) {
				if (!isFirst) {
					breadcrumb.append(' / ');
				}

				breadcrumb.append($(this));
				isFirst = false;
			}
		});

        $('a:first', breadcrumb).replaceWith($('a:first', breadcrumb).text());
		$('a:last', breadcrumb).addClass('last');

        breadcrumb.css({ visibility: 'visible' });

		// transform product list
		$('tr td:nth-child(2)', productGrid).each(function(){$(this).addClass('middle');});
		$('tr td:first-child', productGrid).each(function(){$(this).addClass('first');});
		$('tr td:last-child', productGrid).each(function(){$(this).addClass('last');});
	}

// category widget

	if ($('#widgetCategories').length == 1) {
		$('#widgetCategories .body>ul>li>a:contains("All Products")').parent().attr('id', 'cat-all');
		$('#widgetCategories .body>ul>li>a:contains("Signature Series")').parent().attr('id', 'cat-signature');
		$('#widgetCategories .body>ul>li>a:contains("Angus Collection")').parent().attr('id', 'cat-angus');
		$('#widgetCategories .body>ul>li>a:contains("Gift Packs")').parent().attr('id', 'cat-gift');

		// append GC link
//		$('#cat-gift>ul').append('<li><a href="/cart.php?m=gift_certificates">Gift Certificate</a></li>');
//		$('#widgetCategories .body>ul').append('<li id="cat-gc"><a href="/cart.php?m=gift_certificates">Gift Certificates</a></li>');

        $('#widgetCategories .body>ul>li>a[href=#]').click(function(event){
            event.preventDefault();

            $(this).parent().siblings('li').children('ul:visible').slideUp('fast');
            $(this).siblings('ul').slideDown('fast');
        });
	}

// product page

	if ($('body.page-product_detail').length == 1) {
		// update price label
		$('#priceLabel').text($('#productDetail h1').text());

		// add hooks to parent DIV's
		$('#priceLabel').parent().attr('id', 'priceContainer');
		$('#skuLabel').parent().attr('id', 'skuContainer');
		$('#qtyLabel').parent().attr('id', 'qtyContainer');
		$('#addToCartButton').parent().attr('id', 'addToCartContainer');
	}

// shopping cart page

	if ($('body.page-view').length == 1) {
		// add hooks to item rows
		$('#cartItems tr > :nth-child(1)').addClass('itemQty');
		$('#cartItems tr > :nth-child(2)').addClass('itemSku');
		$('#cartItems tr > :nth-child(3)').addClass('itemName');
		$('#cartItems tr > :nth-child(4)').addClass('itemPrice');
		$('#cartItems tr > :nth-child(5)').addClass('itemSubTotal');
		$('#cartItems tr > :nth-child(6)').addClass('itemDelete');

		// add hooks to amounts
		$('#cartAmounts > :nth-child(1)').addClass('amountSubTotal');
		$('#cartAmounts > :nth-child(2)').addClass('amountShipping');
		$('#cartAmounts > :nth-child(3)').addClass('amountNewTotal');
		$('#cartAmounts > :nth-child(4)').addClass('calculateShipping');

		// add hooks to buttons
		$('#cartButtons > :nth-child(1)').addClass('btnUpdate');
		$('#cartButtons > :nth-child(2)').addClass('btnEmpty');
		$('#cartButtons > :nth-child(3)').addClass('btnContinue');
		$('#cartButtons > :nth-child(4)').addClass('btnCheckout');
	}


// login page
	if ($('#customerLogin').length == 1) {
//		$('#existingCustomer .body > div').eq(0).addClass('loginUsername');
//		$('#existingCustomer .body > div').eq(1).addClass('loginPassword');
//		$('#existingCustomer .body > div').eq(2).addClass('loginForgot');
//		$('#existingCustomer .body > div').eq(3).addClass('loginRemember');
//		$('#existingCustomer .body > div').eq(4).addClass('loginButton');
	}

// create customer form
	if ($('#createCustomer').length == 1) {
		$('#createCustomer .body > div').eq(0).addClass('loginUsername clearfix');
		$('#createCustomer .body > div').eq(1).addClass('loginPassword1 clearfix');
		$('#createCustomer .body > div').eq(2).addClass('loginPassword2 clearfix');
		$('#createCustomer .body > div').eq(3).addClass('loginButton clearfix');
	}

// payment info page
	if ($('#paymentOpts').length == 1) {
		$('#paymentOpts label').eq(1).addClass('paymentOptsCC');
	}

// default GC form message
	if ($('#giftCerts').length == 1) {
//		if ('' == $('textarea[name="msg"]').val()) {
//			$('textarea[name="msg"]').val("Who am I kidding? Of course you want steak! That's why I got you this Great Steaks gift card. Check out what they've got at GreatSteaks.com, and enter your gift card code at checkout. You'll be grilling in no time (and I wouldn't say no if you invited me to your next cookout). Happy holidays!");
//		}

		// append GC sample link after the last child
		$('#giftCerts > div:last').after('<div id="gc-lightbox">{ <a href="/gsm/images/the_gc.jpg" class="lightbox">Click here to see our snazzy e-gift certificate!</a> }</div>');
		$('#gc-lightbox a').colorbox();
	}

// checkout > payment page
	if ($('#checkoutPayment').length == 1) {
		$('textarea[name="shipInstructions"]').after('<div style="font-size:10px;padding-left:170px;width:200px;line-height:12px;">If you like to have your order arrive at a certain future date, please include a note in the above field.</div>');
	}

// hide apron add to cart
	if (($('input[name=productID]').length > 0) && ($('input[name=productID]').val() == '194')) {
		$('#qtyContainer').hide();
		$('#addToCartButton').hide();
	}

// view cart
/*
	if ($('#viewCart').length == 1) {
		$('#viewCart h2').after('<p id="apron1-lightbox" style="text-align:right;">Purchase a Gift Pack or place an order over $75 we\'ll throw in our classy Grillmaster Apron...free. <b><i><a href="/gsm/images/the_apron.jpg" class="lightbox" style="color:#ab2125;">Check it out &gt;&gt;</a></i></b></p>');
		$('#apron1-lightbox a').colorbox();
	}
*/

// checkout confirm page
	if ($('#checkoutConfirm').length == 1) {
		$('#checkoutConfirm .submitOrder').prepend('<div id="checkout-confirm-note">Your credit card will not be charged until you click "Submit My Order"</div>');
	}

	$('a.lightbox').colorbox();

// apply input hints
    $('input[data-hint], textarea[data-hint]').each(function(){
        if ('' == $(this).val() && $(this).is(':visible')) {
            $.mwDataHint.add($(this));
        }
    });

}); // document.ready()

$(window).load(function(){
    $.mwDataHint.reset();
});

