if(typeof hotvitrinplayer == "undefined") {
	var hotvitrinplayer = new Object();
	hotvitrinplayer.dataArray=new Array();
	hotvitrinplayer.arrayWidth=6;
	hotvitrinplayer.mainCanvas;
	hotvitrinplayer.lastPicture=1;
	hotvitrinplayer.looperTimeout;
}

hotvitrinplayer.vitrin = function(cnt,cfg) {
	this.configuration = {
		data:''
	};
	for(itm in this.configuration) {
		if(cfg[itm] != undefined) {
			if (itm.indexOf('color') > 0) { 
				this.configuration[itm] = cfg[itm].substr(cfg[itm].length-6);
			} else {
				this.configuration[itm] = cfg[itm];
			}
		}
	}
	
	hotvitrinplayer.dataArray=this.configuration['data'].split("|");

	Silverlight.createObjectEx({
		source:'silver/vitrin.xaml',
		parentElement:cnt,
		properties:{
			width:'960',
			height:'358',
			version:'1.0',
			framerate:'30',
			inplaceInstallPrompt:true,
			isWindowless:true,
			background:'#2d2d2d'
		},
		events:{
		},
		context:this
	});
}


function vitrinMainCanvasLoaded(sender) {
	hotvitrinplayer.mainCanvas=sender;
	buildVitrinEnvironment(sender);
	sender.findName("slideLeftSmallPicture1").Begin();
	vitrinPlayerLooper(1);
}

function buildVitrinEnvironment(sender){
var w=hotvitrinplayer.arrayWidth;	
var xamlSTR;
var xamlTAG;
var arrLen = hotvitrinplayer.dataArray.length;
var imageSpace= Math.round((358-arrLen/w*60)/(arrLen/w+1));

	for (x=0; x<arrLen; x++) {
	  if ((x%w)==0) {
		xamlSTR="<Image Name='BigPicture"+Math.floor(x/w+1)+"' Source='"+hotvitrinplayer.dataArray[x]+"' Width='640' Height='358'  />";					
		xamlTAG=sender.getHost().content.createFromXaml(xamlSTR);
		sender.findName("vitrinLeftPanel").children.add(xamlTAG);

		xamlSTR="<StackPanel><StackPanel.Resources><Storyboard  Name='fadeinBigPicture"+Math.floor(x/w+1)+"'><DoubleAnimation Storyboard.TargetName='BigPicture"+Math.floor(x/w+1)+"' Storyboard.TargetProperty='Opacity' From='0.0' To='1.0' Duration='0:0:1' /></Storyboard><Storyboard  Name='slideLeftSmallPicture"+Math.floor(x/w+1)+"'><DoubleAnimation Storyboard.TargetName='Rectangle"+Math.floor(x/w+1)+"' Storyboard.TargetProperty='Width' From='260' To='300' Duration='0:0:1' /><DoubleAnimation Storyboard.TargetName='SmallPicture"+Math.floor(x/w+1)+"' Storyboard.TargetProperty='(Canvas.Left)' From='0' To='-40' Duration='0:0:1' /></Storyboard><Storyboard  Name='slideRightSmallPicture"+Math.floor(x/w+1)+"'><DoubleAnimation Storyboard.TargetName='Rectangle"+Math.floor(x/w+1)+"' Storyboard.TargetProperty='Width' From='300' To='260' Duration='0:0:1' /><DoubleAnimation Storyboard.TargetName='SmallPicture"+Math.floor(x/w+1)+"' Storyboard.TargetProperty='(Canvas.Left)' From='-40' To='0' Duration='0:0:1' /></Storyboard></StackPanel.Resources></StackPanel>";		
		xamlTAG=sender.getHost().content.createFromXaml(xamlSTR);
		sender.findName("tweenHolder").children.add(xamlTAG);
		
		xamlSTR="<Canvas Name='SmallPicture"+Math.floor(((x+1)/w)+1)+"' Canvas.Top='"+(Math.floor(x/w)*(60+imageSpace)+imageSpace)+"' MouseLeftButtonDown='watchVideo_MouseLeftButtonDown' MouseLeave='SmallPicture_MouseLeave' MouseEnter='SmallPicture_MouseEnter' ><Rectangle  Name='Rectangle"+Math.floor(x/w+1)+"' Height='60' Width='260' Fill='Black' Stroke='#FFFAFAFE' StrokeThickness='1' RadiusX='5' RadiusY='5' Opacity='0.5'/><Image Source='"+hotvitrinplayer.dataArray[x+1]+"' Width='79' Height='52' Canvas.Top='4' Canvas.Left='4' /><TextBlock Text='' Canvas.Top='4' Canvas.Left='90' Foreground='Red' FontSize='12'/><TextBlock Text='"+hotvitrinplayer.dataArray[x+3]+"' Canvas.Top='14' Canvas.Left='90' Foreground='White' FontSize='11' TextWrapping='Wrap' LineHeight='14'  Width='160' LineStackingStrategy='BlockLineHeight'/></Canvas>";
		xamlTAG=sender.getHost().content.createFromXaml(xamlSTR);
		sender.findName("vitrinRightPanel").children.add(xamlTAG);
	  }
		  
	}
}



function vitrinPlayerLooper (imgID){
	var w=hotvitrinplayer.arrayWidth;	
	repaintImages(imgID);
	if (imgID<hotvitrinplayer.dataArray.length/w) {	imgID++; }
	else {	imgID=1; }
	var timeoutstr="vitrinPlayerLooper("+imgID+")";
	hotvitrinplayer.looperTimeout=setTimeout(timeoutstr,5000);
	
}


function SmallPicture_MouseEnter(sender, eventArgs) {
	sender.Cursor='Hand';
	clearTimeout(hotvitrinplayer.looperTimeout);
	var imgID= sender.Name.charAt(sender.Name.length-1);	
	repaintImages(imgID);
}

function SmallPicture_MouseLeave(sender, eventArgs) {
	var imgID= sender.Name.charAt(sender.Name.length-1);
	vitrinPlayerLooper(imgID);
}


function repaintImages(imgID) {
	var w=hotvitrinplayer.arrayWidth;
	var main=hotvitrinplayer.mainCanvas;
	
	for (x=1; x<=hotvitrinplayer.dataArray.length/w; x++) {
		main.findName("BigPicture"+x)["Canvas.ZIndex"]=0; 
	}

	main.findName("BigPicture"+imgID)["Canvas.ZIndex"]=200;
	main.findName("BigPicture"+hotvitrinplayer.lastPicture)["Canvas.ZIndex"]=100;
	
	main.findName("watchTitle").Text=hotvitrinplayer.dataArray[(imgID-1)*w+2];
	main.findName("watchText").Text=hotvitrinplayer.dataArray[(imgID-1)*w+4];


	if (imgID!=hotvitrinplayer.lastPicture)	{
		main.findName("fadeinBigPicture"+imgID).Begin();
		main.findName("slideLeftSmallPicture"+imgID).Begin();
		main.findName("slideRightSmallPicture"+hotvitrinplayer.lastPicture).Begin();
		main.findName("watchVideoFader").Begin();
	}
	hotvitrinplayer.lastPicture=imgID;
}

function watchVideo_MouseLeftButtonDown(){
	var w=hotvitrinplayer.arrayWidth;	
	window.location = hotvitrinplayer.dataArray[(hotvitrinplayer.lastPicture-1)*w+5]
}

function watchVideo_MouseEnter(sender, eventArgs){
	sender.Cursor='Hand';
}


