var inAnim = false;
	
$(document).ready(function(){
    // Carga contenido de cada piso
    //$("#piso-0-load").load('inicio.html');
    //$("#piso-1-load").load('descargas.html');
    //$("#piso-0-load").load('aplicaciones.html');
    //$("#piso-0-load").load('promociones.html');
    //$("#piso-0-load").load('servicios.html');
		
    // Sliders
    /*var slider = $('#ascensor').bxSlider({
	    infiniteLoop: false,
	    speed: 50,
            auto: false,
            controls: false,
            mode: 'vertical'
        });
        */
        
    function cambiarPagina(htmlfile, contenido){
        setTimeout(function(){
            $("#contenido").load(htmlfile);
				
            if(contenido != null) {
                $(".contenidointerior,.inicio,#ancholoco").load(contenido);
            }
				
        },1000);
    };
		
    function cambiarZindex(valor, tiempo){
        setTimeout(function(){
            $("#contenido").css('z-index', valor);
        },tiempo);
    }
		
    // Floor select
    function selectfloor(element, floorvar, htmlfile, contenido){
        if(!inAnim) {
            inAnim = true;
            cambiarZindex(77777, 1);
            $("#door-left").animate({
                width: '305px'
            }, 1000);
            $("#door-right").animate({
                width: '305px'
            }, 1000, function(){
                cambiarPagina(htmlfile, contenido);
                $("#fondo").delay(1000).animate({
                    backgroundPosition: '0px '+ floorvar*25 +'%'
                }, 2000);
                $("#door-left").delay(2000).animate({
                    width: '1px'
                }, 1000);
                $("#door-right").delay(2000).animate({
                    width: '1px'
                }, 1000, function(){
                    cambiarZindex(777777, 1);
                    $('#botones ul li').removeClass('pager-active');
                    element.addClass('pager-active');
                    inAnim = false;
            			
                    if(contenido != null) {
                        $(".contenidointerior,.inicio,#ancholoco").load(contenido);
                    }
            			
                });

            });
        }
    }
				
    $('#footer-botones div').live('click',function(){
        if ($(this).attr('rel') != null){
            selectfloor($(this),4 - ($('#footer-botones div').index(this)),$(this).attr('rel')+ '.html', null);
        } 
    });
        
    $("#botones ul li").live('click',function(){
        if ($(this).attr('rel') != null){
            selectfloor($(this), $('#botones ul li').index(this), $(this).attr('rel')+ '.html', null);
        }
    });
    
    GoTo = function(page){       
        
        _gaq.push(['_trackPageview', '/'+page+'.html']);
        selectfloor($('#botones ul li').eq(0),0,page+'.html', null);
    }
    
    GoToInicio = function(){
        selectfloor($('#botones ul li').eq(4),4,'inicio.html', null);
        _gaq.push(['_trackPageview', '/inicio.html']);
    }
		
    GoToDescargas = function(){
        selectfloor($('#botones ul li').eq(3),3,'descargas.html', null);
        _gaq.push(['_trackPageview', '/descargas.html']);
    }
 
 
    GoToServicios = function(){
        selectfloor($('#botones ul li').eq(0),0,'servicios.html', null);
        _gaq.push(['_trackPageview', '/servicios.html']);
    }
    GoToTwitter = function(){
        selectfloor($('#botones ul li').eq(0),0,'servicios.html', 'tweet.html');
        _gaq.push(['_trackPageview', '/tweet.html']);
    } 
    GoToMensajeria = function(){
        selectfloor($('#botones ul li').eq(0),0,'servicios.html', 'mensaje.html');
        _gaq.push(['_trackPageview', '/mensaje.html']);
    } 
    GoToStore = function(){
        selectfloor($('#botones ul li').eq(0),0,'servicios.html', 'store.html');
        _gaq.push(['_trackPageview', '/store.html']);
    }
 
 
    GoToPromociones = function(){
        selectfloor($('#botones ul li').eq(1),1,'promociones.html', null);
        _gaq.push(['_trackPageview', '/promociones.html']);
    }
    GoToConcursos = function(){
        selectfloor($('#botones ul li').eq(1),1,'promociones.html', 'concursos.html');
        _gaq.push(['_trackPageview', '/concursos.html']);
    }
    
    
    GoToAplicaciones = function(){
        selectfloor($('#botones ul li').eq(2),2,'aplicaciones.html', null);
        _gaq.push(['_trackPageview', '/aplicaciones.html']);
    }  
    GoToChat = function(){
        selectfloor($('#botones ul li').eq(2),2,'aplicaciones.html', 'chat.html');
        _gaq.push(['_trackPageview', '/chat.html']);
    }
    GoToClaroMessenger = function(){
        selectfloor($('#botones ul li').eq(2),2,'aplicaciones.html', 'msg.html');
        _gaq.push(['_trackPageview', '/msg.html']);
    }
    GoToPlugger = function(){
        selectfloor($('#botones ul li').eq(2),2,'aplicaciones.html', 'plugger_cont.html');
        _gaq.push(['_trackPageview', '/plugger_cont.html']);
    }
    GoToJuegosHD = function(){
        selectfloor($('#botones ul li').eq(2),2,'aplicaciones.html', 'juegos.html');
        _gaq.push(['_trackPageview', '/juegos.html']);
    }
    GoToOperaMini = function(){
        selectfloor($('#botones ul li').eq(2),2,'aplicaciones.html', 'opera.html');
        _gaq.push(['_trackPageview', '/opera.html']);
    }
    
		
    var page = getURLParameter('page');
    if(page != null) {
        GoToPage(page);
    }
 
});
	
$(".contenidointerior ul li,.navbar ul li").live('click',function(){
    $(".contenidointerior ul li,.navbar ul li").each(function() {
        $(this).css('color', 'white');
    });
		
    $(this).css('color', 'red');
		
    if ($(this).attr('rel') != null){
        $(".contenidointerior,.inicio,#ancholoco").load($(this).attr('rel')+ '.html');
    }
});
	
$("input[type=button]").live('click', function(){
    $("#descripcionampliada").css('z-index','9999999');
    $("#fondotrans").css('z-index','9999998');
    $("#descripcionampliada").fadeIn();
    $("#fondotrans").fadeIn();
    $("#descripcionampliada").load($(this).attr('rel')+ '.html');
});
	
$("a,area").live('click',function(e){
    if($(this).attr('rel') == "externo") {
        console.log('link');
    } else {
        if ($(this).attr('href') != 'javascript:GoToServicios();'){
            e.preventDefault();
        }else{
            return true;
        }
        if ($(this).attr('href') == 'javascript:cerrar();'){
            cerrar();
        }
        if ($(this).attr('href') != '#'){
            $.ajax({
                url: $(this).attr('href'),
                cache: false,
                success: function(response){
                    shadowbox(response);
                }
            });
        }
    }
});
	
$("#formulario").live('click',function(){
		
    if((document.selModelo.brand.value != -1) && (document.selModelo.idTel.value != -1)) {
        if(validaFiltro()) {
            $.ajax({
                url: $(this).attr('urlfile'),
                type:'POST',
                cache: false,
                data: $(this).parents('form').serialize(),
                success: function(response){
                    shadowbox(response);
                }
            });
        }
    } else {
        $('#message_error').html('Debes seleccionar la marca y modelo de tu equipo');
        $('#message_error').show('fast');
    }
});
	
shadowbox = function(htmlcontent){
    $("#descripcionampliada").css('z-index','9999999');
    $("#fondotrans").css('z-index','9999998');
    $("#descripcionampliada").fadeIn();
    $("#fondotrans").fadeIn();
    $("#descripcionampliada").html("");
    $("#descripcionampliada").html("<div class='cerrar'><img src='/images/cerrar.jpg'></div>");
    $("#descripcionampliada").append(htmlcontent);
    $("#contenido").css('display', 'none');
}
	
cerrar = function(){
    $("#descripcionampliada").fadeOut().css('z-index','0');
    $("#fondotrans").fadeOut().css('z-index','0');
    $("#descripcionampliada").html('');
    $("#contenido").css('display', 'block');
}
	
$("#fondotrans, .cerrar").live('click',function(){
    cerrar();
});
	
$('a').click(function(e) {
    e.preventDefault();
    console.log('link');
});

$("img, a, li").live('mouseenter',function(e){
    if ($(this).attr('alt')){
        if ($(this).attr('alt')!= null){
            if ($(this).attr('alt')!= " "){
                if ($(this).attr('alt') != ""){
                    $("#tooltip").fadeIn(100);
                    $("#tooltip").html($(this).attr('alt'));
                }
            }
        }
    }
});
$("img, a, li").live('mouseleave click',function(e){
    $("#tooltip").fadeOut(100);
    $("#tooltip").html('');
});
	
$("img, a, li").live('mousemove',function(e){
    $("#tooltip").css('left', (e.pageX + 20) + 'px');// e.pageX - gives you X position
    $("#tooltip").css('top', (e.pageY - 30) +'px');// e.pageY - gives you Y position
});

// parametros url
function GoToPage(page) {
    
    //GoTo(page)
    
    switch(page) {
        case 'twitter':
            GoToTwitter();
            break;
        case 'tweet':
            console.log('abriendo tweet');
            GoToServicios();
            $.ajax({
                url: '/tweet_cont.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/tweet_cont.html']);
                    shadowbox(response);
                }
            });
            break;
        case 'descargas':
            GoToDescargas()
            break
        case 'concursos':
            GoToConcursos()
            break        
        case 'chat':
            GoToChat();
            break
        case 'aplicaciones':
            GoToAplicaciones()
            break
        case 'promociones':
            GoToPromociones()
            break
        case 'servicios':
            GoToServicios()
            break;
        case 'mensajeria':
            GoToMensajeria()
            break
        case 'store':
            GoToStore()
            break;
        case 'claromessenger':
            GoToClaroMessenger()
            break;
        case 'plugger':
            GoToAplicaciones()
            $.ajax({
                url: '/plugger_cont.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/plugger_cont.html']);
                    shadowbox(response);
                }
            });
            break;            
        case 'store':
            GoToStore()
            $.ajax({
                url: '/store.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/store.html']);
                    shadowbox(response);
                }
            });
            break;
        case 'xbox':
            GoToPromociones()
            $.ajax({
                url: '/shadowconcurso1.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/shadowconcurso1.html']);
                    shadowbox(response);
                }
            });
            break;
        case 'wii':
            GoToPromociones()
            $.ajax({
                url: '/shadowconcurso3.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/shadowconcurso3.html']);
                    shadowbox(response);
                }
            });
            break;
        case 'giftcard':
            GoToPromociones()
            $.ajax({
                url: '/shadowconcurso2.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/shadowconcurso2.html']);
                    shadowbox(response);
                }
            });
            break;
        case 'messenger':
            GoToServicios()
            $.ajax({
                url: '/msn_cont.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/msn_cont.html']);
                    shadowbox(response);
                }
            });
            break;
        case 'wap':
            GoToServicios()
            $.ajax({
                url: '/wap.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/wap.html']);
                    shadowbox(response);
                }
            });
            break;
        case 'opera':
            GoToAplicaciones()
            $.ajax({
                url: '/opera.html',
                cache: false,
                success: function(response){
                    _gaq.push(['_trackPageview', '/opera.html']);
                    shadowbox(response);
                }
            });
            break;
        default:
            GoTo(page)
            // do nothing
            break
    }
}

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
        );
}

