var sp_scroll;
var ac_scroll;
var vc_scroll;
var od_scroll;

window.addEvent('domready', function(){
	new ImagePreview('imgpreview');	//Image View
	new MediaPlayer('audio_clip', {login_page:false});
	new MediaPlayer('video_clip', {setWidth:480, setHeight:380, login_page:false});
	new MediaPlayer('on_demands', {setWidth:800, setHeight:500});
	new MediaPlayer('lightbox', {type: 'flashVideo', setWidth: 702, setHeight: 520, cssFile: false, login_page:false});	//SOS On-Demands
	
	//Scrolling Resources
	sp_scroll = new Scroll('sample_page_content','sample_page_bar',{scrollLinks: {forward: 'sample_page_down',back: 'sample_page_up'}});
	ac_scroll = new Scroll('audio_clip_content','audio_clip_bar',{scrollLinks: {forward: 'audio_clip_down',back: 'audio_clip_up'}});
	vc_scroll = new Scroll('video_clip_content','video_clip_bar',{scrollLinks: {forward: 'video_clip_down',back: 'video_clip_up'}});
	od_scroll = new Scroll('on_demands_content','on_demands_clip_bar',{scrollLinks: {forward: 'on_demands_clip_down',back: 'on_demands_clip_up'}});
	// Tabs
	function switch_tabs(i, subnav){
		var subnav = subnav || false;
		if($type(i) == 'string'){
			var title = i;
			$$('li.tab_toggle').each(function(el, pos){
				if(el.innerHTML.indexOf(title) != -1) i = pos;
			}.bind(this));
		}
		if(i == activeTab) {
			if($chk(subnav)) switch_sub_tab(i, subnav);
			return;
		}
		toggles[i].addClass('selected');
		toggles[activeTab].removeClass('selected');
		tabs[activeTab].setStyle('display','none');
		tabs[i].setStyles({'opacity':'0','display':'block'});
		tabs[i].effects({duration: 500, transition: Fx.Transitions.quadInOut}).custom({'opacity': [0, 1.0]}).chain(function(){
			sp_scroll.update(tabs[i].getElements('li').length * (window.opera?3:2.5));
			ac_scroll.update(tabs[i].getElements('li').length * (window.opera?3:2.5));
			vc_scroll.update(tabs[i].getElements('li').length * (window.opera?3:2.5));
		}.bind(this));
		activeTab = i;
		
		if($chk(subnav)) switch_sub_tab(i, subnav);
	}
	
	function switch_sub_tab(pos, i){
		if($type(i) == 'string'){
			var title = i;
			subnav[pos].togglers.each(function(el, pos){
				if(el.innerHTML.indexOf(title) != -1) i = pos;
			}.bind(this));
		}
		if(i == subnav[pos].active) return;
		subnav[pos].togglers[i].getElement('a').addClass('selected');
		subnav[pos].togglers[subnav[pos].active].getElement('a').removeClass('selected');
		subnav[pos].tabs[subnav[pos].active].setStyle('display','none');
		subnav[pos].tabs[i].setStyles({'opacity':'0','display':'block'});
		subnav[pos].tabs[i].effects({
			duration: 500,
			transition: Fx.Transitions.quadInOut
		}).custom({'opacity': [0, 1.0]}).chain(function(){
			if(subnav[pos].togglers[i].innerHTML.indexOf('Sample Pages') != -1)
				sp_scroll.update(subnav[pos].tabs[i].getElements('li').length * (window.opera?3:2.5));
			if(subnav[pos].togglers[i].innerHTML.indexOf('Audio Clips') != -1)
				ac_scroll.update(subnav[pos].tabs[i].getElements('li').length * (window.opera?3:2.5));
		}.bind(this));
		subnav[pos].active = i;
	}
	
	var activeTab = 0;
	var tabs = $$('div.collapse');
	var toggles = $$('li.tab_toggle');
	toggles.each(function(el,i){
		if(i != activeTab) {
			tabs[i].setStyle('display','none');
			el.removeClass('selected');
		}	else {
			tabs[i].setStyle('display','block');
			el.addClass('selected');
		}
		el.addEvent('click', function(e){
			if(e){
				new Event(e).stop();
			}
			switch_tabs(i);
			
			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});
			scroll.toElement('tabs');
		}.bindWithEvent(this));
	}.bind(this));

	var subnav = new Array();
	tabs.each(function(el,pos){
		var temp_tabs = el.getElements('div[class^=subnav]');
		if (temp_tabs=='') return;
		var temp_togglers = el.getElement('ul[class^=subnav]').getElements('li');
		if (temp_togglers=='') return;
		subnav[pos] = {
			'active': 0,
			'tabs': temp_tabs,
			'togglers': temp_togglers
		};
		subnav[pos].togglers.each(function(el2,i){
			if(i!=0) subnav[pos].tabs[i].setStyle('display','none');
			el2.addEvent('click', function(e){
				if(e) {
					new Event(e).stop();
				}
				switch_sub_tab(pos, i);
			}.bindWithEvent(this));
		}.bind(this));
	});

	// Audio Clips
	if($('product_audio_clip')){
		$('product_audio_clip').addEvent('click', function(e){
			e = new Event(e).stop();

			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});

			if($('tabs')){
				switch_tabs('Resources', 'Audio Clips');
				scroll.toElement('tabs');
			}else{
				$$('#resources .subnav li a').each(function(el, index){
					if(el.getAttribute('title') == 'Audio Clips') {
						el.getParent().fireEvent('click', e);
					}
				});
				scroll.toElement('resources');
			}
		});
	}

	// Video Clips
	if($('product_video_clip')){
		$('product_video_clip').addEvent('click', function(e){
			e = new Event(e).stop();

			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});

			if($('tabs')){
				switch_tabs('Resources', 'Video Clip');
				scroll.toElement('tabs');
			}else{
				$$('#resources .subnav li a').each(function(el, index){
					if(el.getAttribute('title') == 'Video Clip') {
						el.getParent().fireEvent('click', e);
					}
				});
				scroll.toElement('resources');
			}
		});
	}

	// Sample Pages
	if($('product_sample_page')){
		$('product_sample_page').addEvent('click', function(e){
			e = new Event(e).stop();

			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});

			if($('tabs')){
				switch_tabs('Resources', 'Sample Pages');
				scroll.toElement('tabs');
			}else{
				$$('#resources .subnav li a').each(function(el, index){
					if(el.getAttribute('title') == 'Sample Pages') {
						el.getParent().fireEvent('click', e);
					}
				});
				scroll.toElement('resources');
			}
		});
	}

	// On Demand
	if($('product_on_demand')){
		$('product_on_demand').addEvent('click', function(e){
			e = new Event(e).stop();

			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});

			if($('tabs')){
				switch_tabs('Resources', 'On-Demand Presentations');
				scroll.toElement('tabs');
			}else{
				$$('#resources .subnav li a').each(function(el, index){
					if(el.getAttribute('title') == 'On-Demand Presentations') {
						el.getParent().fireEvent('click', e);
					}
				});
				scroll.toElement('resources');
			}
		});
	}

	// Scope and Sequence
	if($('product_scope')){
		$('product_scope').addEvent('click', function(e){
			e = new Event(e).stop();

			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});

			if($('tabs')){
				switch_tabs('Resources', 'Scope &amp; Sequence');
				scroll.toElement('tabs');
			}else{
				$$('#resources .subnav li a').each(function(el, index){
					if(el.getAttribute('title') == 'Scope & Sequence') {
						el.getParent().fireEvent('click', e);
					}
				});
				scroll.toElement('resources');
			}
		});
	}

	// Science Supply
	if($('product_science_supply')){	
		$('product_science_supply').addEvent('click', function(e){
			e = new Event(e).stop();
			
			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});
			
			if($('tabs')){
				switch_tabs('Resources', 'Science Supply List');
				scroll.toElement('tabs');
			}else{
				$$('#resources .subnav li a').each(function(el, index){
					if(el.getAttribute('title') == 'Science Supply List') {
						el.getParent().fireEvent('click', e);
					}
				});
				scroll.toElement('resources');
			}
		});
	}

	// Lab List
	if($('product_lab_list')){
		$('product_lab_list').addEvent('click', function(e){
			e = new Event(e).stop();

			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});

			if($('tabs')){
				switch_tabs('Resources', 'Science Lab List');
				scroll.toElement('tabs');
			}else{
				$$('#resources .subnav li a').each(function(el, index){
					if(el.getAttribute('title') == 'Science Lab List') {
						el.getParent().fireEvent('click', e);
					}
				});
				scroll.toElement('resources');
			}
		});
	}

	// Resource List
	if($('product_resource_list')){
		$('product_resource_list').addEvent('click', function(e){
			e = new Event(e).stop();

			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});

			if($('tabs')){
				switch_tabs('Resources', 'Resource List');
				scroll.toElement('tabs');
			}else{
				$$('#resources .subnav li a').each(function(el, index){
					if(el.getAttribute('title') == 'Resource List') {
						el.getParent().fireEvent('click', e);
					}
				});
				scroll.toElement('resources');
			}
		});
	}

});
