/*
  Necessary varialbe set on the page itself...
  var navigationTrackSize
  var dishArray//dishIDs seperated by a space
  var currentRestaurantID 
  var currentDishPositionInNav //set to first position in dishArray
*/

function scrollDishNav(direction){
    // 1 = forward;  -1 = backwards
	
    scrollDistance = $('foodNavigationTrack').getWidth();
	
    currentScrollPosition = $('foodNavigationTrack').scrollLeft;
	
    if (direction > 0){
        $('foodNavigationTrack').scrollLeft = currentScrollPosition + scrollDistance;
    }else if (direction < 0){
        $('foodNavigationTrack').scrollLeft = currentScrollPosition - scrollDistance;
    }
	
}


function nextDish(){

    nextDishPosition = currentDishPositionInNav + 1;
	
    if (nextDishPosition != dishArray.length){
        /*
          1. Make sure next dish is not past the last dish
          2. Make sure nextDishPosition != 1 because the Modulus operator will return 1 in that case, 
          but that is the exception as we will use it to test for = 1 wich means on to the next set
          3. Use the Modulus operator to see if we need to scroll to the previous 6 to view selected dish
        */
        if ((nextDishPosition != 1) & ((nextDishPosition+1) % navigationTrackSize == 1)){scrollDishNav(1);}
		
        changeDish(dishArray[nextDishPosition],currentRestaurantID);
		
    }

}

function previousDish(){

    previousDishPosition = currentDishPositionInNav - 1;

    if (previousDishPosition >= 0) {
        /*
          1.make sure it's not the first dish, b/c you can't go back further than that
          2. then use the Modulus operator to see if we need to scroll to the previous 6 to view selected dish
        */
        if (((previousDishPosition) % navigationTrackSize == 5)){scrollDishNav(-1);}
		
        changeDish(dishArray[previousDishPosition],currentRestaurantID);

    }
	
}

function manageDishNavigation(dishID){
	
    currentDishPositionInNav = dishArray.indexOf(dishID);
	
    /*hide foodNavigationArrowBackLink if on first dish, and make sure it is shown otherwise*/
    if(currentDishPositionInNav == 0){
        $('foodNavigationArrowBackLink').hide();
    }else if($('foodNavigationArrowBackLink').visible() == false){
        $('foodNavigationArrowBackLink').show();
    }

    /*hide foodNavigationArrowForwardLink if on last dish, and make sure it is shown otherwise*/
    if(currentDishPositionInNav == (dishArray.length - 1)){
        $('foodNavigationArrowForwardLink').hide();
    }else if($('foodNavigationArrowForwardLink').visible() == false){
        $('foodNavigationArrowForwardLink').show();
    }	
	
}


function changeDish(dishID, restID){
	
    manageDishNavigation(dishID);

    /*
      Set background to loading animation
    */
    $('mainColumnGalleryArea').setStyle({
            backgroundImage: 'url(/static/portfolio/img/loading-gif-animation.gif)'
                });

    /* DISH UPDATE TESTING CODE: for local testing only
       $('mainColumnGalleryArea').setStyle({
       backgroundImage: 'url(static/common/img/FPO/FPO-portfolio_dish' + dishID + '.jpg)'
       });
    */
	
    /* DISH UPDATE LIVE SITE CODE: comment out for local testing */

    /* Update everything at once with single AJAX call that returns JSON
       serialization of Dish object. */

    new Ajax.Request('/ajax/restaurant/' + restID + '/dish/' + dishID + '/', {
            method:'get',
                onSuccess: function(transport) {
				
				//alert(transport.responseText);
                var response = transport.responseText.evalJSON();

                $('foodDescription').update(response['description']);
                $('foodTitle').update(response['title']);
                $('foodChefs').update(response['chefs']);
				
				$('mainColumnGalleryArea').setStyle({
                        backgroundImage: 'url('+ response['image'] +')'
                    });
            }
    });
	
	/*BEGIN - Google Analytics Ajax Tracking*/
	pageTracker._trackPageview('/ajax/dish_view/' + currentRestaurantSlug + '/' + dishID);
	/*END - Google Analytics Ajax Tracking*/
	
    /*updates nav to highlight current dish*/
    $('foodNavigation').select('div').each(function(item){
            if ($(item).hasClassName('navElementCurrent')){
                $(item).removeClassName('navElementCurrent');
            }
            if ($(item).identify() == ('foodNavigation-dish' + currentDishPositionInNav) ){	
                $(item).addClassName('navElementCurrent');
            }
	})
	
}