/* Declare a namespace for the site */
var Site = window.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {
	
	//same as $(document).ready();
	$(function() {
		      
		      
		  /* NAVIGATION */
		  //$('#nav li.active').delay(500).animate({ height: '+=7px', marginTop: '3px'}, 250, 'easeOutSine');
	      
	      $('#nav li:not(".active")').live({
	                   mouseenter: function() {
	                       $(this).animate({ height: '+=7px',  marginTop: '3px'}, 250, 'easeOutSine');
	                       $('.accordion li:nth('+$(this).index()+')').mouseover();
	                       if (typeof toggleStartImages == 'function') {
	                           toggleStartImages('show', $(this).index());
	                       }
	                    },
	                    mouseleave: function() {
	                       $(this).animate({ height: '-=7px',  marginTop: '10px'}, 250, 'easeOutSine');
  	                       $('.accordion li:nth('+$(this).index()+')').mouseleave();
	                       if (typeof toggleStartImages == 'function') {
	                           toggleStartImages('hide', $(this).index());
	                       }
	                   }
	       });
	       
      		
            images = new Array();      
      				
      		      		
      		getImage = function(index, gallery) {
      			if (index == -1) 
      				return false;
      		    					
				if (isImageLoaded(images[index])) {
/* 					console.log("loaded..."); */
    				var size = gallery._getContainedImageSize(images[index][0].width, images[index][0].height);
    				//console.log("getImage" + images[index][0].width + " " + images[index][0].height);
    				
	    			images[index].attr({ width: size.width, height: size.height});
					
					return images[index];
				} else {
					return getImage(index, gallery);
				}

    			
        	}
        
        imagesQ={onComplete:function(){},onLoaded:function(){},current:null,qLength:0,images:[],inProcess:false,queue:[],queue_images:function(){var arg=arguments;for(var i=0;i<arg.length;i++){if(arg[i].constructor===Array){this.queue=this.queue.concat(arg[i])}else if(typeof arg[i]==='string'){this.queue.push(arg[i])}}},process_queue:function(){this.inProcess=true;this.qLength+=this.queue.length;while(this.queue.length>0){this.load_image(this.queue.shift())}this.inProcess=false},load_image:function(imageSrc){var th=this;var im=new Image;im.onload=function(){th.current=im;th.images.push(im);(th.onLoaded)();if(th.queue.length>0&&!th.inProcess){th.process_queue()}if(th.qLength==th.images.length){(th.onComplete)()}};im.src=imageSrc}}
        
        
    	imagesQueue = imagesQ;
			
	               	
        	preloadImages = function() {
        		for (i = 0; i < $('#gallery .scrollableArea').children().not('.text').size(); i++) {
        				var src = $($('#gallery .scrollableArea').children().not('.text').get(i)).attr('src');
						//var img = $(new Image()).attr('src', src);
						//var img = new Image();
						//img.src = src;
						images[i] = src;
						//console.log(images[i]);
						
/*				    
						if (!isImageLoaded($(img))) {
                            console.log("not loaded");
							img.load(function() {
							     console.log(response);
            					 console.log("img loaded");
								 img.preloaded = true;
              					 img.size = { width: this.width, height: this.height };
							});
						} else {
							img.preloaded = true;
          				    img.size = { width: $(img).width, height: $(img).height };
						}   
*/
			   }
			     //console.log(images);
                imagesQueue.queue_images(images);        
                imagesQueue.process_queue();
			 
			   return true;
        	} 
	        
        	
        	isImageLoaded = function(img) {
			  if(typeof img.complete != 'undefined' && !img.complete) {
			    return false;
			  };
			  if(typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) {
			    return false;
			  };
			  return true;
			}
			
					
			function QueuesCompleteAll(){
/* 				console.log('All Done '); */
				if ($('.preload')) {
                   $('.preload').hide();
                }
                if ($('.preloader')) {
                   $('.preloader').hide();
                }
                $('#gallery').fadeIn(800);
                $("div#gallery").smoothDivScroll();

			}
			
			// When done say so.
			imagesQueue.onComplete = function()
			{
				QueuesCompleteAll();
			}
			
			
			
			//disable right click
			$(this).bind("contextmenu", function(e) {
                e.preventDefault();
            });
            
    /*
        if (Modernizr.audio) {
                var audio = $('audio').get(0);
                if(audio) {
                  $(audio).bind('play',function() {
                    $("#player").html('>');  
                  }).bind('pause ended', function() {
                    $("#player").html('<');
                  });  
                     
                  $("#player").click(function() {    
                    if (audio.paused) { audio.play(); }
                    else { audio.pause(); }    
                  });
            
                  $(function() {
                    audio.play();
                  });
              }
            }
*/
            
                        
            function loadcontent() {
                if ($('#content-wrapper').has('#gallery').size() > 0) {
                        images = new Array();
                        preloadImages();
                }          
            }
       
         /*
            function open_link(hash) {
                if (hash == '') return false;
                $('#content-wrapper').load('/' + hash + '.html', function() {
                    var navItem = hash.split('_');

                    //load gallery
                    loadcontent();
                    
                    //set active body class to change site color
                    $('body').removeAttr('class').addClass(navItem[0]);
                    //set new top right image
                    $('div.photo img').fadeOut(function() {
                        $('div.photo img').attr('src', '/photos/header/' + navItem[0] + '.png').load(function() {
                            $(this).fadeIn();
                        });
                    });
                    

                    //set active nav item
                    $('#nav li.active').delay(500).animate({ height: '28px', marginTop: '10px'}, 250, 'easeInSine');
                    $('#nav li.active').removeClass('active');
                    
                    $('#nav li.'+navItem[0]).not('.active').addClass('active');
                    $('#nav li.'+navItem[0]).delay(500).animate({ height: '35px', marginTop: '3px'}, 250, 'easeOutSine');
                

                });
            }
            
     
   $.history.init(open_link);
            $("#nav a, #header-nav a:not('.no-ajax'), .subnavigation a").live('click', function(e) {
                e.preventDefault();
                var href = $(this).attr('href').replace(/-/, '');
                var splitted = href.split('/');
                var hash = splitted[1];
                if (splitted[2] != '') {
                    hash += '_' + splitted[2];
                }
                $.history.load(hash);
                return false;
            });
*/
            
  		 			
	});


	$(window).load(function() {
/* 	   console.log("load event"); */
	   if (!window.location.hash) {

	   if ($('#content-wrapper').has('#gallery').size() > 0) {
	     
    		if (preloadImages()) {
                if ($('.preload')) {
                   $('.preload').hide();
                }
                if ($('.preloader')) {
                   $('.preloader').hide();
                }
                $('#gallery').fadeIn(800);
                $("div#gallery").smoothDivScroll();
            }
            
        }
        }				
	});
})(jQuery);
