$(function() {

    $(".home li.home").removeClass("home").addClass("current_page_item");

            
    var $mainContent = $("#content"),
        URL = '',
        siteURL = "http://lot30restaurant.ca/",
        $internalLinks = $("a[href^='"+siteURL+"']"),
        hash = window.location.hash,
        $ajaxSpinner = $("#ajax-loader"),
        $el, $allLinks = $("a");
        
    
    function hashizeLinks() {
        $("#access a[href^='"+siteURL+"']").each(function() {
            $el = $(this);
            
            //temp test dir fix
            var pathtokens = this.pathname.split("/");
            
            if ($el.hasClass("no_hash")) {
             // do nothing
            }else{
            
	            if ($.browser.msie) {
	            
	            	var newpath =  pathtokens[0] + "/";
	            	if (newpath == "/") {

	             		var new_a = $el.attr("href", "#" + "/home/").attr("rel", "internal");           	
	            	}else{
	                	$el.attr("href", "#/" + newpath)	//replace this.pathname with newpath
	                	.attr("rel", "internal");
	            	}
	            
	            }else{
	            	var newpath = "/" + pathtokens[1] + "/";
	            
	            	if (newpath == "//") {
	             		var new_a = $el.attr("href", "#" + "/home/").attr("rel", "internal"); 
	            	}else{            
	            
	             		$el.attr("href", "#" + newpath)
	                	.attr("rel", "internal");
	                }
	            
	            }   
            
            }         

        });
    };
    
    hashizeLinks();  
        
    $("a[rel='internal']").live("click", function() {
        $ajaxSpinner.fadeIn();
        $mainContent.animate({ opacity: "0.1" });
        $el = $(this);
        $(".current_page_item").removeClass("current_page_item");
        $allLinks.removeClass("current_link");
        URL = $el.attr("href").substring(2);
        URL = URL + "#page_content .page_copy";
        $mainContent.load(URL, function() {
            $el.addClass("current_link").parent().addClass("current_page_item");
            $ajaxSpinner.fadeOut();
            $mainContent.animate({ opacity: "1" });
            hashizeLinks();   
			
			$('.scroll-pane').jScrollPane({verticalDragMinHeight: 70, verticalDragMaxHeight: 70 });
			Cufon.replace('h1'); // Works without a selector engine
			Cufon.replace('h2'); // Works without a selector engine
			Cufon.replace('h3'); // Works without a selector engine
			Cufon.replace('.heading_font'); // Requires a selector engine for IE 6-7, see above
			Cufon.replace('.nice_letter'); // Requires a selector engine for IE 6-7, see above
			
			// GALLERY JS 
			gallerySwap();			
        });
    });
    
    if ((hash) && (hash != "#/")) {
        $("a[href*='"+hash+"']").trigger("click");
    }

	// GALLERY JS  (see this link for a great pre-loading tutorial http://jqueryfordesigners.com/image-loading/ )
	function gallerySwap() {	
		$("a.gallery_thumb").click(function(){
		
				var largePath = $(this).attr("href");
				var largeAlt = $(this).attr("title");
  	
			  	//show the spinner thingy
		 		$("#ajax-loader").css("left", "38%").show();
		 		
		 		  // wrap our new image in jQuery, then:
				  $("#image_box img").hide()
				  
				    // once the image has loaded (from the attr src below), execute this code
				    .load(function () {
				     
					     $(this).fadeIn();
					     		  
						//hide the spinner thingy
				 		$("#ajax-loader").hide();
				     
				    })
				    
				    // *finally*, set the src attribute of the new image to our image
				    .attr({ src: largePath, alt: largeAlt, width: null, height: null });
		
				return false;
		});
	}
	
	gallerySwap();

});
