/*
window.addEvent('domready', function(){	
	window.addEvent('resize', function(){
		if ( windowSizeChanged() ){
			setTimeout("onWindowResize();",500);
		}
	});
	
});
*/

function onload_disable () {

	//window.onresize = function(){};

}

window.onresize = function(){
		if ( windowSizeChanged() ){
			setTimeout("onWindowResize();",500);
		}
}

function search_option_hide(){
    search_option.slideOut() //toggle the slider up and down.
}


window.addEvent('domready', function() {
/*
    var search_option = new Fx.Slide('searchmenu',{mode: 'horizontal'});
  		search_option.hide();
  
     $('input_search_query').addEvent('click', function(e){
       e = new Event(e);
      search_option.slideIn();
      e.stop();
     });

     
     $('search_type_get').addEvent('change', function(e){
       e = new Event(e);
      search_option.slideOut();
      e.stop();
     });


     $('search_query_input').addEvent('submit', function(e){
       e = new Event(e);
      search_option.slideOut();
      e.stop();
      document.getElementById('input_search_query').value='';
     });     
     
*/
});


var windowW= '';
var windowH= '';
var memory_div ='left_block_videos';
var memory_url = 'newvideo.php?'+s+'do=build&channel='+channel;

function windowSizeChanged(){
	tmp_windowW= document.body.offsetWidth;
	tmp_windowH= document.body.offsetHeight;
	// && (tmp_windowH == windowH) 
	return !( (tmp_windowW == windowW));
}

function onWindowResize(){
	windowW= document.body.offsetWidth;
	windowH= document.body.offsetHeight;
	if (memory_url!='') {
		loadToDiv(memory_url,memory_div);
	}
	//alert('url:'+memory_url+'///div:'+ memory_div);
}

var CurrentNewMewssageName='flashnewmessage';

function fetch_new_message (num_msg) {
	 
		var FlashNewMessage;
		   if (navigator.appName.indexOf("Microsoft") != -1) {
			  FlashNewMessage= window[CurrentNewMewssageName]
			}else {
			   FlashNewMessage= window.document[CurrentNewMewssageName]
			}
			if (FlashNewMessage.CheckNewMessage) {
	FlashNewMessage.CheckNewMessage(num_msg);
			}
}


function video_init(upt) {
	if (upt=='1') {
		loadToDiv('video.php?'+s+'do=build&vid='+video_id+'&title_noupdate=1','video_info_build');
	} else {
		loadToDiv('video.php?'+s+'do=build&vid='+video_id,'video_info_build');
	}
}

function index_init (info,type) {
	if (type=='user') {
		loadToDiv('user.php?'+s+'u='+info,memory_div);
		memory_url = '';
	} else if (type=='group') {
		loadToDiv('group.php?'+s+'do=info&gid='+info,memory_div);
		memory_url = '';
	} else if (type=='card') {
		loadToDiv('card.php?'+s+'do=cardview&card='+info,memory_div);
		memory_url = '';
	} else if (type=='album') {
		loadToDiv('album.php?'+s+'aid='+info,memory_div);
		memory_url = '';
	} else if (type=='news') {
		loadToDiv('news.php?'+s+'do=full&nid='+info,memory_div);
		memory_url = '';
	}else if (type=='filmsale') {
		loadToDiv('filmsale.php?'+s+'genre='+info,memory_div);
		memory_url = '';
	} else {
		loadToDiv('newvideo.php?'+s+'do=build',memory_div);
	}
}

function lostpassword_init (code) {
	pop_up('register.php?'+s+'do=passwordnew&p='+code,'500', '400');
}

function swith_right_content (show_div) {


	if (show_div=='info') {
		document.getElementById('content_player').style.display='none';
		document.getElementById('content_right').style.display='block';
	} else {
		document.getElementById('content_right').style.display='none';		
		document.getElementById('content_player').style.display='block';
		
	}

}

function js_nl2br (s) {
	s = s.replace(/\r\n|\r/g, '\n');
	s = s.replace(/\\/g, '\\\\');
	s = s.replace(/'/g, '\\\'');
	s = s.replace(/\n/g, '<BR>');
return s;
}	

function fetch_update_content (div,text) {
	if (document.getElementById(div)) {
		text=js_nl2br(text);
		document.getElementById(div).innerHTML = text;
	}
}

function fetch_update_value (div,text) {
	if (document.getElementById(div)) {
		document.getElementById(div).value = text;
	}
}

function cards_preview_update (cid) {
	if (document.getElementById('card_background')) {
		document.getElementById('card_background').style.background = 'url(\'cards/'+cid+'.jpg\') no-repeat';
	}
}

function show_video(vid,type) {
	//video_id=vid;
	//swith_right_content('player');
	
	//alert('type:' + type);
	if (vid!=video_id) {
	//player_setvideo(video_url);
		document.getElementById('video_info_status').innerHTML="";
		if (type==2) {
			loadToDiv('video.php?'+s+'do=info&vid='+vid,'video_info');
		} else {
			loadToDiv('video.php?'+s+'do=info&play=yes&vid='+vid,'video_info');
			window.scroll(0,0);
		}
	
	}

}

function clear_div(div) {
	document.getElementById(div).innerHTML="";
}


function video_add_quicklist(vid) {
	//alert('add to quick:' + vid);
	document.getElementById('video_info_status').style.display = 'block';
	loadToDiv('quicklist.php?'+s+'do=add&vid='+vid+'&'+Math.floor(Math.random() * (0 + 1000000)),'video_info');
     //if (quicklist_open==1) { 
     	//loadToDiv('quicklist.php?'+s+'do=list','video_info');
     //}	
}

function video_del_quicklist(message,vid) {
	//alert('add to quick:' + vid);
	if (confirm(message)) {
	document.getElementById('video_info_status').style.display = 'block';
	loadToDiv('quicklist.php?'+s+'do=del&vid='+vid+'&'+Math.floor(Math.random() * (0 + 100000)),'video_info');
	}
     //if (quicklist_open==1) { 
     	//loadToDiv('quicklist.php?'+s+'do=list','video_info');
     //}	
}

function fetch_confirm(message,url) {
    if (confirm(message)) {
		loadToDiv(url,'system_messages');
    }
}

function init_language(lang) {
	alert(window.location);
	url ='index.php?'+s+'language='+lang+'&'+Math.floor(Math.random() * (0 + 1000000))
	window.top.location=url;
	//alert(window.location);
}


function showDescription(div){
    var vtbl = "tbl" + div;
    var vdiv = "div" + div;
    	document.getElementById(vtbl).className = 'video_border_on';
    	document.getElementById(vdiv).style.display = '';
}
    
function hideDescription(div){
    var vtbl = "tbl" + div;
    var vdiv = "div" + div;
    	document.getElementById(vtbl).className = 'video_border video_bg';
    	document.getElementById(vdiv).style.display = 'none';
}  


//view window system message
var popup_width=500;
var popup_height=500;
function pop_up (url,width, height) {
    if (!$('popup')){
    new Element('iframe').setProperty('id', 'popup_HideSelect').injectInside(document.body);
		$('popup_HideSelect').setOpacity(0);
		new Element('div').setProperty('id', 'popup_overlay').injectInside(document.body);
		$('popup_overlay').setOpacity(0);
		popup_overlaySize();
    
		new Fx.Style('popup_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0,0.6);
		new Element('div').setProperty('id', 'popup').injectInside(document.body);
		  //$('video_edit').setOpacity(0);
		  $('popup').setStyles({display:'block',opacity:'1',position:'absolute'});
 		}

    popup_position(width,height);
    loadToDiv(url,'popup');
    $("popup_overlay").onclick=popup_remove;
}

function popup_overlaySize(){
	// we have to set this to 0px before so we can reduce the size / width of the overflow onresize
	$("popup_overlay").setStyles({"height": '0px', "width": '0px'});
	$("popup_HideSelect").setStyles({"height": '0px', "width": '0px'});
	$("popup_overlay").setStyles({"height": window.getScrollHeight()+'px', "width": (document.body.offsetWidth+16)+'px'});
	$("popup_HideSelect").setStyles({"height": window.getScrollHeight()+'px',"width": (document.body.offsetWidth+16)+'px'});
	$('popup_HideSelect').setOpacity(0.05);
}

function popup_remove(){


	new Fx.Style('popup', 'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){$('popup').remove();} }).start(1,0);
        $("popup_overlay").onclick=null;
	     new Fx.Style('popup_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut, onComplete:function(){$('popup_overlay').remove();} }).start(0.6,0);
	     $('popup_HideSelect').remove();

}


function popup_position(width,height) {
	$("popup").setStyles({width: width+'px',
				 left: (window.getScrollLeft() + (window.getWidth() - width - 425)/2)+'px',
				 top: (window.getScrollTop() + (window.getHeight() - height)/2)+'px'
         });
}

//---------------SYSTEM MESSAGE---------------------

function system_message(message,delay_time) {
    if (!$('system_message_glow')){
    new Element('iframe').setProperty('id', 'glow_HideSelect').injectInside(document.body);
		$('glow_HideSelect').setOpacity(0);
		new Element('div').setProperty('id', 'glow_overlay').injectInside(document.body);
		$('glow_overlay').setOpacity(0);


		  new Fx.Style('glow_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0,0.6);
		  new Element('div').setProperty('id', 'system_message_glow').injectInside(document.body);
		  //$('video_edit').setOpacity(0);
		  $('system_message_glow').setStyles({display:"block",opacity:'1',position:'absolute'});
 		}
    $('system_message_glow').setHTML(message);

    glow_position();
    glow_overlaySize();
    glow_remove.delay(delay_time*1000);
}

function glow_overlaySize(){
	// we have to set this to 0px before so we can reduce the size / width of the overflow onresize
	$("glow_overlay").setStyles({"height": '0px', "width": '0px'});
	$("glow_HideSelect").setStyles({"height": '0px', "width": '0px'});

   sm_glow_height = ($('system_message_glow').getSize().size.y+20);

	$("glow_overlay").setStyles({width: 310+'px',height: sm_glow_height+'px',
				 left: (window.getScrollLeft() + (window.getWidth() - 310 - 425)/2)+'px',
				 top: (window.getScrollTop() + (window.getHeight()-20)/2)+'px'
         });
	$("glow_HideSelect").setStyles({width: 310+'px',height: sm_glow_height+'px',
				 left: (window.getScrollLeft() + (window.getWidth() - 310 - 425)/2)+'px',
				 top: (window.getScrollTop() + (window.getHeight()-20)/2)+'px'
         });
         $('glow_overlay').setOpacity(0.5);
         $('glow_HideSelect').setOpacity(0.5);
}

function glow_remove(){

	new Fx.Style('system_message_glow', 'opacity',{duration: 250, transition: Fx.Transitions.sineInOut, onComplete:function(){$('system_message_glow').remove();} }).start(1,0);
        $("glow_overlay").onclick=null;
	     new Fx.Style('glow_overlay', 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut, onComplete:function(){$('glow_overlay').remove();} }).start(0.6,0);
	     $('glow_HideSelect').remove();

}


function glow_position() {
	$("system_message_glow").setStyles({width: 290+'px',
				 left: (window.getScrollLeft() + (window.getWidth() - 290 - 425)/2)+'px',
				 top: (window.getScrollTop() + (window.getHeight())/2)+'px'
         });

}

//------------------------------------

function playlist_play (pid, div) {
	loadToDiv('playlist.php?'+s+'do=play&pid='+pid, div);
}

function playlist_delete(message,id,div,type) {
	if (type==1) {
	    if (confirm(message)) {
			loadToDiv('playlist.php?'+s+'do=delete&del=1&rid='+id, div);
		}
	}
	if (type==2) {
		if (confirm(message)) {
			loadToDiv('playlist.php?'+s+'do=delete&del=2&pid='+id, div);	
		}
	}
}

      
function QL_ico_over(oLink) {
	if (oLink.getAttribute('clicked'))
	 return;
	 var img = oLink.childNodes[0];
	 img.src = 'images/icon_addplaylist_over.gif';
};

	function QL_ico_out(oLink) {
	 if (oLink.getAttribute('clicked'))
	 return;
	 var img = oLink.childNodes[0];
	 img.src = 'images/icon_addplaylist.gif';
	};

function QL_ico_clicked(oLink, track_id) {
	 var type = 'common';
     //for ajax
     video_add_quicklist(track_id);
     //alert('ttt:'+quicklist_open);

	 if (oLink.onmouseover == null) {
	 	type = '';
	 }

	 oLink.onmouseover = null;
	 oLink.onmouseout = null;
	 oLink.setAttribute('clicked',true);
	 var img = oLink.childNodes[0];

	 if (type == 'common') {
	 	img.src = '/images/icon_addplaylist_clicked.gif';
	 } else {
	 	img.src = '/images/icon_addplaylist_clicked.gif';
	 }
	 
};

		function loadToDiv(url, div) {
			url = encodeURI(url) +'&ww='+document.body.offsetWidth+'&wh='+document.body.offsetHeight;
			pageTracker._trackPageview(decodeURI(url));
			$(div).setHTML('<p id="pLoader"><img src="images/loading_red.gif"> Loading...</p>');
			var ajax = new Ajax(url, {
						method: 'get',
						update: div,
						evalScripts: true,
						onComplete: function() {
						// when complete, we remove the spinner
							//alert('Updated!')
						}
				});
				ajax.request();
		//return false;
		}	
		
		function loadToDivSave(url, div) {
			alert('test');
			url = url +'&ww='+document.body.offsetWidth+'&wh='+document.body.offsetHeight;
			$(div).setHTML('<p id="pLoader"><img src="images/loading_red.gif"> Loading...</p>');
			var ajax = new Ajax(url, {
						method: 'get',
						update: div,
						evalScripts: true,
						onComplete: function() {
						// when complete, we remove the spinner
							//alert('Updated!')
						}
				});
				ajax.request();
		//return false;
		}				
		
		
function form_send (form_id,div){
	$(form_id).send({
		update: div,
		evalScripts: true,
		onComplete: function() {
		}
	});
}


function check_for_delete(div,hidden_input) {
	var div_check = document.getElementById(div);

	var input_total = div_check.getElementsByTagName("INPUT").length;

	for (var i = 0; i < input_total; i++) {
 		var input_id = div_check.getElementsByTagName("INPUT")[i].id; 		
 		if (input_id != div_check.id) {
  			if (input_id.substring(0, 6) == 'delete' && div_check.getElementsByTagName("INPUT")[i].checked) {
				document.getElementById(hidden_input).value=document.getElementById(hidden_input).value+','+div_check.getElementsByTagName("INPUT")[i].name;
			}
 		}
	}
}


function video_way_change(n,f) {
	if(n=='1') {
		f.video_file.disabled=true;
		f.video_url.disabled=false;
	}else{
		f.video_file.disabled=false;
		f.video_url.disabled=true;
	}
}

function Show_Hide_Prod(tbl)
{

var tbl = document.getElementById(tbl)
//var winheight=(window.innerHeight)?window.innerHeight:((document.all)?document.body.offsetHeight:null);

var allTbl = document.getElementsByTagName("DIV").length;

for (var i = 0; i < allTbl; i++) {
 var tbl_id = document.getElementsByTagName("DIV")[i].id;
 if (tbl_id != tbl.id) {
  if (tbl_id.substring(0, 3) == 'sub') {
   //document.getElementsByTagName("DIV")[i].innerHTML = '';
   document.getElementsByTagName("DIV")[i].style.display = 'none';
  }
 } 
}
if (tbl.style.display == 'none') {
 tbl.style.display = '';

}else {
 tbl.style.display = 'none';

}
return false;
}


/* #################################### TABS #################################### */
function tabs_mouseover(tabs_name,id) {
	if (id==1) {
		document.getElementById(tabs_name+'_'+id).className='tabs_first tabs_select';
	} else {
		document.getElementById(tabs_name+'_'+id).className='tabs_select';
	}
}

function tabs_mouseout(tabs_name,id) {
	if (id==1) {
		if (eval(tabs_name+'_clicked')==id) {
			document.getElementById(tabs_name+'_'+id).className='tabs_first tabs_clicked';
		} else {
			document.getElementById(tabs_name+'_'+id).className='tabs_first';
		}
	} else {
		if (eval(tabs_name+'_clicked')==id) {
			document.getElementById(tabs_name+'_'+id).className='tabs_clicked';
		} else {
			document.getElementById(tabs_name+'_'+id).className='';
		}
	}
}

function tabs_onclick(tabs_name,id) {
	
	if (eval(tabs_name+'_clicked')) {
		tmp_id=eval(tabs_name+'_clicked');
		if (tmp_id==1) {
			document.getElementById(tabs_name+'_'+tmp_id).className='tabs_first';
		} else {
			document.getElementById(tabs_name+'_'+tmp_id).className='';
		}
		
	}
	
	eval(tabs_name+'_clicked ='+id);
	if (id==1) {
		document.getElementById(tabs_name+'_'+id).className='tabs_first tabs_clicked';
	} else {
		document.getElementById(tabs_name+'_'+id).className='tabs_clicked';
	}
}

/* #################################### NEW MESSAGES #################################### */
function new_messages(new_messages,text) {
	if (document.getElementById('new_messages').innerHTML) {
		//if (document.getElementById('new_messages').InnerHTML!=text){
			document.getElementById('new_messages').innerHTML=text;
		//}
	}
}



function ctrl_enter(k,form,div)
{
    if (k) {
               ctrl=k.ctrlKey;
               k=k.which;
    } else {
               k=event.keyCode;
               ctrl=event.ctrlKey;
	}
    if ((k==13 && ctrl) || (k==10)) {
		form_send(form,div);
		return false;
	}
}

function button_submit_disable(btn_s)
{
	document.getElementById(btn_s).disabled=true;
	//document.getElementById(btn_s).value=message;
	return true;
}

function select_this (x) {
	x.focus();
	x.select();
}



function show_hide_div(div) {
	if (document.getElementById(div).style.display=='none') {
		document.getElementById(div).style.display='block';
	} else {
		document.getElementById(div).style.display='none';
	}
}

function menu_member_drop(id){
    var TDMenu = $('menu_member_drop_'+id)
    var SubMenu = $('menu_member_'+id) 
	var coords = TDMenu.getCoordinates();
	var top = coords['top'].toInt() + 15;
	var left = coords['left'].toInt();
    
    SubMenu.setStyle('display','block')
    SubMenu.setStyle('left',left)
    SubMenu.setStyle('top',top)
}

function menu_member_remove(id){
    $('menu_member_'+id).setStyles({'display':'none'});
}


function fetch_invoice_group (uid,gid) {
	loadToDiv('group.php?'+s+'do=invoice&gid='+gid+'&u='+uid,'system_messages');
}

function people_ico_clicked(oLink, uid,gid) {
     
     fetch_invoice_group (uid,gid);

	 if (oLink.onmouseover == null) {
	 	type = '';
	 }

	 oLink.onmouseover = null;
	 oLink.onmouseout = null;
	 oLink.setAttribute('clicked',true);
	 var img = oLink.childNodes[0];

	 img.src = '/images/icon_addplaylist_clicked.gif';	 
};

/* #################################### TOOLTIP #################################### */
function show_tooltip_info(id){

 if(document.getElementById('myTip') == null){

   var coords =$(id).getCoordinates();
   var top = coords['top'].toInt() + 10;
   var left = coords['left'].toInt();
   var win = window.getSize();

   var Tip = new Element('div', {
           'styles': {

            'padding':'10px',
            'width':'150px',
            'height':'auto',
            'background-color':'#f6f6f6',
            'position':'absolute',
            'visibility':'hidden',
            'border':'1px solid #dedede',
            'opacity':'1',
            'left':left,
            'top':top
       }
   });

    Tip.setAttribute('id','myTip');
        Tip.injectInside(document.body);

    Tip.addEvent('mouseout', remove_user_tip.bindWithEvent($('myTip')));
    $(id).addEvent('mouseout', remove_user_tip.bindWithEvent($(id)));
    
    var user_info_tooltip=$('tooltip_'+id).innerHTML;
    $('myTip').innerHTML = user_info_tooltip;
   }
}

function remove_user_tip(event){
	//alert(event.relatedTarget.id+'::'+event.target.id);
	//if (event.relatedTarget.id != event.target.id && event.relatedTarget.id != "myTip") {
 		if(document.getElementById('myTip') != null){
  			document.getElementById('myTip').remove();
  		}
 	//}
};

function hide_tooltip_info(){

	if(document.getElementById('myTip') != null){
		document.getElementById('myTip').remove();
	}
}


function update_var (name,val) {
	eval(name+' = '+val);
}


function IsNumeric(strString) {

var strValidChars = "0123456789";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;
//  test strString consists of valid characters listed above
 for (i = 0; i < strString.length && blnResult == true; i++) {
	      strChar = strString.charAt(i);
	      if (strValidChars.indexOf(strChar) == -1)
	         {
	         blnResult = false;
	         }
	      }
	return blnResult;
}

function div_hide_fade(id_div){
	new Fx.Style(id_div, 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0.6,0);
}
	
function div_show_fade(id_div){
	$(id_div).setOpacity(0);
	new Fx.Style(id_div, 'opacity',{duration: 400, transition: Fx.Transitions.sineInOut}).start(0,0.6);
}


// #######################################################################
// ###################### FETCH UPDATE OPTION OF SELECT ##################
// #######################################################################		
function fetch_change_select_data(url, owid, div){

    		var optionID = $(owid).getValue();
			$(div).setHTML('<p id="pLoader"><img src="images/loading_red.gif"> Loading...</p>');
			var ajax = new Ajax(url + optionID, {
						method: 'get',
						update: $(div),
						evalScripts: true
				});
			
			ajax.request();

}		          
