/***************************************************************
This function give the food-navigation bar an accordian effect
***************************************************************/

function foodNavigation() {
	var self = this;
	
	self.init = function(navID) {
		$("#food .navigation ul").hide();
		$("#food .navigation > li > a").click(function(){
			self.openNav($(this).parent().attr('id'));
			//return false;
		});
		if(navID!=null){
			self.openNav(navID);
		}
	}
	self.openNav = function(navID){
		$("#food .navigation > li > a").removeClass("active");	
		$("#"+navID).children('a').addClass("active");
		
		if($("#"+navID).next().children('ul:hidden').length > 0) {		
			$(".navigation ul:visible").hide();
			$("#"+navID).next().children('ul').show();
		}
		
	}
}


/**************************************************************/

function scrollImages() {
	var self = this;
	self.speed = 500;
	self.scrollLeft = 30;
	self.scrollRight = -220;
	self.init = function() {
		if($("#memories").length > 0) {
			$("a.scroll_left").unbind("click").click(function() {
				$("#scroll_photos .scroll_content").animate({left: self.scrollLeft + "px"},self.speed);
				return false;
			});
			$("a.scroll_right").unbind("click").click(function() {
				$("#scroll_photos .scroll_content").animate({left: self.scrollRight + "px"},self.speed);
				return false;
			});
		}
	}
}
/***************************************************************
This function adds the offset striping to the slideout tables
allowing for changes without having to readjust inline classes.
***************************************************************/
function offsetRowsInTable() {
	var self = this;
	self.init = function() {
		if($(".slideout table").length > 0){
			$(".slideout table").each(function() {
				$(this).children("tbody").children("tr:gt(0):odd").addClass("offsetRow");
			});
		}
	}
}
/***************************************************************
This function adds the wonderfull rounded corners and icons into the
slideout panels, again, omitting the need to add the inline classes 
to the html markup.
**************************************************************/
function addCornersToSlideout() {
	var self = this;
	self.init = function() {
		if($(".slideout").length > 0){
			//$(".slideout").append("<span class='tlc'></span><span class='trc'></span><span class='blc'></span><span class='brc'></span>");
			$(".slideout").append("<span class='table-icons'></span>");
		}
	}
}
/***************************************************************
This function adds the wonderfull rounded corners to the yellow_box class
omitting the need to add the inline classes to the html markup.
***************************************************************/
function addCornersToYellowBox() {
	var self = this;
	self.init = function() {
		if($(".yellow_box").length > 0){
			$(".yellow_box").wrap("<div class='yellow_box_tlc'></div>");
			$(".yellow_box").wrap("<div class='yellow_box_trc'></div>");
			$(".yellow_box").wrap("<div class='yellow_box_blc'></div>");
			$(".yellow_box").wrap("<div class='yellow_box_brc'></div>");
			//$(".yellow_box").append("<span class='tlc'></span><span class='trc'></span><span class='blc'></span><span class='brc'></span>");
		}
	}
}
/***************************************************************
This function adds the wonderfull rounded corners and icons into the
popups in the Burger Family section, again, omitting the need to
add the inline classes to the html markup.
***************************************************************/
function addCornersToPopup() {
	var self = this;
	self.init = function() {
		if(($("#burger_family").length > 0) || ($("#memories").length > 0)){
			$(".popup").append("<div class='top'><span class='tlc'></span><span class='trc'></span></div><div class='bottom'><span class='blc'></span><span class='brc'></span></div>");
			$(".popup").append("<a href='#' class='table-icons'></a>");
		}
	}
}
/***************************************************************
This function adds the show popup and hide popup functionality to
the Burger Family buttons
***************************************************************/
function showPopup() {
	var self = this;
	self.id = "";
	self.init = function() {
		if($("#burger_family").length > 0) {
			$(".navigation a").each(function(){
				$(this).unbind("click").click(function(){
					self.id = ".pu_" + $(this).attr("id");
					$(".popup").fadeOut("fast");
					if($(self.id).length > 0) {
						self.revealPopup(self.id);
					}
					return false;
				});
			});
		}
	}
	self.revealPopup = function(passThis) {
		$(passThis).fadeIn("fast");
		$(passThis + " a").unbind("click").click(function() {
			$(".popup").fadeOut("fast");
		});
	}
}

/***************************************************************
This function adds the show popup and hide popup functionality 
coupled with the fade background for the Memories page
***************************************************************/
function showPopupFadeBg() {
	var self = this;
	self.id = "";
	self.contentWidth = $("#content").width();
	self.popupWidth = 0;
	self.popupLeft = -10;
	self.popupContentTop = 0;
	self.init = function() {
		if($("#memories").length > 0) {
			$("#content").append('<div class="popupBG"></div>');
			$("#memories .popupBG").css({'opacity' : 0,'z-index' : 0});
			$(".navigation a").each(function(){
				$(this).unbind("click").click(function(){
					self.id = ".pu_" + $(this).attr("id");
					$(".popup").fadeOut("fast");
					if($(self.id).length > 0) {
						self.popupHeight = $(self.id).height();
						self.popupContentHeight = $(self.id + " .popupContent").height();
						self.popupContentTop = (self.popupHeight - self.popupContentHeight)/2;
						$(".popup").css("left",self.popupLeft);
						$(".popupContent").css("padding-top",self.popupContentTop/2);
						$("#memories .popupBG").css({'z-index':3});
						$("#memories .popupBG").animate({opacity:.7},500);
						self.revealPopup(self.id);
						
					}
					return false;
				});
			});
		}
	}
	self.revealPopup = function(passThis) {
		$(passThis).fadeIn("fast");
		$("#memories .popupBG").click(function() {
			$(this).stop();
			$(this).animate({opacity:0},500);
			$(this).css({'z-index':0});
			$(".popup").fadeOut("fast");
		});
		$(passThis + " a").unbind("click").click(function() {
			$("#memories .popupBG").stop();
			$("#memories .popupBG").animate({opacity:0},500);
			$("#memories .popupBG").css({'z-index':0});
			$(".popup").fadeOut("fast");
		});
	}
}


/***************************************************************
This will add the open and close functionality of the slideout
panels found on the food item pages. ie. Teen Burger. The
Nutritional Information and Your Order slideouts. It will calculate
how much information is hidden within the slideout, and will slide
to reveal that content up to a maximum width. The maximum width will
prevent the slideout from sliding out too far.
***************************************************************/
function setUpSliderOpenClose() {
	var self = this;
	self.passThis = "";
	self.timer = 250;
	self.maxWidth = 728;
	self.openWidth = 300;
	self.openHeight = 390;
	self.closeWidth = 300;
	self.closeHeight = 30;
	self.tableWidth = 300;
	self.totalWidth = 0;
	self.scrollableContentRightPos = 0;
	self.newWidth = 0;
	self.itemsPerScreen = 1;
	self.startingLeft = 118;
	self.columnWidth = 112;
	self.needsToExpand = false;
	
	self.init = function(){
		//This controls wether the click of the Nutritional Information or Your Order button will open or close itself.
		//self.openHeight = ($('.slideout_content').length > 0) ? parseInt($('.slideout_content').height()) + 40 : self.openHeight;
		self.openWidth = parseInt($(".slideout").width())+20;
		self.tableWidth = parseInt($(".slideout").width());
		self.closeWidth = parseInt($(".slideout").width())+20;
		if($('#work_at_aw').length == 0){
			$('.yellow_box_tlc').css('width', self.closeWidth+'px');
		}
		
		
		$(".slideout h2 a").unbind("click").click(function(){
			self.passThis = $(this).parent().parent();
			if($(self.passThis).is(".open")) {
				self.closeIt(self.passThis);
			} else {
				self.openIt();
			}
			return false;
		});
		
		//setup a specific click for nutritional info
		$("#nutrition.slideout h2 a").unbind("click").click(function(){
			self.passThis = $(this).parent().parent();
			if($(self.passThis).is(".open")) {
				self.closeIt(self.passThis);
			} else {
				self.openAndExpand();
			}
			return false;
		});
		
		//This calculates based on the slider's content's width, how wide to make the slider. It will stop at a maximum width.
		$(".slideout table").each(function() {
			self.width = ($(this).width());
			//self.openWidth = self.openWidth + self.width;
			if(self.openWidth > self.maxWidth) {
				self.openWidth = self.maxWidth;
			}
		}); 
		if($(".slideout.opened").length > 0){
            self.passThis = $(".slideout.opened");
            self.openIt();
        }
	};
	//This is the function that actually controls the opening motion for the slider.
	self.openIt = function() {
		self.needsToExpand = false;
		$(".slideout.open").each(function(){
			self.closeIt($(this));
		});
		$(self.passThis).addClass("open");
		$(self.passThis).children(".table-icons").css("background-position","0 -16px");
		
		$(self.passThis).children('p').show();
		$(self.passThis).animate({height: self.openHeight + "px"}, {duration: self.timer, easing: 'easeInOutQuad'});
		
		//Remove when done testing
		//self.expandIt($(this));
		

		if($(self.passThis).attr('class').indexOf('expandable') > 0){
			$(self.passThis).append("<a class='table-icons-expand' href='#'>+</a>");
			$('a.table-icons-expand').unbind('click').click(function(){
				self.expandIt($(this));
				self.itemsPerScreen = 5;
				return false;
			});
		}
		self.setUpOrderScroller();
	};
	
	//This is the function that maximizes the slider.
	self.expandIt = function(linkObj){
		self.itemsPerScreen = 5;
		if(($(".scrollable_content").position().left-self.columnWidth) < (self.startingLeft +((self.itemsPerScreen-1) * self.columnWidth)-$(".scrollable_content").width())){
			$(".scrollable_content").animate({left: parseInt(self.startingLeft +(self.itemsPerScreen * self.columnWidth)-$(".scrollable_content").width())+"px"}, {duration: self.timer, easing: 'easeInOutQuad'});
			$('.scroll_right').hide();
		}
	
		$('#rightbar').animate({
			width: (self.maxWidth+20) + "px"
		}, {duration: self.timer, easing: 'easeInOutQuad'});
		
		$(self.passThis).parent().parent().parent().parent().css('width','auto');
		$(self.passThis).animate({
			width: self.maxWidth + "px"
		}, {duration: self.timer, easing: 'easeInOutQuad'});
		
	
		$(linkObj).replaceWith("<a class='table-icons-minimize' href='#'>+</a>");
		$('a.table-icons-minimize').unbind('click').click(function(){
			self.minimizeIt($(this));
			return false;
		});
	};
	
	//This is the function that minimizes the slider.
	self.minimizeIt = function(linkObj){
		self.itemsPerScreen = 1;
		if(($(".scrollable_content").position().left-self.columnWidth) < (self.startingLeft +((self.itemsPerScreen-1) * self.columnWidth)-$(".scrollable_content").width())){
			$('.scroll_right').hide();
		} else {
			$('.scroll_right').show();
		}
		
		$('#rightbar').animate({
			width: self.closeWidth+"px"
		}, {duration: self.timer, easing: 'easeInOutQuad'});
		$(self.passThis).animate({
			width: self.tableWidth + "px"
		}, {duration: self.timer, easing: 'easeInOutQuad'});
		
		$(linkObj).replaceWith("<a class='table-icons-expand' href='#'>+</a>");
		$('a.table-icons-expand').unbind('click').click(function(){
			self.expandIt($(this));
			return false;
		});
	};
	
	//This is the function that actually controls the closing motion for the slider.
	self.closeIt = function(passThis) {
		$(passThis).removeClass("open");
		$(passThis).children(".table-icons").css("background-position","0 0");
		$(passThis).children(".table-icons-expand").remove();
		$(passThis).children(".table-icons-minimize").remove();
		$(passThis).css("overflow","hidden");
		
		
		$(passThis).animate({height: self.closeHeight + "px" },
			'fast',
			function() {
				$('#rightbar').animate({
					width: self.closeWidth+"px"
				}, 'fast', function(){
					if($('#work_at_aw').length == 0){
						$(passThis).parent().parent().parent().parent().css('width', self.closeWidth+'px');
					}
				});
		});
		$(self.passThis).children('p').hide();
		$(passThis).css('overflow','hidden');
		$(passThis).animate({width: self.tableWidth + "px"}, 250);
		
		$(".table-icons-expand").remove();
	};
	
	self.setUpOrderScroller = function() {
		self.newWidth = 0;
		self.itemsPerScreen = 1;
		self.columnWidth = 112;
		//Position the content area
		$(".scrollable_content table").each(function() {
			self.width = $(this).width();
			self.newWidth = self.newWidth + self.width;
			$(".scrollable_content").width(self.newWidth);
		});
		$(".scrollable_content").css("left",$("#legend").width());
		self.startingLeft = $("#legend").width();
		
		$(".scroll_left").unbind('click').click(function() {
			if(($(".scrollable_content").position().left+self.columnWidth) <= self.startingLeft){
				if((parseInt($(".scrollable_content").position().left) - parseInt(self.startingLeft)) % self.columnWidth == 0){
					self.scrollContent(self.columnWidth);
					$(".scroll_right").show();
					if(($(".scrollable_content").position().left+self.columnWidth) == self.startingLeft){
						$(this).hide();
					}
				}
			} else {
				$(this).hide();
			}
			return false;
		});
		
		$(".scroll_right").unbind('click').click(function() {
			if(($(".scrollable_content").position().left-self.columnWidth) > (self.startingLeft +((self.itemsPerScreen-1) * self.columnWidth)-$(".scrollable_content").width())){
				if((parseInt($(".scrollable_content").position().left) - parseInt(self.startingLeft)) % self.columnWidth == 0){
					self.scrollContent(0-self.columnWidth);
					$(".scroll_left").show();
					if(($(".scrollable_content").position().left-(self.columnWidth*2)) == (self.startingLeft +((self.itemsPerScreen-1) * self.columnWidth)-$(".scrollable_content").width())){
						$(this).hide();
					}
				}
			} else {
				$(this).hide();
			}
			return false;
		});
		$('.scroll_right').show();
		$(".scroll_left").hide();
	};
	//Add functionality to the slide button
	self.scrollContent = function(addWidth) {
		newLeft = $(".scrollable_content").position().left+addWidth;
		$(".scrollable_content").animate({left: newLeft+"px"}, {duration: self.timer, easing: 'easeInOutQuad'});		
	};
	/********************************************************************
		SETUP NUTRITIONAL SCROLLER
	********************************************************************/
	
	self.openAndExpand = function(){
		self.needsToExpand = true;
		if($("#your_order.slideout.open").length > 0){
			$("#your_order.slideout.open").removeClass("open");
			$("#your_order.slideout").children(".table-icons").css("background-position","0 0");
			$("#your_order.slideout").children(".table-icons-expand").remove();
			$("#your_order.slideout").children(".table-icons-minimize").remove();
			
			
			$("#your_order.slideout").animate({height: self.closeHeight + "px" },
				'fast',
				function() {
					$("#your_order.slideout").parent().parent().parent().parent().css('width', self.closeWidth+'px');
			});
			$("#your_order.slideout").animate({width: self.tableWidth + "px"}, 250);
			
			$("#your_order .table-icons-expand").remove();
		}
		
		
		$(self.passThis).addClass("open");
		$(self.passThis).children(".table-icons").css("background-position","0 -16px");
		
		$(self.passThis).animate({height: self.openHeight + "px"}, {duration: self.timer, easing: 'easeInOutQuad'});
		
		$(self.passThis).animate({
			width: self.maxWidth + "px"
		}, {duration: self.timer, easing: 'easeInOutQuad'});
		$('#rightbar').animate({
			width: (self.maxWidth+20) + "px"
		}, {duration: self.timer, easing: 'easeInOutQuad'});
		$(self.passThis).parent().parent().parent().parent().css('width','auto');
		self.setUpNutScroller();
		
	}
	
	self.setUpNutScroller = function() {
		self.newWidth = 0;
		self.itemsPerScreen = 1;
		self.columnWidth = 110;
		//Position the content area
		$(".ingredients_content table").each(function() {
			self.width = $(this).width();
			self.newWidth = self.newWidth + self.width;
			$(".ingredients_content").width(self.newWidth);
		});
		$(".ingredients_content").css("left",0);
		self.startingLeft =  0;
		
		$(".scroll_nleft").unbind('click').click(function() {
			if(($(".ingredients_content").position().left+self.columnWidth) <= self.startingLeft){
				if((parseInt($(".ingredients_content").position().left) - parseInt(self.startingLeft)) % self.columnWidth == 0){
					self.scrollNutContent(self.columnWidth);
					$(".scroll_nright").show();
					if(($(".ingredients_content").position().left+self.columnWidth) == self.startingLeft){
						$(this).hide();
					}
				}
			} else {
				$(this).hide();
			}
			return false;
		});
		
		$(".scroll_nright").unbind('click').click(function() {
			if(($(".ingredients_content").position().left-self.columnWidth) > (self.startingLeft +((self.itemsPerScreen-1) * self.columnWidth)-$(".ingredients_content").width())){
				if((parseInt($(".ingredients_content").position().left) - parseInt(self.startingLeft)) % self.columnWidth == 0){
					self.scrollNutContent(0-self.columnWidth);
					$(".scroll_nleft").show();
					if(($(".ingredients_content").position().left-(self.columnWidth*2)) == (self.startingLeft +((self.itemsPerScreen-1) * self.columnWidth)-$(".ingredients_content").width())){
						$(this).hide();
					}
				}
			} else {
				$(this).hide();
			}
			return false;
		});
		$('.scroll_nright').show();
		$(".scroll_nleft").hide();
	};
	//Add functionality to the slide button
	self.scrollNutContent = function(addWidth) {
		newLeft = $(".ingredients_content").position().left+addWidth;
		$(".ingredients_content").animate({left: newLeft+"px"}, {duration: self.timer, easing: 'easeInOutQuad'});		
	};
}

/***************************************************************
This will apply AJAX in order to retrieve any required "popup"
content.
***************************************************************/
function AjaxNav(nav_id, content_id){
	var self = this;

	self.container_div = nav_id;
	self.content_div = (content_id) ? content_id : 'content';
	
	self.setup_nav = function(){
		$("#"+self.container_div+" a").each(function(){
			$(this).unbind('click').click(function(){
				self.replace_content($(this));
				return false;
			});
		});
	}
	
	self.replace_content = function(link_obj) {
		$.post($(link_obj).attr('href'),'&dyn=1',function(data){
			if(data.length > 0){
				$("#"+self.content_div).html(data);
				$('.active').removeClass('active');
				$(link_obj).addClass('active');		
				mediaObj = new MediaObj();
				
				mainNavObj = new AjaxNav('content');
			}
		});
	}
	
	self.setup_nav();
}

/***************************************************************
This will apply the updated styling on the various forms
***************************************************************/
function prettyInputs() {
	var self = this;
	
	self.init = function(){
		$("input[type='text']").each(function(){
			$(this).wrap("<div class='prettyInput'></div>");
			var new_width = parseInt($(this).width());
			$(this).parent('div.prettyInput').css('width',(new_width+32)+"px");
			$(this).addClass('pretty');
			$(this).wrap("<span class='linput'></span>");
			$(this).wrap("<span class='rinput'></span>");
		});
	}
	
	self.init();
}


/* FORM HELPER
*************************************************************/

function formHelper() {
	var self = this;
	self.radio_height = "27px";
	self.radio_width = "30px";
	self.radio_padding_top = "15px";
	self.background_box = "/_img/food/checkbox.gif";
	self.background_pos_start = "0 0";
	self.background_pos_end = "0 -122px";
	self.cursor = "pointer";
	
	self.init = function(){
		if($("input[type='radio']").length > 0){
			self.init_radio();
		}		
	}
		
	self.reset_radios = function (n){
		$("input[type='radio'][name='"+ n +"']").each(function(){
			$(this).parent().css('backgroundPosition', self.background_pos_start);
		});
	}
		
	self.init_radio = function() {
		//hide the radio buttons
		$("input[type='radio']").hide();
		
		//style up the labels
		$("input[type='radio']").parent().css('paddingTop', self.radio_padding_top);
		$("input[type='radio']").parent().css('cursor', self.cursor);
		$("input[type='radio']").parent().css('height', self.radio_height);
		$("input[type='radio']").parent().css('width', self.radio_width);
		$("input[type='radio']").parent().css('background', 'url(' + self.background_box + ') ' + self.background_pos_start + ' no-repeat');
		
		if($("input[type='radio']:checked").length > 0){
			$("input[type='radio']:checked").parent().css('backgroundPosition', self.background_pos_end);
		}
		
		//hijack the click event
		$("input[type='radio']").parent().unbind("click");
		$("input[type='radio']").parent().click(function() {		
			//figure out which background position to use
			var tmp_pos = ($(this).css('backgroundPosition') == self.background_pos_end) ? self.background_pos_start : self.background_pos_end;
			
			//setup the radio button to be checked
			if($(this).children('input:checked').length == 0){
				self.reset_radios($(this).children("input[type='radio']:eq(0)").attr('name')); //reset only the radio buttons that have the same name as the one that was clicked.
				
				$(this).children('input').attr('checked','checked');
				//setup the new background for the parent label
				$(this).css('background', 'url(' + self.background_box + ') ' + tmp_pos + ' no-repeat');
				
			}
			return false;
		});
	}
	
	self.init();
}

$(document).ready(function() {

	var addCorners = new addCornersToSlideout();
	addCorners.init();
	
	var addCornersYellow = new addCornersToYellowBox();
	addCornersYellow.init();
	
	var offsetRows = new offsetRowsInTable();
	offsetRows.init();
	
	var popupCorners = new addCornersToPopup();
	popupCorners.init();
	
	var popup = new showPopup();
	popup.init();
	
	var popupFade = new showPopupFadeBg();
	popupFade.init();
	
	var scrollImage = new scrollImages();
	scrollImage.init();
	
	var foodMenu = new foodNavigation();
	if($('#content.burgers').length > 0){
		foodMenu.init('burgers');
	} else if($('#content.rest_opps').length > 0){
		$('#applyrest a').addClass('active');	
	} else if($('#content.corp_opps').length > 0){
		$('#corpopp a').addClass('active');	
	} else {
		foodMenu.init();
	}

	var sliderOpenClose = new setUpSliderOpenClose();
	sliderOpenClose.init();
	
	if($("#food_options").length > 0) {
		var formHelp = new formHelper();
	}
});
