﻿/** Model cover stuff **/
var _cover = null;

/* gets the modal cover.  if it doesn't exist, create it. */
function getModalCover()
{
	if(_cover == undefined)
	{
		_cover = document.createElement("div");
		_cover.style["display"] = "none";
		_cover.style["width"] = _cover.style["height"] = "100%";
		_cover.style["background"] = "#000";
		_cover.style["position"] = "absolute";
		_cover.style["top"] = "0";
		_cover.style["left"] = "0";
		_cover.style["zIndex"] = "999";
		_cover.style["opacity"] = "0.6";
		_cover.id = "__cover";
		
		_cover.show = function() {
			var cover = getModalCover();
			cover.style["opacity"] = "0";
			cover.style["display"] = "block";
			_cover._fadeIn();
		};
		
		_cover._fadeIn = function()
		{
			var cover = getModalCover();
			var opacity = parseFloat(cover.style["opacity"]);
			if(opacity < 0.6)
			{
				opacity += .1;
				cover.style["opacity"] = opacity;
				cover.style["filter"] = "alpha(opacity=" + opacity * 100 + ")";
				setTimeout("_cover._fadeIn();", 10);
			}
		};

		_cover.hide = function()
		{
			var cover = getModalCover();
			var opacity = parseFloat(cover.style["opacity"]);
			if(opacity > 0)
			{
				opacity -= .05;
				cover.style["opacity"] = opacity;
				cover.style["filter"] = "alpha(opacity=" + opacity * 100 + ")";
				setTimeout("_cover.hide();", 50);
			}
			else
			{
				cover.style["display"] = "none";
			}
		};
	
		document.body.insertBefore(_cover, document.body.firstChild);
		_cover = document.getElementById("__cover");
	}
	return _cover;
}

/** Twitter stuff **/
twitter_popup = function()
{
	var wnd = window.open("http://www.twitter.com/aptimize", 'twitterWindow','width=800,height=600');
	if(window.focus) wnd.focus();
}

/** Slideshow **/
function updatePosition()
{
	var distance = Math.abs(targetPos - currentPos);
	if(distance <= 1)
	{
		currentPos = targetPos;
	}
	else
	{
		if(distance < 16)
		{
			document.getElementById("banner-arrow").className = newEle.className.substr(0, newEle.className.indexOf("active") - 1);
		}
		currentPos = currentPos + Math.round((targetPos - currentPos) / 2);
		setTimeout("updatePosition()", 50);
	}
	document.getElementById('banner-arrow').style["left"] = currentPos + "px";
}

function updateTransparency()
{
	var trans = parseFloat(newPanel.style["opacity"]);
	if(trans < 1)
	{
		trans = Math.min(1, trans + .1);
		newPanel.style["opacity"] = trans;
		newPanel.style["filter"] = "alpha(opacity=" + trans * 100 + ")";
		setTimeout("updateTransparency()", 50);
	}
	else
	{
		oldPanel.style["opacity"] = 0;
		oldPanel.style["zIndex"] = 98;
		oldPanel = newPanel;
		inTransit = false;
	}
}

function flipPanels(ele)
{
	ele.style["opacity"] = 0;
	ele.style["filter"] = "alpha(opacity=0)";
	ele.style["zIndex"] = 100;
	oldPanel.style["zIndex"] = 99;
	newPanel = ele;

	updateTransparency();
}

String.prototype.setCharAt = function(index,chr) {
	if(index > this.length-1) return str;
	return this.substr(0,index) + chr + this.substr(index+1);
}

function bannerSelect(ele, dontReset)
{
	if(typeof(dontReset) == "undefined" && slideshow_int != null)
	{
		clearInterval(slideshow_int);
		slideshow_int = null;
		setTimeout("doSlideshow()", 7500);
	}
	if(oldEle == ele) return;
	if(inTransit) return;
	inTransit = true;
	var parent = ele.parentNode;
	
	// Determining where we're going
	if(ele.id.charAt(1) == "1")
	{
		targetPos = parseInt(ele.id.charAt(3)) * 60 + 18;
		slideshow_cur = parseInt(ele.id.charAt(3));
	}
	else if(ele.id.charAt(1) == "2")
	{
		targetPos = 858 - (parseInt(ele.id.charAt(3)) * 58);
		slideshow_cur = 7 - parseInt(ele.id.charAt(3));
	}
	
	var img = document.getElementById(oldEle.id + "-img");
	var imgsrc = img.getAttribute('waxsrc')!=null ? img.getAttribute('waxsrc') : img.src;
	img.src = imgsrc.setCharAt(imgsrc.length - 5, 'a');
	
	img = document.getElementById(ele.id + "-img");
	imgsrc = img.getAttribute('waxsrc')!=null ? img.getAttribute('waxsrc') : img.src;
	img.src = imgsrc.setCharAt(imgsrc.length - 5, 'b');
	
	newEle = ele;
	ele.className += " active";
	oldEle.className = oldEle.className.substr(0, oldEle.className.indexOf("active") - 1);
	oldEle = ele;
	flipPanels(document.getElementById("banner-" + ele.id.charAt(1) + "-" + ele.id.charAt(3)));

	updatePosition();
}

doSlideshow = function()
{
	if(slideshow_int == null)
	{
		slideshow_int = setInterval("doSlideshow();", 5000);
	}
	else
	{
		if(++slideshow_cur >= slideshow_slides.length) slideshow_cur = 0;
		bannerSelect(document.getElementById(slideshow_slides[slideshow_cur]), true);
	}
}

// For default text in text boxes
function setDefaultText(ele, text)
{
	if(text != undefined)
	{
		ele.defaultText = text;
	}
	else if(ele.value == "")
	{
		ele.value = ele.defaultText;
	}
}
function clearDefaultText(ele)
{
	if(ele.value == ele.defaultText)
	{
		ele.value = "";
	}
}
