$(window).load(function(){
    
});



$(document).ready(function(){
    
    $(".hidden img").one('load', function() {
      
       
       $(this).parents().find('.hidden-container img.preloader').remove();
       $(this).parents().find('.hidden:first').css('visibility', 'visible').animate({opacity: 1.00}, 500, function() {});
       
      
    }).each(function() {
      if(this.complete) $(this).load();
    });
    
//    $('.hidden img').load(function(){
//        
//       
//       if(document.location.toString().indexOf('collection') == 0 && document.location.hash != '') {
//           changeCategory(document.location.hash.replace('#',''));
//       } else {
//           $('h2 a').html('triggered');
//           $(this).parents().find('.hidden-container img.preloader').remove();
//           $(this).parents().find('.hidden:first').css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});  
//           
//       }
//       
//    });
    
    
    $("a").each(function(){
        $(this).click(function(event){
            //event.preventDefault();
            switch($(this).attr("href")){
                case "#sign_in":
                    $(".account").hide();
                    $(".sign_in").show();
                    return false;
                    break;
                case "#sign_up":
                    $(".account").hide();
                    $(".sign_up").show();
                    return false;
                    break;
                case "#account_close":
                    $(".sign_up").hide();
                    $(".sign_in").hide();
                    $(".reset_password").hide();
                    $(".account").show();
                    return false;
                    break;
                case "#lost_password":
                    $(".sign_up").hide();
                    $(".sign_in").hide();
                    $(".account").hide();
                    $(".reset_password").show();
                    return false;
                    break;
                case "#show_hide_photographs":
                    if($("ul.list1a").hasClass("photos")){
                        $("ul.list1a").removeClass("photos");
                        $("ul.list1a img").hide();
                        $(this).text("Show photographs")
                    }else{
                        $("ul.list1a").addClass("photos");
                        $("ul.list1a img").show();
                        $(this).text("Hide photographs")
                    }
                    return false;
                    break;
                case "#editions":
                    if($(this).parent().find('ul').is(':visible')) {
                        
                        $(this).parent().find('ul').hide();
                        $(this).parent().removeClass('space');
                    } else {
                        
                        hideAllMenuItems();
                        
                        $(this).parent().addClass('space');
                        $(this).parent().find('ul').show();
                    }
                    
                    return false;
                    break;
                case "#walldone":
                    if($(this).parent().find('ul').is(':visible')) {
                        
                        $(this).parent().find('ul').hide();
                        
                    } else {
                        
                        hideAllMenuItems();
                        
                        $(this).parent().find('ul').show();
                    }
                    
                    return false;
                    break;
                case "#magazine":
                    if($(this).parent().find('ul').is(':visible')) {
                        
                        $(this).parent().find('ul').hide();
                        if(!$(this).parent().find('ul').hasClass('opened'))
                            $(this).parent().removeClass('space');
                    } else {
                        
                        hideAllMenuItems();
                        
                        if(!$(this).parent().find('ul').hasClass('opened'))
                            $(this).parent().addClass('space');
                        $(this).parent().find('ul').show();
                    }
                    
                    return false;
                    break;
                case "#add_to_lightbox":
                    editToBag();
                    
                    return false;
                    break;
                
                case "#search":
                    
                    if($('#phrase').css('marginLeft') == '0px') {
                        goToSearch();
                    } else {
                        $('#phrase').animate({
                            'marginLeft': '0'
                          }, 500,  function() {});
                    }
                    return false;
                    break;
                case "#load-more":
                    getBoxes();
                    return false;
                    break;
                case "#":
                    return false;
                    break;
                
            }
        });
    });

    $('a.newsletter-label').click(function(event){
        //event.preventDefault();
        if($('.newsletter-form').is(':visible'))
            $('.newsletter-form').hide();
            //document.location.hash = '';
        else {
            //document.location.hash = 'newsletter';
            $('.newsletter-form').show();
        }
        
    });
    
    $(".menu ul ul li a").click(function(event){
       event.preventDefault();
       $(this).parent().toggleClass("active");
       
    });
    
    $(".menu ul ul ul li a").click(function(event) {
        
       event.preventDefault();
       $(this).parents('ul.opened > li.active').removeClass('active');
       changeCategory($(this).attr('href'));
       
    });
    
    $(".menu ul li").hover(
        function(){
            $(this).parents('li:first').addClass('false');
        },
        function(){
            $(this).parents('li:first').removeClass('false');
        }
    );

    
    $("form").mouseout(function(){
        $(this).parent().addClass("toHide");
    }).mouseover(function(){
        $(".toHide").removeClass("toHide");
    });
    
    $(document).click(function(){
        if($(".toHide").length){
            $(".sign_up").hide();
            $(".sign_in").hide();
            $(".reset_password").hide();
            $(".account").show();
            $(".toHide").removeClass("toHide");
        }
    });
    
    $("input").each(function(){
        
        if($(this).hasClass('focusless')) return true;
        
        if($(this).attr('type')==undefined || $(this).attr('type')=='text'){
            $(this).attr("OkiValue",$(this).attr("value"));
            $(this).focusin(function(){
                if($(this).attr("value")==$(this).attr("OkiValue"))
                    $(this).attr("value",'');
            });
            $(this).focusout(function(){
               if($(this).attr("value")=='')
                   $(this).attr("value",$(this).attr("OkiValue"));
            });
        }
        if($(this).attr('OkiType')=="password"){
            $(this).focus(function(){
                var clone=$(this).clone();
                clone=clone.attr("type", "password");
                $(this).replaceWith(clone);
                clone.focus();
            });
        }
    });
    
    $("textarea").each(function(){
        
        if($(this).hasClass('focusless')) return true;
        
        
        $(this).attr("OkiValue",$(this).attr("value"));
        $(this).focusin(function(){
            if($(this).attr("value")==$(this).attr("OkiValue"))
                $(this).attr("value",'');
        });
        $(this).focusout(function(){
           if($(this).attr("value")=='')
               $(this).attr("value",$(this).attr("OkiValue"));
        });
        
        
    });
    

    $('input.checkbox').prettyCheckboxes({'display': 'inline'});
    $('input.radio1').prettyCheckboxes({'display': 'inline'});
    
    $("ul").each(function(){
        $(this).children("li").first().addClass("first");
        $(this).children("li").last().addClass("last");
    });
    
    $(".menu2 > li").click(function(){
        $(this).children("ul").toggle();
        $(this).toggleClass("active");
    });
    $(".list8 > li span").click(function(){
        $(this).parent().children("ul").toggle();
        $(this).toggleClass("active").parent().toggleClass("active");
    });
    
    
    $('#phrase').keypress(function(e) {
        if(e.keyCode == 13) {
            top.location = 'search/'+$('#phrase').val()
        }
    });
    
    
    
});

function goToSearch() {
    top.location = 'search/'+$('#phrase').val()
}

function confirmation(question, url) {

    if(confirm(question))
        top.location = url;
    else return false;
}

function confirmation2(question, todo) {

    if(confirm(question))
        eval(todo);
    else return false;
}

function hideAllMenuItems() {
    $('.menu > li > ul').hide();
}
