function _div_out(tagname,text) {
    var obj = find_obj('div',tagname)[0];
    obj.innerHTML = text;
}

function find_obj(tag,id,obj) {

    if (typeof(obj) == 'undefined') { var objs = document.getElementsByTagName(tag); }
    else { var objs = obj.getElementsByTagName(tag); }

    var i = 0;
    var ret = new Array();

    for(i=0;i<objs.length;i++) {
        if (objs[i].id == id) ret[ret.length] = objs[i];
    }
    
    return ret;
}


function _playme(elm) {
    if (elm == '') {
        alert("No Video found");
        return;
    }
    _reset();
    _play(elm);
}

function _play(id1) {
    if (window.chid === undefined)
		{chid = '';} 
	
	
    $(document).ready(function(){  
        var load = $('<div id="screenloader"><img src="/_graphics/loader.gif"></div>');
        $(this).parents().find('videocontent').toggle();
        load.css("opacity", 0.8);
        load.show().fadeIn(150);
        
        $("#videocontent").append(load);
        load.show();
        _goTop('html,body',800);
            
        $.post('/action/video/play',{d: id1, sub: 'v', c:chid}, function(data) {
          load.empty();
          load.hide();
          
          $("#v_screen").html(data.title + data.player);
          $("#inbox").html(data.info);
          $('#embedContent1').text(data.videoemb);
          return false;
        }, 'json'); 
        
        return false;
        
    });   
  
     
}


function _goTop(elm,timer) {
   
    var targetOffset = $(elm).offset().top;
    
    var scrollElement = 'html, body';
    if(elm == 'html,body') $('html, body').attr('scrollTop', 1);
    if ($('html').attr('scrollTop') == 1) {
        scrollElement = 'html';
        if(elm == 'html,body') targetOffset = $('html').offset().top;
    } else if ($('body').attr('scrollTop') == 1) {
        scrollElement = 'body';
        if(elm == 'html,body') targetOffset = $('body').offset().top;
    }
     
    if(elm == 'html,body') $('html, body').attr('scrollTop', 0);
    if ($(scrollElement).scrollTop() != targetOffset)
        $(scrollElement).animate({scrollTop: targetOffset}, timer);
}



function addme(vid) {
    if (vid == '') {
        alert("No Video found");
        return;
    }
    addplaylist(vid);
	
   alert("Video added to playlist");
   
}

function _addme(vid) {
    if (vid == '') {
        alert("No Video found");
        return;
    }
    //addplaylist(vid);
   _doplaylist(vid,'add');	
   //alert("Video added to playlist");
}

function _delme(vid) {
    if (vid == '') {
        alert("No Video found");
        return;
    }
    _doplaylist(vid,'del');
	
    //alert("Video added to playlist");
}

function _doplaylist(id,act) {
    
    $(document).ready(function(){
        
        $.post('/action/video/doplaylist',{vid: id, act: act}, function(data) {
                
                if (data == 1) {
                    
                    if (act == 'del') {
                        text = 'Add playlist';
                        act1 = 'add';
                        class1 = 'playlst';
                    }
                    else if (act == 'add') {
                        text = 'Remove';
                        act1 = 'del';
                        class1 = 'delplaylst';
                    }
                    
                    $("#p_" + id).html("<a href=\"javascript:_" + act1 + "me(" + id + ")\" >"+text+"</a>");
                    $("#p_" + id).removeClass();
                    $("#p_" + id).addClass(class1);
                    _tab('playlist','',0);
					_goTop('html,body',1000);
                    
                }
				else {
					alert(data);
				}
       
        }
        );
   });
    
}

function _dofavourite(id,act,op) {
    
    $(document).ready(function(){
        
        $.post('/action/video/dofavourite',{vid: id, act: act}, function(data) {
                if (act == 'del' && data == 1) {
					_tab2('favour','',0);
				}
				else
					alert(data);
				
        }
        );
   });
    
}


function addfavourite(vid) {
    if (vid == '') {
        alert("No Video found");
        return;
    }
    
     _dofavourite(vid,'add');
    
}


function _del(vid,op) {
    if (vid == '') {
        alert("No Video found");
        return;
    }
    if (confirm('Are you confirm to remove the video?')) {
        _doplaylist(vid,'del');
        _tab('playlist','',op);
    }
   
}

function _delfavour(vid,op) {
    if (vid == '') {
        alert("No Video found");
        return;
    }
    
    if (confirm("Want to remove the video from favourite")) {
        _dofavourite(vid,'del',op);
	}
    
}


function doDelete(Elem,op) {
    //var id = '111';
    var vid = Elem;
    var post = '&op='+op+'&vid='+vid;
	var url  = 'http://test.earth247.tv/mod/video/actions/do.php';
	var req  = false;

    
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    try {
		req = new XMLHttpRequest();
       } catch(e) {
		req = false;
       }
    }

	else if(window.ActiveXObject) {
     	try {
       	req = new ActiveXObject("Msxml2.XMLHTTP");
     	} catch(e) {
	       	try {
	         		req = new ActiveXObject("Microsoft.XMLHTTP");
	       	} catch(e) {
	         		req = false;
	       	}
	      }
    }
    
    if (req) {
        req.onreadystatechange = function() {
        
	    if (req.readyState == 4) {

	        if (req.status == 200) {
						//_div_out('myvid',req.responseText);
						//_play_info(id);

	        } else {
	            alert("Error :\n" + req.statusText);
	        }
	    }
		}

		req.open("POST", url, true);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.send(post);
    }
}


function _reset() {
    $(document).ready(function(){
        $("#toolexpland").hide();
   });
}

function _tab2next(act,sub1,start) {
    _tab2(act,sub1,start);
}

function _tab2(act1,sub1,start1) {
     if (start1 == '')
        start1 = 0;
     
     $(document).ready(function(){
        $("#toolexpland").hide("slow");
        $("#tab_list").html(loading('load1','')); 
        $("li").removeClass("selected");
        
        $("#tab2_" + act1).addClass("selected");
        
        $.post('/action/video/tab',{act: act1, sub: sub1, start: start1, guid: uid}, function(data) {
          //$("#tab_list").html(data);
          $("#tab_list").html(data.result);
          $("#tab_page").html(data.pages);
        },'json'
        );
        
     });
     
}

function loading(cal,text) {
    if (text == '') { text = ''; }   
	var load = '<div id="loader"><img class="'+ cal +'"  src="/_graphics/loader.gif"></div>';
	return load;
}


function toolload(css,text) {
     
     $("#toolexpland").slideDown("slow");
     //$("#toolform").empty();
     $("#toolform").html(loading(css,text));
}

function _submit(chid) {
     
     $(document).ready(function(){
        $('#toolexpland').height(300);
        toolload('load3','');
 
        $.post('/action/video/uploaddialog',{cid:chid}, function(data) {
           
          $("#toolform").html(data);
        }
        
        );
     });
}

$('#v_a').click(function(event) {
    alert("a");
    return false;
 }); /* Clear form plugin */
function clearform(form) {
    $(':input', form).each(function()  {
     var type = this.type;
     var tag = this.tagName.toLowerCase(); // normalize case
      
      if (type == 'text' || type == 'password' || tag == 'textarea')
        this.value = '';
      else if (type == 'checkbox' || type == 'radio')
        this.checked = false;
      else if (tag == 'select')
         this.selectedIndex = 0;
    });
}
	
function _tabnext(act,sub1,start) {
    _tab(act,sub1,start);
}

function _getTab(act) {
    
    $("#toolexpland").hide("slow");

    $("li").removeClass("newsselected");
    $("li").removeClass("middleselected");
    $("li").removeClass("rightselected");
    
    if (act == 'news' || act == 'channel' ) {
        $("#tab_" + act).addClass("newsselected");
    }
    else if (act == 'playlist') {
        $("#tab_" + act).addClass("rightselected");
    }
    else {
        $("#tab_" + act).addClass("middleselected");
    }
    $("#elist").html('');
    $("#epage").html('');
    $("#elist").html(tablist1[act].result);
    $("#epage").html(tablist1[act].pages);
}

function _tab(act1,sub1,start1) {
     
     if (start1 == '')
        start1 = 0;
       $("#elist").html(loading('load1','')); 
       $("#epage").html('');
       $(document).ready(function(){
        $("#toolexpland").hide("slow");
        var bgcolor = '8px solid #2fa5ff';
        if (act1 == 'submit') {
            bgcolor = '8px solid #000000';
        }
       
        $("li").removeClass("newsselected");
        $("li").removeClass("middleselected");
        $("li").removeClass("rightselected");
        
        if (act1 == 'news' || act1 == 'channel' ) {
        $("#tab_" + act1).addClass("newsselected");
        }
        else if (act1 == 'playlist') {
        $("#tab_" + act1).addClass("rightselected");
        }
        else {
        $("#tab_" + act1).addClass("middleselected");
        }
        //$("#t_menu").css({'border-bottom' : bgcolor});
        
        $.post('/action/video/tab',{act: act1, sub: sub1, start: start1}, function(data) {
          $("#elist").html(data.result);
          $("#epage").html(data.pages);
        },"json"
        );
        
     });
     
}

function _tool(act1,id1,sub1) {
     
	 if (window.chid === undefined)
		{chid = '';} 
	 
     $(document).ready(function(){
        $('#toolexpland').height(400);
        
        if (act1 == 'tell') {
            $('#toolexpland').height(560);
        }
        toolload('load3','');
        
        $.post('/action/video/tool',{act: act1, id: id1, sub: sub1,chid:chid}, function(data) {
          
          $("#toolform").html(data);
        }
        
        );
     });
     
}

function _submittool(frm) {
   
   var fname = frm.name;
    
    $(document).ready(function(){
                   
        var variables = $("#" + fname).serialize();
        
		$.post('/action/video/tool',variables,function(data) {
            $("#toolform").html(data);
        });
	    return false;
    });
    
   return false;
}

function _closediv(div,div2) {
    $(document).ready(function(){
        //end();
        $("#"+div2).empty();
        $("#"+div).hide();
     });
}


function _getlist(s,act,query) {
    $(document).ready(function(){
        //if (query != '')
            //post = post + '&' + query;
         $("li").removeClass("sel");
         $("#s_" + s).addClass("sel");
        //_search(act,0,query);
        $('#listbox').html('');
        $('#prevb').html('');
        $('#nextb').html('');
        
        $('#listbox').html(bottomlist1[s].result);
        $('#nextb').html(bottomlist1[s].next);
        
    });
}

function _nextlist(act,start,key) {
    query = "s="+start;
    //_getlist(act,query);
    
    if (act != 'tag') {
        _search(act,start,key,'right');
    }
    else {
        _searchTag('',key,start);
    }    
    
}

function _prevlist(act,start,key) {
    
	_search(act,start,key,'left');
    
}


function _search111(act1,start1,f1) {
   
     $(document).ready(function(){
        //$("#vlist").html(loading('load2','')); 
        //alert(bottomlist1);
        $('#prevb').html('');
        $('#nextb').html('');
        $('#listbox').html(loading('load2',''));
        
        /*if ((act1 < 3 && act1 >= '0') && start1 == 0) {
            //$('#listbox').html(bottomlist1.result);
            //$('#nextb').html(bottomlist1.next);
            //$('#prevb').html(bottomlist1.prev);
            //$('#listbox').html($('#listbox'+act1).show());
			//$('#listbox').html(bottomlist1[act1].result);
			
        }
        else {*/
            $.post('/action/video/search',{act: act1, s: start1, f: f1}, function(data) {
              //$("#vlist").html(data);
              $('#prevb').html(data.prev);
              $('#nextb').html(data.next);
              $('#listbox').html(data.result);
			  $('#listbox').show('slide', {direction: 'left'}, 1000);
            },"json");
       // }
     });
     
}

function _search(act1,start1,f1,direction1) {
    
	var temp = $('#listbox');
	var nexttemp = $('#nextb');
	var prevtemp = $('#prevb');
	
	var back1 = 'left';
	if (direction1 == 'left') back1 = 'right';
	
	//temp.hide('slide', {direction: back1}, 3000);
	$('#prevb').html('');
    $('#nextb').html('');
    $('#listbox').html(loading('load2',''));
		
	$.ajax({
	   type: "POST",
	   url: "/action/video/search",
	   cache: true,
	   dataType: "json",
	   data: ({act: act1, s: start1, f: f1}),
	   success: function(data, textStatus){
		  prevtemp.html(data.prev);
		  nexttemp.html(data.next);
		  temp.html(data.result);
		  temp.show('slide', {direction: direction1}, 1000);
	   }
	});

}

function _searchTag(id,value,start) {
     
     $(document).ready(function(){
        //$("#vlist").html(loading('load2','')); 
        $("li").removeClass("sel");
       
        _goTop('#viv_list',700);
        
        $('#prevb').html('');
        $('#nextb').html('');
        $('#listbox').html(loading('load2',''));
        $.post('/action/video/search',{act:'tag',key:value, s: start, d: id}, function(data) {
          //$("#vlist").html(data);
          $('#prevb').html(data.prev);
          $('#nextb').html(data.next);
          $('#listbox').html(data.result);
        },"json");
        return false;
     });
}


function _vote(vid1,point) {
     
     $(document).ready(function(){
        
        $.post('/action/video/tool',{act: 'rate', id: vid1, r: point}, function(data) {
          alert(data);
        }
        );
     });
     
}




function clickme() {
        $(document).ready(function(){
            //$("#viv_banner").onmouseup(function(){
            $("#viv_banner").hide("slow");
            });
        //});
    }

function clickads() {
        $(document).ready(function(){
           
            $("#viv_banner").hide("slow");
            $("#banner_layer").show("slow");
            });
       
}

function ajaxload(cal,text) {
    if (text == '') { text = ''; }
        
    load.css("opacity", 0.8);
    load.fadeIn(150);
    
    $("body").append(load.click(function() { 
        load.show();
    }));
    
}

//Ajax loader
function loader() 
{   
    //Create our overlay object
    var overlay = $("<div id='modal-overlay'></div>");
	//Create our modal window
	var modalWindow = $("<div id='modal-window'></div>");
    
    var msg = "<br/>Video Uploading process. It may take some minutes. Please open another new tab or new window for browser <br/>Note: Please don't close this window";
    
    if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
        $("body","html").css({height: "100%", width: "100%"});
        $("html").css("overflow","hidden");
    }
    
    //Append the overlay to the document body
    $("body").append(overlay.click(function() { 
        overlay.show();
        modalWindow.show();
    }));
    
    //Add a loader to our page
    $("body").append("<div id='upload_loader'><p>"+ msg +"</p></div>");
    
    //Set the css and fade in our overlay
    overlay.css("opacity", 0.8);
    overlay.fadeIn(150);

    //Load the image
    var img = new Image();
    $(img).load(function () {
        var imageWidth = img.width / 2 ;
        var imageHeight = img.height / 2;
        modalWindow.css({
            "margin-left": -imageWidth,
            "margin-top": -imageHeight
        });	
        $("#modal-load").remove();
        modalWindow.append(img);
        $(this).addClass("modal-image");
        $("body").append(modalWindow);
        modalWindow.fadeIn(150);
    })
}


function checkURL(html) {
    
    var ex = new RegExp("^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}((:[0-9]{1,5})?\/.*)?$","i");
    
    return ex.test(html);
     
}

function _deletevideo(id,act,s) {
    
    if (confirm('Are you sure want to delete the video')) {
        $.post('/action/video/edit',{op: 'delVideo', vid: id}, function(data) {
           if (data == 1) {
              _nextlist(act,s,'');
           }
           else {
               alert("The video can't be delete.");
           }
        });
    }
    
}


function _eventmanager(e1,e2,s1) {
    $(document).ready(function(){
        $("#toolform").html(loading('load1')); 
        $("#toolexpland").show("slow");
       
        $.post('/action/video/eventmanager',{e:e1,s:s1}, function(data) {
          $("#toolform").html(data);
        }
        );
    });
  }




