var images = new Object;
var subHideTimer;
var isLoaded = false;

function flipImage(base, dir, dot, autoflip) {
	
	if(images[base + "_max"] > 0 ) {
		
		images[base + "_last"]=images[base + "_current"];
		
		switch(dir) {	
		
			case 'dot':
				images[base + "_current"] = dot;		
			break;
			
			case 'left':
				images[base + "_current"]--;
			break;
			
			case 'right':
				images[base + "_current"]++;
			break;
			
		}
			
		if(images[base + "_current"] < 0) 
			images[base + "_current"] = images[base + "_max"];
			
		if(images[base + "_current"] > images[base + "_max"]) 
			images[base + "_current"] = 0;
				
		for(i=0;i<=images[base + "_max"];i++) {
			if (images[base + "_current"] == i) {
				$(base + '_Dot_' + i).setAttribute('class', 'flipTransDot');			
			} else {
				$(base + '_Dot_' + i).setAttribute('class', 'flipTransDot trans');
			}			
		}
	}
	
	if(images[base + "_last"] != images[base + "_current"])
	Effect.Fade($(base + '_Hidden_' + images[base + "_last"]));
	Effect.Appear($(base + '_Hidden_' + images[base + "_current"]));
	
	if(images[base + "_isLanding"]) {
		$(base + '_Text_' + images[base + "_last"]).style.display = 'none';
		$(base + '_Text_' + images[base + "_current"]).style.display = 'block';
	}
	
	if(autoflip != true) {
		images[base + "_isRotating"] = false;
		clearTimeout(images[base + "_timer"]);
	}
		
	if(images[base + "_isRotating"] == true) {
		//images[base + "_timer"] = setTimeout("flipImage('" + base + "', 'right', 0, true)", 7500);
		images[base + "_timer"] = setTimeout("flipImage('" + base + "', 'right', 0, true)", (images[base + "_flipSpeed"] * 1000));
	} 
}


/*
**  STARTUP METHODS
*/
//jQuery.noConflict();

//jQuery(document).ready(function() {
Event.observe(window, 'load', function() {
	allSubNavDivs = $$('div.subNavSet');		
	allNavLinks = $$('a.navLink');
	Custom.init();	
	isLoaded = true;
	
	$$('.wheretobuymap a').each(function(elmt) {	 
		elmt.observe('mouseover', function(ev) {	 
			ev.target.className = 'On';
			//alert('test');
		});
		
		elmt.observe('mouseout', function(ev) {	
			ev.target.className = ''; 
		});
	});
	
	$$('a.navLink').each(function(elmt) {	 
		elmt.observe('mouseover', function(ev) {
			//alert(ev.target.id);
			//alert('navLink_' + currentSection);
			if(ev.target.id != 'navLink_' + currentSection) { 
				ev.target.className = 'navLink Over'; 
			}
		});
		
		elmt.observe('mouseout', function(ev) {	
			if(ev.target.id != 'navLink_' + currentSection) {
				ev.target.className = 'navLink'; 
			} else {
				ev.target.className = 'navLink On'; 
			}
		});
	});
	
	$$('a.subNavLink').each(function(elmt) {	
			//alert(elmt.id); 
		elmt.observe('mouseover', function(ev) { 
			ev.target.className = 'subNavLink On'; 
		});
		
		elmt.observe('mouseout', function(event) {
			if((event.target.id != 'subNav_' + currentSection + '_' + currentSubSection) && (event.target.id != 'subNav_auto_' + currentSubSection)) {
				event.target.className = 'subNavLink'; 
			} else {
				event.target.className = 'subNavLink On'; 
			}
		});
	});
	
	if(currentSection != '') {
		higlightCurrentPage();
	}
	
	
	
});

/*
**  Flip Functions
*/	


var TextFlip = Class.create({	
	
	dotClick: function(event) {
		this.currentItem = arguments[0];
		this.setTransDot();				
	},
	
	nextClick: function(event) {
		this.currentItem == this.itemCount ? this.currentItem = 0 : this.currentItem+=1;		
		this.setTransDot();
	},
	
	backClick: function(event) {
		this.currentItem == 0 ? this.currentItem = this.itemCount : this.currentItem-=1;
		this.setTransDot();
	},	
		
	setTransDot: function(event) {
		for(i=0;i<this.flipList.length;i++) {
			
			if (this.currentItem == i) {
				this.flipNav.select('img#flipDot_' + i)[0].setAttribute('class', 'flipTransDot');			
			} else {
				this.flipNav.select('img#flipDot_' + i)[0].setAttribute('class', 'flipTransDot trans');
			}
			
		}
		
		this.flipText();
		
	}, 
	
	flipText: function(event) {
		//this.mainDiv.innerHTML = "<h2>" + this.flipList[this.currentItem].title + "</h2>" + this.flipList[this.currentItem].text;	
		this.mainDiv.innerHTML = this.flipList[this.currentItem].text;// + this.flipList[this.currentItem].text;	
		Cufon.replace('h2', { fontFamily: 'gothamNarrowLight' });
		//Cufon.replace('.gothamText', { fontFamily: 'gotham-narrow' });	
		
	},
	
	initialize: function (flipContainerDiv, flipList) {
	
		this.currentItem = 0;	
		this.flipList = flipList;
		this.itemCount = flipList.length-1;	
		
						
		this.mainDiv = $(flipContainerDiv).select('[class="textFlipContent"]')[0];
				
		this.flipNav = $(flipContainerDiv).select('[class="flipTransParent"]')[0];	
		flipArrowParent = this.flipNav;	
		//flipArrowParent = this.flipNav.select('[class="flipTransParent"]')[0];

		
		var flipLeftArrow = flipArrowParent.select('[class="flipTransLeftArrow trans"]')[0];
		var flipRightArrow = flipArrowParent.select('[class="flipTransRightArrow trans"]')[0];	
		for(i=0; i<this.flipList.length; i++) {
			var currentDot=document.createElement("img");
			
			currentDot.setAttribute('id', 'flipDot_' + i);
			currentDot.setAttribute('src', 'images/flip_dot_on.png');
			currentDot.setAttribute('alt', '*');				
			currentDot.observe('click', this.dotClick.bind(this, i));
			flipArrowParent.insertBefore(currentDot, flipRightArrow);
		}
			
		this.setTransDot();
		flipRightArrow.observe('click', this.nextClick.bind(this));
		flipLeftArrow.observe('click',  this.backClick.bind(this));
		
		this.flipText();
	}
	
});

function FlipItem(image, title, url, text) {
	this.image = image;
	this.title = title;
	this.url = url;
	this.text = text;
}

/*
*  Carousel Functions
*/

function Carousel(carouselContainerDiv, carouselCols, carouselImageWidth, carouselItems) {
	var carouselLeftButton;
	var carouselRightButton;
	var carouselContentDiv;
	var carouselWidth = carouselImageWidth * carouselCols;
	var carouselItemWidth = carouselImageWidth*(carouselItems-carouselCols);
	var carouselAtEnd = false;
	var dragCount = 0;	
	var dragLinked = true;
	
	Event.observe(window, 'load', function() {
		//carouselParentDiv = $(carouselContainerDiv).select('[class="carouselParent"]')[0];
		//if (!carouselParentDiv) {
		carouselParentDiv = $(carouselContainerDiv).select('[class="carouselParent carouselParentHome"]')[0];
		//}
		carouselContentDiv = $(carouselContainerDiv).select('[class="carouselBody"]')[0];
		
		carouselContentDiv.setStyle({
			width: (carouselImageWidth*carouselItems) + 'px'
		});
	
		carouselLeftButton=$('carouselLeft');
		carouselRightButton=$('carouselRight');
		
		setCarouselButtons();
		
		new Draggable(carouselContentDiv, { constraint:'horizontal', zindex: 5, onStart: onDragStart, onDrag:onDragDrag, onEnd: onDragEnd, snap:
		
				function(x,y,draggable) {
					function constrain(n, lower, upper) {
						carouselAtEnd = false;
						var moveSnap = Math.round(n/carouselImageWidth);
						var n=moveSnap*carouselImageWidth;
						
						if (n > upper){
							return upper;
						}else if (n < lower){
							carouselAtEnd = true;
							return lower;
						}else{
							return n;
						}
					}
					
					return[ constrain(x, -carouselItemWidth, 0) ];              
				}
		});
	});
	
	function onDragStart() {
		dragCount=0;
	}
	 
	
	function onDragDrag() {
		dragCount+=1;
		if (dragLinked==true && dragCount>5) {
			jQuery(".carouselLink").replaceWith(function(){
				return '<span class="' + jQuery(this).attr('class') + '" href="' + jQuery(this).attr('href') + '">' +  jQuery(this).html() + "</span>";			
			});
			dragLinked=false;
		}
		
	}
	
	function onDragEnd(obj) {
		setCarouselButtons(obj.element);
		if (dragLinked==false) {
			jQuery(".carouselLink").replaceWith(function(){
				return '<a class="' + jQuery(this).attr('class') + '" href="' + jQuery(this).attr('href') + '">' +  jQuery(this).html() + "</a>";					
			});
			dragLinked=true;
		}
	}
	
	function setCarouselButtons() {	
		if (carouselContentDiv.getStyle('left') !=  '0px') {
			// IF WE'RE NOT AT THE FIRST ITEM ON THE LIST WE SHOULD ENABLE THE 'BACK' BUTTON
			carouselLeftButton.appear({ duration: .5 });		
			carouselLeftButton.observe('click', function(event) { scrollCarousel(1) }); 
			
		} else {
			// WE'RE AT THE FIRST ITEM ON THE LIST SO WE SHOULD DISABLE THE 'BACK' BUTTON
			carouselLeftButton.stopObserving('click');
			carouselLeftButton.fade({ duration: .5 });		
		}
		
		if (carouselAtEnd == true ) {
			// WE'RE AT THE LAST ITEM ON THE LIST SO WE SHOULD DISABLE THE 'NEXT' BUTTON
			carouselRightButton.stopObserving('click'); 
			carouselRightButton.fade({ duration: .5 });
			
		}  else {
			// WE'RE NOT AT THE LAST ITEM ON THE LIST SO WE SHOULD ENABLE THE 'NEXT' BUTTON
			carouselRightButton.appear({ duration: .5 });
			carouselRightButton.observe('click', function(event) { scrollCarousel(-1) }); 
		}
		
	}
	
	function scrollCarousel(dir) {		
		carouselAtEnd = false;
		carouselLeftButton.stopObserving('click');
		carouselRightButton.stopObserving('click'); 
		
		new Effect.Move(carouselContentDiv, {x: (carouselImageWidth*dir), y: 0, duration:0.5, afterFinish: OnCarouselFinish}); 
				
		if ((-carouselItemWidth+carouselImageWidth) + "px" == carouselContentDiv.getStyle('left')) {
			carouselAtEnd = true;
		}
		return false;
	}
}

/*
*  Menu Functions
*/




function higlightCurrentPage() {
	
	if(currentSection && currentSection!='gift_cards') {
		$('navLink_' + currentSection).className = 'navLink On';
		$('subNav_' + currentSection).style.visibility = 'visible';
	}
}

var tmpNavName = '';

function toggleSubMenu(name, dir) {	
	if(isLoaded) {
		switch(dir) {
			case 1:
				stopSubHideTimer();
				tmpNavName = name;
				highlightSubMenuTimer = setTimeout(highlightSubMenu, 300);
				break;
			default:
				if(window.highlightSubMenuTimer)clearTimeout(highlightSubMenuTimer);
				startSubHideTimer();
			break;
		}
	}
}

function hideSubMenus() {
	for(i = 0; i < allSubNavDivs.length; i++) {
		allSubNavDivs[i].style.visibility = 'hidden';
	}

	higlightCurrentPage();	
}

function highlightSubMenu() {
	hideSubMenus();
	$('subNav_' + tmpNavName).style.visibility = 'visible';
	if(tmpNavName != currentSection && currentSection!='gift_cards') {
		$('subNav_' + currentSection).style.visibility = 'hidden';				
	}
}

function startSubHideTimer() {
	subHideTimer = setTimeout(hideSubMenus, 1500);
}

function stopSubHideTimer() {
	clearTimeout(subHideTimer);
}
