/*
 Javascript for FirstEight
*/
var auth_token='';
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*
	Editor/Clip Navigation
*/
/* HL Clips */
cn_hl=function(e,t){
	if(t == 1){
		//mouseover
		var selected = $('main').select('div.clip_nav a.selected');
		selected.invoke('removeClassName','selected');
		selected.invoke('addClassName','was_selected');
		//Check settings on Cats
		var cats = $('editor_nav').select('ul.clipstyle a.selected');
		if(cats.length>0){
			cats.pluck('rel').each(function(c){
				$('main').select('div.clip_nav a.'+e+'.cat_'+c).invoke('addClassName','hover');
			});
		}else{
			$('main').select('div.clip_nav a.'+e).invoke('addClassName','hover');
			try{
				var id=e.substr(7);
				Object.keys(fe_clips.e[id]).each(function(a){
					$('cat_'+a).addClassName('hover');
				});
			}catch(e){}
		}
	}else if(t == 2){
		//mouseout
		var selected = $('main').select('div.clip_nav a.was_selected');
		selected.invoke('removeClassName','was_selected');
		selected.invoke('addClassName','selected');
		$('main').select('div.clip_nav a.'+e).invoke('removeClassName','hover');
		try{
			var id=e.substr(7);
			Object.keys(fe_clips.e[id]).each(function(a){
				$('cat_'+a).removeClassName('hover');
			});
		}catch(e){}
	}else{
		$('main').select('div.clip_nav a').invoke('removeClassName','selected');
		$('main').select('div.clip_nav a').invoke('removeClassName','was_selected');
		$('main').select('div.clip_nav a.'+e).invoke('addClassName','selected');
	}
};

/* HL Editors */
cn_ehl=function(c,t){
	if(t == 1){
		//mouseover
		//Clear selected
		var selected = $('main').select('div.clip_nav a.selected');
		selected.invoke('removeClassName','selected');
		selected.invoke('addClassName','was_selected');
		$('editor_nav').select('div.editors a.'+c).invoke('addClassName','hover');
		$('main').select('div.clip_nav a.cat_'+c).invoke('addClassName','hover');
	}else if(t == 2){
		//mouseout
		var selected = $('main').select('div.clip_nav a.was_selected');
		selected.invoke('removeClassName','was_selected');
		selected.invoke('addClassName','selected');
		$('editor_nav').select('div.editors a.'+c).invoke('removeClassName','hover');
		$('main').select('div.clip_nav a.cat_'+c).invoke('removeClassName','hover');
	}
};

/* Add Event Listeners unobstrusive */
fe_init = function(){
	var claim_template = new Template('url(/images/image_thumb.php?width=470&height=130&size=dyn&text=#{text}&typ=conduit1)');
	
	//CAT NAV
	$('editor_nav').select('ul.clipstyle a.pixel').each(function(el){
		if(!el.id)
			return;
		el.onmouseover = function(e){
			var editor = $('editor_nav').select('div.editors a.selected');
			if(editor.length>0)	{ 
				editor = editor[0]; 
			}	else {	
				editor = false;
			}
			
			if(editor){
				cn_ehl(el.rel+'.'+editor.rel,1);
			}else{
				cn_ehl(el.rel,1);
			}
			
		}.bindAsEventListener();
		//Mouseout
		el.onmouseout = function(e){
			var editor = $('editor_nav').select('div.editors a.selected');
			if(editor.length>0)	{ 
				editor = editor[0]; 
			}	else {	
				editor = false;
			}
			
			if(editor){
				cn_ehl(el.rel+'.'+editor.rel,2);
			}else{
				cn_ehl(el.rel,2);
			}
			
		}.bindAsEventListener();
		//Onclick
		el.onclick = function(e){
			Event.stop(e);
			var el = Event.element(e).up('a');
			if (el.hasClassName('deactivate')){
				return;
			}
			
			var cats = $('editor_nav').select('ul.clipstyle a.selected').pluck('rel');
			var editor = $('editor_nav').select('div.editors a.selected');
			if(editor.length>0)	{ 
				editor = editor[0]; 
			}	else {	
				editor = false;
			}
			
			//
			if (Event.element(e).up('a').hasClassName('selected') && cats.length == 1){
				//Last selected point was unselected
				//Clear all Cats	
				$('editor_nav').select('ul.clipstyle a').invoke('removeClassName','selected');
				
				//Clear ALL Editors
				$('editor_nav').select('div.editors a').invoke('removeClassName','selected');
				$('editor_nav').select('div.editors a').invoke('removeClassName','deactivate');
		
				//Clear all Clips
				$('main').select('div.clip_nav a.selected').invoke('removeClassName','selected');
				$('main').select('div.clip_nav a.was_selected').invoke('removeClassName','was_selected');
				
				//Hide Options and revert Claim
				if ($('editor_nav').down('div.editor_options').visible()){
					new Effect.DropOut($('editor_nav').down('div.editor_options'));
				}
				if (!static_heading){
					$('claim').setStyle({
						backgroundImage: claim_template.evaluate({text: "Hello,|we%27re|firsteight!"})
					});
				};
				
			}else{
				if(false && el.hasClassName('selected')){
					//One from multiple selected points is de-selected
					
					//De-Select Cat und remove from list
					el.removeClassName('selected');
					cats = cats.without(el.rel);
					
					//Check if editor is already chosen or if we should re-select
					$('editor_nav').select('div.editors a').invoke('addClassName','deactivate');
					cats.each(function(c){
						$('editor_nav').select('div.editors a.deactivate.'+c).invoke('removeClassName','deactivate');
					});
				}else if (false && cats.length>0){
					//An additional(!) category is selected
					
					//Select Cat and add to list
					el.addClassName('selected');
					cats.push(el.rel);
					
					//Check if editor is already chosen or if we should re-select
					$('editor_nav').select('div.editors a.deactivate.'+el.rel).invoke('removeClassName','deactivate');
					
				}else{
					//First category is selected
					
					//Clear all Cats	
					$('editor_nav').select('ul.clipstyle a').invoke('removeClassName','selected');
					
					//Clear ALL Editors
					$('editor_nav').select('div.editors a').invoke('removeClassName','selected');
					$('editor_nav').select('div.editors a').invoke('removeClassName','deactivate');
					//editor = false;
			
					//Clear all Clips
					$('main').select('div.clip_nav a.selected').invoke('removeClassName','selected');
					$('main').select('div.clip_nav a.was_selected').invoke('removeClassName','was_selected');
					
					//Hide Options and revert Claim
					if(!editor){
						if ($('editor_nav').down('div.editor_options').visible()){
							new Effect.DropOut($('editor_nav').down('div.editor_options'));
						}
						if (!static_heading){
							$('claim').setStyle({
								backgroundImage: claim_template.evaluate({text: "Hello,|we%27re|firsteight!"})
							});
						};
					}
					
					//Select Cat and add to list
					el.addClassName('selected');
					//cats.push(el.rel);					
					cats = [el.rel];
					
					//Select Editors (Deactivate nonpossibles)
					$('editor_nav').select('div.editors a:not(.'+el.rel+')').invoke('addClassName','deactivate');
				}
				
				//Make sure editor is still selected
				var editor_filter = '';
				if (editor) {
					editor.addClassName('selected');
					editor_filter = '.'+editor.rel;
					var lname=editor.down('span.alt').innerHTML ;
					var fname=lname.substring(0,lname.indexOf(' '));
					lname = lname.substring(fname.length+1);
					if (!static_heading){
						$('claim').setStyle({
							backgroundImage: claim_template.evaluate({text: 'Hello,|my+name+is|'+fname+'+'+lname})
						});
					};
				}
								
				//Arrange and select the clips
				var els = [];
				var dls = $('main').select('div.clip_nav a');
				cats.each(function(c){
					els = els.concat($('main').select('div.clip_nav a.cat_'+c+editor_filter));
				});
				els.invoke('addClassName','selected');
				els.invoke('absolutize');
				els.sort(function() {return 0.5 - Math.random();});
				var px=0,py=5,hidden=0;
				els.each(function(el){
					if (el.hasClassName('hidden')){
						el.setStyle({width:'65px',height:'51px'});
						el.show();
						el.removeClassName('hidden');
						hidden++;
					}
					dls = dls.without(el);
					new Effect.Morph(el,{style:'top:'+py+'px;left:'+px+'px;',duration:0.8});
					px += 68;
					if (px > 880){
						px=0;
						py+=54;
					}
				});
				
				if(els.length==0){
					if ($('editor_nav').down('div.editor_options').visible()){
						new Effect.DropOut($('editor_nav').down('div.editor_options'));
					}
					if (!static_heading){
						$('claim').setStyle({
							backgroundImage: claim_template.evaluate({text: "Hello,|we%27re|firsteight!"})
						});
					};
				}
				
				//Arrange the remaining clips
				dls.invoke('removeClassName','selected');
				dls.invoke('removeClassName','was_selected');
				dls.invoke('absolutize');
				dls.sort(function() {return 0.5 - Math.random();});
				dls.each(function(el){
					if (el.visible() && hidden > 0){
						el.hide();
						el.addClassName('hidden');
						hidden--;
					}else if (el.visible()){
						new Effect.Morph(el,{style:'top:'+py+'px;left:'+px+'px;',duration:0.8});
						px += 68;
						if (px > 880){
							px=0;
							py+=54;
						}
					}
				});
			}
			return true;
			}.bindAsEventListener();
	});
	
	//EDITOR NAV
	$('editor_nav').select('div.editors a').each(function(el){
		el.onmouseover = function(e){
			var ele = Event.element(e).up('a');
			if (ele && ele.hasClassName('deactivate')){	return;	}
			cn_hl(el.rel,1);
		}.bindAsEventListener();
		el.onmouseout = function(e){
			var ele = Event.element(e).up('a');
			if (ele && ele.hasClassName('deactivate')){	return;	}
			cn_hl(el.rel,2);
		}.bindAsEventListener();
		el.onclick = function(e){
			Event.stop(e);
			var el = Event.element(e).up('a');
			if (el.hasClassName('deactivate')){
				return;
			}
			
			//Change H1 Claim
			var lname=el.down('span.alt').innerHTML ;
			var fname=lname.substring(0,lname.indexOf(' '));
			lname = lname.substring(fname.length+1);
			if (!static_heading){
				$('claim').setStyle({
					backgroundImage: claim_template.evaluate({text: 'Hello,|my+name+is|'+fname+'+'+lname})
				});
			};

			//Check selected cats and editors
			var cats = $('editor_nav').select('ul.clipstyle a.selected').pluck('rel');
			//Check on Editor Action
			if (false && el.hasClassName('selected')){
				//De-Select Editor
				$('editor_nav').select('div.editors a.selected').invoke('removeClassName','selected');
				var editor = false;
				cats = ['none'];
				$('editor_nav').select('ul.clipstyle a').invoke('removeClassName','selected');
				//Activate all deactivated Cats again
				$('editor_nav').select('ul.clipstyle a').invoke('removeClassName','deactivate');
				
			}else{
				//Select Editor
				$('editor_nav').select('div.editors a.selected').invoke('removeClassName','selected');
				el.addClassName('selected');
				var editor = el;
				
				//Select Categories if not selected yet
				try{
					//$('editor_nav').select('ul.clipstyle a').invoke('addClassName','deactivate');
					$('editor_nav').select('ul.clipstyle a.selected').invoke('addClassName','was_selected');
					$('editor_nav').select('ul.clipstyle a.selected').invoke('removeClassName','selected');
					var new_cats = [];//RESET Cats
					var id=el.rel.substr(7);
					Object.keys(fe_clips.e[id]).each(function(a){
						//$('cat_'+a).removeClassName('deactivate');
						if (cats.length==0 || $('cat_'+a).hasClassName('was_selected')){
							$('cat_'+a).addClassName('selected');
							new_cats.push(a);
						}
					});
					cats = new_cats;
					$('editor_nav').select('ul.clipstyle a').invoke('removeClassName','was_selected');
				}catch(e){}
			}

			//Prepare Filter
			var editor_filter = (editor) ? '.'+editor.rel : '';
							
			//Arrange and select the clips
			var els = [];
			var dls = $('main').select('div.clip_nav a');
			if (cats.length>0) {
				cats.each(function(c){
					els = els.concat($('main').select('div.clip_nav a.cat_'+c+editor_filter));
				});
			}else{
				els = $('main').select('div.clip_nav a'+editor_filter);
			}
			els.invoke('addClassName','selected');
			els.invoke('absolutize');
			els.sort(function() {return 0.5 - Math.random();});
			var px=0,py=5,hidden=0;
			els.each(function(el){
				if (el.hasClassName('hidden')){
					el.setStyle({width:'65px',height:'51px'});
					el.show();
					el.removeClassName('hidden');
					hidden++;
				}
					
				dls = dls.without(el);
				new Effect.Morph(el,{style:'top:'+py+'px;left:'+px+'px;',duration:0.8});
				px += 68;
				if (px > 880){
					px=0;
					py+=54;
				}
			});
			
			//Arrange the remaining clips
			dls.invoke('removeClassName','selected');
			dls.invoke('removeClassName','was_selected');
			dls.invoke('absolutize');
			dls.sort(function() {return 0.5 - Math.random();});
			dls.each(function(el){
				if (el.visible() && hidden > 0){
					el.hide();
					el.addClassName('hidden');
					hidden--;
				}else if (el.visible()){
					new Effect.Morph(el,{style:'top:'+py+'px;left:'+px+'px;',duration:0.8});
					px += 68;
					if (px > 880){
						px=0;
						py+=54;
					}
				}
			});
						
			//Set Vita and Play All Links
			if(els.length > 0){
				if (!$('editor_nav').down('div.editor_options').visible()){
					new Effect.Appear($('editor_nav').down('div.editor_options'));
				}
				if (auth_token!=''){
					$('editor_nav').down('div.editor_options div.play a').href = '/reel::'+auth_token+'.html?reels='+els.pluck('rel').invoke('substr',5).join(',');
					$('editor_nav').down('div.editor_options div.vita a').href = '/vita::'+auth_token+'.html?cn_editor='+el.rel.substr(7);
				}else{
					$('editor_nav').down('div.editor_options div.play a').href = 'reel.html?reels='+els.pluck('rel').invoke('substr',5).join(',');
				$('editor_nav').down('div.editor_options div.vita a').href = 'vita.html?cn_editor='+el.rel.substr(7);
				}
			}else if ($('editor_nav').down('div.editor_options').visible()){
				new Effect.DropOut($('editor_nav').down('div.editor_options'));
			}
					
			return;
			
			}.bindAsEventListener();
	});
	
	//Clips Click+Mouseover
	if ($('main').select('div.clip_nav a')){
		$('main').select('div.clip_nav a').each(function(el){
			el.onclick = function(e){
				
				new Effect.Appear($('black'),{duration:0.5});
				Event.stop(e);
				(function(){window.location = Event.element(e).up('a').href}).delay(0.5);
				return true;
				}.bindAsEventListener();
			
			el.onmouseover = function(e){	
				try{
				var clip_id = Event.element(e).up('a').rel.substr(5);
				$('cat_'+fe_clips.r[clip_id][0]).addClassName('hover');
				$('editor_'+fe_clips.r[clip_id][1]).addClassName('hover');
				}catch(e){}
				Event.stop(e);
				return true;
				}.bindAsEventListener();
				
			el.onmouseout = function(e){	
				try{
				var clip_id = Event.element(e).up('a').rel.substr(5);
				$('cat_'+fe_clips.r[clip_id][0]).removeClassName('hover');
				$('editor_'+fe_clips.r[clip_id][1]).removeClassName('hover');
				}catch(e){}
				Event.stop(e);
				return true;
				}.bindAsEventListener();				
		});
	}
	//Play All
	if ($('editor_nav').select('div.editor_options div.play a')){
		$('editor_nav').select('div.editor_options div.play a').each(function(el){
			el.onclick = function(e){
				
				new Effect.Appear($('black'),{duration:0.5});
				Event.stop(e);
				(function(){window.location = Event.element(e).up('a').href}).delay(0.5);
				return true;
				}.bindAsEventListener();
		});
	}
};

