/*
--------------------------------------------------

White Label
application.js

Joe Morrow [joe.morrow@acquitygroup.com]
03/18/2009

Copyright © 2009 Acquity Group LLC

--------------------------------------------------
*/

$(document).ready(function() {

    // Print Link
    $('#print a').click(function() {
        window.print();
        return false;
    });

    // Auto-clear inline prompt text, stored in "title" attribute
    $(".inline-prompt").each(function() {
        if ($(this).attr("title").length == 0)
            $(this).attr("title", $(this).val());
    }).focus(function() {
        if ($(this).val() == $(this).attr("title"))
            $(this).val("");
    }).blur(function() {
        if ($(this).val() == "")
            $(this).val($(this).attr("title"));
    });

    $('#addEditAddress-country').change(function(){
        $('#selectCountrySubmit').click();
    });

    // embroidery js

    $('#lettering').click(function() {
        clearValues('logo');
        $('.readyMadeLogoDescr').hide();
        $('.readyMadeLogoLetteringDescr').hide();
        $('.readyMadeLogo-group').hide();
        $('.letteringDecr').show();
        $('.lettering-group').show();
        $('#guarantee').show();
        return false;
    });

    $('#logo').click(function() {
        clearValues('lettering');
        $('.readyMadeLogoDescr').show();
        $('#logoPlacement').hide();
        $('.readyMadeLogoLetteringDescr').hide();
        $('.readyMadeLogo-group').show();
        $('.letteringDecr').hide();
        $('.lettering-group').hide();
        $('#guarantee').show();
        return false;
    });

    $('#logoletter').click(function() {
        $('.readyMadeLogoDescr').hide();
        $('.readyMadeLogoLetteringDescr').show();
        $('.readyMadeLogo-group').show();
        $('.letteringDecr').hide();
        $('.lettering-group').show();
        $('#logoPlacement').show();
        $('#guarantee').show();
        return false;
    });

    $('#thread-swatches div a img').click(function() {
        $('#thread-swatches div').removeClass("selected");
        $(this).parent().parent().addClass("selected");
        $('#currentThreadColor').html($(this).attr("alt"));
        $('#letterColorInput').val($(this).attr("alt"));
        return false;
    });

    $('#readyMadeLogo').change(function() {
        val = $(this).val();
        valArr = val.split(":");
        $('#logoInput').val(valArr[0]);
        $('#logoColorInput').val(valArr[1]);
        $('#logoImage').attr("src",valArr[2]);
        $('#logoSkuCode').val(valArr[3]);
        return false;
    });

    if ($('#readyMadeLogo').val() != null) {
        valArr = $('#readyMadeLogo').val().split(":");
        if (valArr.length > 2) {
            $('#logoImage').attr("src",valArr[2]);
        }
    }

    $('#embroideryLettering').change(function() {
        val = $(this).val();
        valArr = val.split(":");
        $('#letterFontInput').val(valArr[0]);
        $('#fontImage').attr("src",valArr[1]);
        return false;
    });

    if ($('#embroideryLettering').val() != null) {
        valArr = $('#embroideryLettering').val().split(":");
        if (valArr.length > 1) {
            $('#fontImage').attr("src",valArr[1]);
        }
    }
    
    $('#acceptTerms').click(function() {
    	var val = $(this).attr('checked');    	
        if (val) {
        	$(this).attr("value","true");        	
        } else {
        	$(this).attr("value","false");        	
        }        
    });

    // end embroidery js

    if ($('#jqModal').html() != null) {
         // tell a friend modal
         $('#jqModal').jqm({ajax: '@href', onLoad: tellForm, closeClass: 'modalClose', trigger: 'a.tellModal', modal: true});
     }

    // given js from Life Uniform
    $('input[type="text"][name!="SEARCH_INPUT<>searchFor"]').addClass("input");
    
    $('#embroideryLine1, #embroideryLine2').focus(function() {        	
	    $(this).removeClass("input").addClass("focusField");
	    if (this.value == this.defaultValue){
	        this.value = '';
	    }
	    if(this.value != this.defaultValue){
	        this.select();
	    }
    });
    $('input[type="text"][name!="SEARCH_INPUT<>searchFor"]').blur(function() {
        $(this).removeClass("focusField").addClass("input");
        if ($.trim(this.value) == ''){
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });

    $('input[type="text"][disabled="disabled"]').addClass("disabled");
        $('input[type="text"][disabled="disabled"]').focus(function() {
            $(this).removeClass("disabled").addClass("focusField");
        if (this.value == this.defaultValue){
            this.value = '';
        }
        if(this.value != this.defaultValue){
            this.select();
        }
    });
    $('input[type="text"][disabled="disabled"]').blur(function() {
        $(this).removeClass("focusField").addClass("disabled");
        if ($.trim(this.value) == ''){
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });

    $('input[type="password"]').addClass("input");
        $('input[type="password"]').focus(function() {
            $(this).removeClass("input").addClass("focusField");
        if (this.value == this.defaultValue){
            this.value = '';
        }
        if(this.value != this.defaultValue){
            this.select();
        }
    });
    $('input[type="password"]').blur(function() {
        $(this).removeClass("focusField").addClass("input");
        if ($.trim(this.value) == ''){
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });


    var tabContainers = $('div.dealTabs > div');
     tabContainers.hide().filter(':first').show();

     $('div.dealTabs ul.tabNavigation a').click(function () {
            tabContainers.hide();
            tabContainers.filter(this.hash).show();
            $('div.dealTabs ul.tabNavigation a').removeClass('selected');
            $(this).addClass('selected');
            return false;
     }).filter(':first').click();


     $(".subcatDisplay:nth-child(3n), .productDisplay:nth-child(3n)").css("margin-right", "0");

     $(".storesDisplay:nth-child(4n), .productDisplay:nth-child(3n)").css("margin-right", "0");

     $(".crossSells.four li:nth-child(4n)").css("margin-right", "0");

     $(".crossSells.five li:nth-child(5n)").css("margin-right", "0");

});

Cufon.replace('.dealTabs .tabNavigation a');

function tellForm() {
    var options = {
            data: {tell_submit: "submitType"},
            success: tellAFriendResponse  // post-submit callback
    };

    // bind form using 'ajaxForm'
    $('#tell_a_friend').ajaxForm(options);
}
function tellAFriendResponse(responseText, statusText)  {
    if ($(responseText).find('#tell_a_friend_div').html() != null) {
        $('#tell_a_friend_div').html($(responseText).find('#tell_a_friend_div').html());
        //reattach ajax to form
        tellForm();
    } else {
        $('#jqModal').jqmHide();
    }
}

function clearValues(type) {
    if (type == 'lettering') {
        $("#letterFontInput").val('');
        $("#embroideryLettering").val('');
        $("#letterColorInput").val('');
        $("#thread-swatches div").removeClass("selected");
        $("#currentThreadColor").html('');
        $("#embroiderySideLetter").val('');
        $("#fontImage").attr("src","/media/LFU001/UNC/images/static/embroiderySig.png");
        $("#embroideryLine1").val("Embroidery Line 1");
        $("#embroideryLine2").val("Embroidery Line 2");
    }
    else {
        $("#readyMadeLogo").val('');
        $("#logoColorInput").val('');
        $("#logoInput").val('');
        $("#embroiderySide").val('');
        $("#logoPlacement").val('');
        $("#logoImage").attr("src","/media/LFU001/UNC/images/static/readyMadeLogo.png");
    }
}