window.addEvent('domready', function(){
	var contactText = false;
	new Element('div', {'id':'backgroundPng'}).inject(document.body, 'top');
	$$('.videoLink').each(function(el){
		var img = el.getElement('img');
		var hoverLoad = new Image();
		hoverLoad.src = img.src.replace('thumbs/', 'hovers/');
		var link = new Element('a', {'href':baseHref+img.alt+'/', 'title':img.alt, 'class':'openVideo'}).adopt(img).inject(el);
		var noOverlay;
		if(!img.hasClass('no_overlay')){
			var hoverText = new Element('span', {'class':'hoverText', 'html':img.alt}).addEvents({'mouseover':function(){
				hoverText.inject(link, 'top');
				img.set('src', hoverLoad.src);
				}, 'mouseout':function(){
				hoverText.dispose();
				img.set('src', img.src.replace('hovers/', 'thumbs/'));
				}});;
			noOverlay = false;
			}
		else
			noOverlay = true;
		link.addEvents({'mouseover':function(){
			if(!noOverlay)
				hoverText.inject(link, 'top');
			img.set('src', hoverLoad.src);
			}, 'mouseout':function(){
			if(!noOverlay)
				hoverText.dispose();
			img.set('src', img.src.replace('hovers/', 'thumbs/'));
			}});
		if(img.hasClass('fullscreen')){
			link.store('ratioWidth', '480');
			el.getElement('.videoInfo').setStyle('float', 'none');
			}
		else
			link.store('ratioWidth', '640');
		});
	var boxTimer;
	function closeVideo(e){
		new Event(e).stop();
		displayCase.setStyles({'display':'none', 'opacity':0});
		backingBoard.setStyles({'display':'none', 'opacity':0});
		$('videoCase').empty();
		$('descripCase').empty();
		clearInterval(boxTimer);
		}
	var displayCase = new Element('div', {'id':'displayCase'}).adopt(new Element('a', {'href':'./close/', 'id':'closeCase', 'html':'close'}).addEvent('click', closeVideo), new Element('br'), new Element('div', {'id':'videoCase'}), new Element('div', {'id':'descripCase'})).inject(document.body, 'bottom');
	var backingBoard = new Element('div', {'id':'displayBackingBoard'}).addEvent('click', closeVideo).inject(document.body, 'bottom');
	
	$('navigation').getElements('a').each(function(el){
		var hoverLoad = new Image();
		hoverLoad.src = el.getElement('img').src.replace('_0.png', '_1.png');
		el.addEvents({
		'mouseover':function(){
			el.firstChild.set('src', hoverLoad.src);
			},
		'mouseout':function(){
			el.firstChild.set('src', el.firstChild.src.replace('_1.png', '_0.png'));
		}});
		});
	$('displayBackingBoard').setStyle('opacity', 0).set('tween', {'onComplete':function(){
		if($('displayCase').getStyle('opacity') == 0)
			$('displayCase').setStyle('display', 'none');
		}});
	$('displayCase').setStyle('opacity', 0).set('tween', {'onComplete':function(){
		if($('displayCase').getStyle('opacity') == 0){
			$('displayCase').setStyle('display', 'none');
			}
		}});
	$$('.openVideo').addEvent('click', function(e){
		new Event(e).stop();
		clearInterval(boxTimer);
		var video = 'http://www.zoxfilms.com/videos/'+this.title+(this.getElement('img').hasClass('is_mov') ? '.mov' : '.flv');
		if(video.contains('/.'))
			video = video.replace('/.', '.');
		var ratioWidth = parseInt(this.retrieve('ratioWidth'));
		displayCase.setStyles({'width':ratioWidth, 'margin-left':(0 - ratioWidth/2), 'margin-top':0});
		$('descripCase').setStyle('display', 'block');
		$('descripCase').set('html', this.getPrevious('span.videoText').get('html'));
		if(this.getElement('img').hasClass('is_mov')){
			new Quickie(video, {
				id: 'currentlyPlaying',
				width: ratioWidth,
				height: 380,
				container: 'videoCase',
				attributes: {
				controller: 'true',
				autoplay: 'true'
				}
				});
			}
		else{
			new Swiff('components/flvplayer.swf', {
				id:'currentlyPlaying',
				container:'videoCase',
				width:ratioWidth,
				height:380,
				vars:{
					'file':video,
					'autostart':true
					}
				});
			}
		new Fx.Tween(backingBoard.setStyle('display', 'block'), {'onComplete':function(){
			displayCase.setStyle('display', 'block').setStyle('opacity', 1);
			}}).start('opacity', .7);
		});
	$('contactLink').addEvent('click', function(e){
		new Event(e).stop();
		$('videoCase').grab(new IFrame({'id':'contactFrame', 'src':'http://www.zoxfilms.com/components/getPage.php?s=contact', 'styles':{'border':0, 'width':'480px', 'padding':0, 'margin':0}, 'frameborder':0, 'border':0, 'scrolling':'no'}));
		var contactFrame = $('contactFrame');
		boxTimer = window.setInterval(function(){
			if(contactFrame.contentWindow.document && contactFrame.contentWindow.document.getElementById('content').offsetHeight != contactFrame.offsetHeight){
				contactFrame.setStyle('height', contactFrame.contentWindow.document.getElementById('content').offsetHeight);
				}
			}, 100);
		displayCase.setStyles({'width':480, 'margin-left':-240, 'margin-top':0});
		$('descripCase').setStyle('display', 'none');
		new Fx.Tween(backingBoard.setStyle('display', 'block'), {onComplete:function(){
			displayCase.setStyle('display', 'block').setStyle('opacity', 1);
			}}).start('opacity', .7);
		});
	$('screeningLink').addEvent('click', function(e){
		new Event(e).stop();
		$('videoCase').grab(new IFrame({'id':'loginFrame', 'src':'http://www.zoxfilms.com/components/screening.php', 'styles':{'border':0, 'width':'480px', 'padding':0, 'margin':0, 'overflow':'hidden'}, 'frameborder':0, 'border':0, 'scrolling':'no'}));
		displayCase.setStyles({'width':480, 'margin-left':-240, 'margin-top':0});
		$('descripCase').setStyle('display', 'none');
		boxTimer = window.setInterval(function(){
			var loginFrame = $('loginFrame');
			if(loginFrame.contentWindow.document.body.scrollHeight > loginFrame.offsetHeight){
				loginFrame.setStyle('height', loginFrame.contentWindow.document.body.scrollHeight);
				}
			else if(loginFrame.contentWindow.document.body.scrollHeight + 50 < loginFrame.offsetHeight && loginFrame.contentWindow.document.body.scrollHeight > 115){
				loginFrame.setStyle('height', loginFrame.contentWindow.document.body.scrollHeight);
				}
			
			
			var vidCase = loginFrame.contentWindow.document.getElementById('protectedVideoCase');
			if(vidCase && vidCase.offsetWidth != loginFrame.getSize().x){
				var newWidth = loginFrame.contentWindow.document.getElementById('protectedVideoCase').offsetWidth;
				displayCase.setStyles({'width':newWidth, 'margin-left':(0 - newWidth/2)});
				loginFrame.setStyle('width', newWidth);
				}
			}, 500);
		
		new Fx.Tween(backingBoard.setStyle('display', 'block'), {onComplete:function(){
			displayCase.setStyle('display', 'block').setStyle('opacity', 1);
			}}).start('opacity', .7);
		});
	});