// JavaScript Document

function gallery_slider()
{
	// get how many thumbs we have
	var total_thumbs = $$('.thumbnail').length;
	
	// work out how many slides we have
	var total_slides =  Math.ceil(total_thumbs/7);
	
	// page start
	var slide_focus = 1;
	
	// selected_img
	var selected_img = 1;
	
	//URI
	var first_image = $('img_'+selected_img).get('title');
	
	
	// tween vars
	 
	var main_image_fade = new Fx.Morph('main_image', {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
	main_image_fade.set({ 'opacity': 0 });
	
	var url_array = window.location.hash.split('/');
	if(typeof(url_array[1]) == 'undefined')
	{
		var myURI = new URI('#/image/'+first_image+'/').go();
	}
	

	//////////////////////////////////////////////////////////////////////////////
	///////////////////          BIG IMAGE CLICK               //////////////////
	////////////////////////////////////////////////////////////////////////////
	
	$('main_image').addEvent('click:relay(img)', function() 
	{ 
		// increase the counter
		selected_img++; 
			
		// if we are at the end
		if(selected_img > total_thumbs)
		{
			selected_img =1;
		}
			else
		{
			selected_img = selected_img;
		}
			
		var image_id =  $('img_'+selected_img).get('title');
			
			main_image_fade.start({ 'opacity': 0 });
			main_image_fade.addEvent( 'complete', function ()  { 
				
			var myURI = new URI('#/image/'+image_id+'/').go();
			
		} );
		
		 				
	});
	
	

	//////////////////////////////////////////////////////////////////////////////
	///////////////////         THUMBNAIL SCROLLER             //////////////////
	////////////////////////////////////////////////////////////////////////////
	
	
	// resize the holder
	var new_width = parseInt(document.id('container-width').get('text'));
	$('thumb_slilder').setStyle('width',new_width);

	var scrollElement = $('thumbnails_box_container');  
	var portfolioScroller = new Scroller(scrollElement, {area: 300, velocity:0.2, fps:50  });
	
	scrollElement.addEvent('mouseover', portfolioScroller.start.bind(portfolioScroller));
	scrollElement.addEvent('mouseleave', portfolioScroller.stop.bind(portfolioScroller));
 
 
	
	// thumnbail events
 
	$$('.thumbnail').addEvents({ 
		'mouseover' : function()
		{
			this.fade(1,0.5);
		},
		'mouseout' : function()
		{
			this.fade(0.5,1);
		},
		'click' : function()
		{
			
			var image_id =  this.get('title');
			selected_img = this.get('id').replace('img_','');
			
			var myURI = new URI('#/image/'+image_id+'/').go();
			
		}
	});
	
	
}

function videoPlayer()
{
	var im = $$('img.loadedImage');
	if(im.get('rel')[0]!=='image')
	{
	
		var video = (im.get('rel')[0]);
		var poster = (im.get('src')[0]);
		
		var newobj = new Swiff('/video_player.swf', {
		    id: 'videoPlayerSWF',
		    width: 800,
		    height: 550,
		    params: {
		        'wmode': 'opaque',
		        'bgcolor': '#000000',
		        'allowFullScreen': false
		    },
		    vars: {
		        video_asset: video,
		        video_poster: poster	    
		      } 
		});
		newobj.inject(document.id('flashLoader'));
		im.destroy();
	}
}


function requestFile(file_path)
{
	var loadFile = new Request.HTML({url:file_path,  
	onSuccess: function(htmlImages) 
	{ 
		$('main_image').set('html', '');
		$('main_image').adopt(htmlImages);
		
		videoPlayer();
		
		Cufon.refresh('.cufon_text');
		},
		onComplete: function()
		{
						
			$('main_image').fade('in');
		}
		}).get();
}

var HistoryManage = new Class({
     
     	
		urlValue : null,
	initialize: function(firstValue){
		
		this.urlValue = firstValue;
		this.counter = '0';
		
    },
	
	checkURL: function()
	{
		var page_var = location.hash.replace(/^#/,'');
		return page_var;
	},
	
	
	checkState: function()
	{	    
		if(this.checkURL() != this.urlValue)
		{	
			
			// set values forselfhistory
			this.urlValue = this.checkURL();
			
			//var url_array = new Array();
			url_array = this.checkURL().split('/');
			
			//if(typeof(url_array[1]) !== 'undefined')
			//{
				var pageType = url_array[1];
				var pageNo = url_array[2]
	  
				var main_image_fade = new Fx.Morph('main_image', {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
				main_image_fade.start({ 'opacity': 0 });
				main_image_fade.addEvent( 'complete', function ()  { 
				 
					requestFile('/image/' + pageNo + '/') 
					Cufon.refresh('.cufon_text');
				 });
			 //}
		} 
		
	},

	refreshLoad: function()
	{	   		
			// set values forselfhistory
			this.urlValue = this.checkURL();
			
			//var url_array = new Array();
			url_array = this.checkURL().split('/');
	

			//if(typeof(url_array[1]) !== 'undefined')
			//{ 
				var pageType = url_array[1];
				var pageNo = url_array[2]
	   
				var main_image_fade = new Fx.Morph('main_image', {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		 		main_image_fade.start({ 'opacity': 0 });
				main_image_fade.addEvent( 'complete', function ()  { 
				 
					requestFile('/image/' + pageNo + '/') 
					Cufon.refresh('.cufon_text');
				 });
			// }
		
	},
	
	startListener: function()
	{
		this.checkRefresh = this.checkState.bind(this).periodical(100);
	},
	
	stopListener: function()
	{
		$clear(this.checkRefresh);
	}
	 
});


