/**
*@package EnvisioNext javascript library
*@subpackage Tabs
*@copyright (C) 2007 EnvisioNext.com  <support@EnvisioNext.com>
*@author Anton Zavidov <anton.zavidov@envisionext.com.ua>
*/
//var Tabs = new Object();
var Tabs = {
	
	// Init
	init : function(){
		//return;
		Object.extend(this, Tabs);
		var tabs = $$("div.tabs");
		for (var i=0; i<tabs.length; i++) {
			/*if ($('tab_content_id')){
				$('tab_content_id').parentNode.removeChild($('tab_content_id'));	 
			}*/
			new Insertion.Bottom(tabs[i],"<div class='tab_content'></div>");
			if (tabs[i].getElementsByTagName('dl').length==0){   
				refreshScenes();
			}
			else{
				var dl = tabs[i].getElementsByTagName('dl')[0];
				var dt = dl.getElementsByTagName('dt');
				var dd = dl.getElementsByTagName('dd');
				for (var d=0; d<dd.length; d++) {
					var ul = dd[d].getElementsByTagName('ul')[0];
					if (ul == undefined) {
						dt[d].style.display = "none";
					} else {
							Event.observe(dt[d], 'click', this.show.bindAsEventListener(this));
					}
				} 
			}
		}
		var tabs = $$("div.tabs dt");
		window.defTabId = getSettings("tabs");
		window.defTabId = (window.defTabId != null && window.defTabId != "undefined")?window.defTabId:'flv';
		for(var s=0; s<tabs.length; s++){
			tabs[s].num = tabs[s].getAttribute('id');
			if(tabs[s].num == window.defTabId){
				active = tabs[s];
			}
		}
		if (typeof active == "undefined" ) {
			active = tabs[0];
		}
		if(active == "undefined"){
			active = tabs['flv'];
		}
		window.def = false;
		try{
			var clas = active.getElementsByTagName("p")[0].className || active.getElementsByTagName("span")[0].className ;
			window.def = clas;
		}catch(e){}
		try{this.show(active);}catch(e){};
		Tabs.initFormat();
	},
	
	// Show tabs
	show : function(els){
		if(typeof els.tagName == "string"){
			var el = els;
		} else {
			var el = Event.element(els);
			if(el.tagName!="DT"){
				el = el.parentNode;
				if(el.tagName!="DT"){
					el = el.parentNode;
				}
			}
		}
		var dtt = $$("dt.active");
		try{
			for(var r=0; r<dtt.length; r++){
			if(dtt[r]!=el){
				if(dtt[r].className == "active") {
					if(navigator.appName != "Microsoft Internet Explorer") {
						dtt[r].removeAttribute('class');
					} else {
					dtt[r].className = '';
					}			
				} else {
					dtt[r].className = dtt[r].className.split(" ")[1];
				}
			}
			}
		} catch(e){}
		if (el.className == "link") {
			if(el.className.length<0) {
				el.className = "active";
			} else {
				el.className = "active " + el.className;
			}
			return;
		}
		if(el.className.length<0) {
				el.className = "active";
			} else {
				if(el.className!="active"){
					el.className = "active " + el.className;
				}
			}
		element = el;
		window.currentTab = element.id;
		window.defTabId = element.id;
		try{setSettings("tabs",el.id);}catch(e){}
		if(window.startTab){
			try{window.tabChange(el.id)}catch(e){}	
		}
		window.startTab=true;
		//Tabs.hiddenTab(el);	  
		
		refreshScenes();
		while(element.tagName != "DD")
		{
			element = element.nextSibling;
		}
		var content = element.innerHTML;
		var tab_content = el.parentNode.parentNode.getElementsByTagName('div');
		for(var i=0; i<tab_content.length; i++) {
			if(tab_content[i].className == "tab_content") {
				try{
					tab_content[i].innerHTML = "";
					new Insertion.Top(tab_content[i],content);
				}catch(e){}
				break;
			}
		}
		if(window.defTabId == 'flv' || window.defTabId == 'hd') {
			if(window.defTabId == 'flv') {
				var startLink = $$("UL.tabs_quality LI A")[0];
			} else if (window.defTabId == 'hd') {
				var startLink = $$("UL.hd_quality LI A")[0];
			}
			setCurrentQuality(startLink);
		}
		
		
		if (window.defTabId == 'flv') {
			Tabs.initFormat();
			
				if ($('en_flash_videoholder')!= null) {
					$('playerholder').setStyle({ width:'573px', height:'476px'});
					$('en_flash_videoholder').style.display = 'block';
				}
		}
		
		if(window.defTabId == 'hd') {
			Tabs.initFormat();							
			if ($('en_flash_videoholder')!= null) {
				$('playerholder').setStyle({ width:'860px', height:'573px'});
				$('en_flash_videoholder').style.width = 860 + "px";
				$('en_flash_videoholder').style.height = 573 + "px";
				$('en_flash_videoholder').style.display = 'block';
			}
		}
		if (window.defTabId != 'hd' && window.defTabId != 'flv') {
			$('en_flash_videoholder').style.display = 'none'; 
			$('playerholder').setStyle({ width:'0px', height:'0px'});
		}
	
	},
	initFormat : function() {
	if(window.defTabId == 'flv') {	
		$$("UL.tabs_quality LI A").each(function(elem){	
			if(elem.up(3).id != "placeholder_ipod") {
				elem.addClassName('fquality');
			}
		});
	}
	if(window.defTabId == 'hd') {	
		$$("UL.hd_quality LI A").each(function(elem){	
			if(elem.up(3).id != "placeholder_ipod") {
				elem.addClassName('fquality');
			}
		});
	}
	
		$$('a.fquality').each(function(item){
			Event.observe(item, 'click', function(event){
				Event.stop(event);
				setCurrentQuality(Event.element(event));
			});
		});
	}
}
// Run init
//Event.observe(window, 'load', Tabs.init.bindAsEventListener(Tabs));

/* code update by Kseniya Redunova */
function refreshScenes(){
	var viewType=getSettings("viewtype") || "thumbnail";	//"thumbnail";  
	if ($('separate_clips')){
		$('separate_clips').immediateDescendants().each(function(item){
			if (item.tagName.toLowerCase()=="div"){
				if (item.hasClassName(viewType))
					item.show();
				else
					item.hide();		
			}
		});
		var ulArrs=new Array();
		var rows=$('separate_clips').down('.'+viewType,0).immediateDescendants('div.clearfix').length;
		for (var q=0; q<rows;q++){
			//number of divs class="item" containing scenes	in this row
			var numScenes=$('separate_clips').down('.'+viewType,0).immediateDescendants('div')[q].immediateDescendants('div').length;	
			for (var k=0; k<numScenes; k++){
				//each array item contains an array of ul objects
				if (viewType=="thumbnail")
					ulArrs[k]=$('separate_clips').down('.'+viewType,0).immediateDescendants('div')[q].immediateDescendants('div')[k].down('.info').immediateDescendants('ul');
				else
					ulArrs[k]=$('separate_clips').down('.'+viewType,0).immediateDescendants('div')[q].immediateDescendants('div')[k].immediateDescendants('ul');
			}
			for (var k=0; k<numScenes; k++){
				for (var i=0; i<ulArrs[k].length; i++){
					if (ulArrs[k][i].tagName.toLowerCase()=="ul"){	
						if(!ulArrs[k][i].hasClassName(window.currentTab)){	  //window.currentTab - the id of chosen tab = class of displayed ul
							ulArrs[k][i].hide();
						}
						else{
							ulArrs[k][i].show();
						}
					}
				}
			}
		}
	}
	else
		console.log("no separate clips"); 						
}

/* end code update by Kseniya Redunova */	

