// <![CDATA[
$(document).ready(
		function() {
		    // input clearing
		    /*var input_values = Array();
		    $('input[type=text]').each(
				function() {
				    input_values[$('input[type=text]').index(this)] = $(this).val();
				}
			);*/
		    $("#matching-prods").click(
				function() {
				    showLB('.lb-1');
				    return false;
				}
			);
		    $("#add-cart").click(
				function() {
				    showLB('#lb-2');
				    return false;
				}
			);
		    $("#save-sl").click(
				function() {
				    showLB('#lb-3');
				    return false;
				}
			);
		    $('#fon-lb, span.closelb1, div.lb .close a').click(
				function() {
				    closeLB('.lb');
				    return false;
				}
			);
		    /*$(window).scroll(
				function() {
				    centroVert($('.lb'));
				}
			);*/
		}
	);

function numericFloat(control) {
    $(control).bind('keypress', function(e) {
        
        return (e.which != 8 && e.which !== 0 && e.which != 44 && (e.which < 48 || e.which > 57)) ? false : true;
    });
}

function numeric(control) {

    $(control).bind('keypress', function(e) {
        return (e.which != 8 && e.which !== 0 && (e.which < 48 || e.which > 57)) ? false : true;
    }); 
}

function IsNumeric(input) {
    var RE = /^-{0,1}\d*\.{0,1}\d+$/;
    return (RE.test(input));
}

function numericDecimal(control) {
    $(control).bind('keypress', function(e) {
        return (e.which != 8 && e.which !== 0 && e.which != 46 && (e.which < 48 || e.which > 57)) ? false : true;
    });
}

function ShowImageModal(imageUrl, itemId) {

    $("#lb-2 #productImage").attr('src', imageUrl);

    //setTimeout(function() { ShowImage(imageUrl, itemId); }, 1000);
    
    $("#lb-2 #productImage").load(function() {
        ShowImage(imageUrl, itemId);
    });
    
    return false;
}

function ShowImage(imageUrl, itemId) {

    $("#lb-2").show();

    var height = $("#lb-2 #productImage").attr('height');

    var width = $("#lb-2 #productImage").attr('width');

    serviceModal('lb-2', width, height);
}

function encHTML(html) {
    return html.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

/// Recieves an html element to be used as message container and the text. Uses Jquery UI Dialog to show a message box.
function ShowMessageDialog(elementId, message) {

    $.ui.dialog.defaults.bgiframe = true;

    var dialogElement = $("#" + elementId);

    $(dialogElement).html("<p>" + message + "</p>");
    $(dialogElement).dialog('open');
}

function ExecuteJsonCall(route, inputData, successHandler, failureHandler) {

    $.ajax({                       //create an AJAX request
        type: "POST",              //use POST (we could also load this from the form if wanted to)
        cache: false,
        url: route,
        dataType: "json",
        data: inputData,
        success: function(data) {
            successHandler(data);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

            RedirectToErrorView();
        }
    });
}

function ExecuteHtmlCall(route, inputData, successHandler, failureHandler) {

    $.ajax({                       //create an AJAX request
        type: "POST",              //use POST (we could also load this from the form if wanted to)
        cache: false,
        url: route,
        dataType: "html",
        data: inputData,
        success: function(data) {
            successHandler(data);

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {

            RedirectToErrorView();
        }
    });
}
function ExecuteAjaxCall(route, inputData, successHandler, failureHandler, type, dataType) {

    if (typeof (failureHandler) !== 'function') {
        failureHandler = RedirectToErrorView;
    }

    if (typeof (type) === 'undefined' || type === null) {
        type = "GET";
    }

    if (typeof (dataType) === 'undefined' || dataType === null) {
        dataType = "json";
    }

    if (dataType == "") {
        dataType = null;
    }

    $.ajax({                       //create an AJAX request
        type: type,              //use POST (we could also load this from the form if wanted to)
        cache: false,
        url: route,
        dataType: dataType,
        data: inputData,
        success: function(data) {
            successHandler(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            failureHandler();
        }
    });
}


function RedirectToErrorView() {

    // Redirects to the error view
    window.location = BASE_URL + "Error";
}

function SetDropDownValue(dropDown, value) {
    $(dropDown).val(value);
}

/////////////
// opacity
function opacidad(elemento, valor) {
    var valor_noie = valor / 100;
    $(elemento).css('filter', 'alpha(opacity=' + valor + ')');
    $(elemento).css('-moz-opacity', valor_noie);
    $(elemento).css('-khtml-opacity', valor_noie);
    $(elemento).css('opacity', valor_noie);
}

// center element in screen
function centro(lo_que) {
    var x = parseFloat($(window).width()) / 2 - parseFloat($(lo_que).width()) / 2;
    var y = $(window).scrollTop() + $(window).height() / 2 - parseFloat($(lo_que).height()) / 2;
    x = x + 'px';
    y = y + 'px';
    $(lo_que).css({ 'left': x, 'top': y });
}

// centrar vertically
function centroVert(lo_que) {
    var y = $(window).scrollTop() + $(window).height() / 2 - parseFloat($(lo_que).height()) / 2;
    $(lo_que).css({ 'top': y });
}
// lightbox
function showLB(who) {

    $('#fon-lb').css('width', $(window).width() + 'px');
    $('#fon-lb').css('height', $(document).height() + 'px');

    opacidad('#fon-lb', 70);
    centro(who);

    $('#fon-lb').fadeIn('normal', function() { $(who).fadeIn('normal'); });
}
function closeLB(who) {
    $(who).fadeOut('normal', function() { $('#fon-lb').fadeOut('normal'); });
}

function UpdateCartItemCount(data) {

    /*var quantity = 0;

    var items = typeof (data.Items) != 'undefined' ? data.Items : data;

    for (var i in items) {
        quantity += items[i].Quantity;
    }*/

    /*if (typeof(data.Items) != 'undefined')
        RefreshCartQuantity(data.Items.length);*/
    
}

function RefreshCartQuantity(quantity) {

    $("#itemCount").text(quantity);
}

function ImageError(img) {

    img.src = '<%= Url.Images("img-prod-mini.png") %>';
}

function SetupDialog(elementId, dialogPosition) {

    var position = IsDef(dialogPosition) ? dialogPosition : 'center';

    $("#" + elementId).dialog({ resizable: false, modal: true, autoOpen: false, position: position, dialogClass: "flora" });
}

// Returns true if the vatiable passed as parameter is defined, otherwise returns false.
function IsDef(variable)
{
    return typeof(variable) != 'undefined' ? true : false;
}

function ClearFields(jqueryCollection)
{
    $(jqueryCollection).val('');
}

/* Client Sorting */

function DoSort(td, tableId, columnDictionary, isNumericColumn) {

    // Gets the index of the column that was clicked
    var columnIndex = $(td).parent().children().index($(td));

    // Gets the current sort order for that column
    var sort_order = columnDictionary[columnIndex];

    // Gets the table to sort
    var $table = $("#" + tableId);

    // Gets a boolean value indicating if is going to order items in descending order
    var isDescendingOrder = sort_order == "desc";

    // Gets the table rows
    var rows = $table.find('tbody > tr').get();

    // Sort function
    rows.sort(function(a, b) {

        var keyA, keyB;
        
        var a = $(a).children('td').eq(columnIndex).text().toUpperCase();
        var b = $(b).children('td').eq(columnIndex).text().toUpperCase();
        
        if(isNumericColumn)
        {
            keyA = parseInt(a, 10);

            keyB = parseInt(b, 10);
        }
        else
        {
            keyA = a;

            keyB = b;
        }

        if (!isDescendingOrder) {
            if (keyA < keyB) return -1;

            if (keyA > keyB) return 1;
        }
        else {
            if (keyA < keyB) return 1;

            if (keyA > keyB) return -1;
        }

        return 0;

    });

    // Reders ordered table.
    $.each(rows, function(index, row) {

        $table.children('tbody').append(row);

    });

    // Updates column sort state
    columnDictionary[columnIndex] = columnDictionary[columnIndex] != 'desc' ? 'desc' : 'asc';
}

/*function onUserNameKeyPress(e)
{
    var KeyPress
    //if which property of event object is supported  
    if (e && e.which)
    {
        e = e

        //character code is contained in NN4's which property
        KeyPress = e.which
    }
    else
    {
        KeyPress = e.keyCode
    }
    //13 is the key code of enter key
    if (KeyPress == 13)
    {
        $('#Password').focus();
        return false
    }
    else
    {
        return true
    }
}
function onLoginEnterKeyPress(e)
{
    var KeyPress
    //if which property of event object is supported  
    if (e && e.which)
    {
        e = e

        //character code is contained in NN4's which property
        KeyPress = e.which
    }
    else
    {
        KeyPress = e.keyCode
    }
    //13 is the key code of enter key
    if (KeyPress == 13)
    {
        $('#LoginForm').submit();
        return false
    }
    else
    {
        return true
    }
}*/
// ]]>


