(function(){try{document.createEvent("TouchEvent")}catch(b){return}["touchstart","touchmove","touchend"].each(function(d){Element.NativeEvents[d]=2});var a={mousedown:"touchstart",mousemove:"touchmove",mouseup:"touchend",mouseover:"touchstart"};var c=function(d){var e=d.event.changedTouches[0];d.page={x:e.pageX,y:e.pageY};return true};for(var b in a){Element.Events[b]={base:a[b],condition:c}}})();


(function(){
// dollar safe mode (enscapsulate)

var $ = document.id;

/*
$ main controls of site
+  positioning...
+ style...
+ history
*/
var mcp =
   new Class({  
   
    initialize: function(firstUrlValue){
	//core.
	this.path = "",
	this.container = $("main")
	this.content = $("main-content"),
	this.imageViewer = $('image-viewer'),
	this.thumbViewer = $('thumb-viewer'),
	this.imageBlock = $('image-block')
	this.clickAble = true,
	this.loadImage = [],
	this.imgArray = [],
	this.thumbArray = [],
	this.imgDiv = [],
	this.currentDivPos = 0,
	// history management
	this.iframe = null,
	this.ieHistory = 0,
	this.pageVar,
	this.urlValue = firstUrlValue,
  	this.urlArray = new Array(),
  	this.createIframe(),
	this.siteURI,
	// global 
	intThis = this;
},
 
createIframe : function(){ 
	if(Browser.ie6) 
	{
		this.iframe = new IFrame({ src: "blank.htm", styles : { "position":"absolute","top": 0,"left": 0,"width":"1px","height":"1px","display":"none"}}).inject(document.body);
	 	doc = (this.iframe.contentDocument) ? this.iframe.contentDocument : this.iframe.contentWindow.document;
		doc.open();
		doc.write("<html><body id='state'" +this.checkURL() + "</body></html>");
		doc.close(); 
	}
 },
	
checkURL: function()
{
	this.pageVar = location.hash.replace(/^#/,"");
	return this.pageVar;
},

checkState: function()
{
	if(Browser.ie6) 
	{ 
		doc = (this.iframe.contentDocument) ? this.iframe.contentDocument  : this.iframe.contentWindow.document;
		var  docURL = doc.body.innerHTML;
		if(this.urlValue !== docURL)
		{
			var myURI = new URI("#" + docURL).go();
			this.urlValue = docURL;
		}
	}
  
	if(intThis.checkURL() != intThis.urlValue)
	{
  		// set values for history...
  		intThis.urlValue = intThis.checkURL();
		// tells our page what to do 
  		intThis.setContent('',intThis.urlValue);

		if(Browser.ie6)
  		{
			doc = (this.iframe.contentDocument) ? this.iframe.contentDocument  : this.iframe.contentWindow.document;
			doc.open();
			doc.write("<html><body id='state'>" + this.checkURL() + "</body></html>");
			doc.close();
		}
	}
},

refreshLoad: function()
{ 
	intThis.urlValue = intThis.checkURL();
  	// load content into page.
  	intThis.setContent('',intThis.urlValue);
},

startListener: function()
{
	intThis.checkRefresh = intThis.checkState.periodical(500);
},

stopListener: function()
{
	clearInterval(intThis.checkRefresh);
},

doesTouch : function()
{
	try{document.createEvent("TouchEvent");return true;}catch(e){return false;}
},

thumbs : function()
{

	// build all the div tags for the current page
	intThis.imgDiv = [],   intThis.thumbArray = ($$('div.thumb'));
		intThis.thumbArray.each(function(thumb,i){
		
			intThis.imgDiv[i] = new Element('div', {'id': 'main-'+thumb.getElement('a').get('href').replace('#/','').replace("/","-").replace("/","-"), 'class':'full-image'}).inject(intThis.imageBlock); 
			intThis.imgDiv[i].store('pos',i);
			intThis.imgDiv[i].store('url',thumb.getElement('a').get('href'));
			intThis.imgDiv[i].store('loaded',false);
			intThis.imgDiv[i].store('video',thumb.getElement('img').get('rel'));
			intThis.imgDiv[i].store('title',thumb.getElement('img').get('title'));
	});
    	
    
    // clicking on a thumb	
	intThis.content.addEvents({
		'click:relay(div.thumb)':function(e){
			e.preventDefault();
			
			intThis.stopListener();
			
			window.location.hash = this.getElement('a').get('href');
			
			// update the current urlValues, otherwise we will get a reload of the files...
			intThis.urlValue = (window.location.hash).replace("#","");
		
			// load page
			intThis.setContent(this,'');
				
			intThis.startListener();
		}
	});
	
	
	// Close bar
	$('close-image').hide();
	$('close-image').addEvents({
	    'click':function(e)
	    {
	    	intThis.stopListener();
			
			window.location.hash = ''
			
			// update the current urlValues, otherwise we will get a reload of the files...
			intThis.urlValue = (window.location.hash).replace("#","");
		
			// load page
	    	intThis.imageViewer.morph({'opacity':[1,0],'display':'none'});
			if(intThis.imgDiv[intThis.currentDivPos].retrieve('video')!==null) { intThis.imgDiv[intThis.currentDivPos].empty(); }
				
			intThis.startListener();
			
			$(this).hide();
			intThis.thumbViewer.show();
			$(document.body).setStyle('overflow','visible');
		}
    });
    
    $('right-arrow').addEvents({
    	'click':function(e)
    	{     		
    		
    		if(intThis.clickAble)
    		{
				if(intThis.imgDiv[intThis.currentDivPos].retrieve('video')!==null) { intThis.imgDiv[intThis.currentDivPos].empty(); }
				intThis.clickAble = false;
				
				intThis.stopListener();
				
				// update position 
				intThis.currentDivPos++; 
				if(intThis.currentDivPos>intThis.imgDiv.length-1)
				{
					intThis.currentDivPos = 0;
				}
			
				window.location.hash = intThis.imgDiv[intThis.currentDivPos].retrieve('url')
				
				// update the current urlValues, otherwise we will get a reload of the files...
				intThis.urlValue = (window.location.hash).replace("#","");
			
				// load page
				intThis.setContent('',intThis.imgDiv[intThis.currentDivPos].retrieve('url').replace("#",""));
				
				intThis.startListener();
			}
    	}
    });
    
    $('left-arrow').addEvents({
    	'click':function(e)
    	{     		
			// update position
			if(intThis.clickAble)
    		{
				if(intThis.imgDiv[intThis.currentDivPos].retrieve('video')!==null) { intThis.imgDiv[intThis.currentDivPos].empty(); }
				intThis.clickAble = false;
				
				intThis.currentDivPos--;
				if(intThis.currentDivPos< 0)
				{
					intThis.currentDivPos = intThis.imgDiv.length-1;
				}
				
				intThis.stopListener();
				
				window.location.hash = intThis.imgDiv[intThis.currentDivPos].retrieve('url')
				
				// update the current urlValues, otherwise we will get a reload of the files...
				intThis.urlValue = (window.location.hash).replace("#","");
			
				// load page
				intThis.setContent('',intThis.imgDiv[intThis.currentDivPos].retrieve('url').replace("#",""));
				
				intThis.startListener();		
			}	
    	}
    });
    
},

setContent: function(img , url)
{  
	// it is a selected page, work out what to load in
	if(url.length > 0)
	{
		intThis.urlArray = new Array(),
		intThis.urlArray =url.split("/");
		var id = url.replace("/","-").replace("/","-").replace("/","-");
		 
		var imgEl = [], tmpImg = [];
		  
		$$('.full-image').setStyles({'opacity':0,'display':'none'});
		
		intThis.imageViewer.morph({'opacity':1,'display':'block'});
		
		if($('main'+id).retrieve('loaded')==false)
 		{
 			if($('main'+id).retrieve('video')!==null)
 			{
 				var e = function()
				{
					intThis.manageSize();
					$('main'+id).morph({'opacity':1,'display':'block'}); 
	 			}
	 		 
	 			e.delay(10)
		 		
 				intThis.videoPlayer('vid-'+id,$('main'+id), $('main'+id).retrieve('video'), $('thumb'+id).get('src').replace('thumb-','large-'));
 				
 				intThis.clickAble = true;
		 		new Element('div',{ 'class':'asset-text', html:$('main'+id).retrieve('title')}).inject($('main'+id));
 			}
 				else
 			{
		  		imgEl = new Image(); 
				imgEl.inject($('main'+id));
		 		imgEl.onload = function()
				{ 
					$('main'+id).morph({'opacity':1,'display':'block'});
					
					imgEl.set('width', imgEl.width)
					imgEl.set('height', imgEl.height);
					 
					// fit image
					intThis.fitImage(imgEl, intThis.imageBlock);	
								
					// updated load status
					$('main'+id).store('loaded',true);	
					var d = function()
					{
						intThis.clickAble = true;
		 			}
		 				d.delay(500)
		 		}
		 		
		 		imgEl.src = $('thumb'+id).get('src').replace('thumb-','large-');
		 		new Element('div',{ 'class':'asset-text', html:$('main'+id).retrieve('title')}).inject($('main'+id));
	 		}
	 	}
	 		else
	 	{
			imgEl = $('main'+id).getElement('img');
			// fit image
			var a = function() { $('main'+id).morph({'opacity':1,'display':'block'}); intThis.fitImage(imgEl, intThis.imageBlock) } 
			a.delay(50);
			intThis.clickAble = true;
	 	}
	 		
		// update position
		intThis.currentDivPos = $('main'+id).retrieve('pos');
		// show close
		$('close-image').show();
		intThis.thumbViewer.hide();
		$(document.body).setStyle('overflow','hidden');
	}
		else
	{
		$('close-image').hide();
		intThis.thumbViewer.show();
		intThis.imageViewer.morph({'opacity':0,'display':'none'});
		$(document.body).setStyle('overflow','visible');
	}
	
	if(img!='')
	{ 
		var imgEl = [], tmpImg = [], id = img.getElement('img').get('id').replace('thumb-','').replace("/","-").replace("/","-");
		$$('.full-image').setStyles({'opacity':0,'display':'none'});
		
		intThis.imageViewer.morph({'opacity':1,'display':'block'});
 		 
 		if($('main-'+id).retrieve('loaded')==false)
 		{
 			if($('main-'+id).retrieve('video')!==null)
 			{	 
 				var e = function()
				{
					intThis.manageSize();
					$('main-'+id).morph({'opacity':1,'display':'block'}); 
	 			}
		 		e.delay(100)
 				intThis.videoPlayer('vid-'+id,$('main-'+id), $('main-'+id).retrieve('video'), $('thumb-'+id).get('src').replace('thumb-','large-'));
 				
 				intThis.clickAble = true;
 			}
 				else
 			{
		 		imgEl = new Image();
		 		imgEl.inject($('main-'+id));
		 		imgEl.onload = function()
				{ 
					imgEl.set('width', imgEl.width)
					imgEl.set('height', imgEl.height);
					
					// updated load status
					$('main-'+id).store('loaded',true);
					intThis.clickAble = true;
					
					// fit image
					var a = function() { $('main-'+id).morph({'opacity':1,'display':'block'}); intThis.fitImage(imgEl, intThis.imageBlock) } 
					a.delay(50);
	 			}
	 		
	 			imgEl.src = img.getElement('img').get('src').replace('thumb-','large-');
		 	}
		 		
		 	new Element('div',{ 'class':'asset-text', html:$('main-'+id).retrieve('title')}).inject($('main-'+id));
			intThis.currentDivPos = $('main-'+id).retrieve('pos');
	 			
 		}
 			else
 		{
 			imgEl = $('main-'+id).getElement('img');
			
			// fit image
			var a = function() { $('main-'+id).morph({'opacity':1,'display':'block'}); intThis.fitImage(imgEl, intThis.imageBlock) } 
			a.delay(50);
				
			intThis.clickAble = true;
 		}
		// show close
		$('close-image').show();
		intThis.thumbViewer.hide();
		$(document.body).setStyle('overflow','hidden');
	}
	
},

videoPlayer : function(passId,el, video, poster)
{
	
		var newobj = new Swiff('/video_player.swf', {
		id: passId,
		width: '100%',
		height:'100%',
		params: {
		    'wmode': 'opaque',
		    'bgcolor': '#ffffff',
		    'allowFullScreen': false
		},
		vars: {
		    video_asset: video,
		    video_poster: poster	    
		  } 
		});
		
		newobj.inject(el);
			
	if(Browser.Platform.ios == true)
	{
		el.set('html', '<video id="vidobj" src = "/uploads/video/'+video+'" controls poster="'+poster+'" autoplay loop width="900" height="506"></video>');
	 
	}	 
},

 
fitImage : function (el, parentEl)
{	
	var windowX = 0, windowY = 0, imgRatio = 0,imgWidth=0,imgHeight=0;
  
	windowX = parentEl.getWidth();
 	windowY =  parentEl.getHeight()   
 	
 	if(el.get('tag')=='img')
 	{
 		imgRatio = el.get('width')/el.get('height');
 		imgWidth = el.get('width');
 		imgHeight = el.get('height');
 	}
 		else
 	{
		imgRatio = el.getSize().x/el.getSize().y;
		imgWidth = el.getSize().x;
		imgHeight = el.getSize().y;
	}  
	
	var newHeight = windowY;
	var newWidth = newHeight*imgRatio;
	
	if(parentEl.getWidth() < newWidth)
	{
		 newWidth = windowX;
		 newHeight = newWidth/imgRatio
 	}
 	
 	el.setStyles({'width' :newWidth, 'height' :newHeight});
},

manageSize:function()
{
	intThis.content.setStyles({
		'height' : $(window).getHeight() - 150
	});	
	 
	intThis.imageViewer.setStyles({
		'width':intThis.content.getWidth(),
		'height' : $(window).getHeight() - 160
	});	 
	
	intThis.imageBlock.setStyles({
		'width': $(window).getWidth() - (intThis.container.getPosition().x*2)-60,
		'height' : $(window).getHeight() - 160
	});	
	
	if(typeof(intThis.imgDiv[intThis.currentDivPos])!=='undefined')
	{
		intThis.imgDiv[intThis.currentDivPos].setStyles({
			'width': $(window).getWidth() - (intThis.container.getPosition().x*2)-60,
			'height' : $(window).getHeight() - 160
		});	
		
		if(intThis.imgDiv[intThis.currentDivPos].getElement('img') !== null)
		{
			var imgEl = intThis.imgDiv[intThis.currentDivPos].getElement('img');
					
			// fit image
			intThis.fitImage(imgEl, intThis.imgDiv[intThis.currentDivPos]);
		}	
	}

	if($('page-text')!==null)
	{
		$('page-text').setStyle('margin-top', ((intThis.content.getHeight() - $('page-text').getHeight())/2)-30 )
	}
	
	if($('index-image')!==null)
	{
		$('thumb-viewer').setStyles({
			'width':intThis.content.getWidth(),
			'height' : $(window).getHeight() - 160
		});
		
		$('index-image').setStyles({
			'width':intThis.content.getWidth(),
			'height' : $(window).getHeight() - 160
		});

		$('index-image').setStyles({'opacity':1,'display':'block'});
		var imgEl = $('index-image').getElement('img');
		intThis.fitImage(imgEl, $('index-image'));
			
	}
	
	if($('index-image')==null && $('page-text')==null)
	{
	if($$('.thumb').length<3)
	{
		$('thumb-viewer').setStyles({
			'width' : 360,
			'left': (intThis.content.getWidth()-360)/2
		});
	}
	}
}
});

// 
var callMCP;
window.addEvent("domready", function(){
	var URL = location.hash.replace(/^#/,"");
	callMCP = new mcp(URL);
	callMCP.thumbs();
	callMCP.manageSize();
	callMCP.refreshLoad();
	callMCP.startListener();
 }).addEvent("resize", function(){	
  
	callMCP.manageSize();
 });
 })();
