function whereAmI() {
	var		agent = navigator.userAgent.toLowerCase();
	
	if (agent.match(/applewebkit/i))
		return 1;
	else if (agent.match(/firefox/i))
		return 2;
	if (agent.match(/msie/i))
		return 3;
	else
		return 1;
}

function whichOS() {
	var		agent = navigator.userAgent.toLowerCase();
	
	if (agent.match(/mac/i))
		return 1;
	else if (agent.match(/win/i))
		return 2;
	else
		return 1;
}

function showMovieIE() {
	var		images = document.getElementById("thumbTable").getElementsByTagName("img"), i, count = images.length;
	
	for (i = 0; i < count; i++)
		if (!images[i].src.match(/BW/i))
			break;
	
	showMovie(i);
}

function cacheImages() {
	var colorThumb1 = new Image();
	var colorThumb2 = new Image();
	var colorThumb3 = new Image();
	var colorThumb4 = new Image();
	var colorThumb5 = new Image();
	var colorThumb6 = new Image();
	var movieSprite1 = new Image();
	var movieSprite2 = new Image();
	var movieSprite3 = new Image();
	
	colorThumb1.src = "images/changeTitleThumb.jpg";
	colorThumb2.src = "images/deleteThumb.jpg";
	colorThumb3.src = "images/duplicateThumb.jpg";
	colorThumb4.src = "images/moveThumb.jpg";
	colorThumb5.src = "images/resizeThumb.jpg";
	colorThumb6.src = "images/sortThumb.jpg";
	
	movieSprite1.src = "images/movieBoxSprite.png";
	movieSprite2.src = "images/movieBoxSidesSprite.png";
	movieSprite3.src = "images/movieBoxFill.png";
	
	var		agent = navigator.userAgent.toLowerCase();
	
	if (agent.match(/applewebkit/i) && agent.match(/mobile/i))
		window.location = "iPhone/tutorials.html";
	
	
	
	if (whereAmI() == 3) {
		document.getElementById("highlighBox").attachEvent("onclick",showMovieIE);
	}
}

function highlightThumb(index) {
	var	browser = whereAmI();
	var	highlight = document.getElementById("highlighBox");
	var	targetImage = document.getElementById("thumbTable").getElementsByTagName("td")[index].getElementsByTagName("img")[0];

	if ((browser < 3) && (highlight.style.display == "none")) {
		var	imageNames = new Array("images/changeTitleThumb.jpg","images/deleteThumb.jpg","images/duplicateThumb.jpg","images/moveThumb.jpg","images/resizeThumb.jpg","images/sortThumb.jpg");
		highlight.style.display = "block";
		var		left = targetImage.offsetParent.offsetLeft;
		var		top = targetImage.offsetParent.offsetTop;
		
		if (browser == 1) {
			highlight.style.top = top - 14 + "px";
			highlight.style.left = left + 31 + "px";
			targetImage.src = imageNames[index];
		}
		else if (browser == 2) {
			left = targetImage.offsetParent.offsetParent.offsetLeft;
			top = targetImage.offsetParent.offsetParent.offsetTop;	
			highlight.style.top = top + 4 + "px";
			highlight.style.left = left + 31 + "px";
			targetImage.src = imageNames[index];
		}	
	}
	else if ((browser == 3) && (highlight.currentStyle.display == "none") && (window.event.srcElement == targetImage)) {


		var	imageNames = new Array("images/changeTitleThumb.jpg","images/deleteThumb.jpg","images/duplicateThumb.jpg","images/moveThumb.jpg","images/resizeThumb.jpg","images/sortThumb.jpg");
		var		left = targetImage.offsetParent.offsetLeft;
		var		top = targetImage.offsetParent.offsetTop;

		highlight.style.display = "block";
		left = targetImage.offsetParent.offsetParent.offsetLeft;
		top = targetImage.offsetParent.offsetParent.offsetTop;	
		highlight.style.top = top - 14 + "px";
		highlight.style.left = left + 31 + "px";
		targetImage.src = imageNames[index];
	}
	
}

function mouseOut(index) {
	var	targetImage = document.getElementById("thumbTable").getElementsByTagName("td")[index].getElementsByTagName("img")[0];
	var	highlight = document.getElementById("highlighBox");
	var	imageNames = new Array("images/changeTitleThumbBW.jpg","images/deleteThumbBW.jpg","images/duplicateThumbBW.jpg","images/moveThumbBW.jpg","images/resizeThumbBW.jpg","images/sortThumbBW.jpg");

	if (whereAmI() == 3) {
		if (window.event.srcElement != targetImage) {
			var i, count = imageNames.length;
			
			for (i = 0; i < count; i++) {
				targetImage = document.getElementById("thumbTable").getElementsByTagName("td")[i].getElementsByTagName("img")[0];
				targetImage.src = imageNames[i];
			}
			
			highlight.style.display = "none";
		}
	}
	else {
		highlight.style.display = "none";
		targetImage.src = imageNames[index];
	}
}

function fenixTotalLeft(anElement) {
	var		totalOffset = 0;
	var		parent = anElement;
	
	while (parent) {
		totalOffset += parent.offsetLeft;
		parent = parent.offsetParent;
	}

	return totalOffset;
}

function fenixTotalTop(anElement) {
	var		totalOffset = 0;
	var		parent = anElement;
	
	while (parent) {
		totalOffset += parent.offsetTop;
		parent = parent.offsetParent;
	}

	return totalOffset;	
}

function fenixWindowResize() {
	var		movieWindow = document.getElementById("movieWindow");
		
		if (fenixShowWindow) {		
			movieWindow.style.width = parseInt(movieWindow.style.width) + 14 + "px";
			movieWindow.style.height = parseInt(movieWindow.style.height) + 24 + "px";
		
			var	newWidth = parseInt(movieWindow.style.width);
			var newHeight  = parseInt(movieWindow.style.height);
			var	oldLeft = parseInt(movieWindow.style.left)+180;
			var	oldTop = parseInt(movieWindow.style.top)+285;
			var	theWindowWidth, theWindowHeight;
			
			if (whereAmI() != 3) {
				theWindowWidth = innerWidth;
				theWindowHeight = innerHeight;
			}
			else {
				if (document.documentElement && document.documentElement.clientWidth) {
					theWindowWidth = document.documentElement.clientWidth;
					theWindowHeight = document.documentElement.clientHeight;
				}
				else if (document.body.clientWidth) {
					theWindowWidth = document.body.clientWidth;
					theWindowHeight = document.body.clientHeight;
				}
			}
			
				
			if (oldLeft > theWindowWidth/2.0)
				movieWindow.style.left = parseInt(movieWindow.style.left) - (oldLeft - theWindowWidth/2.0)/6.0 + "px";
			else
				movieWindow.style.left = parseInt(movieWindow.style.left) + (theWindowWidth/2.0 - oldLeft)/6.0 + "px";
		
			if (oldTop > theWindowHeight/2.0)
				movieWindow.style.top = parseInt(movieWindow.style.top) - (oldTop - theWindowHeight/2.0)/6.0 + "px";
			else
				movieWindow.style.top = parseInt(movieWindow.style.top) + (theWindowHeight/2.0 - oldTop)/6.0 + "px";
		
		
			if (newHeight >= 560) {
				clearInterval(fenixInterval);
				movieWindow.style.width = "360px";
				movieWindow.style.height = "570px";

				document.getElementById("movieProduct").style.visibility = "visible";
			
				var	movieObject = document.getElementById("qtMoviePlayer");
				
//				if (whereAmI() != 3) {
					var	movieNames = new Array("movies/title.mp4","movies/delete.mp4","movies/duplicate.mp4","movies/move.mp4","movies/resize.mp4","movies/sort.mp4");
					movieObject.innerHTML = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="320" height="476" codebase="http://www.apple.com/qtactivex/qtplugin.cab">\n<param name="SRC" value="'+movieNames[fenixIndex]+'">\n<param name="AUTOPLAY" value="true">\n<param name="controller" value="true">\n<embed src="'+movieNames[fenixIndex]+'" width="320" height="476" autoplay="true" controller="true" pluginspage="http://www.apple.com/quicktime/download/"></embed>\n</object>\n';
				// }
				// else {
				// 	var	movieNames = new Array("movies/title.mp4","movies/delete.mp4","movies/duplicate.mp4","movies/move.mp4","movies/resize.mp4","movies/sort.mp4");
				// 	movieObject.innerHTML = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="320" height="476" codebase="http://www.apple.com/qtactivex/qtplugin.cab">\n<param name="SRC" value="'+movieNames[fenixIndex]+'">\n<param name="AUTOPLAY" value="true">\n<param name="CONTROLLER" value="true">\n<embed src="'+movieNames[fenixIndex]+'" width="320" height="476" autoplay="true" controller="true" pluginspage="http://www.apple.com/quicktime/download/"></embed>\n</object>\n';
					// var	movieNames = new Array("movies/title.wmv","movies/delete.wmv","movies/duplicate.wmv","movies/move.wmv","movies/resize.wmv","movies/sort.wmv");
					// movieObject.innerHTML = '<object width="576" height="324">\n<param name="movie" value="movies/test.flv">\n<embed src="movies/test.flv" width="576" height="324"></embed>\n</object>';
					
//					'<object id="MediaPlayer" width=320 height=490 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components…" type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112">\n<param name="filename" value="'+movieNames[fenixIndex]+'"><param name="Showcontrols" value="True">\n<param name="autoStart" value="True">\n<embed type="application/x-mplayer2" src="'+movieNames[fenixIndex]+'" name="MediaPlayer" width=320 height=490></embed>\n</object>';
			// 	}
			 }
		}
		else {
			movieWindow.style.width = parseInt(movieWindow.style.width) - 14 + "px";
			movieWindow.style.height = parseInt(movieWindow.style.height) - 24 + "px";
		
			var	newWidth = parseInt(movieWindow.style.width);
			var newHeight  = parseInt(movieWindow.style.height);
			var	oldLeft = parseInt(movieWindow.style.left);
			var	oldTop = parseInt(movieWindow.style.top);
			var	theWindowWidth, theWindowHeight;
			
			if (whereAmI() != 3) {
				theWindowWidth = innerWidth;
				theWindowHeight = innerHeight;
			}
			else {
				if (document.documentElement && document.documentElement.clientWidth) {
					theWindowWidth = document.documentElement.clientWidth;
					theWindowHeight = document.documentElement.clientHeight;
				}
				else if (document.body.clientWidth) {
					theWindowWidth = document.body.clientWidth;
					theWindowHeight = document.body.clientHeight;
				}
			}
			
			if (oldLeft > fenixTargetLeft)
				movieWindow.style.left = parseInt(movieWindow.style.left) - (oldLeft - fenixTargetLeft)/6.0 + "px";
			else
				movieWindow.style.left = parseInt(movieWindow.style.left) + (fenixTargetLeft - oldLeft)/6.0 + "px";
		
			if (oldTop > fenixTargetTop)
				movieWindow.style.top = parseInt(movieWindow.style.top) - (oldTop - fenixTargetTop)/6.0 + "px";
			else
				movieWindow.style.top = parseInt(movieWindow.style.top) + (fenixTargetTop - oldTop)/6.0 + "px";
		
		
			if (newHeight <= 195) {
				clearInterval(fenixInterval);
				movieWindow.style.top = fenixTargetTop + "px";
				movieWindow.style.left = fenixTargetLeft + "px";
				movieWindow.style.width = "135px";
				movieWindow.style.height = "190px";
				
				document.getElementById("movieWindow").style.display = "none";
				document.getElementById("movieBlackout").style.display = "none";
				
			}
		}
}

function showMovie(index) {
	var		movieWindow = document.getElementById("movieWindow");
	var		movieBackground = document.getElementById("movieBlackout");
	var		targetImage = document.getElementById("thumbTable").getElementsByTagName("td")[index].getElementsByTagName("img")[0];
	var		top = fenixTotalTop(targetImage);
	var		left = fenixTotalLeft(targetImage);
	var		products = new Array("Fenix notes 1.0, Fenix notes lite 1.0","Fenix notes 1.0.1, Fenix notes lite 1.0.1","Fenix notes 1.1, Fenix notes lite 1.1","Fenix notes 1.1","Fenix notes 1.0, Fenix notes lite 1.0","Fenix notes 1.1");

	movieWindow.style.top = top + "px";
	movieWindow.style.left = left - 2 + "px";
	movieWindow.style.width = "135px";
	movieWindow.style.height = "190px";
	movieWindow.style.display = "block";
	movieBackground.style.display = "block";
	
	document.getElementById("movieProduct").innerHTML = "<strong>Products: </strong>" + products[index];
	
	fenixTargetLeft = parseInt(movieWindow.style.left);
	fenixTargetTop = parseInt(movieWindow.style.top);
	fenixIndex = index;
	fenixShowWindow = true;
	fenixInterval =  setInterval(fenixWindowResize, 10);
}

function closeMovie() {
	var		movieProduct = document.getElementById("movieProduct");
	var		movieObject = document.getElementById("qtMoviePlayer");
			
	movieObject.innerHTML = "";
	
	fenixShowWindow = false;
	fenixInterval =  setInterval(fenixWindowResize, 10);
	
	movieProduct.style.visibility = "hidden";
}