var text = null; 
var parentHeight = null;
var textHeight = null;
var distance = null;
var top = 0;
var move = 1;
var upDown = true;
var finished = false;

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
	document.getElementById("VideoFlash").innerHTML = "<div class=\"VideoContainer\" style=\"width:350px;\tmargin:auto; height:auto; text-align:center; padding:0px;\">\n\t<div id=\"Video\" style=\"width:auto;\theight:auto; display:block;\tfont-family:Arial, Helvetica, sans-serif;\">\n    \t<div id=\"VideoHolder\" style=\"background-color:#000;\">\n    \t\t<video controls preload=\"none\">\n                <source  type=\"video\/mp4\"\/>\n\t\t\t<\/video>\n        <\/div>      \n\t\t<h2 style=\"text-align:left;\tdisplay:block;\tmargin:0px; margin-top: 3px;\tfont-size:12px;\tcolor:#666;\">Description<\/h2>\n        <p id=\"Info\" style=\"text-align:left; font-size:11px; color:#fff; padding:0px; margin:0px; display:block; padding-left:5px;\"><\/p>\n    <\/div>\n    <div class=\"VideoTile\" style=\"display:inline-block;\twidth:100px; height:auto; text-align:center; margin-right:10px;\tposition:relative;\">\n    \t<img src=\"data\/cms\/images\/facet.jpg\" style=\"width:100px; height:100px; padding:1px; border:1px solid #000; cursor:pointer;\">\n    \t<video src=\"http:\/\/www.kodiakvideoproductions.com\/FACET%20FINAL%20VIDEO%20HD.m4v\" style=\"display:none; width:0px; height:0px; visibility:hidden;\" width=\"\" controls=\"controls\"><\/video>\n        <p style=\"font-size:12px; font-family:Arial, Helvetica, sans-serif;\tpadding:0px; margin:0px; text-align:center;\">Shop Facet<\/p>\n    <\/div>\n    <div class=\"VideoTile\" style=\"display:inline-block;\twidth:100px; height:auto; text-align:center; margin-right:10px;\tposition:relative;\">\n    \t<img src=\"data\/cms\/images\/once-upon-a-time.jpg\" style=\"width:100px; height:100px; padding:1px; border:1px solid #000; cursor:pointer;\">\n    \t<video src=\"http:\/\/www.kodiakvideoproductions.com\/Facet_Not-Fantasy1_HD.m4v\" style=\"display:none; width:0px; height:0px; visibility:hidden;\"><\/video>\n        <p style=\"font-size:12px; font-family:Arial, Helvetica, sans-serif;\tpadding:0px; margin:0px; text-align:center;\">It\'s Not Fantasy<\/p>\n    <\/div>\n    <div class=\"VideoTile\" style=\"display:inline-block;\twidth:100px; height:auto; text-align:center; margin-right:10px;\tposition:relative;\">\n    \t<img src=\"data\/cms\/images\/we-buy-gold.jpg\" style=\"width:100px; height:100px; padding:1px; border:1px solid #000; cursor:pointer;\">\n        <video src=\"http:\/\/www.kodiakvideoproductions.com\/Facet_We-Buy-Gold1_HD.m4v\" style=\"display:none; width:0px; height:0px; visibility:hidden;\"><\/video>\n        <p style=\"font-size:12px; font-family:Arial, Helvetica, sans-serif;\tpadding:0px; margin:0px; text-align:center;\">We Buy Gold<\/p>\n    <\/div>\n<\/div>";
	var Tiles = document.getElementsByClassName("VideoTile");
	var Play = "<div class='Play' style= 'position:absolute;left:2px;top:2px;height:100px;width:100px;z-index:1000;background-image:url(http://www.kodiakvideoproductions.com/play.png);cursor:pointer;'></div>";
	var innerHTML;
	var VideoPlayer = document.getElementById("VideoPlayer");
	var Info = document.getElementById("Info");
	var VideoContent = document.getElementById("VideoHolder");
	var currentVideo;

	innerHTML = Tiles[0].innerHTML;
	Tiles[0].innerHTML += Play;
	currentVideo = Tiles[0];
	Swap(Tiles[0],true);
	for (x = 0; x < Tiles.length; x++)
	{
		Tiles[x].onclick = function() {
			if (this != currentVideo || bool)
	{
		currentVideo.innerHTML = innerHTML;
		currentVideo = this;
		innerHTML = currentVideo.innerHTML;
		currentVideo.innerHTML += Play;			
		for (y = 0; y < this.childNodes.length; y++)
			if(this.childNodes[y].tagName == "VIDEO")
			{
				var ua = navigator.userAgent;					
				var string = this.childNodes[y].src;
				if (ua.indexOf("OS 3") > -1)
				{
					string = string.replace(".m4v", "-iphone.m4v");
				}
				VideoHolder.innerHTML = "<video controls preload='none'><source src=\"" + string + "\"  type='video/mp4'/></video>";
			}
			else if(this.childNodes[y].tagName == "P")
				Info.innerHTML = this.childNodes[y].innerHTML;
	}
		}
	}
}
function Swap(input, bool)
{
	if (input != currentVideo || bool)
	{
		currentVideo.innerHTML = innerHTML;
		currentVideo = input;
		innerHTML = currentVideo.innerHTML;
		currentVideo.innerHTML += Play;			
		for (y = 0; y < input.childNodes.length; y++)
			if(input.childNodes[y].tagName == "VIDEO")
			{
				var ua = navigator.userAgent;					
				var string = input.childNodes[y].src;
				if (ua.indexOf("OS 3") > -1)
				{
					string = string.replace(".m4v", "-iphone.m4v");
				}
				VideoHolder.innerHTML = "<video controls preload='none'><source src=\"" + string + "\"  type='video/mp4'/></video>";
			}
			else if(input.childNodes[y].tagName == "P")
				Info.innerHTML = input.childNodes[y].innerHTML;
	}
}












/*********SLIDESHOW*****************/

var slideshow = "<div id=\"Slideshow\" style=\"position:relative;\n\twidth:500px;\n\theight:250px;\n\tbackground-color:#000;\n\tmargin:auto;\">\n\t<div id=\"SlideshowImage1\" style=\"position:absolute;\n\ttop:0px;\n\tleft:0px;\n\theight:100%;\n\twidth:100%;\n\tz-index:1000;\t\n\tborder:4px solid #666;\n\tborder-bottom-left-radius:10px;\n\tborder-bottom-right-radius:10px;\">&nbsp;<\/div>\n\t<div id=\"SlideshowImage2\" style=\"position:absolute;\n\ttop:0px;\n\tleft:0px;\n\theight:100%;\n\twidth:100%;\n\tz-index:1000;\t\n\tborder:4px solid #666;\n\tborder-bottom-left-radius:10px;\n\tborder-bottom-right-radius:10px; visibility:hidden\">&nbsp;<\/div>\n<\/div>\n<div id=\"TextInfo\" style=\"width:500px;\n\theight:20px;\n\tposition:relative;    \n    margin:auto;\n\tmargin-top:12px;\n\toverflow:hidden;\">\n<p id=\"Text\" style=\"padding:0px;\n\tmargin:0px;\n\tposition:absolute;\n\tleft:5px;\n\ttop:0px;\n\tfont-size:14px;\n\tfont-weight:bold;\n\tfont-family:Helvetica;\n\tcolor:#999;\"><\/p>\n<\/div> \n";

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
	document.getElementById("flashMovie").innerHTML = slideshow;
	var nav = document.getElementById("primary-nav");
	nav.childNodes[1].style.paddingLeft = 9 + "px";
	nav.childNodes[1].style.paddingRight = 9 + "px";
	for (x = 0; x < nav.childNodes.length; x++)
	{
		if(nav.childNodes[x].tagName == "LI")
		{
			nav.childNodes[x].firstChild.style.fontSize = 11 + "px";
		}
	}
	var Images = 3;
	var Height = 250;
	var Width = 500;
	var WaitInterval = 4500; //Change me to change the time it takes to swap images. (time is in milliseconds
	var CurrentImage = 1;
	var BlurTimer;	
	var BlurCounter = 100; 
	var BlurLength = 10;  //Change the time it takes to blur the fade the images. (int stands for how many times to cycle each x milliseconds)
	var Swap = 1;
	var FirstRun = true;
	var PicArray = new Array();
	var Season;
	var dontrun = false;	
	var test = 0;
	var url = "url(data/cms/images/slideshow-";
	var textArray = new Array();
	textArray[0] = "Our Amelia Showroom";
	textArray[1] = "Skilled Jewelers On-Site";
	textArray[2] = "Our Milford Showroom";
			
	for (x = 1; x < Images + 1; x++)
	{
		PicArray[x] = new Image(Width,Height); 
		PicArray[x].src = "data/cms/images/slideshow-"+ x + ".jpg";
	}
	
	//Write the first image of the slideshow
	document.getElementById("SlideshowImage1").style.background = url + "" + 1 + ".jpg)";
	window.onload = new function(){
	setTimeout("Change();", WaitInterval);
}

function Change()
{
	if (CurrentImage == Images)
			CurrentImage = 0;
	if (Swap == 0 || FirstRun)
	{
		document.getElementById("SlideshowImage2").style.background = url + "" + (CurrentImage + 1) + ".jpg)";		
	}
	else
	{
		document.getElementById("SlideshowImage1").style.background = url + ""  + (CurrentImage + 1) + ".jpg)";
	}
	CurrentImage++;
	upDown = true;
	finished = false;
	Blur();
}
	
function Blur()
{	
	if(!dontrun)
		setTimeout("BlurMath()", BlurLength);	
}	

function pauseBlur()
{
	dontrun = true;
}

function runBlur()
{
	dontrun = false;
	Blur();
}
function BlurMath()
{
	if(!finished)
		moveText();
	for (x = 0; x < 2; x++)
	{
		if (Swap == 0 || FirstRun)
		{
			document.getElementById("SlideshowImage1").style.filter = "alpha(opacity=" + BlurCounter + ")";
			document.getElementById("SlideshowImage1").style.MozOpacity = BlurCounter/100;
			document.getElementById("SlideshowImage1").style.opacity = BlurCounter/100;
			
			document.getElementById("SlideshowImage2").style.visibility = "visible";
			document.getElementById("SlideshowImage2").style.filter = "alpha(opacity=" + (100 - BlurCounter) + ")";
			document.getElementById("SlideshowImage2").style.MozOpacity = (100 - BlurCounter)/100;
			document.getElementById("SlideshowImage2").style.opacity = (100 - BlurCounter)/100;
			
		}
		else
		{
			
			document.getElementById("SlideshowImage2").style.filter = "alpha(opacity=" + BlurCounter + ")";
			document.getElementById("SlideshowImage2").style.MozOpacity = BlurCounter/100;
			document.getElementById("SlideshowImage2").style.opacity = BlurCounter/100;
			
			document.getElementById("SlideshowImage1").style.visibility = "visible";
			document.getElementById("SlideshowImage1").style.filter = "alpha(opacity=" + (100 - BlurCounter) + ")";
			document.getElementById("SlideshowImage1").style.MozOpacity = (100 - BlurCounter)/100;
			document.getElementById("SlideshowImage1").style.opacity = (100 - BlurCounter)/100;
			
		}
	}
	if (BlurCounter <= 0)
	{
		
		if (Swap == 0 || FirstRun)
		{
			document.getElementById("SlideshowImage1").style.visibility = "hidden";
			Swap = 1;
			
			FirstRun = false;
		}
		else
		{
			document.getElementById("SlideshowImage2").style.visibility = "hidden";
			Swap = 0;
		}
		
		BlurCounter = 100;
		setTimeout("Change();", WaitInterval);
	}
	else
	{
		BlurCounter-=2;
		Blur();
	}
}




///TEXT FUNCTIONS///

init();
}
function init()
{
	 text = document.getElementById("Text");
	 text.innerHTML = textArray[CurrentImage - 1];
	 parentHeight = document.getElementById("TextInfo").offsetHeight;
	 parentHeight -=2;
	 textHeight = text.offsetHeight;
}
function moveText()
{
	if(top >= parentHeight)
	{
		upDown = false;
		top -= move;
		if (top %10 == 0)
			move--;
		text.style.top = top + "px";
		swapText();
		//setTimeout("moveText();",30);
	}
	else if(upDown)
	{
		top += move;
		if (top %10 == 0)
			move++;
		text.style.top = top + "px";
		//setTimeout("moveText();",30);
	}
	else
	{
		top -= move;
		if (top %10 == 0)
			move--;
		text.style.top = top + "px";
		if(top <= 0)
		{
			top = 0;
			move = 1;
			text.style.top = top + "px";
			finished = true;
		}
	}
}

function swapText()
{
	text.innerHTML = textArray[CurrentImage - 1];
}
