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) {
				jQuery('#' + base + '_Dot_' + i).attr('class', 'flipTransDot');			
			} else {
				jQuery('#' + base + '_Dot_' + i).attr('class', 'flipTransDot trans');
			}			
		}
	}
	
	if(images[base + "_last"] != images[base + "_current"]) {
		jQuery('#' + base + '_Hidden_' + images[base + "_last"]).fadeOut(1250);
		//jQuery('#' + base + '_Hidden_' + images[base + "_last"]).css('display', 'none');
		//alert('ok');
	}
	//alert(base + '_Hidden_' + images[base + "_current"]);
	jQuery('#' + base + '_Hidden_' + images[base + "_current"]).fadeIn(1250);
	//jQuery('#' + base + '_Hidden_' + images[base + "_current"]).css('display', 'block');
	
	if(images[base + "_isLanding"]) {
		jQuery('#' + base + '_Text_' + images[base + "_last"]).css('display', 'none');
		jQuery('#' + base + '_Text_' + images[base + "_current"]).css('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)", (images[base + "_flipSpeed"] * 1000));
	} 
	
	//alert(jQuery('#homepageflip_Hidden_0').css('display'));
	
	//jQuery(base + '_Hidden_' + images[base + "_current"]).css('display', 'none');
}


/*
**  STARTUP METHODS
*/
//jQuery.noConflict();

//jQuery(document).ready(function() {

function enableMenu() {
  // Handler for .load() called.
  //console.log('loaded menu');
  var nav= jQuery("#nav li span");
		
	nav.each(function(index) {
		var menuName = jQuery(this).attr("id");
		var position =  jQuery(this).position();
		jQuery("#menuContent_" + menuName).css("left",position.left-30);
		
		jQuery(this).bind('mouseover', function() {
			jQuery("#menu_noozletter_email").blur();
			toggleSubMenu(menuName, 1);
		});
		jQuery(this).bind('click', function() {
			toggleSubMenu(menuName, 1);
		});
		
		jQuery(this).bind('mouseout', function() {
			toggleSubMenu('', 0);
		});
	});
  
	jQuery(".menuContent").bind('mouseover', function() {
		stopSubHideTimer2();
	});
	
	jQuery(".menuContent").bind('mouseout', function() {
		startSubHideTimer();
	});
};

/*
jQuery('#nav').load(function () {
	//alert('loaded');
	var nav= jQuery("#nav li span");
		
	nav.each(function(index) {
		var menuName = jQuery(this).attr("id");
		var position =  jQuery(this).position();
		jQuery("#menuContent_" + menuName).css("left",position.left-30);
		
		jQuery(this).bind('mouseover', function() {
			jQuery("#menu_noozletter_email").blur();
			toggleSubMenu(menuName, 1);
		});
		jQuery(this).bind('click', function() {
			toggleSubMenu(menuName, 1);
		});
		
		jQuery(this).bind('mouseout', function() {
			toggleSubMenu('', 0);
		});
	});
  
	jQuery(".menuContent").bind('mouseover', function() {
		stopSubHideTimer2();
	});
	
	jQuery(".menuContent").bind('mouseout', function() {
		startSubHideTimer();
	});
	
});
*/

jQuery(document).ready(function() {
 // alert('hello');
	allSubNavDivs = jQuery('div.subNavSetVert');		
	allNavLinks = jQuery('a.navLink');
	Custom.init();	
	isLoaded = true;
	
	jQuery('.wheretobuymap a').each(function(elmt) {
		elmt.observe('mouseover', function(ev) {	 
			ev.target.className = 'On';
		});
		
		elmt.observe('mouseout', function(ev) {	
			ev.target.className = ''; 
		});
	});
	
	jQuery('.messageStackWarning, .messageStackError, .messageStackSuccess, .messageStackCaution').slideDown('slow');
	
});



/*
**  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 carouselContentDiv;
	var carouselWidth = carouselImageWidth * carouselCols;
	var carouselItemWidth = carouselImageWidth*(carouselItems-carouselCols);
	var carouselAtEnd = false;
	var dragCount = 0;	
	var dragLinked = true;
	
	jQuery(document).ready(function() {
		
		carouselParentDiv = jQuery(".carouselParentHome");
		carouselContentDiv = jQuery('.carouselBody');
		
		var carouselContentWidth = carouselImageWidth*carouselItems
		carouselContentDiv.css('width', (carouselContentWidth));
		
		setCarouselButtons();
		var offset = carouselParentDiv.offset();
		
		//alert(carouselContentDiv.attr('id'));
		carouselContentDiv.draggable({ axis: 'x', "grid": [carouselImageWidth, 0], zindex: 5, containment: [offset.left-carouselContentWidth+carouselParentDiv.width()-carouselImageWidth, 0, offset.left, 0] });
		carouselContentDiv.bind( "dragstop", function(event, ui) {
			setCarouselButtons();
		});
	});
	 
	function setCarouselButtons() {	
		var position = carouselContentDiv.position();
		
		if (position.left !=  0) {
			// IF WE'RE NOT AT THE FIRST ITEM ON THE LIST WE SHOULD ENABLE THE 'BACK' BUTTON
			jQuery('#carouselLeft').fadeIn();		
			jQuery('#carouselLeft').bind('click', function(event) { scrollCarousel(1) }); 
			
		} else {
			// WE'RE AT THE FIRST ITEM ON THE LIST SO WE SHOULD DISABLE THE 'BACK' BUTTON
			jQuery('#carouselLeft').unbind('click');
			jQuery('#carouselLeft').fadeOut();		
		}
		
		if (carouselParentDiv.width()-position.left-carouselImageWidth*carouselItems-4==0 ) {
			// WE'RE AT THE LAST ITEM ON THE LIST SO WE SHOULD DISABLE THE 'NEXT' BUTTON
			jQuery('#carouselRight').unbind('click'); 
			jQuery('#carouselRight').fadeOut();
			
		}  else {
			// WE'RE NOT AT THE LAST ITEM ON THE LIST SO WE SHOULD ENABLE THE 'NEXT' BUTTON
			jQuery('#carouselRight').fadeIn();
			jQuery('#carouselRight').bind('click', function(event) { scrollCarousel(-1) }); 
		}
		
	}
	
	function OnCarouselFinish(obj) {
		setCarouselButtons(obj.element);
	}
	
	function scrollCarousel(dir) {		
		//carouselAtEnd = false;
		var position = carouselContentDiv.position();
		jQuery('#carouselLeft').unbind('click');
		jQuery('#carouselRight').unbind('click'); 
		
		carouselContentDiv.animate({ left: position.left + (carouselImageWidth*dir) }, 500, function() { setCarouselButtons(); });
		

	//	new Effect.Move(carouselContentDiv, {x: (carouselImageWidth*dir), y: 0, duration:0.5, afterFinish: OnCarouselFinish}); 
				
		//if ((-carouselItemWidth+carouselImageWidth) + "px" == carouselContentDiv.css('left')) {
		//	carouselAtEnd = true;
		//}
		return false;
	}
}

/*
*  Menu Functions
*/




var tmpNavName = '';

function toggleSubMenu(name, dir) {	
	if(isLoaded) {
		switch(dir) {
			case 1:
				stopSubHideTimer2();
				tmpNavName = name;
				highlightSubMenuTimer = setTimeout(highlightSubMenu, 250);
				break;
			default:
				if(window.highlightSubMenuTimer)clearTimeout(highlightSubMenuTimer);
				startSubHideTimer();
			break;
		}
	}
}



function hideSubMenus() {
	if (jQuery(document.activeElement).id!="menu_noozletter_email") {
       
		jQuery('.menuContent').css("display","none");
		jQuery("#nav li span").removeClass("over");
    }
}

function highlightSubMenu() {
	hideSubMenus();
	jQuery('#menuContent_' + tmpNavName).css("visibility","visible");
	jQuery('#menuContent_' + tmpNavName).css("display","block");
	jQuery("#nav li span#" + tmpNavName).addClass("over");
}

function startSubHideTimer() {
	subHideTimer = setTimeout(hideSubMenus, 500);
}



	
function stopSubHideTimer2() {
	clearTimeout(subHideTimer);
}



jQuery(function(){
			
	
			
	jQuery("#menu_noozletter_submit").click(function(){
		
		//set the button up
		jQuery('#menu_noozletter_submit').val('Submitting...');
		
		if(checkemail()) {
			
			//Retrieve the contents of the textarea (the content)
			var signup_email = jQuery("#menu_noozletter_email").val();
			var signup_url = '/noozletter/index.php?p=subscribe&id=1';
			var signup_listname = "Nooka Noozletter";
			var signup_listnum = 2;
			var signup_htmlemail = 1;
			var list_params = "";
		
			//Build the paramy of lists
			jQuery('.menu_noozletter_multlists').each(function(index) {
				list_params = list_params + "&list[" + jQuery(this).val() + "]=signup";
			}); 
		
		
			//Build the URL that we will send
			var url_paramies = "VerificationCodeX=&htmlemail=" + signup_htmlemail + "&listname=" + signup_listname + "&subscribe=subscribe&email=" + escape(signup_email) + list_params;
			
			//Use jQuery's ajax function to send it
			jQuery.ajax({
				type: "POST",
				url: signup_url,
				data: url_paramies,
				success: function(){
					jQuery("#menu_noozletter_newsletterform").html(jQuery("<p id='menu_noozletterlista_success' class='menu_noozletterlista_resultmsg pink'>Thanks for signing up!  Please check your email to confirm your address.</p>").fadeIn("slow"));
					jQuery('.submission-elements').fadeOut("slow");
					//alert('test 1');
				},					   
				error: function(){
					jQuery("#menu_noozletter_mesg").html(jQuery("<p id='menu_noozletterlista_failure' class='menu_noozletterlista_resultmsg'>Sorry, we couldn't add your address. Please try again or contact the system administrator.</p>").fadeIn("slow"));
					//alert('test 2');
				}
			});
		}
		
		jQuery('#menu_noozletter_submit').val('Sign up now');
		
		//We return false so when the button is clicked, it doesn't follow the action
		return false;
		
	});

});


function checkemail() {
	var hasError = false;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	jQuery("#menu_noozletter_mesg").html(jQuery('').fadeIn("slow"));
	var emailaddressVal = jQuery("#menu_noozletter_email").val();
	if(emailaddressVal == '') {
		jQuery("#menu_noozletter_mesg").html(jQuery('<span class="error">Please enter your email address.</span>').fadeIn("slow"));
		hasError = true;
	}

	else if(!emailReg.test(emailaddressVal)) {
		jQuery("#menu_noozletter_mesg").html(jQuery('<span class="error">Please enter a valid email address.</span>').fadeIn("slow"));
		hasError = true;
	}

	if(hasError == true) { return false; } else { return true; }
}

function menu_noozletter_clearDefault(myelement) {
	if (myelement.defaultValue==myelement.value) myelement.value = "";
} 
	
/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only things you may need to change in this file are the following
variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)

The numbers you set for checkboxHeight and radioHeight should be one quarter
of the total height of the image want to use for checkboxes and radio
buttons. Both images should contain the four stages of both inputs stacked
on top of each other in this order: unchecked, unchecked-clicked, checked,
checked-clicked.

You may need to adjust your images a bit if there is a slight vertical
movement during the different stages of the button activation.

The value of selectWidth should be the width of your select list image.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "16";
var radioHeight = "16";
var selectWidth = "190";


/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		
		//alert(inputs.length);
		for(a = 0; a < inputs.length; a++) {
						//alert('wtf');
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
						//span[a].ondblclick='alert("wtf")'
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(inputs[a].getAttribute("onclick")) {
					//alert('here');
					//span[a].onclick = Custom.onclick; //'alert("wtf")';
				}
				inputs[a].onchange = Custom.clear;
				if(!inputs[a].getAttribute("disabled")) {
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
				} else {
					span[a].className = span[a].className += " disabled";
				}
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
		}
		document.onmouseup = Custom.clear;
	},
	onclick: function() {
		element = this.nextSibling;
		element.onclick="alert('wtf');";
		//alert(element.checked);
	},
	clicked: function() {
		element = this.nextSibling;
		alert(element.checked);
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
				
	},
	check: function() {
		element = this.nextSibling;
		
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.click();
			element.checked = true;
		}
		if(typeof checkOverride	 == 'function') {
			checkOverride(element.checked);
		}
		
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
		
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
//window.onload = Custom.init();
