﻿/*
 * application.js
 * Site Wide JavaScript file
 *
 */

// addItemToSample
// Adds an item to a sample order via AJAX
function addItemToSample(sellingStyleNumber, sellingColorNumber, itemAddElement ) {
    $.ajax({
        type: 'POST',
        url: '/SampleOrder/AddItem/' + sellingStyleNumber + '/' + sellingColorNumber,
        datatype: 'json',
        success: function (data) {
            //alert("in additem success fn");
            updateCartQuantity(data.quantity);
            updateItemAddElement(itemAddElement, data.success, data.errorMsg);
        },
        error: function (data, type) {
            ///alert("in additem error fn");
            updateItemAddElement(itemAddElement, false, 'Error');
        }
    });
}

function updateItemAddElement(elementName, successBool, errorMsg) {

    if (successBool) {
        $(elementName).html('<a href=\'javascript: void(0);\'>Sample Added</a>');
    }
    else {
        $(elementName).html('<a href=\'javascript: void(0);\' class=\'SampleOrderError\' >' + errorMsg + '</a>');
    }
}


// Updates the Cart Quantity
function updateCartQuantity(quantity) {
    $('#myCart').html('My Cart (' + quantity + ' items)');

    displayCart();
};

function displayCart() {

   
    $('#myCartContents').html('<img src="/Content/loading.gif"/>');
    if (!$('#myCartContents').is(':visible')) {
        $('#myCartContents').show('slow');
    }

    $.ajax({
        type: 'POST',
        url: '/ShoppingCart/Show',
        datatype: 'html',
        success: function (data) {
            $('#myCartContents').html(data);
            $('#shoppingBagHeader a').click(function (event) {
                event.preventDefault();
                $('#myCartContents').hide();
            });
            $('#myCartContents').idle(5000).fadeOut("slow");
        },
        error: function (data, type) {
            $('#myCartContents').html("Error: Unable to load shopping cart.");
            $('#myCartContents').idle(5000).fadeOut("slow");
        }
    });
}

function WriteErrors(jsonData) {
    var message = 'html: ' + jsonData.HTMLData;
    for (var err in jsonData.Errors) {
        var propertyName = jsonData.Errors[err].PropertyName;
        var errorMessage = jsonData.Errors[err].Error;
       message += ', ' + errorMessage; 
    }
    return message;
}

/* ProductSpecs
***************************************************/

function tabifyProductDetails() {
    //Default Action
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function () {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab

        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        var newHeight = $(activeTab).height();

        $(".tab_container").height(newHeight);

        $(".tab_content:visible").hide(); //Hide all tab content

        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });
}

/***************************************************/


/* QuickLook 
**************************************************/

function displayQuicklook() {
    paginateColors('#searchresults_QuicklookBtm_tiles', 10, 0);

    var scrollTop = $(window).scrollTop();
    var windowHeight = $(window).height();
    var wrapperWidth = $('#wrapper').width();

    // center the div
    $('#searchresults_QuickLook').css('top', scrollTop + (windowHeight / 2 - $('#searchresults_QuickLook').height() / 2));
    $('#searchresults_QuickLook').css('left', wrapperWidth / 2 - $('#searchresults_QuickLook').width() / 2);

    $('#searchresults_QuickLook').show();
};

function closeQuickLook() {
    $('#searchresults_QuickLook').hide();
};



/**************************************************/


/* Colors
**************************************************/

function paginateColors(domId, per_page, start_page) {
    $(domId).pajinate({
        start_page: start_page,
        items_per_page: per_page,
        nav_label_first: '',
        nav_label_prev: 'Previous',
        nav_label_next: 'Next',
        nav_label_last: ''
    });
};

/**************************************************/


$(function () {
    $('#myCart').bind('mouseover', function () {
        displayCart();
    });

});

/* jQuery Extensions
----------------------------------------------------------*/
$.fn.clearForm = function () {
    return this.each(function () {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};

$.fn.exists = function () {
    return (this.length > 0);
};

$.fn.hideElement = function () {
    this.slideUp('fast');
};

// Helper function for delaying a function call
$.fn.idle = function (time) {
    var o = $(this);
    o.queue(function () {
        setTimeout(function () {
            o.dequeue();
        }, time);
    });
    return this;
};
