/* Util detecta ie6 ie7*/
var ie_old=false;
if((jQuery.browser.msie)&&(jQuery.browser.version.substr(0,1)<7)){ie_old=true;}
/**
 * JqueryMenu / Ofertix.com
 * Version 1.0 - November 11th, 2009
 * Copyright (c) 2009 Invasionweb.com
 **/
var divArbre="#microsite_arbre_menu";
jQuery(document).ready(function(){
    initMenu();
    jQuery(divArbre+" .activo").parent("ul").show();
    jQuery(divArbre+" li").not("li:has(ul)").click(function(){//No tiene mas subcat++
        location.href=jQuery(this).children("a").attr("href");//Click normal
    });
    jQuery(divArbre+" li").filter("li:has(ul)").children("a").append("<span class='arrow'> &#187;</span>");
    jQuery(divArbre+" li").click(
        function(){
            if(jQuery(this).children("ul").css("display")=="none"){
                //Cierra todo:
                initMenu();
                jQuery(divArbre+" ul:first a").removeClass("fx_selec");
                jQuery(this).parents("ul").show();//Abre todas las cat de la cat selecionada
                jQuery(this).parents("li").children("a").addClass("fx_selec");//Flecha activa para las cats selecionada

                jQuery(this).children("a").addClass("fx_selec");
                jQuery(this).children("ul").stop().fadeIn(600,function(){jQuery(this).css({"opacity":"1"});});
                moverFxMenu();//Actualiza posicion
            }
            return false;
        }
    );
    function initMenu(){//Inicia el menu por defecto esconde todo
            jQuery(divArbre+" ul").hide();
            jQuery(divArbre+" ul:first").show();//FIX
    }
    /* Funcion menu siempre en pantalla */
    jQuery(window).scroll(function(){
        moverFxMenu();
    })
    if(jQuery.browser.safari){//Hack Chrome & Safari
        jQuery(window).bind("resize",function(){
            jQuery(divArbre).css({"position":"relative"});
            jQuery(divArbre).animate({"position":"fixed"},100,function(){
                moverFxMenu();
            })
        })
    }
    function moverFxMenu(){
        var fx_con_div=jQuery(divArbre);
        var fx_scroller_fix=152;
        var fx_scroller=jQuery(this).scrollTop()-fx_scroller_fix;//Puesta 0 scoll
        var mainHeight=parseInt(jQuery("#main_content").height());
        var elementHeight=parseInt(fx_con_div.height());//Altura div
        var fx_cont=mainHeight-elementHeight-fx_scroller;//Altura maxima para el scroll
        if((jQuery(this).scrollTop()>fx_scroller_fix)&&(fx_cont>0)){
            if(ie_old){
                fx_con_div.css({"top":fx_scroller+"px"});
                /*fx_con_div.stop().animate({"top":fx_scroller+"px"},200);*/
            }else{
                fx_con_div.css({"position":"fixed","top":"0"});
            }
        }else if(fx_cont<1){//Te has pasado
            fx_con_div.css({"top":(fx_scroller+fx_cont)+"px"});
            if(!ie_old){fx_con_div.css({"position":"absolute"});}
            
        }else{
            fx_con_div.css({"top":"0px"});
            if(!ie_old){fx_con_div.css({"position":"absolute"});}//Relative? hack ie7 (Absolute)
        }
    }
})
 
/* ADD hack IE */
function hackIE(a,b){
    if(ie_old){
        jQuery(document).ready(function() {
            if(b==1){jQuery("#"+a).css("display", "none");
            }else{jQuery("#"+a).css("display", "block");}
        })
    }
}
/* ScrollTo */
function f_scrollTo(a){
    var destination = jQuery('#'+a).offset().top;
    jQuery("html").animate({scrollTop: destination}, 1000 );
    jQuery("body").animate({scrollTop: destination}, 1000,function(){location.href='#'+a;});
return false;}


jQuery(document).ready(function(){
/**
 * JqueryMenuPrincipal / Ofertix.com
 * Copyright (c) 2010 Invasionweb.com
 **/
    jQuery('#mainmenu a').add('#homemenu a').hover(function(){
        jQuery(this).stop().animate({opacity:1},300);
    },function(){
        jQuery(this).not('.acitve').stop().animate({opacity:0},1000);
    })
/**
 * JqueryMenu Iconos / Ofertix.com
 * Copyright (c) 2010 Invasionweb.com
 **/

/* Diseño y variable */
    jQuery('.menu_icos_txt').css({opacity:0});
    jQuery('.menu_icos_txt').each(function(){
        jQuery(this).html('<div class="gritter-top"></div><div class="gritter-item">'+jQuery(this).html()+'</div><div class="gritter-bottom"></div>')
    })

    //Abrimos el msg al pasar por encima
    jQuery('#menu_icos a.ico').hover(function(){
        jQuery('.menu_icos_txt:visible').not(this).stop().css({'top':'100px','opacity':'0'}).hide();
        jQuery(this).next('.menu_icos_txt').show()
        .animate({top:'36',opacity:1},500);
        hackIE("fitxa_producte_atributs form",1);
    },function(){
        jQuery(this).next('.menu_icos_txt').stop()
            .animate({top:'100',opacity:0},200,function(){
                jQuery('.menu_icos_txt').hide();
                hackIE("fitxa_producte_atributs form",2);
            });
    })
    //Evitamos el cierre con otro evento
    jQuery('.menu_icos_txt').hover(function(){
        if(jQuery(this).css('opacity')>0.8){
            jQuery(this).addClass('hover').stop().show().animate({top:'36',opacity:1},200);
            hackIE("fitxa_producte_atributs form",1);
        }
    },function(){
        jQuery(this).removeClass('hover').animate({top:'100',opacity:0},300,function(){
            jQuery(this).hide();
            hackIE("fitxa_producte_atributs form",2);
        })
        
    })
})//fin ready
function openCarrito(){

    jQuery('#carrito_head').stop().css({'top':'100px','opacity':'0'}).show()
        .animate({top:'36',opacity:1},500);
        hackIE("fitxa_producte_atributs form",1);
    
}
//Fix update carrito
function addGritter(id){
jQuery(id).html('<div class="gritter-top"></div><div class="gritter-item">'+jQuery(id).html()+'</div><div class="gritter-bottom"></div>')
}

// Añadir a la cesta
function addCarrito(){
    var offset = jQuery('#carrito_ico').offset();
    var offsetIni = jQuery('#foto').offset();
    jQuery('.clone').remove();
        jQuery('.foto_principal').clone(true).addClass('clone').removeClass('foto_principal')
        .prependTo('body')
        .css({"position":"absolute","z-index":9999,top:offsetIni.top+"px",left:offsetIni.left+"px"})
        .animate({top:(offset.top+20)+"px",left:(offset.left+20)+"px",width:10,height:10})
        .fadeOut(1000,function(){jQuery(this).remove()})
    }
/* Notificación popup */
var popNotimage='<img src="/images/pix.gif" alt="ico" class="gritter-image" />';


/* HELP TOOLTIP */
function helpTool(){
    jQuery(document).ready(function(){
        jQuery(".helpForm li").find('input').focus(function () {
            inicializar();
            moHelpTool(jQuery(this).next(".help"), 1);
        });
        jQuery('.helpForm li').find('input').hover(function(){
            inicializar();
            moHelpTool(jQuery(this).next(".help"), 1);
        },function(){
            moHelpTool(jQuery(this).next(".help"), 2);
        });
    });
}
function inicializar(){
    moHelpTool(jQuery(".helpForm li").find('input').next(".help"), 2);
}
function moHelpTool(a,cerrar){

    if(cerrar==1){
        jQuery(a).animate({top:'-10',opacity:1},{queue:false, duration:400})
    }else{
        jQuery(a).stop().animate({top:'10',opacity:0},{queue:false, duration:400})
    }
}
//Eliminar repeticion de errores form
 function errorList(a){
    jQuery(document).ready(function(){
        jQuery("#"+a+" li").find('input').focus(function () {
            inicializar();
            jQuery(this).nextAll(".error_list").fadeOut(400);
        });
    });
}

//MODAL BASE!!
//
function ModalOpen(){
jQuery('#ModalInfo').modal({
    minHeight:200,
    minWidth: 300,
    onClose: function(dialog){
        dialog.container.fadeOut('slow', function(){
            dialog.overlay.fadeOut('slow', function(){
                jQuery.modal.close();
                jQuery('#ModalInfo').text('');
            });
        });
    },onOpen: function(dialog){
        dialog.container.fadeIn('slow', function(){
            dialog.overlay.fadeIn('slow', function(){
                dialog.data.slideDown('slow',function(){
                    jQuery('#simplemodal-container').css({backgroundImage:'none'});
                })
            });
        });
    }
});
}


/* Loader Generifo*/
var loaderSec=0;
function loaderAjax(a){
        jQuery('#'+a).html('<div class="loaderAjax">Cargando...</div>');
        loaderSec=setTimeout('loaderAjaxOpen("'+a+'")',5000);
}
function loaderAjaxOpen(a){
        clearInterval(loaderSec);
        jQuery('#'+a).css({'opacity':0});
        jQuery('#'+a).stop().animate({'opacity':1},450);
        
}
//Modal loadText
function cargando(){
jQuery('#ModalInfo').html('</div><p>Verificando los datos. <br /><br /> Espere porfavor...</p>');
ModalOpen();
}

//Ver Video del producto!
function verVideo(direc,w,h){
    if(!w)w=415;
    if(!h)h=500;
    jQuery('#ModalInfo').html('<a href="'+direc+'" style="display:block;width:'+w+'px;height:'+h+'px" id="trailer"></a>');
     $f("trailer", "/swf/flowplayer-3.1.5.swf",{
        plugins:{controls:{
                    backgroundGradient: 'none',
                    backgroundColor: '#000000',
                    bufferColor: '#333333',
                    progressColor: '#bb0000',
                    tooltipColor: '#333333',
                    buttonColor: '#333333',
                    buttonOverColor: '#bb0000',
                    height:24,
                    volume: false,
                    time:false
        }}})
ModalOpen();
}
//VER TRAILER!!
function verTrailer(video,fondo){
    jQuery('#ModalInfo').html('<a href="'+video+'" style="display:block;width:729px;height:440px" id="trailer"></a>');
    $f("trailer", "/swf/flowplayer-3.1.5.swf",{
        plugins:{
            // controlbar settings
            controls:  {
                    backgroundGradient: 'none',
                    backgroundColor: '#000000',
                    bufferColor: '#333333',
                    progressColor: '#bb0000',
                    tooltipColor: '#333333',
                    buttonColor: '#333333',
                    buttonOverColor: '#bb0000',
                    height:24,
                    volume: false,
                    time:false
            }
        },
        canvas:{backgroundGradient:'none',backgroundColor:'#000000',backgroundImage: 'url('+fondo+')'},
        screen:{width:425, height:260, top:140, right:'50%'}
    });
//Abrir ventana
jQuery('#ModalInfo').modal({
    minHeight:440,
    minWidth: 729,
    onClose: function(dialog){
        dialog.container.fadeOut('slow', function(){
            dialog.overlay.fadeOut('slow', function(){
                jQuery.modal.close();
                jQuery('#ModalInfo').text('');
            });
        });
    },onOpen: function(dialog){
    jQuery('#simplemodal-container').css({padding:0,border:0});//fullScreen
        dialog.container.fadeIn('slow', function(){
            dialog.overlay.fadeIn('slow', function(){
                dialog.data.fadeIn('slow',function(){
                   jQuery('#simplemodal-container a.modalCloseImg').css({top:'-18px',right:'-21px'});
                })
            });
        });
    }
});

    return false;
}


jQuery.fn.center = function (absolute) {
	return this.each(function () {
		var t = jQuery(this);

		t.css({
			position:	absolute ? 'absolute' : 'fixed',
			left:		'50%',
			top:		'50%',
			zIndex:		'999'
		}).css({
			marginLeft:	'-' + (t.outerWidth() / 2) + 'px',
			marginTop:	'-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop:	parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(),
				marginLeft:	parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}
	});
};




/**
 * Funcion resetear formulario
 */
jQuery.fn.reset=function(){
    $(this).each(function(){this.reset();})
}