var passwordField = '<input type="password" name="ac_password" class="styledInput" value="" id="replacedPassword" onblur="passwordBlur($(this));" />';
var passwordFieldReplacment = '<input type="text" name="ac_password" class="styledInput" value="Your password" id="replacedPassword" onfocus="passwordFocus($(this));" />';

function formFieldFocus(idx, f_name) {
	field = fields_ids[idx];
	txt   = fields_txt[idx];
	obj   = document.getElementById(f_name)[field];
	if(obj.value === txt) {
		obj.value = "";
	}
	return true;
}

function formFieldBlur(idx, f_name) {
	field = fields_ids[idx];
	txt   = fields_txt[idx];
	obj   = document.getElementById(f_name)[field];
	if(obj.value === "") {
		obj.value = txt;
	}
	return true;
}

function submitForm(f_name) {
	for(i = 0; i < fields_ids.length; ++i) {
		obj = document.getElementById(fields_ids[i]);
		if(obj.value === fields_txt[i]) {
			obj.value = "";
		}
	}
	document.getElementById(f_name)[submit()];
	return false;
}

function openPopup(popupLocation, popupName, popupWidth, popupHeight) {
	var screenWidth = 760, screenHeight = 420;
	if (self.screen.width) {
		screenWidth = self.screen.width;
		screenHeight = self.screen.height;
	}
	var popupTop = (screenHeight - popupHeight) / 2, popupLeft = (screenWidth - popupWidth) / 2;
	newPopupName = window.open(popupLocation, 'newname', 'height=' + popupHeight + ', width=' + popupWidth + ', left=' + popupLeft + ', top=' + popupTop + ', resizable=0');
	if (newPopupName.focus) {
		newPopupName.focus();
	}
	return false;
}

function openInNewWindow() {
	var newWindow = window.open(this.getAttribute('href'), '_blank');
	if(newWindow) {
		newWindow.focus();
	}
	return false;
}

function getAttribs(object) {
	/* [0]this.id, [1]this.name, [2]this.tabIndex, [3]this.parentFormID */
	return [object.attr('id'), object.attr('name'), object.attr('tabindex'), object.parents('form').attr('id')];
}
function focusMe(me) {
	$('#' + me).focus();
}
function passwordFocus(me) {
	var attribs = getAttribs(me);
	/* [0]this.id, [1]this.name, [2]this.tabIndex, [3]this.parentFormID */
	$(me, '#' + attribs[3]).replaceWith(passwordField);
	$('#replacedPassword').attr('id', attribs[0]).attr('name', attribs[1]).attr('tabIndex', attribs[2]);
	setTimeout(function(){focusMe(attribs[0])},100);
}
function passwordBlur(me) {
	if (me.val() === '') {
		var attribs = getAttribs(me);
		/* [0]this.id, [1]this.name, [2]this.tabIndex, [3]this.parentFormID */
		$(me, '#' + attribs[3]).replaceWith(passwordFieldReplacment);
		$('#replacedPassword').attr('id', attribs[0]).attr('name', attribs[1]).attr('tabIndex', attribs[2]);
	}
}
function getNewWindowLinks() {
	if (document.getElementById && document.createElement && document.appendChild) {
		var objWarningText, strWarningText, link, links = document.getElementsByTagName('a'), i;
		for (i = 0; i < links.length; i++) {
			link = links[i];
			if (link.getAttribute && link.getAttribute) {
				if (link.getAttribute("href") && link.getAttribute("rel") == "external") {
					link.onclick = openInNewWindow;
				}
			}
		}
		objWarningText = null;
	}
	if ($('input:password').size() !== 0) {
		$('input:password').each(function(index) {
			var attribs = getAttribs($(this));
			/* [0]this.id, [1]this.name, [2]this.tabIndex, [3]this.parentFormID */
			$($(this), '#' + attribs[3]).replaceWith(passwordFieldReplacment);
			$('#replacedPassword').attr('id', attribs[0]).attr('name', attribs[1]).attr('tabIndex', attribs[2]);
		});
	}
}

jQuery.extend( jQuery.easing,
{
    def: 'easeInOutQuad',
    easeInOutQuad: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) { return c/2*t*t + b};
        return -c/2 * ((--t)*(t-2) - 1) + b;
    }
});

$(document).ready(function() {
/*
	$(".productListing .nonMicroformatWrapper .fn").equalHeights().css('overflow','hidden');
	$(".productListing .nonMicroformatWrapper .priceWrapper").equalHeights().css('overflow','hidden');
	$(".productListing .photo").equalHeights().css('overflow','hidden');
	$(".productListing .item").equalHeights().css('overflow','hidden');
	$(".productListing .description").equalHeights().css('overflow','hidden');
*/
	
	$('.linkArrow').hover(
	function()
	{
		$(this).stop(true, true).animate({
			paddingRight: '20',
			marginLeft: '2'
		}, 75);
	},
	function()
	{
		$(this).delay(50).animate({
			paddingRight: '16',
			marginLeft: '0'
		}, 100);
	});
	
	$('#homeMainLinks a').hover(function() {
		$('span', this).stop(true, true).animate({
			width: 'show',
			opacity: 1
		}, 200, 'easeInOutQuad');
	}, function () {
		$('span', this).animate({
			opacity: 0,
			width: 'hide'
		}, 600, 'easeInOutQuad');
	});
	
	$('#globalNav a, #footerLinks a, .linkSlideUp').hover(function() {
		$(this).stop(true, true).animate({
			marginTop: '-2'
		}, 75);
	}, function () {
		$(this).animate({
			marginTop: '0'
		}, 200);
	});
	
	//home page banner slidey thing
	(function startBannerSlider() {
		var timeShow, slideWidth, numberSlides, listWidth, intervalIndex, timerRunning = false;
		//user variables
		timeShow = 5500;
		slideWidth = 960;
		//-------------------------------
		function slideBanner() {
			$('#banner ul').animate({
				marginLeft: -slideWidth
			}, 2000, 'easeInOutQuad', function () {
				$('#banner ul').append($('#banner ul').find('li').eq(0)).css('margin-left', 0);
			})
			if(timerRunning) {
				stopTimer();
				startTimer();
			}
		}
		function stopTimer() {
			clearInterval(intervalIndex);
			timerRunning = false;
		}
		function startTimer() {
			stopTimer();
			intervalIndex = setTimeout(slideBanner, timeShow);
			timerRunning = true;
		}
		numberSlides = $('#banner li').size(), listWidth = (slideWidth * numberSlides);
		if (listWidth > slideWidth) {
			$('#banner ul').css('width', listWidth);
			//start slideshow
			startTimer();
		};
		$('#banner a.linkBanner').hover(function() {
			stopTimer();
		}, function () {
			startTimer();
		})
	})();
	
	// menu position functionality ------------------------------ start
	$(window).scroll(function () {
		if ($(window).scrollTop() > 132) {
			$("#supplementaryContent").addClass('positionFixed');
		}
		else if ($('#supplementaryContent').hasClass('positionFixed')) {
			$("#supplementaryContent").removeClass('positionFixed');
		}
	});
	// menu position functionality ------------------------------ end

	// Colorbox //
	// Examples of how to assign the ColorBox event to elements //
/*
	$("a[rel='colorbox-elastic']").colorbox();
	$("a[rel='colorbox-fade']").colorbox({transition:"fade"});
	$("a[rel='colorbox-slideshow']").colorbox({slideshow:true});
*/
});
