$(document).ready(function() {

    /* Open External Links in New Window */
    $("a[rel=external]").attr("target","_blank");

    var ie9 = (jQuery.browser.msie && jQuery.browser.version[0] == '9');
    if (!ie9) {

    /* Cufon Font Replacement */
    $("ul#navigation li a").addClass("cufon");
    Cufon.replace( "ul#navigation li a.cufon,div#non-customer-header a", {
	'fontFamily': 'Gotham Rounded',
	'textShadow': "#fff 0 1px",
	'hover': true
    });

    Cufon.replace("#main h1:not('.small'), #deal-detail .secondary a.button:not('.alt'), .deal-list .dashboard a.button, #grid .dashboard a.button, #main #certificate h3", {
	'fontFamily': 'Gotham Rounded Bold',
	'textShadow': "rgba(0, 0, 0, 0.4) 0 -1px"
    });

    Cufon.replace("#deal-detail .secondary a.button.disabled", {
	'fontFamily': 'Gotham Rounded Bold',
	'textShadow': "rgba(255, 255, 255, 1) 0 1px"
    });

    Cufon.replace("#main h1 em", {
	'fontFamily': 'Gotham Rounded',
	'textShadow': "rgba(0, 0, 0, 0.4) 0 -1px",
	'hover': true
    });

    }

    /* Grid Equal Column Heights */
    equalHeight($("#grid .cell"));

    /* Birth Year Lightbox */
    $("a[href='#birth-year']").fancybox({
	'autoSize': true,
	'centerOnScroll' : true,
	'overlayColor' : '#000',
	'overlayOpacity' : 0.75,
	'padding' : 0,
	'scrolling' : false,
	'showCloseButton' : false
    }).trigger("click");
    $("form#birth-year button").click(function() {
	parent.$.fancybox.close();
	return false;
    });

    /* Restrictions Lightbox */
    $("a[href='#restrictions']").fancybox({
	'autoSize': true,
	'centerOnScroll' : true,
	'overlayColor' : '#000',
	'overlayOpacity' : 0.75,
	'padding' : 0,
	'scrolling' : false
    });

    /* Order Confirmation Lightbox */
    $("a[href='#confirmation']").fancybox({
	'autoSize': true,
	'centerOnScroll' : true,
	'overlayColor' : '#000',
	'overlayOpacity' : 0.75,
	'padding' : 0,
	'scrolling' : false,
	'showCloseButton' : false
    }).trigger("click");
    $("#confirmation a.button").click(function() {
	parent.$.fancybox.close();
	return false;
    });

    /** Shipping/billing address same **/
    $("form#checkout .same input").change(function() {
	if ($(this).attr("checked")) {
	    $(this).parent().parent().addClass("disabled");
	}
	else {
	    $(this).parent().parent().removeClass("disabled");
	}
    }).trigger('change');

    /** Send-as-gift **/
    $("form#checkout .send-as-gift input").change(function() {
	if ($(this).attr("checked")) {
	    $(this).parent().parent().removeClass("disabled");
	}
	else {
	    $(this).parent().parent().addClass("disabled");
	}
    }).trigger('change');

    /** cc security code explanation **/
    $("a[href='#cvm-explanation']").fancybox({
	'autoSize': true,
	'centerOnScroll' : true,
	'overlayColor' : '#000',
	'overlayOpacity' : 0.75,
	'padding' : 0,
	'scrolling' : false,
	'showCloseButton' : false
    });
    $("#cvm-explanation a.button").click(function() {
	parent.$.fancybox.close();
	return false;
    });

    /* Multi-deal popup */
    $('a.show-multi').fancybox({
	'autoSize': true,
	'scrolling': false,
	'titleShow': false,
	'showCloseButton': true
    });
    $('table.multi-deals a.close').click(function() {
	parent.$.fancybox.close();
	return false;
    });

    /** Resize long savings/value text **/
    $('#main #deal-detail .secondary ul.value li strong')
    .filter(function(idx){return $(this).text().length > 6})
    .css('font-size', '16px')
    .css('padding-top', '5px');

    /* New User Lightbox */
    $("a[href='#new-user']").fancybox({
	'autoSize': true,
	'padding' : 0,
	'overlayOpacity' : 0.75,
	'overlayColor' : '#000',
	'onComplete' : function() {
	    $("form#new-user a.subscriber").click(function() {
		parent.$.fancybox.close();
		return false;
	    });
	}
    }).trigger('click');
    var nu_form_opts = { 
	'beforeSubmit': function(arr, $form, options) {
	    $('#new-user-results').show().html('<p>loading...</p>');
	},  
	'dataType': 'json',
	'success': function(response) {
	    if (response.success) {
		if ($('#NewUserRedir').length) {
		    window.location = $('#NewUserRedir').val();
		}   
	    }   
	    else {
		var err_str = '<ul class="errors">';
		for (error in response.errors) {
		    var msg = response.errors[error]
		    err_str = err_str + '<li>' + msg + '</li>';
		}
		err_str = err_str + '</ul>';
		$('#new-user-results').html(err_str);
	    }
	}   
    };  
    $('#new-user').ajaxForm(nu_form_opts);

    /* Checkout apply promo code */
    $('.apply-promo-code').click(function(evt) {
	var link = $(evt.target);
	var promo_code = $('#CustomerPromoCode').val();
	var url = link.attr('href');
	url = url.replace('PROMO_CODE', promo_code);
	window.location = url;
	return false;
    });

    /* Gift This Lightbox */
    $("a[href='#gift-this']").fancybox({
	'padding' : 0,
	'overlayOpacity' : 0.75,
	'overlayColor' : '#000'
    });

});

