/*
-----------------------------------------------
WGBH interactive
daniel_bulli@wgbh.org
----------------------------------------------- */

Fx.Properties = Fx.Styles.extend({	
			increase: function(){for (	var p in this.now) this.element[p] = this.now[p];	}	
		});



Fx.FlyingScroller = Fx.Properties.extend({
	initialize: function(el, options){
		this.parent(el, options);
		this.options = Object.extend(this.options || {}, Object.extend({
			paddingLeft: 0,
			paddingTop: 0
		}, options || {}));
	},
	toElement: function(el, paddingLeft, paddingTop){
	
		this.goTo(el.offsetLeft - (paddingLeft || this.options.paddingLeft || 0), el.offsetTop - (paddingTop || this.options.paddingTop || 0));
	},
	goTo: function(x, y){
	
		this.start({
			'scrollLeft': [this.element.scrollLeft, x],
			'scrollTop': [this.element.scrollTop, y]
		})
	}
});


var cur_tab = 1;
var last_on  = new Array;
last_on[0] = 'Dummy';

var nav_anim  = new Array;
nav_anim[0] = 'Dummy';

var nav_links  = new Array;
nav_links[0] = 'Dummy';

var timer_id = null;



var Sleep =
{

	init: function(options)
	{
		this.options = Object.extend({
			default_width: 640,			// Default width (px)
			defaultHeight: 360,			// Default height(px)
			pop_win		 : null
		}, options || {});
		

		//preload images
		var aPreLoad	= new Array();
		var aPreLoadi	= 0;		
		//do rollover
		$$('img','input').each(function(el){

			if(el.hasClass('roll'))
			{
				//let's preload
				aPreLoad[aPreLoadi] = new Image();
				aPreLoad[aPreLoadi].src = el.src.replace(el.src.replace(/(\.[^.]*)$/, "_over$1"));
				aPreLoadi++;

				el.addEvent('mouseover',function(){
					if(!this.hasClass("voff"))
					{
						this.setAttribute('src',this.src.replace(/(\.[^.]*)$/, "_over$1"));
					}
				});

				el.addEvent('mouseout',function(){
					if(!this.hasClass("voff"))
					{				
						this.setAttribute('src',this.src.replace(/_over(\.[^.]*)$/, "$1"));
					}

				});
			}
		});
		
		
	
		
		hrefs = $$('a');
		 
		hrefs.each(function(el) {
			if(el.rel && el.rel.test(/^external/i))
			{
				el.setProperty('target','_blank');
			}
			else if (el.rel && el.rel.test(/^dyk/i))
			{
				el.setProperty('target','dyk');
				el.onclick = this.dyk.pass(el, this);
			}
			else if (el.rel && el.rel.test(/^pop_image/i))
			{
				el.setProperty('target','pop_image');
				el.onclick = this.pop_image.pass(el, this);
			}
			else if (el.rel && el.rel.test(/^video/i))
			{
				el.setProperty('target','pop_image');
				el.onclick = this.video.pass(el, this);
			}
			else if (el.rel && el.rel.test(/^interactive/i))
			{
				el.setProperty('target','interactive');
				el.onclick = this.interactive.pass(el, this);
			}
			else if (el.hasClass('glossary'))
			{
				//disable glossary click
				el.onclick = this.do_glossary.pass(el, this);
			}		
		}.bind(this));
		
		
				
		/* Tips 4 */
		var Glossary = new Tips($$('.glossary'), {
			className: 'glossary-defn'
		});

		//OPENER SEND TO PARENT
		try {
			// access other window here
			if(window.opener && window.opener.cur_tab)
			{
				hrefs.each(function(el) {
					if (el.rel && (el.rel.test(/^external/i) || el.rel.test(/^dyk/i) || el.rel.test(/^pop_image/i) || el.rel.test(/^video/i)) || el.rel.test(/^interactive/i))
					{
						//these are pop ups ... leave them
					}
					else if(el.href && !el.hasClass('pop-link') && !el.hasClass('glossary'))
					{
						//if am an opened window change parent
						el.onclick = this.do_opener.pass(el, this);
					}
				}.bind(this));
			}
		}
		catch (e) {
			// handle exception here
		}
		

		
	},
	
	do_glossary: function(link)
	{
		return true;
	},
	
	
	do_opener: function(link)
	{
		//alert(link.href + window.opener);
		window.opener.location = link.href;
		window.opener.focus();
		return false;
	},
	
	pop_image: function(link)
	{
		var w = '565';
		var h = '596';		
		return this.popit(link.href,w,h,'pop_image','location=0,status=0,menubar=0,scrollbars=1,resizable=1,');
	},

	interactive: function(link)
	{
		var is_small = (link.rel.test(/^interactive-small/i));
		var w = (is_small) ? '830' : '830';
		var h = (is_small) ? '630' : '630';
		
		var win_name = (is_small) ? 'interactive_small' : 'interactive_medium';
		
		return this.popit(link.href,w,h,win_name,'location=0,status=0,menubar=0,scrollbars=1,resizable=1,');
	},
	
	video: function(link)
	{
		if(!link.rel)
		{
			link.rel = 'video-large';
		}
		var is_small = (link.rel.test(/^video-small/i));
		var is_large = (link.rel.test(/^video-large/i))
		var w = (is_small) ? '436' : '661';
		var h = (is_small) ? '527' : '537';
		w = (is_large) ? '661' : w;
		h = (is_large) ? '610' : h;		
		var win_name = (is_small) ? 'video_small' : 'video_medium';
		win_name = (is_large) ? 'video_large' : win_name;
		
		return this.popit(link.href,w,h,win_name,'location=0,status=0,menubar=0,scrollbars=1,resizable=1,');
	},
	video_flash: function(link)
	{

		var is_small = true;
		var is_large = false
		var w = (is_small) ? '456' : '691';
		var h = (is_small) ? '527' : '537';
		w = (is_large) ? '681' : w;
		h = (is_large) ? '610' : h;		
		var win_name = (is_small) ? 'video_small' : 'video_medium';
		win_name = (is_large) ? 'video_large' : win_name;
		this.popit(link,w,h,win_name,'location=0,status=0,menubar=0,scrollbars=1,resizable=1,');
	},	
	
	dyk: function(link)
	{
		var is_text = (link.rel.test(/^dyk-text/i));
		var w = (is_text) ? '501' : '671';
		var h = (is_text) ? '364' : '501';
		
		var win_name = (is_text) ? 'dyk_text' : 'dyk_video';
		
		return this.popit(link.href,w,h,win_name,'location=0,status=0,menubar=0,scrollbars=1,resizable=1,');
	},
	
	popit: function(url,w,h,name,options)
	{
		cancel_flip_it();
		//one popup window
		if(this.pop_win && !this.pop_win.closed)
		{
			this.pop_win.close();
		}
		
		var left_pos = (screen.availWidth - w) / 2;
		var top_pos = (screen.availHeight - h) / 2;	
		
		options += 'width=' + w + ',height=' + h + ',left=' + left_pos + ',top=' + top_pos;
		
		//alert(url+ "\n"+ name+"\n"+options);
		this.pop_win =  window.open(url, name, options);
		
		if (this.pop_win) {
			if (this.pop_win.focus)
			{
				this.pop_win.focus();
			}
			return false;
		}

		this.pop_win = null;
		return true;		
	}

};


var vidcounter = 1;

function inneter()
{

	try {
	  document.execCommand('BackgroundImageCache', false, true);
	} catch(e) {}


	//QUIZ !!
	$$('label.qcheck').each(function(e){
 
   		var ec = e.firstChild;
		function check(el){
		    var tmp = (el.firstChild.checked)?"_on":"_off";
			return "qcheck "+ec.type + tmp;
		}
		e.className = check(e);
		e.style.cursor = 'pointer';
		ec.style.display = 'none';
 
		e.addEvent('click',function(){
			var chi = this.firstChild;
            if(!chi.checked || chi.type != 'radio')	chi.checked = !chi.checked;
 
            if(chi.type != 'radio') this.className = check(this);
			else{$$('label.qcheck').each(function(j){
            	    if(j.firstChild.type == 'radio') j.className = check(j);
	            });
	        }
		});
	});
	
	if($('qbox'))
	{
		$$('div.quizanswer').addClass("quizhide");
		
		$('qbox').addEvent('submit', function(e) {
			var e = new Event(e);
			e.stop();
			
			var j = 0;
			var t = 0;
			$$('input').each(function(e){
				if(e.value=='1' && e.checked)
				{
					j++;
					t++;
				}
				else if(e.checked)
				{
					t++;
				}
				
			});
			
			$$('div.quizanswer').addClass("quizhide");
			
			
			if(t < 10)
			{
				alert('You must answer all questions.');
				return false;
			}
			
			
			if(j == 0)
			{
				 
				$('quiznone').removeClass("quizhide");
				$('quiznone').addClass("quizshow");
				window.location.hash=  'quiznone';
			}	
			else if(j == 1)
			{
				
				$('quiz1true').removeClass("quizhide");
				$('quiz1true').addClass("quizshow");
				window.location.hash=  'quiz1true';
			}				
			else if(j == 2)
			{
				
				$('quiz2true').removeClass("quizhide");
				$('quiz2true').addClass("quizshow");
				window.location.hash=  'quiz2true';
			}
			else if(j > 2)
			{
				
				$('quiz2moretrue').removeClass("quizhide");
				$('quiz2moretrue').addClass("quizshow");
				window.location.hash=  'quiz2moretrue';
			}			
			
			
			return false;
		
		});
	}
	
	
	//home vid for ie
	if($('video-highlights')) 
	{
		$$('.vid-high').each(function(e){
	 
			e.addEvent('mouseenter',function(){
				this.addClass('vid-hover');
				}
			);
			
			e.addEvent('mouseleave',function(){
				this.removeClass('vid-hover');
				}
			);
			
		});	
		
		$$('.vid-box').addClass("vidhide");
		$('vid-high'+vidcounter).removeClass("vidhide");
		
		//$('vidnextimg').setAttribute('src',$('vidnextimg').src.replace(/(\.[^.]*)$/, "_off$1"));
		
		$('vidprevimg').addClass("voff");
		$('vidprevimg').setAttribute('src',$('vidprevimg').src.replace(/(\.[^.]*)$/, "_off$1"));
				
		$('vidprev').addEvent('click',function(e)
		{		
			var e = new Event(e);
			e.stop();
			e.stopPropagation();
			
			if(vidcounter != 1)
			{
				$('vid-high'+vidcounter).addClass("vidhide");
				vidcounter--;
				if(vidcounter < 1) vidcounter=1;
				
				if(vidcounter == 1)
				{
					$('vidprevimg').addClass("voff");
					$('vidprevimg').setAttribute('src',$('vidprevimg').src.replace(/_over(\.[^.]*)$/, "_off$1"));				
				}
				
				$('vid-high'+vidcounter).removeClass("vidhide");
			}
			
			$('vidnum').setAttribute('src',$('vidnum').src.replace(/\d(\.[^.]*)$/, vidcounter+"$1"));
			
			
			
			if($('vidnextimg').hasClass("voff"))
			{
				$('vidnextimg').removeClass("voff");
				$('vidnextimg').setAttribute('src',$('vidnextimg').src.replace(/_off(\.[^.]*)$/, "$1"));
			}
			
			var e = new Event(e);
			e.stop();
			e.stopPropagation();
			return false;				
		});
		$('vidnext').addEvent('click',function(e)
		{		
			var e = new Event(e);
			e.stop();
			e.stopPropagation();
			
			if(vidcounter != 3)
			{		
				$('vid-high'+vidcounter).addClass("vidhide");
				vidcounter++;
				if(vidcounter > 3) vidcounter=3;
				if(vidcounter == 3)
				{
					$('vidnextimg').addClass("voff");
					$('vidnextimg').setAttribute('src',$('vidnextimg').src.replace(/_over(\.[^.]*)$/, "_off$1"));				
				}				
				$('vid-high'+vidcounter).removeClass("vidhide");
			}
			
			//$('vidprevimg').removeClass("voff");
			$('vidnum').setAttribute('src',$('vidnum').src.replace(/\d(\.[^.]*)$/, vidcounter+"$1"));
			
			
			if($('vidprevimg').hasClass("voff"))
			{
				$('vidprevimg').removeClass("voff");
				$('vidprevimg').setAttribute('src',$('vidprevimg').src.replace(/_off(\.[^.]*)$/, "$1"));
			}
			
			var e = new Event(e);
			e.stop();
			e.stopPropagation();
			return false;
		});			
	}
	
	
	//supersleight.run();

	//home vid for ie
	if($('lvl2-content'))
	{
		domtab.init();

		nav_anim[1] = new Fx.FlyingScroller($('slideout1'), { transition: Fx.Transitions.Quad.easeInOut, duration: 700, paddingLeft: 0, paddingTop: 0 });
		nav_anim[2] = new Fx.FlyingScroller($('slideout2'), { transition: Fx.Transitions.Quad.easeInOut, duration: 700, paddingLeft: 0, paddingTop: 0 });
		nav_anim[3] = new Fx.FlyingScroller($('slideout3'), { transition: Fx.Transitions.Quad.easeInOut, duration: 700, paddingLeft: 0, paddingTop: 0 });
		nav_links[1] = new Array();
		nav_links[2] = new Array();
		nav_links[3] = new Array();

		//go thru all links and put in correct array
		var linkuri = $$('a');
		for (m=0; m<linkuri.length; m++)
		{
			if(linkuri[m].getAttribute('rel')=='vid_link1')
			{
				nav_links[1].push(linkuri[m]);
			}
			else if(linkuri[m].getAttribute('rel')=='vid_link2')
			{
				nav_links[2].push(linkuri[m]);
			}
			else if(linkuri[m].getAttribute('rel')=='vid_link3')
			{
				nav_links[3].push(linkuri[m]);
			}
		}

		//go thru each slot and add onlick stuff
		for (k=1; k<=3; k++)
		{
			for (i=0; i<nav_links[k].length; i++)
			{
				if(i == 0)
				{
					$(nav_links[k][i].parentNode).addClass('vid-on');
					last_on[k] = $(nav_links[k][0].parentNode);
				}

				nav_links[k][i].setProperty('this_tab',k);
				nav_links[k][i].parentNode.setProperty('this_num',i);
				nav_links[k][i].onclick = function ()
				{
					cancel_flip_it();
					var k = this.getProperty('this_tab');
					if(last_on[k] != null)
					{
						last_on[k].removeClass('vid-on');
						last_on[k] = null;
					}

					$(this.parentNode).addClass('vid-on');
					last_on[k] = $(this.parentNode);
					fre = this.href.match(/#(\w.+)/)[1];
					var target_vid = $(fre);
					nav_anim[k].clearTimer().toElement(target_vid);
					return false;

				}
			}
		}
		
		//timer to flip stuff
		timer_id = flip_it.periodical(8000);
	}
	else
	{
		//home vid for ie
		if(!$('sleep-home') && !$('lvl2-content'))
		{
			//lvl 3 onwards haev smoth scroll
			new SmoothScroll();
		}
	}
}


var flip_it = function() 
{
	$clear(timer_id);	
	var cur_num = last_on[cur_tab].getProperty('this_num');
	
	if(last_on[cur_tab] != null)
	{
		last_on[cur_tab].removeClass('vid-on');
		last_on[cur_tab] = null;
	}
	cur_num++;	
	if(cur_num == nav_links[cur_tab].length) cur_num = 0;
	
	nav_links[cur_tab][cur_num].parentNode.addClass('vid-on');
	last_on[cur_tab] = nav_links[cur_tab][cur_num].parentNode;
	
	fre = nav_links[cur_tab][cur_num].href.match(/#(\w.+)/)[1];
	var target_vid = $(fre);
	nav_anim[cur_tab].clearTimer().toElement(target_vid);
	
	timer_id = flip_it.periodical(8000);
	return false;
}

var cancel_flip_it = function() 
{
	$clear(timer_id);
}

var reset_flip_it = function() 
{
	$clear(timer_id);
	timer_id = flip_it.periodical(8000);
}


//fix ie png issue
function pngr()
{

	//save ie some CPU
	if($('sleep-home'))
	{
		supersleight.limitTo('sleep-home');
		supersleight.run();
	}
	else if($('nav'))
	{
		supersleight.limitTo('nav');
		supersleight.run();
	}

}
window.addEvent('domready', Sleep.init.bind(Sleep));
window.addEvent('domready', inneter);
