var sp_scroll;
var ac_scroll;
var vc_scroll;

window.addEvent('domready', function(){
	new ImagePreview('imgpreview');	//Image View
	new MediaPlayer('audio_clip');
	new MediaPlayer('video_clip', {setWidth:480, setHeight:380});
	new MediaPlayer('lightbox', {type: 'flashVideo', setWidth: 702, setHeight: 520, cssFile: 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'}});
	
	// Tabs
	function switch_tabs(i, subnav){
		var subnav = subnav || false;
		if($type(i) == 'string'){
			$$('li.tab_toggle').each(function(el, pos){
				if(el.innerHTML.indexOf(i) != -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'){
			subnav[pos].togglers.each(function(el, pos){
				if(el.innerHTML.indexOf(i) != -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){
			e = new Event(e); 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){
				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();
			switch_tabs('Resources', 'Audio Clips');
			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});
			scroll.toElement('tabs');
		});
	}

	// Video Clips
	if($('product_video_clip')){
		$('product_video_clip').addEvent('click', function(e){
			e = new Event(e).stop();
			switch_tabs('Resources', 'Video Clip');
			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});
			scroll.toElement('tabs');
		});
	}

	// Sample Pages
	if($('product_sample_page')){
		$('product_sample_page').addEvent('click', function(e){
			e = new Event(e).stop();
			switch_tabs('Resources', 'Sample Pages');
			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});
			scroll.toElement('tabs');
		});
	}

	// On Demand
	if($('product_on_demand')){
		$('product_on_demand').addEvent('click', function(e){
			e = new Event(e).stop();
			switch_tabs('Resources', 'On-Demand Presentations');
			var scroll = new Fx.Scroll(window, {
				wait: false,
				duration: 800,
				offset: {'x': 0, 'y': -150},
				transition: Fx.Transitions.Quad.easeInOut
			});
			scroll.toElement('tabs');
		});
	}
});