// get position of child node
// var snoogans = getPosition(this.parentNode,this);
function getPosition(main,spec) {
	var items = main.getElementsByTagName(spec.tagName);
	var found = 0;
	for (p = 0; p < items.length; p++) {
		if (items[p] == spec) {
			found = 1;
			break;
		}
	}
	if (found) {
		return p;
	}
	else {
		return -1;
	}
}

$(document).ready(function(){

	cssResets();
	hoverFade();
	opacityHover();
	formAdjustments();
	loginFormButton();
	clearLoginFormLabels();
	checkForAutoFilledValues();

	landingFocusHoverFade();
	photoSlideShow();
	
	expandCollapseContent();
	
	$("div#scrollingContent").jScrollPane();

	// select input text on click
	$("input").click(function(){ $(this).select(); });

	// links with rel="external" will open in a new window
	$("a[rel=external]").click(function(){ window.open(this.href); return false; });

});

function hoverFade()
{

	$("a.hoverFade span.label").before("<span class=\"normal\"></span><span class=\"hover\"></span>");

	$("a.hoverFade").hover(
		function(){
			if ( $(this).hasClass("selectedMain") || $(this).hasClass("selectedSub") ){
				// don't animate this button
			}
			else {
				$(this).find("span.normal").animate({ opacity: 0 }, { queue: false, duration: 250 });
				$(this).find("span.hover").css({opacity: 0}).show().animate({ opacity: 1 }, { queue: false, duration: 250 });
			}
		},
		function(){
			if ( $(this).hasClass("selectedMain") || $(this).hasClass("selectedSub") ){
				// don't animate this button
			}
			else {
				$(this).find("span.hover").animate({ opacity: 0 }, { queue: false, duration: 250 });
				$(this).find("span.normal").animate({ opacity: 1 }, { queue: false, duration: 250 });
			}
		}
	);
}


// landing page focus area hover
function landingFocusHoverFade()
{
	$("a.focusHoverFade").append("<span class=\"focusNormal\"></span><span class=\"focusHover\"></span><div class=\"focusArrow\"></div>");

	$("a.focusHoverFade").hover(
		function(){
			$(this).find("span.focusNormal").animate({ opacity: 0 }, { queue: false, duration: 250 });
		},
		function(){
			$(this).find("span.focusNormal").animate({ opacity: 1 }, { queue: false, duration: 250 });
		}
	);
}


function opacityHover(){
	$("a.opacityHover").hover(
		function(){ $(this).animate( { opacity: .5 }, { queue: false, duration: 250 } ); },
		function(){ $(this).animate( { opacity: 1 }, { queue: false, duration: 250 } ); }
	);
}

function photoSlideShow()
{

	photo_count = $("ul.photos li").length;
	current_photo = 1;

	$("div.contentPhotos a.historyArrow").click(function(){

		if ( $(this).hasClass("previousPhoto") )
		{
			if ( current_photo == 1 )
			{
				$("ul.photos li").fadeOut(100);
				$("ul.photos li:nth-child(" + photo_count + ")").fadeIn(500);
				current_photo = photo_count;
			}
			else {
				current_photo--;
				$("ul.photos li").fadeOut(100);
				$("ul.photos li:nth-child(" + current_photo + ")").fadeIn(500);
			}
		}
		else
		{
			if ( current_photo == photo_count )
			{
				$("ul.photos li").fadeOut(100);
				$("ul.photos li:nth-child(1)").fadeIn(500);
				current_photo = 1;
			}
			else {
				current_photo++;
				$("ul.photos li").fadeOut(100);
				$("ul.photos li:nth-child(" + current_photo + ")").fadeIn(500);
			}
		}

		return false;

	});

}

// check to see if the browser has automatically filled the username/password login fields
function checkForAutoFilledValues()
{
	var auto_filled_user = $("div#retailerLogin input#username").val();
	var auto_filled_pass = $("div#retailerLogin input#password").val();
	if ( auto_filled_user != "" ) { $("div#retailerLogin input#username").css({backgroundPosition: "-200px 0px"}); }
	if ( auto_filled_pass != "" ) { $("div#retailerLogin input#password").css({backgroundPosition: "-200px 0px"}); }
}


function clearLoginFormLabels()
{
	$("div#retailerLogin input").focus(
		function()
		{
			$(this).css({backgroundPosition: "-200px 0px"});
		}
	);
	$("div#retailerLogin input").blur(
		function()
		{
			thisVal = $(this).val();
			if ( thisVal == "" )
			{
				$(this).css({backgroundPosition: "0px 0px"});
			}
			else
			{
				$(this).css({backgroundPosition: "-200px 0px"});
			}
		}
	);
}

function formAdjustments()
{
	// .checkRadio resets height and width of checkboxes and radios
	$("input[type=checkbox]").addClass("checkRadio");
	$("input[type=radio]").addClass("checkRadio");

	// adjust height for textareas
	$("textarea").parent("p").addClass("textArea");
}

function loginFormButton()
{
	// hover for login form button
	$("input#loginbutton").hover(
		function() { $(this).attr("src", "/includes/img/interface/footer-login-submit-hover.gif"); },
		function() { $(this).attr("src", "/includes/img/interface/footer-login-submit.gif"); }
	);
}

function expandCollapseContent()
{
	$("a.expandCollapse").toggle(
		function()
		{
			$(this).addClass("expanded").next("p.expandable").slideDown(500);
		},
		function()
		{
			$(this).removeClass("expanded").next("p.expandable").slideUp(500);
		}
	);
	
}

function cssResets()
{
	$("table tr:last").addClass("lastRow");
	$("table tr:odd").addClass("altRow");

	$("body.pagePeople table tr:first").addClass("firstRow");
	$("body.pagePeople table tr td:nth-child(1)").addClass("firstColumn");

	$("ul.townsServed li:odd").addClass("altListRow");
	$("ul.townsServed li:last-child").addClass("lastListItem");

	$("ul#brandDetails li:last").addClass("noBorder");
	$("div.scrollingNavigation ul li:last").addClass("noBorder");

}
