HW.onload(function(){HW.VideoPlayer.init();});

HW.VideoPlayer = {
	width:0,
	frameWidth:0,
	leftPos:0,
	timer:null,
	inc:2,
	init:function() {
		var obj = this;
		this.container = _$('.libraryInner');
		if(this.container) {
			this.toggle = _$('.libraryOuter a');
			new HW.Ajax(this.toggle.href,function(r){obj.load(r);});
		}
	},
	load:function(response) {
		var obj = this;
		var reg = new RegExp("<body[^>]*>([\\w\\W]+)</body>");
		var reg2 = new RegExp("<script[^>]*>([\\w\\W]+)</script>");
		if(reg.exec(response.text)) {
			this.filmstrip = this.container.createNode('div').addClass('filmstrip').extendObject(HW.VideoPlayer.Filmstrip).init();
			var txt = reg.exec(response.text)[1];
			txt = txt.replace(reg2,'');
			var tmp = HW.createNode('div',document.body,txt).setStyle({display:'none'});
			var lib = _$('#video_library');
			this.frameWidth = this.container.offsetWidth;
			
			// get the video id from the query string if there is one
			var qs = location.search.match(/(&|\?)video=([^&]*)/)
			
			_$$('.videoThumbnail',lib).each(function(o) {
				obj.filmstrip.appendChild(o);
				o.left = -1*obj.width;
				o.right = -1*obj.width - o.offsetWidth + obj.frameWidth;
				o.bind('dragstart',function(){},false);
				obj.width += o.offsetWidth;
				_$('a',o).bind('click',function(){obj.select(o);},false);
				// if video id matches the querystring then load that video
				if(o.id && qs && o.id == qs[2]) {
					obj.select(o);
				}
			});
			this.filmstrip.setStyle({width:this.width+'px'});
			
			this.initToggle();
			if(this.width > this.frameWidth) {
				this.initFilmstrip();
			}
			tmp.empty();
		}
	},
	initToggle:function() {
		var obj = this;
		this.filmstrip.hide();
		this.toggle.bind('click',function(){
			if(obj.filmstrip.hidden) {
				obj.show();
			}
			else {
				obj.hide();
			}
		},false);
	},
	hide:function(f) {
		this.filmstrip.hide('slide',f);
		this.toggle.removeClass('libraryToggleOpen').innerHTML = 'Open Library';
	},
	show:function() {
		this.filmstrip.show('slide');
		this.toggle.addClass('libraryToggleOpen').innerHTML = 'Close Library';
	},
	initFilmstrip:function() {
		var obj = this;
		var ul = this.container.createNode('ul').addClass('filmstripNav');
		var f = function(d) {
			ul.createNode('li').addClass(d)
								.createNode('a')
									.bind('click',function(){},false)
									.bind('mousedown',function(){obj.move(d);},false)
									.bind('mouseup',function(){obj.stop()},false)
									.extendObject({title:'Scroll '+d});
		}
		f('left');
		f('right');
	},
	move:function(d) {
		var obj = this;
		var dx = d=='left'?1:-1;
		var l = this.filmstrip.leftPos + dx*this.inc++;
		this.filmstrip.setLeft(l);
		this.timer = setTimeout(function(){obj.move(d);},50);
	},
	stop:function() {
		this.inc = 2;
		clearTimeout(this.timer);
	},
	select:function(thumb) {
		if(!this.filmstrip.moved) {
			this.focus(thumb);
			var video = _$('a',thumb).href;
			var img = _$('a img',thumb);
			var title = img.alt;
			var desc = (_$('div.longDescription',thumb))?(_$('div.longDescription',thumb).innerHTML):(img.getAttribute('longDesc'));
			this.play(video,title,desc);
		}
	},
	focus:function(thumb) {
		var obj = this;
		var left = this.filmstrip.leftPos;
		if(left < thumb.left || left > thumb.right) {
			var to = this.leftPos;
			if(left < thumb.left) {
				to = thumb.left;
			}
			if(left > thumb.right) {
				to = thumb.right;
			}
			new HW.Animator(null,left,to,function(o,v){obj.filmstrip.setLeft(v);},500,null,true);
		}
	},
	play:function(v,t,d) {
		this.Video = v;
		var obj = this;
		var mov = this.getSwf('flash_video');
		if(mov) {
			if(!mov.GetVariable('videoPath')) {
				var arg = arguments;
				setTimeout(function(){arg.callee.apply(obj,arg);},10);
				return;
			}
			_$('h1.videoTitle').innerHTML = t;
			_$('p.videoDesc').innerHTML = d;
			mov.SetVariable('videoPath',v);
		}
	},
	getSwf:function(n) {
		if(document[n]) {
			return document[n];
		}
		if(HW.isIE) {
			if (document.embeds && document.embeds[n]) {
				return document.embeds[n]; 
			}
		}
		return document.getElementById(n);
	}
}

HW.VideoPlayer.Filmstrip = {
	leftPos:0,
	dragging:false,
	offsetX:0,
	init:function() {
		var obj = this;
		this.bind('mousedown',function(e){obj._drag(e);},false);
		return this;
	},
	setLeft:function(x) {
		this.leftPos = x = Math.min(Math.max(x,HW.VideoPlayer.frameWidth-HW.VideoPlayer.width),0);
		this.setStyle({left:x+'px'});
	},
	_drag:function(e){
		var obj = this;
		e=e||window.event;
		this.dragging = true;
		this.offsetX = e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft-this.leftPos;
		this.initX = this.leftPos;
		this.moved = false;
		
		HW.attachEvent(document.body,'mouseup',function(e){obj._drop();});
		HW.attachEvent(document.body,'mousemove',function(e){obj._move(e);});
	},
	_drop:function(e){
		var obj = this;
		setTimeout(function(){obj.moved = false;},1);
		this.dragging = false;
		
		HW.detachEvent(document.body,'mouseup',function(e){obj._drop();});
		HW.detachEvent(document.body,'mousemove',function(e){obj._move(e);});
	},
	_move:function(e){
		if(this.dragging) {
			e=e||window.event;
			var x = e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft-this.offsetX;
			this.setLeft(x);
			if(Math.abs(this.leftPos - this.initX) > 10) {this.moved = true;}
		}
	}
}

HW.VideoPlayer.LightBox = {
	load:function(id,url) {
		var obj = this;
		//alert(123)
		if(!this.isOpen) {
			new HW.Ajax(strTestimonialLibraryPath,function(r){obj._import(r,id);});
		}
		HW.attachEvent(document.body,'click',function(e){obj.docClose(e);});
	},
	_import:function(response,id) {
		var obj = this;
		var reg = new RegExp("<body[^>]*>([\\w\\W]+)</body>");
		var reg2 = new RegExp("<script[^>]*>([\\w\\W]+)</script>");
		if(reg.exec(response.text)) {
			var txt = reg.exec(response.text)[1];
			txt = txt.replace(reg2,'');
			var tmp = HW.createNode('div',document.body,txt).setStyle({display:'none'});
			
			var lib = _$('#video_library');
			
			_$$('.videoThumbnail',lib).each(function(o) {
				if(o.id == id) {
					obj.play(o);
				}
			});
			tmp.empty();
			tmp.parentNode.removeChild(tmp);
		}
	},
	play:function(thumb) {
		var obj = this;
		this.isOpen = true;
		var img = _$('a img',thumb);
		this.video = {
			flv:_$('a',thumb).href,
			title:img.alt,
			desc:(_$('div.longDescription',thumb))?(_$('div.longDescription',thumb).innerHTML):(img.getAttribute('longDesc'))
		}
		
		var o = _$('#flash');
		var h = o.offsetHeight;
		var top = 0;
		while(o) {
			top += o.offsetTop;
			o = o.offsetParent;
		}
		
		this.container = this.loadContainer();
		
		this.container.setStyle({width:'100px',height:'50px',marginTop:top+h/2-25+'px'});
		
		setTimeout(function(){obj.transition(top,h);},100);
	},
	loadContainer:function() {
		return HW.createNode('div',document.body).addClass('video_lightbox').createNode('div').addClass('container');
	},
	transition:function(top,height) {
		this.transitionUp(top,height);
	},
	transitionUp:function(top,height) {
		var obj = this;
		new HW.Animator(this.container,top+height/2-25,top,function(o,v){o.setStyle({marginTop:v+'px'})},500,null,true);
		new HW.Animator(this.container,50,height,function(o,v){o.setStyle({height:v+'px'})},500,function(){obj.transitionOut();},true);
	},
	transitionOut:function() {
		var obj = this;
		new HW.Animator(this.container,100,940,function(o,v){o.setStyle({width:v+'px'})},500,function(){obj.show();},true);
	},
	show:function() {
		var obj = this;
		this.container.empty();
		var content = this.container.createNode('div').addClass('content').setFade(0);
		var d1 = content.createNode('div').addClass('column01');
		var d2 = content.createNode('div').addClass('column02');
		
		d1.createNode('h1',this.video.title);
		d1.createNode('p',this.video.desc);
		
		d1.createNode('p').createNode('a','close',{href:'#'}).addClass('close').bind('click',function(){obj.close();},false);
		
		d2.id = 'video_lightbox';
		
		var f = new HW.Flash();
		f.src = [8, strVideoPlayerFilePath+'?videoPath='+this.video.flv];
		f.width = 518;
		f.height = 337;
		f.wmode = 'transparent';
		f.load('video_lightbox');
		
		content.fade(100);
		
	},
	close:function() {
		if(this.isOpen) {
			var o = _$('div.video_lightbox');
			o.parentNode.removeChild(o);
			this.isOpen = false;
		}
	},
	docClose:function(e) {
		if(this.isOpen) {
			e = e||window.event;
			var o = e.target||e.srcElement;
			while(o) {
				if(o == this.container) {
					return;
				}
				o = o.parentNode;
			}
			this.close();
		}
	}
}