// Add the capitalize method to the String object . . .

String.prototype.capitalize = function(){
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.slice(1).toLowerCase();
    });
};



function showMenu(course) {
	var filename  = course+".xml";
	var gifname   = course+".gif";
	
	var result="";
    $("#menu").html(""); 
	// APPEND menu header gif . . .
	
	$("#menu").append("<div style='text-align: center; width: 320px;'><img src='images/"+gifname+"'></div>"); 
    
    $.ajax({
    	url: filename,
    	type: 'GET',
    	dataType: 'xml',
    	timeout: 2000,
    	error: function(){   alert('Error loading XML document');  },
    	
    // START XML PARSING  //////////////////////////////////////////////////////////
    	success: function(xml){   // do something with xml
    	
   		//	if success loop through the sections headers for this menu 
   		//  (ie. "appetizers", "soups", "salads" and "main courses" for the 
   		//  "dinner" menu
        	var result ='';
            		
        $(xml).find('section').each(function(){
       		var id_text = $(this).attr('id');
       		
        	// if the section is named 'main' basically it means no header is necessary . . .
        	
        	if (id_text !='main') result=result+"<div class='section'>"+id_text.toUpperCase()+"</div>";
        	
            		$(this).find('item').each(function() {
                		var item_name     = $(this).find('name').text();
                		var item_price    = $(this).find('price').text();
                		var item_comment  = $(this).find('comment').text();
                	result	= result+"<div class='dish'>"+item_name+"</div><div class='price'>"+item_price+
                	"</div><div class='comment'>"+item_comment+"</div><br/>";
            			});  
	               	result	= result+"<hr />";	  
        		});
                		 		$("#menu").append(result); 
    		},    
    // END XML PARSING  //////////////////////////////////////////////////////////    	
    
        complete: function(){ 
              	    $('#menu').fadeIn("slow");
        }   
    });  // end $.ajax method
    
    
    return result;
}	// end show menu function
    


$(document).ready(function(){
    
        load_rotator();

        $('#content2').load('home.php');
   	    $('#menu').hide("fast");

	    //Click events in the nav menu make clicked link active . . .
	    
		$('.navmenu li a').click(function() {
		    var $this = $(this);
   		    $this.parent().siblings().removeClass('current_page_item');
   		    $this.parent().addClass('current_page_item');
    	    $('#content2').fadeOut("slow");
    	    $('#menu').fadeOut("fast");
 
		    return false;
		});

	    //Define linkhome click event . . .

		$('#linkhome').click(function() {
    	    $('#content2').load('home.php');
    	    $('#content2').fadeIn("slow");
    	    return false;
		});

		
		$('#linkbrunch').click(function() {
			$('#menu').fadeOut("slow", function(){showMenu("brunch")});
     
    	    return false;
		});
		
		$('#linkpromos').click(function() {
    	    $('#content2').load('promos.php');
       	    $('#content2').fadeIn("slow");
    	    return false;
		});
	    
		$('#linkdinner').click(function() {
			var select = ['tapas'];
			$('#menu').fadeOut("slow", function(){showMenu("dinner")});
       	    $('#menu').fadeIn("slow");
    	    return false;
		});
		
		$('#linktapas').click(function() {
			var select = ['tapas'];
			$('#menu').fadeOut("slow", function(){showMenu("tapas")});
       	    $('#menu').fadeIn("slow");
    	    return false;
		});
	});
    function load_rotator() {
	    //script to place image rotator in the "container" div
		var s1 = new SWFObject("imagerotator.swf","rotator","290","185","7");
		s1.addParam("allowfullscreen","true");
		s1.addVariable("file","xml/food.xml");
		s1.addVariable("width","290");
		s1.addVariable("height","185");
		s1.write("container");
    }
