/**
*@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++) {
			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: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 (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){};
	},
	
	// 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;
		var oldTabId = window.defTabId;
		window.defTabId = element.id;
		window.format = getSettings("StreamTab");
		
		Event.observe($('scene_menu') , 'mousedown' , function() {
			window.swfobjectLoaded = false;
		});
		
		try{setSettings("tabs",el.id);}catch(e){}
		if(window.startTab){
			if (element.id != 'hd' && oldTabId != 'hd') {
				try{
					if (!window.tabChange(element.id)) return;
				}catch(e){}
			}
		}
		window.startTab=true;
		
		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') {
			if(window.format == 'low'){
				if($$('.type_14')[0] != undefined){
					$$('.type_14').each(function(item){
					item.addClassName('active_format');
				   });
				    $$('.type_6').each(function(item){
					item.removeClassName('active_format');
				   });
					   if(window.sceneformat == undefined) {
						   window.sceneformat = 14;
						}else{
						   window.oldFormat = window.sceneformat;
						}
				}else{
					setStreamTab('high');
					$$('.type_6').each(function(item){
					item.addClassName('active_format');
					});
						if(window.sceneformat == undefined) { 
						   window.sceneformat = 6;
						}else{
						   window.oldFormat = window.sceneformat;
						}
					
				}
				
			}else{
				$$('.type_6').each(function(item){
					item.addClassName('active_format');
				});
				    if(window.sceneformat == undefined) { 
					   window.sceneformat = 6;
					}else{
					   window.oldFormat = window.sceneformat;
					}
			}
			Tabs.changeFormat();
		 }
		
		if(window.defTabId == 'hd') {
				if(window.format == 'low'){
					$$('.type_21').each(function(item){
						item.addClassName('active_format');
					});
					if(window.sceneformat == undefined) {
					   window.sceneformat = 21;
					}else{
					   window.oldFormat = window.sceneformat;
					}
				}else{
					if($$('.type_22')[0] == undefined) {
						$$('.type_21').each(function(item){
							item.addClassName('active_format');
						});
					}else{
						$$('.type_22').each(function(item){
							item.addClassName('active_format');
						});
					}							
					if(window.sceneformat == undefined) { 
						window.sceneformat = 22;
					}else{
					   window.oldFormat = window.sceneformat;
					}
				}
		
				$('side_pane').style.visibility="hidden";
				$('frames').style.width="932px";
				$('frames').style.marginLeft="-257px";
				$('crumb').style.marginLeft="-257px";
				$('playerholder').setStyle({ marginLeft:'-259px', width:'959px', height:'568px'});
				$('scene_menu').style.width = "937px"
				$('js_thumbs_view').style.marginTop = '10px';
				$('en_flash_videoholder').style.marginLeft = '5px';
				Tabs.changeFormat();
		 }else {
				$('side_pane').style.marginTop="";
				if(window.defTabId != 'flv') {
						$('playerholder').setStyle({ marginLeft:'', width:'0px', height:'0px'});
						$('en_flash_videoholder').style.display = 'none';
					}else if(window.defTabId == 'flv') {
						$('playerholder').setStyle({marginLeft:'',width:'',height:''});
						$('en_flash_videoholder').style.display = 'block';
					}
				$('en_flash_videoholder').style.marginLeft = '';
				$('js_thumbs_view').style.marginTop = '';
				$('frames').style.width="";
				$('frames').style.marginLeft="";
				$('crumb').style.marginLeft="";
				$('scene_menu').style.width = ""
			   }
	
			window.sidebarPosition = $('en_flash_videoholder').positionedOffset().top + 605;
			if (window.defTabId == 'hd') {
				$('side_pane').style.marginTop = window.sidebarPosition + 'px';
				$('side_pane').style.visibility = "visible";
				
			}
	},
	changeFormat : function() {
		//NEW METHOD FOR VIDEO QUALITY CHANGING 
		var format = getSettings("tabs");
	    var fQuality = getSettings("StreamTab");
		
		if (format == 'flv' && fQuality == 'low' ) {
			  var Qformat = 14;
		} else if (format == 'flv' && fQuality == 'high') {
			 var Qformat = 6;
		} else if (format == 'hd' && fQuality == 'high') {
			if($$('.type_22')[0] == undefined) {
					setStreamTab('low');	
					var Qformat = 21;
			}else{     
			   var Qformat = 22;
			}
		} else if (format == 'hd' && fQuality == 'low') {
			 var Qformat = 21; 
		}
	 
		 if(window.oldFormat != undefined){
		 	var prevFormat = "format=" + window.oldFormat;
		 }else{
			var prevFormat = "format=" + window.previousFormat;
		 }
		 var changedFormat  = "format=" + Qformat;
		 var flash_xml = window.flash_xml.gsub(prevFormat, changedFormat);	
			
			if (flash_xml != ""){ 
				window.flashvars = {
					en_flash_xml: flash_xml
				};
			}else{
				if(window.orientation == 1){
					 window.flashvars = {
							en_flash_banner_img:window.bannerImg,
							en_flash_banner_link:"?idx=join"
							};
				}else if (window.orientation == 2){
					window.flashvars = {
							en_flash_banner_img:window.bannerImg,
							en_flash_banner_link:"?idx=join"
							};						
				}else if (window.orientation == 3){
					window.flashvars = {
							en_flash_banner_img:window.bannerImg,
							en_flash_banner_link:"?idx=join"
							};						
				}
			}
			
			var params = { menu: "false",wmode:"opaque", allowScriptAccess:"always", allowFullScreen:"true", allownetworking:"all"};
			var attributes = { };
		    window.swfPath = 'http://cdn-small.content.adultcentro.com/web/tmpl/1/intro/scenes_player.swf';
			if (format == 'flv' && window.swfobjectLoaded != true) {
				swfobject.embedSWF(window.swfPath, "en_flash_videoholder", "680", "553", "10", "expressInstall.swf", window.flashvars, params, attributes);
				window.swfobjectLoaded = true;
			}else if(format == 'hd' && window.swfobjectLoaded != true) {
				swfobject.embedSWF(window.swfPath, "en_flash_videoholder", "950", "568", "10", "expressInstall.swf", window.flashvars, params, attributes);
				window.swfobjectLoaded = true;
			}
	//PLAYER INIT FOR CHANGING QUALITY WHITIN ONE SCENE		
	  $$('.format').each(function(item){
								  
		 Event.observe( item , 'click' , function(event){	 
				$$('.active_format').each(function(el){
					el.removeClassName('active_format');
				});	
			   //Event.element(event).addClassName('active_format'); 
			   this.addClassName('active_format');
				if (format == 'flv' && window.swfobjectLoaded != false) {		
					swfobject.embedSWF(window.swfPath, "en_flash_videoholder", "680", "553", "10", "expressInstall.swf", window.flashvars, params, attributes);
					window.swfobjectLoaded = true;
					return false;
			    }else if(format == 'hd' && window.swfobjectLoaded != false) {
					swfobject.embedSWF(window.swfPath, "en_flash_videoholder", "950", "568", "10", "expressInstall.swf", window.flashvars, params, attributes);
					window.swfobjectLoaded = true;
					return false;
			    }
			});
		});
	}		
}


function initPlayer(flvobj) {
        //FIRST INIT OF ALL NESSECCARY VARIABLES FOR PLAYER
        var format = getSettings("tabs");
        var previousFormat =  window.previousFormat;
        var flash_xml =  flvobj;
		if (window.previousFormat == '21'){
            setStreamTab('low');
        }else if(window.previousFormat == '22') {
            setStreamTab('high');
        }else if(window.previousFormat == '14') {
            setStreamTab('low');
        }else{
            setStreamTab('high');
        }
	
    if (flash_xml != ""){ 
            window.flashvars = {
                en_flash_xml: flash_xml,
                en_flash_banner_img: window.bannerImg,
                en_flash_banner_link:"?idx=join"
            };
        }
        else{
            window.flashvars = {
                en_flash_banner_img:window.bannerImg,
                en_flash_banner_link:"?idx=join"
            };
        }
        var params = { menu: "false",wmode:"opaque", allowScriptAccess:"always", allowFullScreen:"true", allownetworking:"all"};
        var attributes = { };
        window.swfPath = 'http://cdn-small.content.adultcentro.com/web/tmpl/1/intro/scenes_player.swf';
            if (window.startQuality == '6' || window.startQuality == '14' ) {		
                swfobject.embedSWF(window.swfPath, "en_flash_videoholder", "680", "553", "10", "expressInstall.swf", window.flashvars, params, attributes);
                    window.swfobjectLoaded = true;
            }else if(window.startQuality == '21' || window.startQuality == '22') {
                swfobject.embedSWF(window.swfPath, "en_flash_videoholder", "950", "568", "10", "expressInstall.swf", window.flashvars, params, attributes);
                window.swfobjectLoaded = true;
            }
}	



/* 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 */	

