
var plugin
var main
var stage_width

var directory="./screens/"
var contentArr=new Array("screen1","screen2","screen3","screen4","screen5","screen6","screen1","screen2","screen3","screen4","screen5","screen6","screen1","screen2","screen3","screen4","screen5","screen6")



var tile_img_width=440
var tile_img_height=500
var tile_spacing
var tile_top=0
var tile_scale=1
var time_to_move=0.6
var pressed=false
var screen_cnt=1
var slideShowInt=0

var objsArr =new Array()
objsArr["tilesHolder"]=new Object()



function mainCanvasLoaded(s) {

	//alert("main loaded")
		
	main=s.findName("mainCanvas")
	plugin = s.getHost()
	
	plugin.content.onFullScreenChange = onFullScreenChanged
	
	stage_width=plugin.width	
	tile_spacing=stage_width/2+tile_img_width/2
	
	hdr=main.findName("info").text="Screen 1/"+contentArr.length
	
	buildImages()
	
}


function onFullScreenChanged() {

	
	if(plugin.content.ActualWidth==1024) {
	
		main.findName("MST").scaleX=1
		main.findName("MST").scaleY=1
	
	}
}


function startSlideShow() {

	if(slideShowInt==0) {
	
		plugin.content.fullScreen = true			
		
		main.findName("MST").scaleX=plugin.content.ActualWidth/1024
		main.findName("MST").scaleY=plugin.content.ActualHeight/768		
		
		arrowRef=main.findName("arrowRight")
		arrowPress(arrowRef)
		slideShowInt=setInterval("arrowPress(arrowRef)",2500)
		window.status="next"
	}
	else {
	
		plugin.content.fullScreen = false   	
		
		main.findName("MST").scaleX=1
		main.findName("MST").scaleY=1
	
		clearInterval(slideShowInt)
		slideShowInt=0
		window.status="stopped"
	}
}



var tilesHolder_left

function buildImages() {

	
	tilesHolder_left=stage_width/2-tile_img_width/2
	
	var tileHolder_str = '<Canvas Name="tilesHolder" Canvas.Left="'+tilesHolder_left+'" Canvas.Top="100">';	
	
	for(i=0;i<contentArr.length;i++) {		
			
		tileHolder_left=i*tile_spacing  //(450+160)	
		tileHolderReflection_top=(tile_img_height*2)
		
				
		tileHolder_str += 		'<Canvas Name="tileHolder_'+i+'" Canvas.Left="'+tileHolder_left+'" Canvas.Top="'+tile_top+'" >'
		
		tileHolder_str += 		'	<Canvas Name="imgHolderReflection_'+i+'" Canvas.Top="'+tileHolderReflection_top+'">'
		tileHolder_str += 		'		<Canvas.OpacityMask>'
		tileHolder_str += 		'			<LinearGradientBrush  StartPoint="0,0" EndPoint="0,1">'           
		tileHolder_str += 		'	      		<GradientStop Offset="0.37" Color="#00000000"  />'      
		tileHolder_str += 		'	      		<GradientStop Offset="1" Color="#88000000"  />'
		tileHolder_str += 		'	      	</LinearGradientBrush>'          
		tileHolder_str += 		'		</Canvas.OpacityMask>'
		tileHolder_str += 		'		<Canvas.RenderTransform><TransformGroup><ScaleTransform Name="ReflectionScaleTransform_'+i+'" ScaleY="-1" ScaleX="1" CenterX="0" CenterY="0" /></TransformGroup></Canvas.RenderTransform>'
		tileHolder_str +=		'		<Image Name="imgReflection_'+i+'" Stretch="Fill" Width="'+tile_img_width+'" Height="'+tile_img_height+'" Source="'+directory+contentArr[i]+'.jpg" />'
		tileHolder_str +=		'	</Canvas>'		

		tileHolder_str += 		'	<Canvas Name="imgHolder_'+i+'"  >'
		tileHolder_str += 		'		<Image Name="img_'+i+'" Stretch="Fill" Width="'+tile_img_width+'" Height="'+tile_img_height+'" MouseEnter="tileEnter" MouseLeave="tileLeaveX" MouseLeftButtonDown="tilePressX" Source="'+directory+contentArr[i]+'.jpg" />'
		tileHolder_str +=		'	</Canvas>'
		
		tileHolder_str +=		'</Canvas>'	

		
	
	}


	tileHolder_str += '</Canvas>';	           
	tileHolderTag=plugin.content.createFromXaml(tileHolder_str)
	main.children.add(tileHolderTag)

}




function arrowEnter(s) {
	s.opacity=0.7
}

function arrowLeave(s) {
	s.opacity=0.15
}

function arrowPress(s,e) {
	
	
	if(e) {
		clearInterval(slideShowInt)
	}
	holder=objsArr["tilesHolder"]
	holderRef=s.findName("tilesHolder")
	var propX=holderRef["Canvas.Left"]
	var num=(contentArr.length-1)*tile_spacing-tilesHolder_left
	
	rightArrowRef=main.findName("arrowRight")
	leftArrowRef=main.findName("arrowLeft")

	//window.status=propX+" "+num
	
	if(!pressed) {
	
		pressed=true

		if(s.name=="arrowRight") {
			
			
			leftArrowRef.visibility="Visible"
			leftArrowRef.opacity=0.15
			
			if(propX!=-num) {
				var final_propX=propX-tile_spacing
				screen_cnt++
				if(screen_cnt==contentArr.length) {
					rightArrowRef.visibility="Collapsed"				
				}				
				
			}
			else {
				var final_propX=propX
				screen_cnt=contentArr.length
				clearInterval(slideShowInt)
			}
		}
		else {
		
			rightArrowRef.visibility="Visible"
			rightArrowRef.opacity=0.15
			
			if(propX!=tilesHolder_left) {
				var final_propX=propX+tile_spacing
				screen_cnt--
				if(screen_cnt==1) {
					leftArrowRef.visibility="Collapsed"				
				}
			}
			else {
				var final_propX=propX
				screen_cnt=1
			}
		}


		holder.tweenX = new Tween(new Object(),'x',Tween.regularEaseOut,propX,final_propX,time_to_move)

		holder.tweenX.onMotionChanged = function(event){	
			holderRef["Canvas.Left"] = event.target._pos
		}

		holder.tweenX.onMotionFinished = function(event){
			pressed=false	
			hdr=main.findName("info").text="Screen "+screen_cnt+"/"+contentArr.length
		}

		
		holder.tweenX.start()	
		
	
	}

}





