/**
*@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 (!document.getElementById('tab_content_id')) {
			new Insertion.Bottom(tabs[i],"<div id='tab_content_id' class='tab_content'></div>");
		}
			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 (/externalLink/.test(dt[d].className)) continue;
				if (ul == undefined) {
					dt[d].style.display = "none";
				} else {
						Event.observe(dt[d], 'click', this.show.bindAsEventListener(this));
				}
			}
		}
		var tabs = $$("div.tabs dt");
		//if(!window.defTabId)window.defTabId=0;
		
		//window.defTabId = (window.defTabId != null && window.defTabId != "undefined")?window.defTabId:window.startFormat;
		
		//SetCookie('defaultTabId', window.startFormat,30);
		window.defTabId = getCookie('defaultTabId');
			
		Event.observe($('choose_format') , 'click' , function(event){
				if (window.defTabId == 'hd') {
					$('video_block').setStyle({
 					 width: '964px'
					});
					$('currentPlayer').setStyle({
						width: '932px',
						height: '568px',
						margin: '0 auto'
					});
					$('info').setStyle({
						width: '100%'						   	   
					});
					
					$('tab_content_id').setStyle ({
						width: '956px'
					});
				
					

					
				} else  {
					
						$('video_block').setStyle({
 					 	  width: '620px'
						});
						$('info').setStyle({
						  width: ''						   	   
					    });
						$('tab_content_id').setStyle ({
						width: ''
						});
						
						
				  }
		});
		var active = (getCookie('defaultTabId') && getCookie('defaultTabId') != '' && $(getCookie('defaultTabId'))) ? $(getCookie('defaultTabId')) : $(window.startFormat);
		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 (active == null ) {
				for(var s=0; s<tabs.length; s++){
				tabs[s].num = tabs[s].getAttribute('id');
				if(tabs[s].num == 'flv'){
					active = tabs[s];
				}	
			}
		}
		
		window.def = false;
		try{
			var clas = active.getElementsByTagName("p")[0].className || active.getElementsByTagName("span")[0].className ;
			window.def = clas;
		}catch(e){}
		/*if(typeof getSettings != "undefined" && window.def != "FLASH"){
			var num = getSettings("tabs");
			if(num!=null){
				try{
				active.className = $$("dt.active")[0].className.split(" ")[1];
				active = tabs[num];
				active.className = "active " + this.className;
				}catch(e){}
			}
		}*/
		
		
		if(active != undefined) {
			try{this.show(active);}catch(e){};
		}
		/*if(active == null){
			try{$('flv').show(active);}catch(e){};
		}*/
	},
	
	/* Show tabs*/
	show : function(els) {
		var el = (typeof els.tagName == "string") ? els : Event.element(els);
		while (el.tagName.toLowerCase() != 'dt') {
			el = el.parentNode;
		}
		if (/externalLink/.test(el.className)) return;
		
		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.hasClassName("active")){
					el.className = "active " + el.className;
				}
			}
		element = el;
		window.currentTab = element.id;
		window.oldTabId = window.defTabId;
		window.defTabId = element.id;
		
		if (window.oldTabId == 'hd') {
			setTimeout(function(){
			initKeywordsMore('More keywords');
			},100);
		} else if (window.defTabId == 'hd' || (window.defTabId != 'hd' && window.oldTabId == 'hd')) {
		 	setTimeout(function(){
			var div = $(window.keywordsList.div);
			var p = div.down('p');
			var pHeight = Element.getHeight(p);
			div.style.height = pHeight + 'px';
			},100);	
		}
		
		
	try{SetCookie("defaultTabId",el.id);}catch(e){}
		if(window.startTab){
			try{window.tabChange(el.id)}catch(e){}	
		}
		window.startTab=true;
		
			if (window.defTabId != 'hd') {
			Tabs.hiddenTab(el);
		}
		
		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);
					tab_content[i].select('a[class*="noInitRounded"]').each(function(link){link.removeClassName('noInitRounded');});
				}catch(e){}
				break;
			}
		}
		try {
			if (el.id == 'flv' || el.id == 'hd') {
				var streamLinks = $$('#tab_content_id ul li a');
				for (var i=0; i<streamLinks.length; i++) {
					if (streamLinks[i].up('li').className.blank()) return;
					streamLinks[i].onclick = function() {setCurrentQuality(this); return false};
				}
				var startLink = (getCookie('defaultQuality') && getCookie('defaultQuality') != '') ? $$('#tab_content_id .flash_'+getCookie('defaultQuality')+' a')[0] : 				(window.defaultQuality) ? $$('#tab_content_id .flash_'+window.defaultQuality+' a')[0] : streamLinks[0];
				setCurrentQuality(startLink);
			}
		} catch(e){}
		
		
		/*if (window.defTabId == 'hd' || (window.defTabId != 'hd' && window.oldTabId == 'hd')) {
			if (window.defTabId == 'hd') {
				$('contentPager_relatedByCid_view').setStyle ({
						height: '',
						overflowY: ''
						});
			} else {
				$('contentPager_relatedByCid_view').setStyle ({
						height: '420px',
						overflowY: 'hidden'
						});
				}
			*/
			if (window.defTabId == 'hd' || (window.defTabId != 'hd' && window.oldTabId == 'hd')) {
				
				var link = $('pager_call');
				if (window.defTabId == 'hd') {
					var curRpp = Array('_rpp=4','_rpp=6');
				} else {
					var curRpp = Array('_rpp=6','_rpp=4');
					}
				window.curPagerPageLink = window.curPagerPageLink.replace(curRpp[0],curRpp[1]);
				window.curPagerPageLink = window.curPagerPageLink.replace(curRpp[0],curRpp[1]);
					while (link.tagName.toLowerCase() != 'a') {
					link = link.parentNode;
					}
				
				var uri = window.curPagerPageLink.split('?')[1];
				var queryParams = uri.toQueryParams();
				var params = window.curPagerPageLinkRel;
				var upBlock = $(params.block+'_view');	
				if (queryParams.Bi_cid) {
					var locationQuery = document.location.href.toQueryParams(), curSceneId = parseInt(document.location.hash.split('#')[1]);
					if ((locationQuery.Bi_cid && locationQuery.Bi_cid == queryParams.Bi_cid) || (!isNaN(curSceneId) && curSceneId == queryParams.Bi_cid)) {
						upBlock.scrollIntoView();
						return false;
					}
					
				
			}
			var opt = {
				method: 'post',
				postBody: uri + '&mode_ajax=true',
				
				onLoading: function(t) {
					try {
						if (queryParams.Bi_cid) {
							document.location.hash = '#' + queryParams.Bi_cid;
							upBlock.scrollIntoView();
						}
					} catch(e){}
					
				},
		
				onSuccess: function(t) {
					var separator = '<!--'+params.module+'_content_separator-->';
					var respond = t.responseText.split(separator)[1];
					respond.evalScripts();
					upBlock.select('.opacity_block')[0].update(respond);
					initPagerListner(upBlock);
					upBlock.removeClassName('updating');
					window.setTimeout(function(){
						try {
							if (typeof createdPagesByPageLoaded == 'function') {createdPagesByPageLoaded(true);}
							Sbr.init(upBlock);
						} catch(e){}
					},200);
				},
		
				onFailure: function(t) {
					alert('Sorry, error in AJAX');
					upBlock.removeClassName('updating');
				}
			}
	
			upBlock.addClassName('updating');
			new Ajax.Request(params.module+'.php', opt);
		} 
		
	},
	/*Hidden slides*/
	hiddenTab : function(el)
	{
		
		var name = el.getElementsByTagName('b');
		var cn = name[0].className || name[0].parentNode.className;
		if(name[0]){
			if( cn!= "DVD" && ((window.def == "FLASH" && cn != "FLASH")||window.def != "FLASH")){
				try{
					if((window.def == "FLASH" && cn != "FLASH")||window.def != "FLASH"){
						var m =$('overHead');
						if(m)m.style.display = "block";
						var m =$('js_scale_tabs');
						if(m)m.style.display = "none";
						var clas = $("end_time");
						if(clas)clas.style.display = "none";
						var clas = $("begin_time");
						if(clas)clas.style.display = "none";
						var m =$('en_flash_videoholder');
						if(m)m.style.display = "none";
					}
					var clas = $$("div.frames div.frame_middle")[0];
					if(clas)clas.className = clas.className.split(" ")[0]+" "+clas.className.split(" ")[1]+" frame_hidden";
					var clas = $$("div.frames div.aboveframe")[0];
					if(clas)clas.className = clas.className.split(" ")[0]+" "+clas.className.split(" ")[1]+" scene_hidden";
					var clas = $$("div.underframe_right")[0];
					if(clas)clas.className = "underframe_right middle_hidden";
					var clas = $("end_time");
					if(clas)clas.style.display = "none";
					var clas = $("begin_time");
					if(clas)clas.style.display = "none";
					var clas = $("aboveframe_center");
					if(clas)clas.style.display = "block";
					var clas = $$(".frame_center")[1];
					if(clas)clas.style.display = "none";
				} catch(e){}
			} else {
				try{
					var m =$('overHead');
					if(m)m.style.display = "none";
					var m =$('js_scale_tabs');
					if(m)m.style.display = "block";
					if(cn != "FLASH"){
						var clas = $$("div.frames div.frame_middle")[0];
						if(clas)clas.className = clas.className.split(" ")[0]+" "+clas.className.split(" ")[1];
						var clas = $$("div.frames div.aboveframe")[0];
						if(clas)clas.className = clas.className.split(" ")[0]+" "+clas.className.split(" ")[1];
						var clas = $$("div.underframe_right")[0];
						if(clas)clas.className = "underframe_right";
						var clas = $$("div.underframe_right")[0];
						if(clas)clas.style.display = "block";
						var clas = $$("div.underframe_left")[0];
						if(clas)clas.style.display = "block";
						var m =$('en_flash_videoholder');
						if(m)m.style.display = "none";
						var clas = $("end_time");
						if(clas)clas.style.display = "block";
						var clas = $("begin_time");
						if(clas)clas.style.display = "block";
						var clas = $("aboveframe_center");
						if(clas)clas.style.display = "none";
						var clas = $$(".frame_center")[1];
						if(clas)clas.style.display = "block";
						
					} else {
						var clas = $$("div.frames div.frame_middle")[0];
						if(clas)clas.className = clas.className.split(" ")[0]+" "+clas.className.split(" ")[1]+" frame_hidden";
						var m =$('en_flash_videoholder');
						if(m)m.style.display = "block";
						var clas = $("end_time");
						if(clas)clas.style.display = "block";
						var clas = $("begin_time");
						if(clas)clas.style.display = "block";
						var clas = $("aboveframe_center");
						if(clas)clas.style.display = "none";
						var clas = $$(".frame_center")[1];
						if(clas)clas.style.display = "block";
					}
				} catch(e){
					var t;
				}
			}
		}
	}
}
/* Run init*/
/*Event.observe(window, 'load', Tabs.init.bindAsEventListener(Tabs));*/

