﻿var searchValue = $('#Search').val();
$(function () {
    setTimeout(checkSearchChanged, 0.1);
});

function checkSearchChanged() {
    var currentValue = $('#Search').val();
    if ((currentValue) && currentValue != searchValue && currentValue != '') {
        searchValue = $('#Search').val();
        $('#submit').click();
    }
    else {
        setTimeout(checkSearchChanged, 0.1);
    }
}

function setInCart() {
    $("#inCart").show();
}

function wireUpEditors() {
    $('textarea.tinymce').tinymce({
        // Location of TinyMCE script
        script_url: scriptUrl,

        // General options
        theme: "advanced",
        plugins: "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

        // Theme options
        theme_advanced_buttons1: "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,forecolor,backcolor",
        theme_advanced_buttons3: "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: false,
        width: 774,

        // Example content CSS (should be your site CSS)
        // content_css: "css/content.css"
    });
}

function wireUpProductSort() {
    $("#productSort").sortable({ axis: 'y', containment: 'parent' });
}

function loadShoppingCart() {
    $("#shoppingCart").load('/ShoppingCart/Icon/');
}

function toggleUserDetails() {
    $("#userDetails").toggle();
}

function wireUpCvv2Help() {
    $("#cvv2Help").hide();
    $("#cvv2HelpIcon").click(function () {
        $("#cvv2Help").show();
    });
}

function wireUpAutoSubmit() {
    $(".autoSubmit").each(function (index) {
        $(this).change(function () {
            $(this).closest('form').trigger('onsubmit');
            $(this).closest('form').submit();
        })
    });
}

$(document).ready(function () {
    wireUpAutoSubmit();
    $("#CreateAccount").change(function () {
        toggleUserDetails();
    });
    $("#UseShippingAddress").change(function () {
        toggleBillingAddress();
    });
    wireUpCvv2Help();
    wireUpFacebook();
});

function toggleBillingAddress() {
    $("#billingAddressDetails").toggle();
}

function wireUpFacebook() {
    (function (d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) { return; }
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
        fjs.parentNode.insertBefore(js, fjs);
    } (document, 'script', 'facebook-jssdk'));
}
