// define global variable

var ssRTN100131_plugin
var ssRTN100131_main
var ssRTN100131_directory
var ssRTN100131_contentArr=new Array()
var ssRTN100131_tile_img_width
var ssRTN100131_tile_img_height
var ssRTN100131_tile_spacing
var ssRTN100131_tile_top=0
var ssRTN100131_time_to_move=0.6
var ssRTN100131_pressed=false
var ssRTN100131_screen_cnt=1
var ssRTN100131_image_ready=false
var ssRTN100131_tilesHolder_left
var ssRTN100131_objsArr =new Array()
ssRTN100131_objsArr["tilesHolder"]=new Object()


//create silverlight object begin
createSlideShow = function(cnt,cfg) {
	this.configuration = {
		width:'',
		height:'',
		imagedirectory:'',
		imagearray:'',
		backgroundcolor:'2d2d2d',
		windowless:'true'
	};
	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];
			}
		}
	}
	ssRTN100131_tile_img_height=this.configuration['height'];
	ssRTN100131_tile_img_width=this.configuration['width'];
	ssRTN100131_directory=this.configuration['imagedirectory'];
	ssRTN100131_contentArr=this.configuration['imagearray'];
	Silverlight.createObjectEx({
		source: "silver/slideshow.xaml",
		parentElement:cnt,
		properties:{
			width:'100%',
			height:this.configuration['height'],
			version:'1.0',
			framerate:'30',
			inplaceInstallPrompt:true,
			isWindowless:this.configuration['windowless'],
			background:'#'+this.configuration['backgroundcolor']
		},
		events:{
		},
		context:this
	});
}


//create silverlight object end



// slide show main begin


function ssRTN100131_mainCanvasLoaded(s) {

	ssRTN100131_main=s.findName("ssRTN100131_mainCanvas")
	ssRTN100131_plugin = s.getHost()
	ssRTN100131_plugin.content.onResize = ssRTN100131_myResize
	ssRTN100131_myResize(s)
}



function ssRTN100131_myResize(s) {
	if (ssRTN100131_plugin.content.actualWidth>0){
		if(!ssRTN100131_image_ready) {
			ssRTN100131_buildImages(s)
		} else {
			ssRTN100131_screen_cnt=1
			s.findName("ssRTN100131_arrowLeft").Visibility="Collapsed"
			s.findName("ssRTN100131_arrowRight").Visibility="Visible"
			s.findName("ssRTN100131_arrowRight").opacity=0.15
			ssRTN100131_tile_img_width=ssRTN100131_tile_img_width*ssRTN100131_plugin.content.actualHeight/ssRTN100131_tile_img_height
			ssRTN100131_tile_img_height=ssRTN100131_plugin.content.actualHeight
			s.findName("ssRTN100131_arrowRight")['Canvas.Left']=ssRTN100131_plugin.content.actualWidth-20
			s.findName("ssRTN100131_arrowRight")['Canvas.Top']=ssRTN100131_plugin.content.actualHeight/2
			s.findName("ssRTN100131_arrowLeft")['Canvas.Top']=ssRTN100131_plugin.content.actualHeight/2
			s.findName("tilesHolder")['Canvas.Left']=ssRTN100131_plugin.content.actualWidth/2-ssRTN100131_tile_img_width/2
			ssRTN100131_tile_spacing=ssRTN100131_plugin.content.actualWidth/2+ssRTN100131_tile_img_width/2
			for(i=0;i<ssRTN100131_contentArr.length;i++) {		
				s.findName("tileHolder_"+i)['Canvas.Left']=ssRTN100131_tile_spacing*i
				s.findName("img_"+i).width=ssRTN100131_tile_img_width*ssRTN100131_plugin.content.actualHeight/ssRTN100131_tile_img_height
				s.findName("img_"+i).height=ssRTN100131_plugin.content.actualHeight
			}
		}
	}
}


function ssRTN100131_buildImages(s) {
	ssRTN100131_tile_spacing=ssRTN100131_plugin.content.actualWidth/2+ssRTN100131_tile_img_width/2
	ssRTN100131_tilesHolder_left=ssRTN100131_plugin.content.actualWidth/2-ssRTN100131_tile_img_width/2
	
	var tileHolder_str = '<Canvas Name="tilesHolder" Canvas.Left="'+ssRTN100131_tilesHolder_left+'" Canvas.Top="0">';	
	
	for(i=0;i<ssRTN100131_contentArr.length;i++) {		
		tileHolder_left=i*ssRTN100131_tile_spacing  
		tileHolderReflection_top=(ssRTN100131_tile_img_height*2)
		tileHolder_str += 		'<Canvas Name="tileHolder_'+i+'" Canvas.Left="'+tileHolder_left+'" Canvas.Top="'+ssRTN100131_tile_top+'" >'
		tileHolder_str += 		'	<Canvas Name="imgHolder_'+i+'"  >'
		tileHolder_str += 		'		<Image Name="img_'+i+'" Stretch="Fill" Width="'+ssRTN100131_tile_img_width+'" Height="'+ssRTN100131_tile_img_height+'"  Source="'+ssRTN100131_directory+ssRTN100131_contentArr[i]+'.jpg" />'
		tileHolder_str +=		'	</Canvas>'
		
		tileHolder_str +=		'</Canvas>'	
	}
	tileHolder_str += '</Canvas>';	           
	tileHolderTag=ssRTN100131_plugin.content.createFromXaml(tileHolder_str)
	ssRTN100131_main.children.add(tileHolderTag)
	ssRTN100131_image_ready=true
	ssRTN100131_myResize(s)
}




function ssRTN100131_arrowEnter(s) {
	s.opacity=0.7
}

function ssRTN100131_arrowLeave(s) {
	s.opacity=0.15
}

function ssRTN100131_arrowPress(s,e) {
	
	holder=ssRTN100131_objsArr["tilesHolder"]
	holderRef=s.findName("tilesHolder")
	var propX=holderRef["Canvas.Left"]
	var num=(ssRTN100131_contentArr.length-1)*ssRTN100131_tile_spacing-ssRTN100131_tilesHolder_left
	
	rightArrowRef=ssRTN100131_main.findName("ssRTN100131_arrowRight")
	leftArrowRef=ssRTN100131_main.findName("ssRTN100131_arrowLeft")

	
	if(!ssRTN100131_pressed) {
	
		ssRTN100131_pressed=true

		if(s.name=="ssRTN100131_arrowRight") {
			
			
			leftArrowRef.visibility="Visible"
			leftArrowRef.opacity=0.15
			
			if(propX!=-num) {
				var final_propX=propX-ssRTN100131_tile_spacing
				ssRTN100131_screen_cnt++
				if(ssRTN100131_screen_cnt==ssRTN100131_contentArr.length) {
					rightArrowRef.visibility="Collapsed"				
				}				
				
			}
			else {
				var final_propX=propX
				ssRTN100131_screen_cnt=ssRTN100131_contentArr.length
			}
		}
		else {
		
			rightArrowRef.visibility="Visible"
			rightArrowRef.opacity=0.15
			
			if(propX!=ssRTN100131_tilesHolder_left) {
				var final_propX=propX+ssRTN100131_tile_spacing
				ssRTN100131_screen_cnt--
				if(ssRTN100131_screen_cnt==1) {
					leftArrowRef.visibility="Collapsed"				
				}
			}
			else {
				var final_propX=propX
				ssRTN100131_screen_cnt=1
			}
		}


		holder.ssRTN100131_TweenX = new ssRTN100131_Tween(new Object(),'x',ssRTN100131_Tween.regularEaseOut,propX,final_propX,ssRTN100131_time_to_move)

		holder.ssRTN100131_TweenX.onMotionChanged = function(event){	
			holderRef["Canvas.Left"] = event.target._pos
		}

		holder.ssRTN100131_TweenX.onMotionFinished = function(event){
			ssRTN100131_pressed=false	
		}

		
		holder.ssRTN100131_TweenX.start()	
		
	
	}

}

// slide show main end

//ssRTN100131_Tween begin


function ssRTN100131_Delegate() {}
ssRTN100131_Delegate.create = function (o, f) {
	var a = new Array() ;
	var l = arguments.length ;
	for(var i = 2 ; i < l ; i++) a[i - 2] = arguments[i] ;
	return function() {
		var aP = [].concat(arguments, a) ;
		f.apply(o, aP);
	}
}

ssRTN100131_Tween = function(obj, prop, func, begin, finish, duration, suffixe){
	this.init(obj, prop, func, begin, finish, duration, suffixe)
}
var ssRTN100131_t = ssRTN100131_Tween.prototype;

ssRTN100131_t.obj = new Object();
ssRTN100131_t.prop='';
ssRTN100131_t.func = function (t, b, c, d) { return c*t/d + b; };
ssRTN100131_t.begin = 0;
ssRTN100131_t.change = 0;
ssRTN100131_t.prevTime = 0;
ssRTN100131_t.prevPos = 0;
ssRTN100131_t.looping = false;
ssRTN100131_t._duration = 0;
ssRTN100131_t._time = 0;
ssRTN100131_t._pos = 0;
ssRTN100131_t._position = 0;
ssRTN100131_t._startTime = 0;
ssRTN100131_t._finish = 0;
ssRTN100131_t.name = '';
ssRTN100131_t.suffixe = '';
ssRTN100131_t._listeners = new Array();	
ssRTN100131_t.setTime = function(t){
	this.prevTime = this._time;
	if (t > this.getDuration()) {
		if (this.looping) {
			this.rewind (t - this._duration);
			this.update();
			this.broadcastMessage('onMotionLooped',{target:this,type:'onMotionLooped'});
		} else {
			this._time = this._duration;
			this.update();
			this.stop();
			this.broadcastMessage('onMotionFinished',{target:this,type:'onMotionFinished'});
		}
	} else if (t < 0) {
		this.rewind();
		this.update();
	} else {
		this._time = t;
		this.update();
	}
}
ssRTN100131_t.getTime = function(){
	return this._time;
}
ssRTN100131_t.setDuration = function(d){
	this._duration = (d == null || d <= 0) ? 100000 : d;
}
ssRTN100131_t.getDuration = function(){
	return this._duration;
}
ssRTN100131_t.setPosition = function(p){
	this.prevPos = this._pos;
	var a = this.suffixe != '' ? this.suffixe : '';
	this.obj[this.prop] = Math.round(p) + a;
	this._pos = p;
	this.broadcastMessage('onMotionChanged',{target:this,type:'onMotionChanged'});
}
ssRTN100131_t.getPosition = function(t){
	if (t == undefined) t = this._time;
	return this.func(t, this.begin, this.change, this._duration);
};
ssRTN100131_t.setFinish = function(f){
	this.change = f - this.begin;
};
ssRTN100131_t.geFinish = function(){
	return this.begin + this.change;
};
ssRTN100131_t.init = function(obj, prop, func, begin, finish, duration, suffixe){
	if (!arguments.length) return;
	this._listeners = new Array();
	this.addListener(this);
	if(suffixe) this.suffixe = suffixe;
	this.obj = obj;
	this.prop = prop;
	this.begin = begin;
	this._pos = begin;
	this.setDuration(duration);
	if (func!=null && func!='') {
		this.func = func;
	}
	this.setFinish(finish);
}
ssRTN100131_t.start = function(){
	this.rewind();
	this.startEnterFrame();
	this.broadcastMessage('onMotionStarted',{target:this,type:'onMotionStarted'});
}
ssRTN100131_t.rewind = function(t){
	this.stop();
	this._time = (t == undefined) ? 0 : t;
	this.fixTime();
	this.update();
}
ssRTN100131_t.fforward = function(){
	this._time = this._duration;
	this.fixTime();
	this.update();
}
ssRTN100131_t.update = function(){
	this.setPosition(this.getPosition(this._time));
	}
ssRTN100131_t.startEnterFrame = function(){
	this.stopEnterFrame();
	this.isPlaying = true;
	this.onEnterFrame();
}
ssRTN100131_t.onEnterFrame = function(){
	if(this.isPlaying) {
		this.nextFrame();
		setTimeout(ssRTN100131_Delegate.create(this, this.onEnterFrame), 0);
	}
}
ssRTN100131_t.nextFrame = function(){
	this.setTime((this.getTimer() - this._startTime) / 1000);
	}
ssRTN100131_t.stop = function(){
	this.stopEnterFrame();
	this.broadcastMessage('onMotionStopped',{target:this,type:'onMotionStopped'});
}
ssRTN100131_t.stopEnterFrame = function(){
	this.isPlaying = false;
}

ssRTN100131_t.continueTo = function(finish, duration){
	this.begin = this._pos;
	this.setFinish(finish);
	if (this._duration != undefined)
		this.setDuration(duration);
	this.start();
}
ssRTN100131_t.resume = function(){
	this.fixTime();
	this.startEnterFrame();
	this.broadcastMessage('onMotionResumed',{target:this,type:'onMotionResumed'});
}
ssRTN100131_t.yoyo = function (){
	this.continueTo(this.begin,this._time);
}

ssRTN100131_t.addListener = function(o){
	this.removeListener (o);
	return this._listeners.push(o);
}
ssRTN100131_t.removeListener = function(o){
	var a = this._listeners;	
	var i = a.length;
	while (i--) {
		if (a[i] == o) {
			a.splice (i, 1);
			return true;
		}
	}
	return false;
}
ssRTN100131_t.broadcastMessage = function(){
	var arr = new Array();
	for(var i = 0; i < arguments.length; i++){
		arr.push(arguments[i])
	}
	var e = arr.shift();
	var a = this._listeners;
	var l = a.length;
	for (var i=0; i<l; i++){
		if(a[i][e])
		a[i][e].apply(a[i], arr);
	}
}
ssRTN100131_t.fixTime = function(){
	this._startTime = this.getTimer() - this._time * 1000;
}
ssRTN100131_t.getTimer = function(){
	return new Date().getTime() - this._time;
}
ssRTN100131_Tween.backEaseIn = function(t,b,c,d,a,p){
	if (s == undefined) var s = 1.70158;
	return c*(t/=d)*t*((s+1)*t - s) + b;
}
ssRTN100131_Tween.backEaseOut = function(t,b,c,d,a,p){
	if (s == undefined) var s = 1.70158;
	return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
ssRTN100131_Tween.backEaseInOut = function(t,b,c,d,a,p){
	if (s == undefined) var s = 1.70158; 
	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
}
ssRTN100131_Tween.elasticEaseIn = function(t,b,c,d,a,p){
		if (t==0) return b;  
		if ((t/=d)==1) return b+c;  
		if (!p) p=d*.3;
		if (!a || a < Math.abs(c)) {
			a=c; var s=p/4;
		}
		else 
			var s = p/(2*Math.PI) * Math.asin (c/a);
		
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	
}
ssRTN100131_Tween.elasticEaseOut = function (t,b,c,d,a,p){
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (!a || a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b);
	}
ssRTN100131_Tween.elasticEaseInOut = function (t,b,c,d,a,p){
	if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) var p=d*(.3*1.5);
	if (!a || a < Math.abs(c)) {var a=c; var s=p/4; }
	else var s = p/(2*Math.PI) * Math.asin (c/a);
	if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
}

ssRTN100131_Tween.bounceEaseOut = function(t,b,c,d){
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
}
ssRTN100131_Tween.bounceEaseIn = function(t,b,c,d){
	return c - ssRTN100131_Tween.bounceEaseOut (d-t, 0, c, d) + b;
	}
ssRTN100131_Tween.bounceEaseInOut = function(t,b,c,d){
	if (t < d/2) return ssRTN100131_Tween.bounceEaseIn (t*2, 0, c, d) * .5 + b;
	else return ssRTN100131_Tween.bounceEaseOut (t*2-d, 0, c, d) * .5 + c*.5 + b;
	}

ssRTN100131_Tween.strongEaseInOut = function(t,b,c,d){
	return c*(t/=d)*t*t*t*t + b;
	}

ssRTN100131_Tween.regularEaseIn = function(t,b,c,d){
	return c*(t/=d)*t + b;
	}
ssRTN100131_Tween.regularEaseOut = function(t,b,c,d){
	return -c *(t/=d)*(t-2) + b;
	}

ssRTN100131_Tween.regularEaseInOut = function(t,b,c,d){
	if ((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
	}
ssRTN100131_Tween.strongEaseIn = function(t,b,c,d){
	return c*(t/=d)*t*t*t*t + b;
	}
ssRTN100131_Tween.strongEaseOut = function(t,b,c,d){
	return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}

ssRTN100131_Tween.strongEaseInOut = function(t,b,c,d){
	if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
	return c/2*((t-=2)*t*t*t*t + 2) + b;
	}
	
//ssRTN100131_Tween end	
