﻿$(document).ready(function () 
{
    try{
    /*$("#navigation").treeview({
        persist: "location",
        collapsed: false,
        unique: true
    });*/
    }catch(ex){
    }

    try{
    $(".urlIframe").fancybox({
        'width': '50%',
        'height': '50%',
        'autoScale': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'type': 'iframe',
        'hideOnOverlayClick': false,
        'titleShow': true
    });
    }catch(ex){
    }

    try{
    $(".img").fancybox({
        'width': '400px',
        'height': '400px',
        'autoScale': true,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'hideOnOverlayClick': false
    });
    }catch(ex){
    }

    try{
    $(".img8").fancybox({
        'width': '20px',
        'height': '20px',
        'autoScale': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'hideOnOverlayClick': false
    });
    }catch(ex){
    }

    try{
    resizeImage();
    }catch(ex){
    }

    try{
    resizeImage2();
    }catch(ex){
    }

    try{
    highlightWords();
} catch (ex) {
    }
});

function resizeImage() {
    var xH = 300;
    var xW = 300;
    var oH = $(".albumImage").height();
    var oW = $(".albumImage").width();
    var nH = 0;
    var nW = 0;

    if (oH > oW) {
        //alert("oH es mayor");
        nH = xH;
        nW = nH * oW / oH;
        $(".albumImage").height(nH);
        $(".albumImage").width(nW);
    }
    else {
        nW = xW;
        nH = nW * oH / oW;
        $(".albumImage").height(nH);
        $(".albumImage").width(nW);
    }
}

function resizeImage2() {
    $.each($(".albumImage2"), function () {
        var xH = 160;
        var xW = 160;
        var oH = $(this).height();
        var oW = $(this).width();
        var nH = 0;
        var nW = 0;

        if (oH > oW) {
            //alert("oH es mayor");
            nH = xH;
            nW = nH * oW / oH;
            $(this).height(nH);
            $(this).width(nW);
        }
        else {
            nW = xW;
            nH = nW * oH / oW;
            $(this).height(nH);
            $(this).width(nW);
        }
    });
}

$.expr[":"].containsNoCase = function (el, i, m) {
    var search = m[3];
    if (!search)
        return false;
    return eval("/" + search + "/i").test($(el).text());
};

jQuery.fn.replaceNoCase = function (oldText, newText) {
    //var o = this; // $(this[0]); // It's your element
    var str = $(this[0]).innerHTML.replace(/algo/gi, newText);
};

function highlightWords() {
    var txtBuscado = $("#MainContent_txtBuscar").val();
    if ((txtBuscado != null) && (txtBuscado != "undefined"))
        if (txtBuscado.length > 0) {
            var encontrados = $("span, td:containsNoCase('" + txtBuscado + "')").not("span>input");
            $.each(encontrados, function () {
                if ($(this).children().size() == 0)
                    $(this).html($(this).html().replace(eval("/" + txtBuscado + "/ig"), '<span style="background-color:yellow;">' + txtBuscado + '</span>'));
            });

        }
}

function maximizePopUp() {
    $("#inline1").height($(window).height() - 80);
    $("#inline1").width($(window).width() - 40);
}

function ConfirmDelete() {
    return confirm("Esta seguro que desea eliminar este registro?\nUna vez eliminado es imposible recuperarlo.");
}

function ConfirmCancel() {
    return confirm("Esta seguro que desea cancelar los cambios aun NO guardados?\nUna vez cancelado es imposible recuperarlo.");
}

function EspecificarTalla(xImage) {
    //alert($(xImage).attr('id'));
    var combo = $(xImage).prev();
    var eltId = $(combo).attr('id');
    //alert(eltId + ':: ' + $("#" + eltId + " option:selected").text());
    var texto = $("#" + eltId + " option:selected").text();
    var posiblesTallas = trim(texto.substring(0, texto.indexOf("$") - 1));
    var r = false;
    var talla = prompt("Porfavor especifique la talla [ " + posiblesTallas + " ]");
    //alert(posiblesTallas.split(',').length);
    //var nArr = posiblesTallas.split(',');
    if (posiblesTallas.indexOf(talla)) 
    {
        var textoTalla = $(xImage).next();
        $(textoTalla).val(talla);
        alert($(textoTalla).attr('id') + " = " + $(textoTalla).val());
        return true;
    }
    return false;
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function ltrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}

function rtrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}













function newsscoller(prev) {

    //Get the current selected item (with selected class), if none was found, get the first item
    var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
    var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');

    //if prev is set to 1 (previous item)
    if (prev) {

        //Get previous sibling
        var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
        var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');

        //if prev is set to 0 (next item)
    } else {

        //Get next sibling
        var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
        var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
    }

    //clear the selected class
    $('#excerpt li, #gallery li').removeClass('selected');

    //reassign the selected class to current items
    next_image.addClass('selected');
    next_excerpt.addClass('selected');

    //Scroll the items
    $('#mask-gallery').scrollTo(next_image, 800);
    $('#mask-excerpt').scrollTo(next_excerpt, 800);

}





