// dummy function
function MM_openBrWindow(a, b, c) {
    // nothing
}

$(document).ready(function() {
    $("#presuform").validate({
        rules: {
            email: {
                email: true
            }
        }
    });
    
    $("#friendform").validate({
        rules: {
            femail: {
                email: true
            }
        }
    });
    
    // Catálogo 2010
    $('#catalogo-button').click(function() {
        $('#catalogo-layer').toggle('blind');
        return false;
    });
    
    // Ofertas desplegables
    $('.colec-button').click(function() {
        // open/close collection adn change button text
        $(this).text(function(){
            var str = $(this).text();
            if (str.charAt(0) == "+") {
                $(this).prev().fadeIn();
                $(this).text("- Cerrar colección");
            } else {
                $(this).prev().fadeOut();
                $(this).text("+ Abrir colección");
            }
        });
        return false;
    });
    
    // Universal Text Preview
    $('.mas-button').click(function() {
        // open/close description adn change button text
        $(this).text(function(){
            var str = $(this).text();
            if (str.charAt(0) == "+") {
                $(this).prev().fadeIn();
                $(this).text("- menos");
            } else {
                $(this).prev().fadeOut();
                $(this).text("+ Info");
            }
        });
        return false;
    });
    // +Información Técnica
    $('.tec-info-button').click(function() {
        // open/close description adn change button text
        $(this).text(function(){
            var str = $(this).text();
            if (str.charAt(0) == "+") {
                $(this).prev().fadeIn();
                $(this).text("- Cerrar");
            } else {
                $(this).prev().fadeOut();
                $(this).text("+ Información Técnica");
            }
        });
        return false;
    });
    // Small Text Preview
    $('.mas-txt-small-button').click(function() {
        // open/close text and change button text
        $(this).text(function(){
            var str = $(this).text();
            if (str.charAt(0) == "+") {
                $(this).prev().fadeIn();
                $(this).text("- menos");
            } else {
                $(this).prev().fadeOut();
                $(this).text("+ Leer Más");
            }
        });
        return false;
    });
    
    // pop it up
    $("#product-popup-layer").dialog({
        autoOpen: false,
        modal: true,
        height: 600,
        width: 950
    });
    
    

    $("a[onclick^='MM_openBrWindow']").click(function(event) {
        event.preventDefault();
        var onclickStr = $(event.target).parent().attr('onclick').toString();

        // extract the quoted arguments (path, "arquiservi" and dimensions)
        var argPattern = new RegExp("[\"|\'][^\"]+?(?=[\"|\'])", "g");
        var argus = onclickStr.match(argPattern);

        // adjust the height of the iframe that loads the external docuement
        // Note: to set the appropriate height for each folder or htm file, use
        // popup_heights.js in this folder
        var pagePath = argus[0].split('/');
        var folderName = pagePath[2];
        var targetHeight = $(event.target).parent().attr('targetheight');
        $('#popup-canvas').attr('height', function() {
            return targetHeight == undefined ? getFolderHeightSpec(folderName) : targetHeight;
        });

        // popup title
        var prod_name = $(event.target).parent().parent().next().contents(':not(em)').text();
        $('#ui-dialog-title-product-popup-layer').html(prod_name.replace('.', ''));

        // set values on the presupuesto and email to a friend forms
        var prodName = prod_name.replace(/\s{2,10}/g, '')
        $('#comentarios').html(prodName)
        $('[name=originurl]').attr('value', window.location)
        $('[name=prodname]').attr('value', prodName)

        // pop it up
        $("#product-popup-layer").dialog('open');

        // set the url
        $('#popup-canvas').attr('src', 'http://popups.pavimentosarquiservi.com/' + argus[0].substring(1));
        $('#popup-canvas').attr('src', $('#popup-canvas').attr('src'));
    });

    // Call me back form magic
    $('#callmeform > input').focus(function(e){
        if ($(e.target).val() == 'Nombre' || $(e.target).val() == 'Teléfono') {
            $(e.target).val('');
            $(e.target).removeClass('light-field');
        }
    });
    
    $('#callmeform > input').blur(function(e){
        if ($(e.target).val() == '') {
            if($(e.target).attr('id') == 'nombre') {
                $(e.target).val('Nombre');
            } else if ($(e.target).attr('id') == 'telefono') {
                $(e.target).val('Teléfono');
            }
            $(e.target).addClass('light-field');
        }
    });
});
