jQuery(document).ready(function($){

	$.ajaxSetup({ cache: false });
	
    var $container 		= $("#container");
    var $content		= $("#content");
    var $ajaxLoader 	= $("#ajax_loader");
    var containerStatus	= "closed";	
    var siteUrl 		= "http://" + top.location.host.toString();
    var url 			= '';
    var newHash 		= '';
    
    $(document).delegate("a[href^='"+siteUrl+"']:not([href*=/wp-content/uploads/]):not([href*=/wp-admin/]):not([href*=/wp-login.php]):not([href$=/feed/])", "click", function() { 

        window.location.hash = $(this).attr("href").substring(23);

        return false; 

    });
    
	$(window).bind('hashchange', function(){
	
		var conceptUrl = "http://www.mixtbaby.com/?page_id=9";
		var galleryUrl = "http://www.mixtbaby.com/?page_id=6";
		
		url = siteUrl + window.location.hash.substring(1);
		
        if (!url) { 
            return; 
        } 
        
        newHash = url + " #content";
        
        var indexUrl = 'http://www.mixtbaby.com';
        
        if (indexUrl != url){
        
	        $content.fadeOut(500, function(){
	        	
	        	$container.animate({width:0}, 500, function(){
					
					$ajaxLoader.fadeIn(500, function(){
					
						$content.hide().load(newHash, function(){
						
							$ajaxLoader.fadeOut (500, function(){
							
								$container.animate({width:660}, 500, function(){
								
									$content.fadeIn(500, function(){
																				
										menuHighlight();
										sbInit();
										
										$("#close_container").slideDown(300, function(){
											
											$("#close_container a").fadeIn(300);
											containerStatus = "open";
										
										});
										
										if (conceptUrl == url){
											
											setInterval(conceptBannerSwitch, 10000);
										
										}

										if (galleryUrl == url){
												
											flickrManager();
											vimeoManager();
										
										}										
									
									});
								
								});
							
							});
							
						});
					
					});
	
	        	});       
	        
	        });
	        
		}	        
            
	});
	
	$(window).trigger('hashchange');
	
	$("#menu_info a.logo, #close_container a").click(function(){
	
		if (containerStatus == "open") {
			
			$content.fadeOut(500, function(){
			
				$container.animate({width: 0}, 500, function(){
					
					$("ul#nav li a").removeClass("active");
				
					$("#close_container a").fadeOut(300, function(){
					
						$("#close_container").slideUp(300);
					
					});
				
				});
			
			});
			
			containerStatus = "closed";
		
		} else {
		
			location.reload();
		
		}
	
	});

	function menuHighlight() {
		
		$("ul#nav li a").removeClass("active");
		$("ul#nav li a[href=" + url + "]").addClass("active");
			
	}	

	function sbInit(){
	
		Shadowbox.init({
		
			skipSetup: true
		    		
		});
		
	    Shadowbox.setup('a.vimeo_video', {
	    	
    		gallery:	"MB_VIDS",
    	    height:     360,
        	width:      640,
	        flashVars:  {
        	    autoplay:   "1"
	        }
    	});

	    Shadowbox.setup('a.flickr_image', {
	    	
    		continuous: true, 
    		displayCounter: false
    		
    	}); 
    	
	    Shadowbox.setup('a.google_maps_link', {
	    	
    		animate: true
    		
    	});     	   	
	
	}
		
	function conceptBannerSwitch() {
		
	    var active = $("#concept_banner img.active");
	
	    if ( active.length == 0 ) { 
	    
	    	active = $("#concept_banner img:last");
	    
	    }
	
	    var next =  active.next().length ? active.next() : $("#concept_banner img:first");
	    var divWidth = $("#concept_banner").width();
	
	    active.addClass('last-active');
	
	    next.css({opacity: 0.0})
	        .addClass('active')
	        .animate({opacity: 1.0}, 1000, function() {
	            active.removeClass('active last-active');
	        });
	        
	}

	function flickrManager(){
		    
	    var set 		= "72157625882244897";
		var apiKey 		= "b18a8852ae1968f0d847c0171d411d7f";

		$.ajaxSetup({ cache: false });
		
		$.getJSON('http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + apiKey + '&photoset_id=' + set + '&page=1&per_page=100&format=json&jsoncallback=?', function(data){
						
			var htmlString = "";
			
			$.each(data.photoset.photo, function(i, item){
				
				htmlString += '<a class="flickr_image" rel="shadowbox[MB]" href="http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_b.jpg">';					
				htmlString += '<img src="http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_s.jpg" alt=""/>';
				htmlString += '</a>';
				
			});
			
			$("#flickr_container").html(htmlString);
			
			sbInit();
	
		});	
		
	}
	
	function vimeoManager(){

		var vimeoUserName 	= 'user5977053';
		var videosUrl 		= 'http://vimeo.com/api/v2/' + vimeoUserName + '/videos.json?callback=?';
		
		$.ajaxSetup({ cache: false });
		
		$.getJSON(videosUrl, function(data){	
		
			var htmlString = "";
			
			$.each(data, function(i, item){
			
				htmlString += '<a class="vimeo_video" href="http://vimeo.com/moogaloop_local.swf?clip_id='+item.id+'&fullscreen=1" title="'+item.title+'">';
				htmlString += '<img src="'+item.thumbnail_small+'" alt="" /><p>'+item.title+'</p>';
				htmlString += '</a>';
			
			});
			
			$("#vimeo_container").html(htmlString);
			
			sbInit();
				
		});
	
	}			       

});
