﻿
function updateLayout(){
    var w = document.body.clientWidth;    
    var c = $('#container-wrapper')[0];
    if (!c || !c.style) { return; }
    if (w > 921){
        c.style.width = '920px';
    } else if (w < 771){
        c.style.width = '770px';
    } else {
        c.style.width = '100%';
    }
}

function giveShadow(jEl){
    jEl.each(function(i){
        $(this).wrap("<div class='shd1'><div class='shd2'><div class='shd3'><div class='shd4'></div></div></div></div>");
    });   
}

function toggleElement(id, doFx){    
    var el = $('#'+id);
    if (doFx==false){
        el.toggle();
        return;
    }
    if (el[0].style.display=='none'){        
        el.slideDown("normal");
    } else {
        el.slideUp("normal");
    }
}

function toggleListFilter (el){
    var filter = $('#'+el);
    if (filter[0].style.display=='none'){
        
        filter.slideDown("normal");
    } else {
        filter.slideUp("normal");
    }
}

function initFocusInputs() {
    var input = $("input.focus");

    input.each(function(i) {
        var el = $(this);


        if (!el.attr("placeholder")) {
            el.attr("placeholder", el.val());
            //el.color('#888');
            el.css({ color: "#888" });
        } else {
            if (el.attr("placeholder") == el.val()) {
                //el.color('#888');
                el.css({ color: "#888" });
            }
        }
        el.blur(function() {
            var DEF_VAL = $(this).attr("placeholder");
            if (!DEF_VAL) { DEF_VAL = ''; }
            if (this.value == "" || this.value == DEF_VAL) {
                this.value = DEF_VAL;
                this.style.color = '#888';
            }
        });
        el.focus(function() {
            var DEF_VAL = $(this).attr("placeholder");
            if (!DEF_VAL) { DEF_VAL = ''; }
            if (this.value == DEF_VAL) {
                this.value = '';
                this.style.color = '#000';
            }
        });
    });
}

function initFooter() {
    $("#feedback-form").hide();
    $(".feedback h4").click(function() {
        $(this).css("cursor", "default");
        $("#feedback-form").slideDown("slow", function() {
            window.scrollBy(0, 450);
        });
    });

    $("#footer .info a:first").click(function() {
        $("#footer .info p").show();
        window.scrollBy(0, 450);
        return false;
    });
}

function initProfession() {
    $(".profession").bind("change", function() {
        if ($(".profession option[value='" + $(this).val() + "']").text() == "Other") {
            $(".other-profession").show();
        } else {
            $(".other-profession").hide();
        }
    });
}

//Custom selector for selecting asp.net elements.  Usage $(":asp('ID')")
//taken from: http://john-sheehan.com/blog/index.php/custom-jquery-selector-for-aspnet-webforms/
String.prototype.endsWith = function(str) {
    return (this.match(str + '$') == str);
};

jQuery.extend(
    jQuery.expr[":"].asp = function(a, i, m) {
        return a.id && jQuery(a).attr('id').endsWith(m[3]);
    }
);


function translateFields($fields, $loadingImage, lang) {
    var languageType = "en";
    if (lang != "") {
        languageType = lang;
    }

    $fields.translate(languageType, {
        start: function() { $loadingImage.removeClass("hide"); },
        complete: function(value) {
            $loadingImage.addClass("hide");
            $fields.each(function() {
                $(this).val($(this).val().replace(/---=\|/g, "\r\n---\r\n|"));
                $(this).val($(this).val().replace(/\|[\s]*=[\s]*\|/g, "|\r\n|"));
                $(this).val($(this).val().replace(/\|=---/g, "|\r\n---\r\n"));
            });
        },
        error: function() {
            $fields.each(function() {
                $(this).val($(this).val().replace(/---=\|/g, "\r\n---\r\n|"));
                $(this).val($(this).val().replace(/\|[\s]*=[\s]*\|/g, "|\r\n|"));
                $(this).val($(this).val().replace(/\|=---/g, "|\r\n---\r\n"));
            });
        }
    });
}
function initProductSpecPDS() {
    var languageType = $(":asp('ddlLanguageAdmin')").val().substring(0, 2);
    $textCollection = $(".edit textarea");
    $loadingImage = $(".ajaxLoader");
    $(".lnkTranslateAll").unbind("click").click(function() {
        $textCollection.each(function() {
            $(this).val($(this).val().replace(/---\n\|/g, "---=|"));
            $(this).val($(this).val().replace(/\|\n\|/g, "|=|"));
            $(this).val($(this).val().replace(/\|\n---/g, "|=---"));
        });
        translateFields($textCollection, $loadingImage, languageType);
    });
}
function initProductSpecLabelAdmin() {
    var languageType = $(":asp('ddlLanguageAdmin')").val().substring(0, 2);
    $textCollection = $(".txtTranslate");
    $loadingImage = $(".ajaxLoader");
    $(".lnkTranslateAll").unbind("click").click(function() {
        translateFields($textCollection, $loadingImage, languageType);
    });
}

$(document).ready(function() {
    initFocusInputs();
    initFooter();
    /*@cc_on
    if (navigator.userAgent.indexOf('MSIE 7') < 0) {
        try {
            // Disable IE flickering
            document.execCommand('BackgroundImageCache', false, true);
        } catch (e) { }
        $(window).resize(updateLayout);
        updateLayout();
    }
    @*/
    $(".vColumn").vjustify();

    $(":asp('ddlState')").change(function() {
        CheckStateVal($(this));
    });

    CheckStateVal($(":asp('ddlState')"));
});

function CheckStateVal(dropdown) {
    if (dropdown.find(":selected").text() == "Other") {
        $(":asp('txtOtherArea')").show();
    } else {
        $(":asp('txtOtherArea')").hide().val("");
    }
}

//function to create equal column heights
//usage: $(".jqColumn").vjustify();
//result: any element with the class of "jqColumn" will have the height equal to the tallest
//source: http://michael.futreal.com/jquery/vjustify
jQuery.fn.vjustify = function() {
    var maxHeight = 0;
    this.each(function() {
        if (this.offsetHeight > maxHeight) { maxHeight = this.offsetHeight; }
    });
    this.each(function() {
        $(this).height(maxHeight + "px");
        if (this.offsetHeight > maxHeight) {
            $(this).height((maxHeight - (this.offsetHeight - maxHeight)) + "px");
        }
    });
};

//This adds a class of 'js' to the html element.  This will allow items to be styled only when javascript is enabled.
$("html").addClass("js");


    
   
