var setSVGBgClass;
var isChrome = /chrome/.test( navigator.userAgent.toLowerCase() );


function setSVGBg()
{
	var svg = d.getElementById('homeSVG');
	var container = svg.contentDocument;
	var photo = container.getElementById('photo');
	var nav = container.getElementById('nav');
	var shadow = container.getElementById('shadow');
	var items = container.getElementById('items');
	
	//var btnPlay = container.getElementById('btnPlay');
	var videoPlayer = d.getElementById('homeVideo');
	
	var configArray = new Array();
	var itemsArray = new Array();
	var total;
	var curIndex = 0;
	var imgList = new Array();
	var navList = new Array();
	var shadowList = new Array();
	var itemsList = new Array();
	var highlightNav;
	var highlightImg;
	var timeout;
	var time = 10000;
	var resizeSVGObjsClass;
	var logoColor;
	var videoIsPlaying = false;
	var self = this;
	
	this.init = function()
	{
		configArray = [
			/*{x:2,	y:2,	logo:0},*/
			{x:2,	y:2,	logo:0},
			/*{x:3,	y:0,	logo:0},*/
			{x:2,	y:2,	logo:0},
			/*{x:1,	y:0,	logo:0},*/
			{x:1,	y:2,	logo:0},
			{x:2,	y:2,	logo:0},
			{x:1,	y:2,	logo:1},
			{x:1,	y:2,	logo:0}
		];
		
		itemsArray = [
			
			{x:1,	y:2},
			{x:0,	y:0},
			/*{x:0,	y:0},*/
			
			/*{x:0,	y:0},*/
			/*{x:0,	y:0},*/
			{x:2,	y:2},
			{x:0,	y:0},
			{x:0,	y:0},
			{x:0,	y:0}
		];
		
		resizeSVGObjsClass = new resizeSVGObjs(svg);
		resizeSVGObjsClass.addObj(photo, true);
		resizeSVGObjsClass.addObj(nav, false, 1, 2, true);
		
		this.setObjs();
		this.genNav();
		//this.setVideo();
		
		/*window.addEventListener('SVGLoad', function()
		{
		}, false);*/
		
		addEvent(window, 'resize', function()
		{
			self.autoPhotoStop();
			if (videoIsPlaying || isChrome)	location.reload();
		});
		
		// all SVG loaded and rendered
		self.autoPhotoGo();
		setTimeout(function(){self.changeNav(navList[0]);}, 200);
		
		shadow.style.display = 'block';
		items.style.display = 'block';
	};
	
	this.setObjs = function()
	{
		//Set photos
		//imgList = setChildNodes(photo, 'image');
		imgList = photo.getElementsByTagNameNS(svgns, 'image');
		shadowList = shadow.getElementsByTagNameNS(svgns, 'g');
		itemsList = items.getElementsByTagNameNS(svgns, 'g');
		total = imgList.length;
		
		for (var i=0; i<total; i++)
		{
			var title = container.getElementById('title_' + (i + 1));
			title.style.opacity = 0;
			title.style.display = 'none';
			resizeSVGObjsClass.addObj(title, false, configArray[i].x, configArray[i].y, true);
			new self.setTitleBtn(title, i);
			
			shadowList[i].style.opacity = 0;
			shadowList[i].style.display = 'none';
			
			itemsList[i].style.opacity = 0;
			itemsList[i].style.display = 'none';
			resizeSVGObjsClass.addObj(itemsList[i], false, itemsArray[i].x, itemsArray[i].y, true);
			
			imgList[i].titleItem = title;
			imgList[i].shadowItem = shadowList[i];
			imgList[i].item = itemsList[i];
			imgList[i].style.opacity = 0;
			imgList[i].style.display = 'none';
		}
		
		photo.style.display = 'block';
		imgList[0].titleItem.style.display = 'block';
	};
	
	this.genNav = function()
	{
		var tempX = 20;
		
		for (var i=0; i<total; i++)
		{
			var use = d.createElementNS(svgns, 'use');
			use.setAttribute('x', tempX);
			use.setAttribute('y', 570);
			use.setAttributeNS(xlinkns, 'xlink:href', '#photoNav');
			
			/*var use = d.createElementNS(svgns, 'circle');
			use.setAttribute('r', 4);
			use.setAttribute('fill', '#FFF');
			use.setAttribute('cx', tempX);
			use.setAttribute('cy', 570);*/
			
			use.style.opacity = 0.5;
			use.num = i;
			
			new self.setBtn(use);
			
			nav.appendChild(use);
			navList.push(use);
			
			tempX += 20;
		}
	};
	
	this.setBtn = function(btn)
	{
		function btnOver()
		{
			if (btn.hit)	return;
			if (btn == highlightNav)	return;
			//new animateSVG(btn, {a:1}, 5);
			btn.style.opacity = 2;
			btn.hit = true;
			
			for (var i=0; i<navList.length; i++)
			{
				if (navList[i] == btn)	continue;
				if (navList[i] == highlightNav)	continue;
				if (navList[i].style.opacity == 0.5)	continue;
				navList[i].style.opacity = 0.5;
			};
		};
		
		function btnOut()
		{
			btn.hit = false;
			if (btn == highlightNav)	return;
			//new animateSVG(btn, {a:0.5}, 5);
			btn.style.opacity = 0.5;
		};
		
		function btnClick()
		{
			if (btn == highlightNav)	return;
			self.changeNav(btn);
			self.autoPhotoStop();
		};
		
		btn.addEventListener('mouseover', btnOver, false);
		btn.addEventListener('mouseout', btnOut, false);
		btn.addEventListener('mousedown', btnClick, false);
	};
	
	this.changeNav = function(btn)
	{
		if (highlightNav)	highlightNav.style.opacity = 0.5;
		btn.style.opacity = 2;
		
		this.photoGo(btn.num);
		
		highlightNav = btn;
		curIndex = btn.num;
	};
	
	this.setTitleBtn = function(btn, id)
	{
		function btnOver()
		{
		};
		
		function btnOut()
		{
		};
		
		function btnClick()
		{
			//alert(id);
		};
		
		btn.addEventListener('mouseover', btnOver, false);
		btn.addEventListener('mouseout', btnOut, false);
		btn.addEventListener('mousedown', btnClick, false);
	};
	
	this.autoPhotoGo = function()
	{
		timeout = setInterval(function()
		{
			curIndex ++;
			if (curIndex >= total)	curIndex = 0;
			
			self.changeNav(navList[curIndex]);
		}, time);
	};
	
	this.autoPhotoStop = function()
	{
		clearTimeout(timeout);
		self.autoPhotoGo();
	};
	
	this.photoGo = function(id)
	{
		var offset = (isMobile)?	0.5:1;
		
		if (highlightImg)
		{
			new animateSVG(highlightImg, {a:0}, 5*offset);
			new animateSVG(highlightImg.titleItem, {a:0}, 3*offset);
			new animateSVG(highlightImg.shadowItem, {a:0}, 3*offset);
			new animateSVG(highlightImg.item, {a:0}, 3*offset);
		}
		imgList[id].style.opacity = 0.01;
		new animateSVG(imgList[id], {a:1}, 8*offset);
		new animateSVG(imgList[id].titleItem, {a:1}, 5*offset);
		new animateSVG(imgList[id].shadowItem, {a:1}, 3*offset);
		new animateSVG(imgList[id].item, {a:1}, 3*offset);
		//sbringToFrontSVG(imgList[id]);
		
		highlightImg = imgList[id];
		
		//Change Logo color
		var color = configArray[id].logo;
		if (color != logoColor)
		{
			changeLogoColor(color);
			logoColor = color
		}
	};
	
	this.setVideo = function()
	{
		var video = videoPlayer.getElementsByTagName('video')[0];
		
		self.setVideoBtn(btnPlay);
		
		if (isMobile && !isAndroid)
		{
			video.addEventListener('click', function()
			{
				video.play();
			},false);
			
			video.addEventListener('ended', function()
			{
				//alert('complete');
				self.videoOpen(false);
			},false);
		}
	};
	
	this.setVideoBtn = function(btn)
	{
		function btnOver()
		{
			btn.setAttribute('preserveAspectRatio', 'xMinYMax slice');
		};
		
		function btnOut()
		{
			btn.setAttribute('preserveAspectRatio', 'xMinYMin slice');
		};
		
		function btnClick()
		{
			self.videoOpen(true);
		};
		
		btn.addEventListener('mouseover', btnOver, false);
		btn.addEventListener('mouseout', btnOut, false);
		btn.addEventListener('mousedown', btnClick, false);
		btn.style.cursor = 'pointer';
	};
	
	this.videoOpen = function(isOpen)
	{
		if (isOpen)
		{
			self.playVideo();
			videoPlayer.style.display = 'block';
			clearTimeout(timeout);
		}
		else
		{
			videoPlayer.style.display = 'none';
			self.autoPhotoGo();
		}
		videoIsPlaying = isOpen;
	};
	
	this.playVideo = function()
	{
		if (isMobile && !isAndroid)
		{	
			var video = videoPlayer.getElementsByTagName('video')[0];
			var source = video.getElementsByTagName('source')[0];
			//source.src = videoPath;
			//video.poster = posterPath;
			
			//Fix for iOS3
			var canPlay = video.canPlayType(source.type);
			if (canPlay == 'probably' || canPlay == 'maybe')
			{
				video.src = source.src;
				video.load();
			}
			
			video.autobuffer = true;
			video.load();
			//if (video.currentTime > 0)	video.currentTime = 0;
			video.play();
			
			video.style.display = 'block';
		}
		else
		{
			self.genSwfVideo();
		}
	};
	
	this.genSwfVideo = function()
	{
		videoPlayer.innerHTML = '';
		
		var div = createTag('span', 'videoSWF');
		videoPlayer.appendChild(div);
		
		var flashvars = {};
		flashvars.swfPath = '';
		//flashvars.preview = posterPath;
		flashvars.flv = cloudServer + 'en/images/videos/home_video.flv';
		
		var params = {};
		params.allowscriptaccess = 'always';
		params.allowfullscreen = 'true';
		params.wmode = 'opaque';
		
		var attributes = {};
		
		swfobject.embedSWF(cloudServer + 'images/swf/flvplayer.swf', 'videoSWF', '100%', '100%', '10.0.0', '', flashvars, params, attributes);
	};
	
	this.init();
}
