var imagencargando = '<img src="Imagenes/Template/ajax-loader.gif" align="center" />';
var w= 300;
var h= 190;
var blinkTimer;
var blinkCurrent = 0;
$(document).ready(function() {
	
    //Fuentes en flash
    /*$.sifr({ path: 'Swf/sifr/' , save: true });
    $('').sifr({font: 'univers-LT',cursor:'pointer', hovercolor:'#ffffff'});        */
    $('.button').corner("10px");
    
    if($("#accioncarrito").val() == "true")
    {
        blinkTimer = setInterval(blinkRotate, 500);
    }
    if($("#mostrarcarrito").val() == "true")
    {   
        $('.contenedor-carrito').show();
    } 
    
    
    //No permitir click derehoc
	document.oncontextmenu = function(){return false}
	
	//Videos
    var idvideo = $("#id-video").val();
    var video = $("#video"+idvideo +" a");
    var str;
	
        
    CambiarInfoVideo();   
    
    $(".contenedor-video a").click(function(event) 
    {        
        str = new String($(this).parent().attr("id"))
        $("#id-video").val(str.slice(5));    
        
        var video = $(this);
        
        DespuesAnimacion(video);
        
        return false;
    });  
	
	//Archivos en flash
	$('.swf').each(function (){
		var actual = this
		$(this).flash(
			{ src: $(actual).attr('rel'), width: $(actual).css("width").replace("px",""), height: $(actual).css("height").replace("px",""), wmode: "transparent", id:$(actual).attr("id"), classid:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"}
		);
	}).click(function(){
		return false;
	});
    
     $("#index .arbol-categorias, #der .arbol-categorias, #productos .arbol-categorias").treeview({
        collapsed: true,
        persist: "cookie",
		unique: true,
        cookieId: "navigationtree"
    });
    
    
	/*		
	$.sifr({ path: 'Swf/sifr/' , save: true }); 
	$('h1').sifr({font: 'century_gothic', fontSize:'28px'});
	$('#productos-categorias .producto .titulo h2').sifr({font: 'century_gothic', fontSize:'26px'});
		*/
    //Menu
    $('ul.principal').jdMenu({disableLinks: false});
    
    
    $(".contenedor-imagenes").carousel({ 
        dispItems: 3,
        nextBtn: "<div class=\"flecha-derecha\"><a href=\"#\" title=\"\" onclick=\"return false;\"></a></div>",
        prevBtn: "<div class=\"flecha-izquierda\"><a href=\"#\" title=\"\" onclick=\"return false;\"></a></div>",
        autoSlide: false,
        autoSlideInterval : 4000
    }); 
    
    $(".carrito-contenedor-recomendados").carousel({ 
        dispItems: 6,
        nextBtn: "<div class=\"flecha-derecha\"><a href=\"#\" title=\"\" onclick=\"return false;\"></a></div>",
        prevBtn: "<div class=\"flecha-izquierda\"><a href=\"#\" title=\"\" onclick=\"return false;\"></a></div>",
        autoSlide: false,
        autoSlideInterval : 4000
    }); 
	
	                
    //Lighbox   
    $(".fancybox").fancybox(); 
    
    $(".fancybox-efecto").fancybox({                
        'transitionIn'    : 'elastic',
        'transitionOut'    : 'elastic'
    }); 
    
    $(".fancybox-frame").fancybox({  
        'width'           : '80%',
        'height'          : '80%', 
        'autoScale'       : true,
        'transitionIn'    : 'none',
        'transitionOut'   : 'none',        
        'title'           : $(this).attr("title"),
        'href'            : $(this).attr("href"),
        'type'            : 'iframe'            
    });/**/
    
    $(".fancybox-quickview").fancybox({  
        'width'           : 800,
        'height'          : '80%', 
        'autoScale'       : true,
        'transitionIn'    : 'none',
        'transitionOut'   : 'none',        
        //'title'           : $(this).attr("title"),
        //'href'            : $(this).attr("href"),
        'type'            : 'iframe'            
    });/**/
    
    $(".fancybox-form").fancybox({  
        'scrolling'       : 'no',
        'width'           : 600,
        'height'          : 500, 
        'autoScale'       : false,
        'transitionIn'    : 'none',
        'transitionOut'   : 'none',        
        //'title'           : $(this).attr("title"),
        //'href'            : $(this).attr("href"),
        'type'            : 'iframe'
                    
    });
    
    $(".fancybox-formquestion").fancybox({  
        'scrolling'       : 'no',
        'width'           : 700,
        'height'          : 350, 
        'autoScale'       : false,
        'transitionIn'    : 'none',
        'transitionOut'   : 'none',        
        //'title'           : $(this).attr("title"),
        //'href'            : $(this).attr("href"),
        'type'            : 'iframe'            
    });
    
    
    $("#urlvideo").click(function() {
    $.fancybox({
            'padding'        : 0,
            'autoScale'      : false,
            'transitionIn'   : 'none',
            'transitionOut'  : 'none',
            'title'          : this.title,
            'width'          : 640,
            'height'         : 390,
            'href'           : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type'           : 'swf',
            'swf'            : {
                'wmode'          : 'transparent',
                'allowfullscreen': 'true'
                }
            });
        return false;
    });
    
    $("#dialog").dialog({ 
        autoOpen: false ,
        draggable: false,
        resizable: false,
        modal: true,
        title: '',
        close: function(event, ui) { 
             
        }
    })
      
});
function AbandonaCompra(titulo,mensaje,boton1,boton2,destino)
{
    DialogConfirm(titulo,mensaje,function() { parent.window.location.replace(destino) }, boton2, boton1 );
}
function Dialog(titulo,mensaje, botones)
{
    $('#dialog').html(mensaje);
    $("#dialog" ).dialog( "option", "title", titulo );
    
    if(!botones)
    {    
        var botones = {};
        botonok = $("#dialog-ok").attr("title") ;
        botones[botonok] = function() { $(this).dialog("close") } ;       
    }
        
    $("#dialog" ).dialog( "option", "buttons", botones);
    
    if( $('#dialog').dialog( "isOpen" ) == false )
        $('#dialog').dialog('open');
    
}
function DialogConfirm(titulo,mensaje,funcion, botonok, botoncancel)
{
    var botones = {};
    
    if(!botonok || botonok == "")
        botonok = $("#dialog-ok").attr("title");
    if(!botoncancel || botoncancel == "")
        botoncancel = $("#dialog-cancel").attr("title");
    
    botones[botoncancel] = function() { $(this).dialog("close") } ;   
    botones[botonok] = funcion;
    
    Dialog(titulo,mensaje,botones)    
}
function DespuesAnimacion(video)
{
    CambiarInfoVideo();
    IniciarVideo(video);      
}
function IniciarVideo(video)
{
    var player = null;
	
	var w= 300;
	var h= 190;
                
    if (video.hasClass("flv"))
    {
        player = flashembed("reproductor-video", 
            {
                src:'Swf/FlowPlayerDark.swf',
                width: w, 
                height: h,
                controls:null,
				wmode: 'transparent'
            },                                    
            {
                config: {   
                    autoPlay: false,
                    autoBuffering: false, 
                    loop:false, 
                    clip: { autoPlay: false,
                    autoBuffering: true},
                    initialScale:'scale',
                    videoFile: video.attr("href")
                    //hideControls: true
                }
                
            }
        );   
    }
    else if(video.hasClass("swf") || video.hasClass("youtube"))
    {           
        player = flashembed("reproductor-video", 
        {               
            src: video.attr("href"),
            width: w, 
            height: h,
            controls:null,
			wmode: 'transparent'
        } 
        );   
    }
    else
    {
         player = flashembed("reproductor-video", 
         {
                src:'Swf/FlowPlayerDark.swf',
                width: w, 
                height: h,
                controls:null,
				wmode: 'transparent'
        },                                    
            {
                config: {   
                    autoPlay: false,
                    autoBuffering: false, 
                    loop:false, 
                    clip: { autoPlay: false,
                    autoBuffering: true},
                    initialScale:'scale',
                    videoFile: video.attr("href")
                    //hideControls: true
                }
                
            }
        );      
    }
}
function CambiarInfoVideo()
{
    var idvideo = $("#id-video").val(); 
    $.ajax(
    {
        url: "Video.php?op=InfoVideo&noheaders=all&idvideo="+idvideo+"",
        async:true,
        cache:false,
        dataType:"html",
        
        success: function(datos){
            $(".informacion").empty().fadeOut('fast');
            $(".informacion").append(datos).fadeIn('slow');
            var idvideo = $("#id-video").val();
            var video = $("#video"+idvideo +" a");
            var str;  
            IniciarVideo(video);
        },
        timeout: 10000                                                                                             
    });          
}
function ArticuloEnLighbox(idarticulo)
{
	$.fancybox({  
        'width'           : '90%',
        'height'          : '90%', 
        'autoScale'       : true,
        'transitionIn'    : 'none',
        'transitionOut'   : 'none',    
        'href'            : "Articulos.php?op=Leer&amp;noheaders=ok&amp;idarticulo="+idarticulo,
        'type'            : 'iframe'            
    });  	
}
 
function ContactoAjax(tipo){
    var nombre = $("#nombre_"+tipo).val()
    var correo = $("#email_"+tipo).val()
	var mensaje = $("#texto_"+tipo).val()
  
   
    if(nombre.length <= 0 )
    {
        alert(trad[49])
        return false;
    }
    
    if(correo.length <= 0)
    {
        alert(trad[50])
        return false;
    }
	
	if(mensaje.length <= 0)
    {
        alert(trad[51])
        return false;
    }
   
    
    $.ajax({
        type: "POST",
        url: "Contacto.php",
        data: "op=ContactoAjax&noheaders=all&nombre="+nombre+"&correo="+correo+"&tipo="+tipo+"&mentaje="+mensaje,
        success: function(datos){
           $("#nombre_"+tipo).val("")
           $("#email_"+tipo).val("")
		   $("#texto_"+tipo).val("")
 
           alert(datos);  
        }
    });
    
    return false;
}
function blinkRotate()
{
    if(blinkCurrent >= 16)
    {    
        clearInterval(blinkTimer);
        blinkCurrent = 0;
        $(".boton-carrito").removeClass("boton-carrito-blinkon");
        $(".boton-carrito").removeClass("boton-carrito-blinkoff");
    }
    else
    {
        if($(".boton-carrito").hasClass( "boton-carrito-blinkon") )
        {   
            $(".boton-carrito").removeClass("boton-carrito-blinkon");
            $(".boton-carrito").addClass("boton-carrito-blinkoff");
        }
        else
        {
            $(".boton-carrito").removeClass("boton-carrito-blinkoff");
            $(".boton-carrito").addClass("boton-carrito-blinkon");
        }
        blinkCurrent++;
    }
        
}
function mycarousel_initCallback(carousel)
{
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
    
}; 
function AgregarCarrito()
{       
    var cad = "";
    
    cad = $('#productos-ver #form_carrito').serialize();
    
    //alert(cad+ "\n\r\n\r .");
    
    Dialog("",imagencargando,{});
    
    $.ajax({
        type: "POST",
        url: "Carrito.php",
        cache: false,
        data: cad,
        dataType: "json", 
        success: function(datos){
            TotalItemsCarrito();
            DialogConfirm(datos.titulo, datos.mensaje, function() { parent.window.location.replace("Carrito.html") }, datos.boton2, datos.boton1 );
        }
    });
}
function BorrarCarrito(idproducto)
{       
    var cad = "";
    
    cad = "op=BorrarCarrito&noheaders=all&idproducto="+idproducto;
    
    //alert(cad+ "\n\r\n\r .");
    $(".lista-carrito .borrarproducto"+idproducto).html(imagencargando);
    $.ajax({
        type: "POST",
        url: "Carrito.php",
        cache: false,
        data: cad,
        success: function(datos){
            TotalItemsCarrito();
            ListaCarrito();
        }
    });/**/
}
function ListaCarrito()
{
    var cad = "";
    
    cad = "op=ListaCarrito&noheaders=all";
    
    //alert(cad+ "\n\r\n\r .");
    
    $.ajax({
        type: "POST",
        url: "Carrito.php",
        cache: false,
        data: cad,
        success: function(datos){
            $(".lista-carrito .listacarrito-ajax").html(datos);
        }
    });/**/
}
function TotalItemsCarrito()
{
    var cad = "";
    
    cad = "op=TotalItemsCarrito&noheaders=all";
    
    //alert(cad+ "\n\r\n\r .");
    
    $.ajax({
        type: "POST",
        url: "Carrito.php",
        cache: false,
        data: cad,
        success: function(datos){
            $("#cabecera .cart-totalitems").html(datos);
        }
    });/**/    
}
function CambiarCantidadCarrito(idproducto)
{       
    var cad = "";
    
    cantidad = $("#cantidadproducto"+idproducto).val();
    
    cad = "op=CambiarCantidadCarrito&noheaders=all&idproducto="+idproducto + "&cantidad="+cantidad;
    
    //alert(cad+ "\n\r\n\r .");
    $(".lista-carrito #cantidadproducto"+idproducto).parent().html(imagencargando);
    $.ajax({
        type: "POST",
        url: "Carrito.php",
        cache: false,
        data: cad,
        success: function(datos){
            TotalItemsCarrito();
            ListaCarrito();
        }
    });/**/
}
function Login()
{
    nick = $("#formLogin #nick_login");
    pass = $("#formLogin #pass_login");
    
    $("#formLogin").attr('action','Sesion.php');
    $("#formLogin #nick_login").attr('name','nick');
    $("#formLogin #pass_login").attr('name','pass');
    
    //alert($("#formLogin").serialize());
    $("#formLogin").submit();
    
}
function EnviarSolicitudEnvio()
{       
    var cad = "";
    
    cad = $('#formulario-solicitud #FormSolicitud').serialize();
    
    //alert(cad+ "\n\r\n\r .");
    $.ajax({
        type: "POST",
        url: "Contacto.php",
        cache: false,
        data: cad,
        success: function(datos){
            var botones = {} ;
            botones[$("#dialog-ok").attr("title")] = function() { parent.$.fancybox.close(); };    
            Dialog("",datos,botones);
            
        }
    });/**/
}
function EnviarPreguntaProducto()
{       
    var cad = "";
    
    cad = $('#formulario-pregunta #FormPreguntaProducto').serialize();
    
    //alert(cad+ "\n\r\n\r .");
    $.ajax({
        type: "POST",
        url: "Contacto.php",
        cache: false,
        data: cad,
        success: function(datos){
            var botones = {} ;
            botones[$("#dialog-ok").attr("title")] = function() { parent.$.fancybox.close(); };    
            Dialog("",datos,botones);
        }
    });/**/
}
