function resizeiFrame(height) {
	if (height > 0) {
		__savedHeight = new Number(height);
		var newHeight = __savedHeight + 50; // addition is the submit button offset

		$('iframe').height(newHeight);
	}
};

$(document).ready(function(){
	
	// Auto-submit certain forms
	$("form.autoSubmit").submit();
	
	// News links open in new tabs
	$("section#news .accordianContent .details a").each(function(){
		$(this).attr("target", "_blank");
	});
	
	// Prevent widows
	$("#imageSwitcher #switcherThumbs li figcaption a").each(function() {
	  var wordArray = $(this).text().split(" ");
	  if(wordArray.length > 3 && (wordArray[wordArray.length-1].length + wordArray[wordArray.length-2].length < 10)){
		  wordArray[wordArray.length-2] += "&nbsp;" + wordArray[wordArray.length-1];
		  wordArray.pop();
		  $(this).html(wordArray.join(" "));
	  }
	});
	
	if($("section.accordian").length > 0){
		$("section.accordian article.highlightBlock h2 a").append("<span>Expand</span>");
		$("section.accordian article.highlightBlock .accordianContent").hide();
		$("section.accordian article.highlightBlock:first").addClass("current").children(".accordianContent").show();
		$("section.accordian article.highlightBlock h2 a").bind("click", function(event){
			event.preventDefault();
			var chosen = $(this).parents("article.highlightBlock");
			if(!chosen.hasClass("current") && $("section.accordian article.highlightBlock .accordianContent:animated").length < 1){
				$("section.accordian article.highlightBlock.current .accordianContent").slideUp(500);
				$("section.accordian article.highlightBlock.current").removeClass("current");
				chosen.addClass("current");
				chosen.children(".accordianContent").slideDown(500);
			}		
		});
	}
	
	if($("#imageSwitcher").length > 0){
		$("#imageSwitcher ul#switcherThumbs li:first").addClass("current");
		$("#imageSwitcher #switcherFocus").wrapInner("<div class=\"focusContent\" />");
		$("#imageSwitcher ul#switcherThumbs li a").bind("click", function(event){
			event.preventDefault();
			var selected = $(this).parents("li");
			if(selected.hasClass("current") !== true && $("#imageSwitcher #switcherFocus .focusContent").length < 2){
				var newImage = $(this).parent("figure").data("image");
				var newTitle = selected.find("figcaption a").text();
				$("#imageSwitcher ul#switcherThumbs li.current").removeClass("current");
				selected.addClass("current");
				$("#imageSwitcher #switcherFocus .focusContent").before("<div class=\"focusContent\" style=\"display:none\"><img alt=\""+newTitle+"\" src=\""+newImage+"\"><figcaption>"+newTitle+"</figcaption></div>");
				$("#imageSwitcher #switcherFocus .focusContent").first().fadeIn(500);
				$("#imageSwitcher #switcherFocus .focusContent").last().fadeOut(500, function(){
					$(this).remove();
				});
			}
		})
	}
	
	if($("ul.slider").length > 0){
	
		function checkButtons(sliderParams){
			var buttons = sliderParams.element.parent(".sliderMask").next("ul.sliderArrows");
			var position = $(sliderParams.element).position().left;
			
			if(position >= 0){
				buttons.children("li.prev").addClass("disabled");
			}else{
				buttons.children("li.prev").removeClass("disabled");
			}
			
			if(((sliderParams.itemWidth*sliderParams.numItems)+position) <= sliderParams.itemWidth*sliderParams.numShown){
				buttons.children("li.next").addClass("disabled");
			}else{
				buttons.children("li.next").removeClass("disabled");
			}
		}
		
		function slideThumbs(sliderParams, position){
			sliderParams.element.animate({"left":position},500,function(){
				checkButtons(sliderParams);
			});
		}
		
		$("ul.slider").each(function(){
		
			if($(this).children("li").length > $(this).data("items")){
			
				var thisSlider = $(this);
				
				var sliderParams = {
					element    : thisSlider,
					numShown   : thisSlider.data("items"),
					numItems   : thisSlider.children("li").length,
					itemWidth  : thisSlider.children("li").first().outerWidth(true),
					itemHeight : thisSlider.children("li").first().height()
				};
				
				
				// Find tallest in list and set height
				var tallestHeight = 0;
				var tallest;
				
				sliderParams.element.children("li").each(function(){
					height = $(this).height;
					if(height > tallestHeight){
						tallestHeight = height;
						tallest = $(this);
					}
				});
				
				// Set slider CSS						
				sliderParams.element.css({
					width: sliderParams.itemWidth * sliderParams.numItems,
					height: tallestHeight,
					position: 'absolute',
					left: 0,
					top: 0
				});
				
				// Set mask and CSS
				var thisMask = $("<div class='sliderMask'></div>");
				thisMask.css({
					width: sliderParams.itemWidth * sliderParams.numShown,
					height: sliderParams.itemHeight,
					overflow: 'hidden',
					position: 'relative'
				});
				
				var thisArrows = $("<ul class='sliderArrows'></ul>");
				thisArrows.append("<li class='prev'><a href='#'>Previous</a></li>");
				thisArrows.append("<li class='next'><a href='#'>Next</a></li>");
				
				thisArrows.find("li a").click(function(event){
				
					event.preventDefault();	
					var clicked = $(this).parent("li");
					var currentPos = sliderParams.element.position();
					if(!(clicked.hasClass("disabled")) && sliderParams.element.is(":not(:animated)")){
						
						switch(clicked.attr("class")){
							case "next":
								var roomLeft = (((sliderParams.itemWidth*sliderParams.numItems)-(sliderParams.itemWidth*sliderParams.numShown))+currentPos.left)/sliderParams.itemWidth;
								if(roomLeft >= sliderParams.numShown){
									slideThumbs(sliderParams, currentPos.left-(sliderParams.itemWidth*sliderParams.numShown));
								}else{
									slideThumbs(sliderParams, currentPos.left-(sliderParams.itemWidth*roomLeft));
								}
								break;
							case "prev":
								var roomLeft = -currentPos.left/sliderParams.itemWidth;
								if(roomLeft >= sliderParams.numShown){
									slideThumbs(sliderParams, currentPos.left+(sliderParams.itemWidth*sliderParams.numShown));
								}else{
									slideThumbs(sliderParams, currentPos.left+(sliderParams.itemWidth*roomLeft));
								}
								break;
						}
						
					}
					
				});
				sliderParams.element.after(thisArrows);
				sliderParams.element.wrap(thisMask);
				checkButtons(sliderParams);
			}
		});		
	}
	
	if($(".formSwitcher").length > 1){
		$(".formSwitcher").css("visibility", "hidden");
		$(".formSwitcher").height(0);
		$(".formSwitcher").first().before("<form action='#' style='margin-bottom:20px;'><fieldset><div class='formRow'><select id='formSwitcherControl'><option value='' selected='selected'>--Please choose a form--</option></select></div></fieldset></form>");
		$(".formSwitcher").each(function(){
			$("#formSwitcherControl").append("<option value='"+$(this).index(".formSwitcher")+"'>"+$(this).data("title")+"</option>");
		});
		$("#formSwitcherControl").live("change", function(){
			if(!$(".formSwitcher").eq($(this).val()).hasClass("open")){
				//$(".formSwitcher").fadeOut(100);
				$(".formSwitcher").css("visibility", "hidden");
				$(".formSwitcher").height(0);
				$(".formSwitcher.open").removeClass("open");
				$(".formSwitcher").eq($(this).val()).addClass("open");
				$(".formSwitcher").eq($(this).val()).css("visibility", "visible");
				$(".formSwitcher").eq($(this).val()).height('auto');
			}
		});
	}
	
	if($("form#login").length > 0 && $("form#login").next("form#forgot_password").length > 0){
	
		$("form#login, form#forgot_password").css({"float" : "left", "margin-right" : "10px"});
	
		var fullWidth = $("form#login").outerWidth(true) + $("form#forgot_password").outerWidth(true);
		var loginWidth = $("form#login").width();
		var maxHeight = 0;
		$("form#login, form#forgot_password").each(function(){
			elementHeight = $(this).outerHeight(true);
			if(elementHeight > maxHeight){
				maxHeight = elementHeight;
			}
		});
		
		var loginWrapper = $("<div id='loginWrapper'></div>");
		var loginMask = $("<div id='loginMask'></div>")
		
		loginWrapper.width(fullWidth).height(maxHeight).css({"position" : "absolute", "top" : 0, "left" : 0});
		loginMask.width(loginWidth).height(maxHeight).css({"overflow" : "hidden", "position" : "relative"});
		
		var wrapper = loginMask.wrapInner(loginWrapper);
		
		$("form#login, form#forgot_password").wrapAll(wrapper);
		
		$("a.formSlider").bind("click", function(event){
			event.preventDefault();
			if($(this).hasClass("toForgot")){
				$("#loginWrapper").animate({"left" : ($("form#forgot_password").position().left*-1)}, 250);
			}else if($(this).hasClass("toLogin")){
				$("#loginWrapper").animate({"left" : ($("form#login").position().left*-1)}, 250);
			}
		});
		
	}
	
	if($("#accessWrapper").length && $("#helpWrapper").length){
		if($("#helpWrapper #help_success").length){
			$("#accessWrapper").hide();
		}else{
			$("#helpWrapper").hide();
		}		
		$("a.helpFader").bind("click",function(event){
			event.preventDefault();
			if($(this).hasClass("toHelp")){
				$("#accessWrapper").fadeOut(250,function(){
					$("#helpWrapper").fadeIn(250);
				})
			}else if($(this).hasClass("toLogin")){
				$("#helpWrapper").fadeOut(250,function(){
					$("#accessWrapper").fadeIn(250);
				})
			}
		});
	}
	
	if($('ul#homeRoundabout').length > 0){
		
		$("ul#homeRoundabout li").each(function(){
			var img = $(this).children("img");
			img.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src='" + img.attr('src') + "')");	        
		});
	
		//var initialIndex = (Math.floor(Math.random()*$("ul#brandSwitcher li").length))-1;
		var initialIndex = 0;
		var currentIndex = initialIndex;
		$("ul#brandSwitcher li.current").removeClass("current");
		$("ul#brandSwitcher li").eq(initialIndex).addClass("current");
		
		function animateTo(index){
			$('ul#homeRoundabout').roundabout_animateToChild(index, 1000); 
			$("ul#brandSwitcher li.current").fadeOut(1000);
			$("ul#brandSwitcher li").eq(index).fadeIn(1000, function(){
				$("ul#brandSwitcher li.current").removeClass("current");
				$("ul#brandSwitcher li").eq(index).addClass("current");
				currentIndex = index;
			});
		}
		
		if($("html").hasClass('ie6')){
			$('ul#homeRoundabout').remove();
			// Fallback
			$('#masthead.home').css('background', 'url(../images/mastheads/home_nojs.jpg) left top');
		}else{
			$('ul#homeRoundabout').roundabout({
				minOpacity: 0.3,
				minScale: 0.3,
				clickToFocus: false
			});
		}
		
		var rotate = setInterval(function(){
			if($('ul#homeRoundabout li').eq(currentIndex+1).length > 0){
				var next = currentIndex+1;
			}else{
				var next = 0;
			}
			//console.log(next);
			animateTo(next);
		}, 5000);
	}
	
	if($('.peopleGrid').length > 0){
		$('.peopleGrid img').bind("contextmenu",function(e){
			return false;
		});
	}
	
	// Add 'View larger' links to images in Approve Store Events
	if($("#storeEvents_photos").length > 0){
		$("#storeEvents_photos .safecracker_file_thumb").each(function(){
			var image = $(this).children("p:last").text();
			$(this).children("p:last").after("<p><a href='/images/uploads/storeEvents/"+image+"' class='lightbox'>View larger</a></p>");
		});
	}
	
	// Bind lightbox
	$("a.lightbox").fancybox();
	
	// Manage zebra stripes
	$("table.zebra tr:odd").addClass('odd');
	
});
