var focusedOnApp = "YES";
var bodyDiv = document.getElementById('bodyID');
//tmp starting this at 1 for proper margin
var appCount = 1;
var appsOutOfFocus = 0;

function initWithAppNamed(appName)
{

	//TODO: Grab these from XML
	
	
	if (BrowserDetect.browser == "Safari") {
		injectApplicationNamed('iGraffiti', 'iGraffiti', '4.99', 'iGraffiti lets you paint on your photos. Kids and adults alike will have fun doodling over pictures of their friends. When you have completed your masterpiece, share it using email or on the web.', 'http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=284671738&amp;mt=8');
		injectApplicationNamed('twittelator', 'Twittelator Pro', '4.99', 'Are you a serious Tweeter? Check out Twittelator Pro, the Twitter client with a twist. Highly engineered and bundled with all the tools a tweeting fanatic needs, you\'re sure to find it truly terrific!', 'http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=288963578&mt=8');
		injectApplicationNamed('talkingPics', 'Talking Pics', '9.99', 'Talking Pics lets you record voice notes over photos. Use it to store your thoughts, record your travels, or even to help remember a new face. Whatever you use it for, Talking Pics keeps your life in sync.', 'http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=284760024&amp;mt=8');


		//Logic to push featured app into focus
		var allNodes = document.getElementById('bodyID').childNodes;

		for (var i = 0; i < allNodes.length; i++) {
			//alert(i + ": " + allNodes[i].className);
			if (allNodes[i].className == "product-container") {
				if (allNodes[i].id == appName) {
					toggleApp(appName);
					appsOutOfFocus = appCount - 1;
				}
			}
		}
	} else {
		window.location = 'http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewArtist?id=284671741';
	}
	
	
}

function checkWidth()
{
	
	var tmpNodes = document.getElementById('bodyID').childNodes;
	
	window.console.log(window.document.width);
	
	if (window.document.width < 934) {
		for (var i = 0; i < tmpNodes.length; i ++) {
			if (tmpNodes[i].className == "product-container") {

				if (tmpNodes[i].attributes.currentlyfocused.value == "NO") {
					var calculatedLeft = (tmpNodes[i].attributes.appNumber.value * appsOutOfFocus) + 25;
					window.console.log(tmpNodes[i].id + ": "+ calculatedLeft);
					tmpNodes[i].style.left = '-'+calculatedLeft+'%';
				}
			}
		}
	} else if (window.document.width < 1000) {
		for (var i = 0; i < tmpNodes.length; i ++) {
			if (tmpNodes[i].className == "product-container") {

				if (tmpNodes[i].attributes.currentlyfocused.value == "NO") {
					var calculatedLeft = (tmpNodes[i].attributes.appNumber.value * appsOutOfFocus) + 25;
					window.console.log(tmpNodes[i].id + ": "+ calculatedLeft);
					tmpNodes[i].style.left = '-'+calculatedLeft+'%';
				}
			}
		}
	} else if (window.document.width < 1109) {
		for (var i = 0; i < tmpNodes.length; i ++) {
			if (tmpNodes[i].className == "product-container") {

				if (tmpNodes[i].attributes.currentlyfocused.value == "NO") {
					var calculatedLeft = (tmpNodes[i].attributes.appNumber.value * appsOutOfFocus) + 20;
					window.console.log(tmpNodes[i].id + ": "+ calculatedLeft);
					tmpNodes[i].style.left = '-'+calculatedLeft+'%';
				}
			}
		}
	} else if (window.document.width > 1110){
		for (var i = 0; i < tmpNodes.length; i ++) {
			if (tmpNodes[i].className == "product-container") {

				if (tmpNodes[i].attributes.currentlyfocused.value == "NO") {
					var calculatedLeft = (tmpNodes[i].attributes.appNumber.value * appsOutOfFocus) + 10;
					window.console.log(tmpNodes[i].id + ": "+ calculatedLeft);
					tmpNodes[i].style.left = '-'+calculatedLeft+'%';
				}
			}
		}
	}
	
}

function toggleApp(appName)
{
	
	//Fade the logo
	var condition = document.getElementById(appName);
	//alert(condition.id);
	//alert(focusedOnApp);
	
	if (condition.attributes.currentlyFocused.value == "NO") {
		
		var tmpNodes = document.getElementById('bodyID').childNodes;
		
		for (var i = 0; i < tmpNodes.length; i ++) {
			if (tmpNodes[i].className == "product-container") {

				if (tmpNodes[i].attributes.currentlyfocused.value == "YES") {
					moveAppOut(tmpNodes[i]);
				}
			}
		}
		
		moveAppIn(condition);
	}
	
	//moveAppOut(condition);
	//moveAppOut(condition);
	
	//If the application has the currentlyFocused custom attribute, then we move it out 1 (right)
	// or 0 (left) as the opposite 
	setTimeout("checkWidth()", 2100);
}

var zIndexPush = 0;
function moveAppOut(condition)
{
	checkWidth();
	zIndexPush = zIndexPush + 1;
	window.console.log(zIndexPush);
	//condition.style.zIndex = 10 + zIndexPush;
	window.console.log("The z-index for"+condition.id+"is "+condition.style.zIndex);
	condition.style.width = '300px';
	var parameters = condition.childNodes;
	
	
	for (var i = 0; i < parameters.length; i ++) {
		if (parameters[i].className == "logo") {
			parameters[i].style.opacity = '0';
		} else if (parameters[i].className == "first-box description") {
			parameters[i].style.marginRight = '295px';
			parameters[i].style.opacity = '0';
		} else if (parameters[i].className == "description") {
			parameters[i].style.marginRight = '295px';
			parameters[i].style.opacity = '0';
		} else if (parameters[i].className == "icon") {
			parameters[i].style.cursor = 'pointer';
		}
	}
	
	//Reset the margin
	condition.style.marginLeft = '0';
	var currentAppNumber = condition.attributes.appNumber.value;
	
	condition.style.left = -1*(20 - 10/currentAppNumber)+'%';
	checkWidth();
	condition.setAttribute('currentlyFocused', 'NO');
	//alert(document.getElementById('bodyID').style.backgroundColor);
}

function moveAppIn(condition)
{
	checkWidth();
	//condition.style.zIndex = 5;
	//window.console.log(condition.style.zIndex);
	condition.style.width = '686px';
	var parameters = condition.childNodes;
	
	for (var i = 0; i < parameters.length; i ++) {
		if (parameters[i].className == "logo") {
			parameters[i].style.opacity = '1';
		} else if (parameters[i].className == "first-box description") {
			parameters[i].style.marginRight = 0;
			parameters[i].style.opacity = '1';
		} else if (parameters[i].className == "description") {
			parameters[i].style.marginRight = 0;
			parameters[i].style.opacity = '1';
		} else if (parameters[i].className == "icon") {
			parameters[i].style.cursor = 'default';
		}
	}
	
	//Reset the margin
	condition.style.marginLeft = '-343px';
	condition.style.left = '50%';
	condition.setAttribute('currentlyFocused', 'YES');
}

//	inImage1.onclick = function (event) { zoomOut(this, event); return false; };	

function injectApplicationNamed(appName, appDisplayName, appPrice, appDescription, iTunesURL) 
{
	appCount += 1;
	var body = document.getElementById('bodyID');
	

	//Main container
	var productContainer = document.createElement("div");
	productContainer.setAttribute('id', appName);
	productContainer.setAttribute('class', 'product-container');
	productContainer.setAttribute('currentlyFocused', 'NO');
	productContainer.setAttribute('appNumber', appCount);
	productContainer.style.left = -1*(20 + appCount)+'%';
	body.appendChild(productContainer);
	
	//Icon
	var icon = document.createElement("img");
	icon.setAttribute('src','images/'+appName+'_icon.png');
	icon.setAttribute('class','icon');
	icon.onclick = function (event) { toggleApp(appName); return false; };
	productContainer.appendChild(icon);
	
		//Main description tag
		var mainDescription = document.createElement("div");
		mainDescription.setAttribute('class', 'first-box description');
		productContainer.appendChild(mainDescription);
	
			//Top Cap for description container
			var descriptionTopCap = document.createElement("div");
			descriptionTopCap.setAttribute('class', 'description-top-cap');
			mainDescription.appendChild(descriptionTopCap);

			//Bottom Cap for description container
			var descriptionBottomCap = document.createElement("div");
			descriptionBottomCap.setAttribute('class', 'description-bottom-cap');
			mainDescription.appendChild(descriptionBottomCap);
		
			//Inner content container for description container
			var descriptionMiddleContainer = document.createElement("div");
			descriptionMiddleContainer.setAttribute('class', 'description-inner');
			mainDescription.insertBefore(descriptionMiddleContainer, descriptionBottomCap);
	
				//Content for descriptionMiddleContainer (TODO: Read from XML)
				var mainDescriptionContent = document.createElement("p");
				mainDescriptionContent.innerHTML = appDescription;
				descriptionMiddleContainer.appendChild(mainDescriptionContent);
		
		//Price description tag
		
		var tagDescription = document.createElement("div");
		tagDescription.setAttribute('class', 'description');
		if (iTunesURL){
			tagDescription.style.cursor = 'pointer';
			tagDescription.onclick = function (event) { window.location = iTunesURL; return false; };
		}
		productContainer.appendChild(tagDescription);
		
			//Add top and bottom caps
			
			var tagTopCap = document.createElement("div");
			tagTopCap.setAttribute('class', 'description-top-cap');
			tagDescription.appendChild(tagTopCap);
			
			var tagBottomCap = document.createElement("div");
			tagBottomCap.setAttribute('class', 'description-bottom-cap');
			tagDescription.appendChild(tagBottomCap);
			
			var tagDescriptionContainer = document.createElement("div");
			tagDescriptionContainer.setAttribute('class', 'description-inner buy-on-itunes');
			tagDescription.insertBefore(tagDescriptionContainer, tagBottomCap);
			
				//Tag innerHTML (TODO: Pull from XML)
				var tagDescriptionContent = document.createElement("p");
				if (appPrice == 'unreleased') {
					tagDescriptionContent.innerHTML = "Coming Soon!";
				} else if (appPrice == 'FREE') {
					tagDescriptionContent.innerHTML = "Click here to get it!";
				} else {
					tagDescriptionContent.innerHTML = "Buy for only $"+appPrice+" on iTunes";
				}
				tagDescriptionContainer.appendChild(tagDescriptionContent);
		
		//App title and authorship
		var bspName = document.createElement("div");
		bspName.setAttribute('class', 'logo');
		bspName.onclick = function (event) { window.location = 'http://stone.com/iPhone'; return false; };
		bspName.style.cursor = 'pointer';
		productContainer.appendChild(bspName);
		
		var bspLogo = document.createElement("h1");
		bspLogo.innerHTML = appDisplayName;
		bspName.appendChild(bspLogo);
}

//browser detect

function checkBrowser() {

}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

