// SET GLOBAL VARS
var subMenu = false;
var cerrandoMenu = false;
var animandoMenu = false;


//INIT

$(document).ready(function(){
    
   
    
   // cambiarSize($("#main"),768,980);
    cambiarSize($("#main"),500,980);
    adjustBackground();
    
   $('div#volver').css({left:$(document).width()-130});
    $('div#menu').css({left:$(document).width()-130});
   
     if($(document).width()<1000) $('div#footer p').css('font-size','9px');        
     if($(document).width()>=1000) $('div#footer p').css('font-size','10px');
//$('div#footer').css({width:$(window).width()-20});
//$('div#footer').css({width:$("div#background img").width()-20});

     
     if ($('div#contenedor').length>0){
        
     
         $('div#contenedor').css({
                    position:'absolute',
                    left: ($(document).width() - $('div#contenedor').outerWidth())/2,
                    top: ($(document).height() - $('div#contenedor').outerHeight())/2
            });
          
         
   } 
   
     if ($('div#contenedor_galeria').length>0){
        
               // $("div#footer p").css("color","#000"); 
         
              // $("div#contenedor_galeria").css("width",$(document).width());
              
              //CENTRADO DE LA GALERIA
               $('div#contenedor_galeria').css({
		position:'absolute',
		left: ($(document).width() - $('div#contenedor_galeria').outerWidth())/2,
		top: ($(document).height() - $('div#contenedor_galeria').outerHeight())/2
            });
            
            
            setTimeout(function(){
                $('div#contenedor_galeria').fadeIn("slow");
               
            
             $("div.galeria").jMyCarousel({
		        visible: '100%',
                        circular:false,
                        speed:200
                        
                        
		    });
                    
                     //LIGHTBOX PARA LA GALERIA
                     $("a#fancy").fancybox(
                        {
                        'overlayShow'	: false,
                        'transitionIn'	: 'elastic',
                        'transitionOut'	: 'elastic'
                        });
              },2000);  
              //GALERIA  
            
             // $("img").lazyload();
             $("img").lazyload({ threshold : 100, effect : "fadeIn",container: $(".galeria") });
              
              
          /*  
              var coleccion = gup( 'coleccion' );
             
                  $("div.galeria").load("php/getimages.php", { gal:coleccion} ,function(){ 
                    
                        $("div.galeria").jMyCarousel({
		        visible: '100%',
                        circular:false,
                        speed:200
                        
                        
		    });
                 
                  
                    
                });    
        */

        }
        
       

    
    //Suavizar carga imagen Background
    $("div#background img").hide(); 

    var bgimg = new Image();   
    bgimg.onload = function () {
      
        adjustBackground();
       // $("div#background img").hide(); 
        $("div#background img").fadeIn("slow");
    };

     bgimg.src = $("div#background img").attr("src");
    
   /* // Pre cargar imágenes 
    $("img").each(function(){
        var img = new Image();
        img.src = $(this).attr("src");
    });
    
    // Pre cargar imágenes BGs
    var tiposBg = ["div","ul","li"];
    for(var i = 0; i < tiposBg.length; i++){
        $(tiposBg[i]).each(function(){
            if($(this).css("background-image") != "none"){
                var img = new Image();
                img.src = String(String($(this).css("background-image")).split('url("')[1]).split('")')[0]    
            }       
        });
    }
     */   
    
    //EVENTOS
    $(window).resize(function() {
        //cambiarSize($("#main"),768,980);
        cambiarSize($("#main"),500,980);
        adjustBackground();
        
        $('div#volver').css({left:$(document).width()-130});
         $('div#menu').css({left:$(document).width()-130});
         if($(document).width()<1000) $('div#footer p').css('font-size','9px');        
         if($(document).width()>=1000) $('div#footer p').css('font-size','10px');
       // $('div#footer').css({width:$(window).width()-20});
      
        //$('div#footer').css({width:$("div#background img").width()-20});
        
         if ($('div#contenedor').length>0){
            
                $('div#contenedor').css({
                        position:'absolute',
                        left: ($(document).width() - $('div#contenedor').outerWidth())/2,
                        top: ($(document).height() - $('div#contenedor').outerHeight())/2
                });
        } 
        
         if ($('div#contenedor_galeria').length>0){
              
              // $("div#contenedor_galeria").css("width",$(document).width());
               $('div#contenedor_galeria').css({
		position:'absolute',
		left: ($(document).width() - $('div#contenedor_galeria').outerWidth())/2,
		top: ($(document).height() - $('div#contenedor_galeria').outerHeight())/2
            });

        }
    });
    
    
    //    $("div#main_menu").mouseenter(function(){
    //        showSubMenu();
    //    });
/*
    $("div#sub_menu").mouseleave(function(){
        hideSubMenu();
    });
    $(document).mouseleave(function(){
        hideSubMenu();
    });
    
    if($("div.look-tag").length){
        $("div.look-tag").click(function(){
            toggleLookDetails();
        })
    }
    
    if($("div#menu-videos").length){
        $("div#menu-videos ul li").each(function(){
            $(this).mouseenter(function(){
                showPlay(this);
            });
            $(this).mouseleave(function(){
                hidePlay(this);
            });
            
        });
    }
    
    if($("div#bloque-accesories").length){
        $("div#bloque-accesories ul li").each(function(){
            $(this).mouseenter(function(){
                showBrillo(this);
            });
            $(this).mouseleave(function(){
                hideBrillo(this);
            });
            $(this).click(function(){
                ampliarAccesorio(this);
            });
            
        });
    }
    
    if($("div#ampli-overlay").length){
        $("div#ampli-wrap a.close").click(function(){
            hideAccesorio();
        });
    
        $("div#ampli-overlay").click(function(){
            hideAccesorio();
        });
    }
    
    if($("div#jobs-overlay").length){
        $("div#jobs-wrap a.close").click(function(){
            hideJobs();
        });
    
        $("div#jobs-overlay").click(function(){
            hideJobs();
        });
    }
    
    if($("div#contact-overlay").length){
        $("div#contact-wrap a.close").click(function(){
            hideContact();
        });
    
        $("div#contact-overlay").click(function(){
            hideContact();
        });
    }
    // FINAL EVENTOS
    
    if($("div#bloque-about").length){
        setTimeout("toggleAbout()",1000);
    }

    */
    
}); // FINAL INIT


// GLOBAL FUNCTIONS

function cambiarSize(elem,h,w){
     
  /*   //Re-Escalar bloque collection ** Poner a altura por defect
     if($("#page-collection").length){
        $("div#page-collection").css("height","800px");
     }
     */
    //ajusta tamaño contenedor principal 
    var ref = ($("body").height() > $("window").height() || $("body").width() > $("window").width()) ? "body" : window;
    
    //    $("html").css("height","100%");
    $("body").css("height","100%");
    // $("body").css("width","100%");
    //    $("#main").css("height","100%");
    
    //    $("html").css("height",document.documentElement.scrollHeight+"px");
    $("body").css("height",document.documentElement.scrollHeight+"px");
    //$("body").css("width",document.documentElement.scrollWidth+"px");
    //    $("#main").css("height",document.documentElement.scrollHeight+"px");

       if($(ref).width() <= parseInt(w)){           
        $(elem).width(parseInt(w));
    }else{
        $(elem).width("100%");
    }
    
    if($(ref).height() <= parseInt(h)){           
        $(elem).height(parseInt(h));
    }else{
        $(elem).height("100%");
    }
    
  /* 
    //Re-Escalar bloque collection ** calcular nueva altura segun alto del documento
    if($("#page-collection").length){
        $("div#page-collection").css("height",($(document).height()-5-$("div#page-collection")[0].offsetTop)+"px");
    }
   */ 
    
    //Re-Escalar bloque Accesories
    //    if($("#page-accesories").length){
    //        $("div#page-accesories").css("height","auto");
    //        $("div#page-accesories").css("height",($(document).height()-$("div#page-accesories")[0].offsetTop)+"px");
    //    }
   
      
    if($(window).width() <= 1180){
        
        //Ajusta tamaño menu superior
        $("div#main_menu").removeClass("normal-size"); 
        $("div#main_menu").addClass("small-size"); 
        $("div#sub_menu").removeClass("normal-size"); 
        $("div#sub_menu").addClass("small-size"); 

        
    //Ajusta posicion bloque news
    //        $("div#page-news").css("height","970px");
    //        $("div#page-news div#tw-fb").css("bottom","335px");
    //        $("div#page-news div#bloque-banner").css("left",0);
    //        $("div#page-news div#bloque-videos").css("left",0);
        
    } else {
        
        //Ajusta tamaño menu superior
        $("div#main_menu").removeClass("small-size"); 
        $("div#main_menu").addClass("normal-size"); 
        $("div#sub_menu").removeClass("small-size"); 
        $("div#sub_menu").addClass("normal-size"); 

        
    //Ajusta posicion bloque news
    //        $("div#page-news").css("height","635px");
    //        $("div#page-news div#tw-fb").css("bottom",0);
    //        $("div#page-news div#bloque-banner").css("left","690px");
    //        $("div#page-news div#bloque-videos").css("left","690px");
    }
    
    
    // Si hay ampliacion
    if($("div#ampli-wrap").length){
        adjustAmpli();
    }
    if($("div#jobs-wrap").length){
        adjustJobs();
    }
    if($("div#contact-wrap").length){
        adjustContact();
    }

                
}
            
//GENERAL            

function adjustBackground(){
     
    $("div#background img").css("height","auto");
    $("div#background img").css("width",($("div#background").width())+"px"); 
    $("div#background img").css("margin-left", 0);
    
    
    
    if($("div#background").height() > ($("div#background img").height()-150)){
        $("div#background img").css("height",($("div#background").height()+150)+"px");
        $("div#background img").css("width","auto");
        $("div#background img").css("margin-left",(($("div#background").width() - $("div#background img").width())/2)+"px");
        
    }
    
   
}



function gup( name ){
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp ( regexS );
	var tmpURL = window.location.href;
	var results = regex.exec( tmpURL );
	if( results == null )
		return"";
	else
		return results[1];
}
/*
function showSubMenu(){
    if(!subMenu && !cerrandoMenu){
        subMenu = true;
        //        animandoMenu = true;
        $("#sub_menu").slideDown("slow");
        $("div#background img").animate({
            marginTop:0
        },"slow");
    
        
    }
    if(cerrandoMenu){
        clearTimeout(cerrandoMenu);
    }
    
}   

function hideSubMenu(){
    cerrandoMenu = setTimeout("hideSubMenu_()",100)
    
}  

function hideSubMenu_(){
    cerrandoMenu = false;
    if(subMenu){
        subMenu = false;
        $("#sub_menu").slideUp("slow");
        $("div#background img").animate({
            marginTop:"-150px"
        },"slow");
    }
    
}






//ABOUT
function toggleAbout(){
    $("div#bloque-about").fadeToggle("slow");
    return false;
}

//COLLECTIONS

function toggleLookBook(){
    $("div#look-box ul").fadeToggle("slow");
    return false;
}

function toggleLookDetails(){
    $("div#bloque-products").fadeToggle("slow");
}
function showLookDetails(){
    $("div#bloque-products").fadeIn("slow");
}
function hideLookDetails(){
    $("div#bloque-products").fadeOut("slow");
}

//EVENTS

function toggleEventsBar(){
    $("div#menu-videos").fadeToggle("slow");
    return false;
}

function showPlay(item)
{
    $(item).children("img.play").fadeIn("slow");
}
function hidePlay(item)
{
    setTimeout(function(){
        $(item).children("img.play").fadeOut("slow");
    },200);
}


/// ACCESORIOS

function toggleSearchBox(){
    $("div#search-box").fadeToggle("slow");
    return false;
}

function showBrillo(item)
{
    $(item).children("img.brillo").fadeIn("slow");
}
function hideBrillo(item)
{
    setTimeout(function(){
        $(item).children("img.brillo").fadeOut("slow");
    },200);
}

function ampliarAccesorio(item){
    $("div#ampli-wrap div.inner-ampli-container").html($(item).children("div.item-data").html()); 
    
    adjustAmpli();
    $("div#ampli-overlay").fadeIn("slow");
    $("div#ampli-wrap").fadeIn("slow");
}
function adjustAmpli(){
    $("div#ampli-overlay").css("height",$(window).height()+"px");
    if($(window).height() > $("div#ampli-wrap").height()) $("div#ampli-wrap").css("top",(($(window).height()/2)-($("div#ampli-wrap").height()/2))+"px");
    if($(window).width() > $("div#ampli-wrap").width()) $("div#ampli-wrap").css("left",(($(window).width()/2)-($("div#ampli-wrap").width()/2))+"px");
    
    
}

function hideAccesorio(){
    $("div#ampli-overlay").fadeOut("slow");
    $("div#ampli-wrap").fadeOut("slow");
}


/* JOBS */
/*
function jobSelect(){
    $('select#area option').each(function(){
                    if($(this).attr("selected") == "selected") $('span.select-bg').text($(this).text());    
    });
}

function adjustJobs(){
    $("div#jobs-overlay").css("height",$(window).height()+"px");
    if($(window).height() > $("div#jobs-wrap").height()) $("div#jobs-wrap").css("top",(($(window).height()/2)-($("div#jobs-wrap").height()/2))+"px");
    if($(window).width() > $("div#jobs-wrap").width()) $("div#jobs-wrap").css("left",(($(window).width()/2)-($("div#jobs-wrap").width()/2))+"px"); 
}

function hideJobs(){
    $("div#jobs-overlay").fadeOut("slow");
    $("div#jobs-wrap").fadeOut("slow");
}
*/

/* CONTACT */
/*
function contactSelect(){
    $('select#area option').each(function(){
                    if($(this).attr("selected") == "selected") $('span.select-bg').text($(this).text());    
    });
}

function adjustContact(){
    $("div#contact-overlay").css("height",$(window).height()+"px");
    if($(window).height() > $("div#contact-wrap").height()) $("div#contact-wrap").css("top",(($(window).height()/2)-($("div#contact-wrap").height()/2))+"px");
    if($(window).width() > $("div#contact-wrap").width()) $("div#contact-wrap").css("left",(($(window).width()/2)-($("div#contact-wrap").width()/2))+"px"); 
}

function hideContact(){
    $("div#contact-overlay").fadeOut("slow");
    $("div#contact-wrap").fadeOut("slow");
}*/
