
function videoComplete(){

}

loadPlayer = new Class({
	__playerContainer: 'mediaplayer',
	__shouldLoop: false,
	__shouldReset: true,
	__shouldPlay: true,
	__scrollers:[],
	__scrollersEffects:[],
	__tabs:[],
	__controls:[],
	__mediaTabs:[],
	__mediaLists:[],
	__controlCount:0,
	__currentPanel:null,
	__maxSteps:[],
	__nowStep:[],
	__countType:'reset',
	
	initialize: function(){
		var hasThumbs;
		if($$('div.medialist ol ul').length > 0){
			hasThumbs = true;
		} else {
			hasThumbs = false;
		}
		if(hasThumbs){
			this.setUpThumbnailView();
		}
		this.setUpPlayer(mediaPath);
	},
	setUpThumbnailView: function(){
		this.setUpTabs();
		this.setUpScrollerControls();
		this.setUpScrollers();

		this.showPanel(1);
		
	},
	setUpThumbnails: function(scroller){
		scroller.getElements('ul li a').each(function(anchor,i){
			anchor.addEvent('click',function(evt){
				this.setUpPlayer(anchor.href);
				this.getMediaItemDetails(anchor.href.split('?')[1].split('=')[1]);
				evt = new Event(evt).stop();
			}.bind(this));
		}.bind(this));
	},
	setUpTabs: function(){
		this.__tabs = $$('div.medialist h3');
		var mediaTabs = new Element('div',{'class':'mediatabs'});
		this.__tabs.each(function(header,i){
			var tab = new Element('div',{'class':'mediatab'});
			tab.setText(header.getText());
			tab.addEvent('click',function(evt){
				this.showPanel(i);
				evt = new Event(evt).stop();
			}.bind(this));
			header.remove();
			this.__mediaTabs.push(tab);
			tab.injectTop(mediaTabs);
		}.bind(this));
		mediaTabs.injectTop($('medialistcontainer'));
	},
	showPanel: function(set){
		this.hidePanels();
		this.__mediaTabs[set].addClass('selected');
		this.__mediaLists[set].setStyle('display','block');
		this.__currentPanel = set;
		this.__nowStep[set] = 1;
		this.scrollReset('reset');
	},
	hidePanels: function(){
		this.__mediaTabs.each(function(tab,i){
			tab.removeClass('selected');
		});
		this.__mediaLists.each(function(list,i){
			list.setStyle('display','none');
		});
	},
	setUpScrollers: function(){
		this.__scrollers = $$('div.medialist ol');
		this.__scrollers.each(function(scroller,i){
			this.setUpThumbnails(scroller);
			this.__mediaLists.push(scroller);
			this.__scrollersEffects[i] = new Fx.Scroll(scroller.getParent(), {duration: 400, wait: false, transition: Fx.Transitions.Quad.easeInOut});
			this.__scrollersEffects[i].addEvent('onComplete',function(){ this.resetScrollingCount(this.__countType); }.bind(this));
			var liCount = scroller.getElements('ul li').length;
			this.__maxSteps[i] = Math.ceil((liCount/4))-1;
			this.__nowStep[i] = 1;
			scroller.setStyles({
				'display': 'none',
				'width': (520 * (this.__maxSteps[i] + 1)) + 'px'
			});
		}.bind(this));
	},
	setUpScrollerControls: function(){
		this.__controls = $$('div.medianavigation a.medianavitem');
		this.__controlCount = $$('div.medianavigation span')[0];
		this.__controls.each(function(control,i){
			if(i == 0) {
				if(this.__scrollersEffects[i]){
					this.__scrollersEffects[i].cancel();
				}
				control.addEvent('click', function(evt){
					this.__countType = 'minus';
					this.scrollLeft();
					evt = new Event(evt).stop();
				}.bindWithEvent(this));
			} else {
				if(this.__scrollersEffects[i]){
					this.__scrollersEffects[i].cancel();
				}
				control.addEvent('click', function(evt){
					this.__countType = 'plus';
					this.scrollRight();
					evt = new Event(evt).stop();
				}.bindWithEvent(this));
			}
		}.bind(this));
	},
	scrollLeft: function(){
		if(this.__nowStep[this.__currentPanel] != 1){
			this.__nowStep[this.__currentPanel] = this.__nowStep[this.__currentPanel]-1;
			this.__scrollersEffects[this.__currentPanel].scrollTo((520 * (this.__nowStep[this.__currentPanel])),0);
		} else {
			this.__scrollersEffects[this.__currentPanel].scrollTo(0,0);
		}
	},
	scrollRight: function(){
		if(this.__nowStep[this.__currentPanel] < this.__maxSteps[this.__currentPanel]){
			this.__scrollersEffects[this.__currentPanel].scrollTo((520 * this.__nowStep[this.__currentPanel]),0);
			this.__nowStep[this.__currentPanel] = this.__nowStep[this.__currentPanel]+1;
		} else {
			this.__scrollersEffects[this.__currentPanel].scrollTo((520 * this.__maxSteps[this.__currentPanel]),0);
		}
	},
	scrollReset: function(){
		this.__countType = 'reset';
		this.__scrollersEffects[this.__currentPanel].scrollTo(0,0);
		this.__nowStep[this.__currentPanel] = 1;
	},
	resetScrollingCount: function(type){
		var selectedList = this.__mediaLists[this.__currentPanel];
		
		var listTotal = selectedList.getElements('ul li').length;
		
		if(type != 'reset'){
			var oldText = this.__controlCount.getText();
			var exFirst = oldText.split(' to ')[0].toInt();
			var exLast = oldText.split(' to ')[1].split(' of ')[0].toInt();
		
		}

		switch(type){
			case 'plus':
				var firstItem = ((exFirst+4)>listTotal)? exFirst : (exFirst+4);
				var lastItem = ((exLast+4)>=listTotal)? listTotal : (exLast+4);
				memberOnlyOffset++;
			break;
			case 'minus':
				if(listTotal > 4){
					var firstItem = ((exFirst-4)<=0)? 1 : (exFirst-4);
					
					if((listTotal-exFirst) < 4){
						var lastItem = (exLast-(listTotal-exFirst))-1;
					} else {
						var lastItem = ((exLast-4)<=0)? exLast : (exLast-4);
					}
				} else {
					var firstItem = 1;
					var lastItem = listTotal;
				}
				memberOnlyOffset--;
			break;
			default:
				var firstItem = 1;
				var lastItem = (listTotal>4)? 4: listTotal;
		}
		var msg = firstItem + ' to ' + lastItem + ' of ' + listTotal + ' items';
		this.__controlCount.setText(msg);
	},
	getMediaItemDetails: function(id){
		var URL = '/site/media/media-details?id=' + id;
		var makeRoadmapAjaxRequest = new Ajax(URL, {
			method: 'get',
			onComplete: function(response){
				$('mediaDetailsContainer').setHTML(response);
				// members only links
				if($$('a.membersonlycheck').length>0){
					new memberOnlyPopup();
				}
				// Configure button rollovers
				$('mediaDetailsContainer').getElements('div.button img').each(function(e) {
					var fileType;
					if(e.src.match(/.gif/)){ fileType = '.gif'; }
					if(e.src.match(/.png/)){ fileType = '.png'; }
					if(e.src.match(/.jpg/)){ fileType = '.jpg'; }
					var imgSrc = e.src;
					if(e.src.match(/-o./)){ imgSrc.replace(/-o./,'.') }
					imgSrc = e.src.split(fileType);
					e.addEvent('mouseover', function(e) { this.setProperty('src', imgSrc[0] + '-o' + fileType) });
					e.addEvent('mouseout', function(e) { this.setProperty('src', imgSrc[0] + fileType) });
				});
				if(window.ie6){
					var ie6Fixing = $('pagecontainer').getElements('img');
					ie6Fixing.each(function(el,i){
						var imgSrc = el.getProperty('src');
						if(imgSrc.match('.png')){
							imgSrc = imgSrc.replace(/.png/,'.gif');
							el.setProperty('src',imgSrc);
						}
					});
				}
				
			}.bind(this)
		});
		makeRoadmapAjaxRequest.request();
	},
	setUpPlayer: function(fileName){
		var mediaFile = fileName.split('?')[0];
		$(this.__playerContainer).empty();
		var insert = new Element('div',{'id':'mediaplayerinsert'}).inject(this.__playerContainer);
		
		// Load Flash Movie
		var flashvars = {
			source: mediaFile,
			loop: this.__shouldLoop,
			autoReset: this.__shouldReset,
			autoPlay: this.__shouldPlay
		};
		var params = {
			allowscriptaccess: 'always',
			allowfullscreen: 'true',
			wmode: 'transparent'
		};
		var attributes = {};
		swfobject.embedSWF('/include/media/mediaplayer.swf','mediaplayerinsert','320','240','9.0.0','/include/media/expressInstall.swf', flashvars, params, attributes);
	}
	
});