﻿$(document).ready(function() {
    switch(document.location.pathname) {

    case '/empresa.asp':
        $('div#submenu a').each(function() {
            $(this).click(function(e) {
                e.preventDefault();
                $('div#submenu a').not(this).removeClass('activo');
                if(!$(this).hasClass('activo'))
                    $(this).addClass('activo');
                switch($(this).attr('id').substr(0, 1)) {
                case 't':
                    cargarGaleria($(this).attr('id').substr(1));
                    break;
                case 'c':
                    cargarEmpresa($(this).attr('id').substr(1));
                }
            });
        });
        switch($('div#submenu a.activo').attr('id').substr(0, 1)) {
        case 't':
            cargarGaleria($('div#submenu a.activo').attr('id').substr(1));
            break;
        case 'c':
            cargarEmpresa($('div#submenu a.activo').attr('id').substr(1));
        }

    case '/marcasblancas.asp':
        $("#marcasblancasCarousel").waterwheelCarousel('horizontal', {
            startingItemSeparation: 87,
            startingWaveSeparation: 12,
            itemDecreaseFactor: 0.65,
            opacityDecreaseFactor: 0.75,
            centerOffset: 0,
            flankingItems: 1,
            movedToCenter: function(img) {
                $('div#marcasblancasOpcionesTitulo').html('<span>' + img.attr('alt').replace(' (', '</span> ('));
            }
        });
        break;

    case '/productos.asp':
        $('div#submenu a').each(function() {
            $(this).click(function(e) {
                e.preventDefault();
                $('div#submenu a').not(this).removeClass('activo');
                if(!$(this).hasClass('activo'))
                    $(this).addClass('activo');
                switch($(this).attr('id').substr(0, 1)) {
                case 'r':
                    cargarPRubro($(this).attr('id').substr(1));
                    break;
                case 't':
                    cargarPConetnido($(this).attr('id').substr(1));
                }
            });
        });
        switch($('div#submenu a.activo').attr('id').substr(0, 1)) {
        case 'r':
            cargarPRubro($('div#submenu a.activo').attr('id').substr(1));
            break;
        case 't':
            cargarPConetnido($('div#submenu a.activo').attr('id').substr(1));
        }
        break;

    case '/contacto.asp':
        $.validator.messages.required = '';
        $.validator.messages.email = '';
        $('form#contacto').validate({
            submitHandler: function(form) {
                var datos = $('form#contacto :input').serialize();
           	    $('div#contactoC2').html('Enviando...');
                $.post(
                    '/contacto2.asp',
                    datos,
                    function(html) {
                       	$('div#contactoC2').html(html);
                    },
                    'html'
                );
            }
        });
        break;
    }
});

function cargarEmpresa(cid) {
    $('div#empresaC2').css('background', 'url(/images/cms/contenidos/' + cid + '_f1.jpg) no-repeat scroll left top');
    if($('div#empresaC3').attr('class') != '') {
        $('div#empresaC3').attr('class', '').html('<div id="empresaC3C1">&nbsp;</div><div id="empresaC3C2">&nbsp;</div><div id="empresaC3C3">&nbsp;</div>');
    }
    $('div#empresaC3C1').html('Cargando...');
    $('div#empresaC3C2').css('background', 'url(/images/cms/contenidos/' + cid + '_f2.jpg) no-repeat scroll left top');
    $('div#empresaC3C3').html('Cargando...');
    $.get(
        '/empresa2.asp?cid=' + cid,
        function(xml) {
            xml = $(xml);
            $('div#empresaC3C1').html(xml.find('texto1').text());
            $('div#empresaC3C3').html(xml.find('texto2').text());
        },
        'xml'
    );
}

function cargarGaleria(tid) {
    $('div#empresaC2').css('background', 'url(/images/cms/tipos/' + tid + '_f1.jpg) no-repeat scroll left top');
    if($('div#empresaC3').attr('class', 'empresaC3G') == '')
        $('div#empresaC3').attr('class');
    $('div#empresaC3').html('Cargando...').load(
        '/empresa3.asp?tid=' + tid,
        function() {
            $("#galeriaCarousel").waterwheelCarousel('horizontal', {
                startingItemSeparation: 412,
                startingWaveSeparation: 0,
                itemDecreaseFactor: 1,
                opacityDecreaseFactor: 1,
                centerOffset: 0,
                flankingItems: 0,
                movedToCenter: function(img) {
                    $('div#galeriaOpcionesNumero').html(img.data('index') + 1);
                    $('div#galeriaTitulo').html(img.attr('alt'));
                }
            });
        }
    );
}

function cargarPRubro(rid) {
    if($('div#productosC1').attr('class') != '')
        $('div#productosC1').attr('class', '');
    if($('div#productosC2').attr('class') != '')
        $('div#productosC2').attr('class', '');
    $('div#productosC1').css('background', '#000');
    $('div#productosC2').html('Cargando...').load(
        '/productos2.asp?rid=' + rid,
        function() {
            $('div#productosSubmenu a').each(function() {
                $(this).click(function(e) {
                    e.preventDefault();
                    $('div#productosSubmenu a').not(this).removeClass('activo');
                    if(!$(this).hasClass('activo'))
                        $(this).addClass('activo');
                    if($(this).attr('id').substr(0, 1) == 'p')
                        cargarPProducto($(this).attr('id').substr(1));
                });
            });
            if($('div#productosSubmenu a').attr('id').substr(0, 1) == 'p')
                 cargarPProducto($('div#productosSubmenu a').attr('id').substr(1));
        }
    );
}

function cargarPProducto(pid) {
    $('div#productosC1').css('background', '#000 url(/images/cms/productos/' + pid + '_f.jpg) no-repeat scroll center 120px');
}

function cargarPConetnido(tid) {
    if($('div#productosC1').attr('class', 'productosC1P') == '')
        $('div#productosC1').attr('class');
    $('div#productosC2').html('');
    if($('div#productosC2').attr('class') != 'productosC2P3')
        $('div#productosC2').attr('class', 'productosC2P3');
    $('div#productosC2').html('Cargando...').load(
        '/productos3.asp',
        function() {
            $("#galeriaCarousel").waterwheelCarousel('horizontal', {
                startingItemSeparation: 412,
                startingWaveSeparation: 0,
                itemDecreaseFactor: 1,
                opacityDecreaseFactor: 1,
                centerOffset: 0,
                flankingItems: 0,
                movedToCenter: function(img) {
                    $('div#galeriaOpcionesNumero').html(img.data('index') + 1);
                    $('div#galeriaTitulo').html(img.attr('alt'));
                }
            });
        }
    );
}
