// Run jQuery namespace
jQuery.noConflict();

// search by class //
document.getElementsByClassName = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
}; 

// search by id //
document.getElementsById = function(cl) {
var retnode = [];
var myclass = new RegExp('\\b'+cl+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].id;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
}; 

//Fixing missed and wrong tags
/////////////////////////////////////////
// Set video_player height to main_info (Watch page)
// Some vars ...

//1. Fix index page
function fixXHtml(){
// Add div=clear after last div=img and after #main_info
var arrImgDiv=document.getElementsByClassName("img");
var lastImgDiv=arrImgDiv[arrImgDiv.length-1];
jQuery(lastImgDiv).after("<div class='clear'></div>");
jQuery("#main_info").after("<div class='clear'></div>");
}

// add AddThis 
var txtJAddThis='<div id="page-up">'+
'<span class="page_title">Share and Bookmark This</span>'+
'<div id="add-this-wrp">'+
'<div class="addthis_toolbox addthis_default_style">'+
'<a href="http://www.addthis.com/bookmark.php?v=250&username=xa-4c5713383e4cbf7e" class="addthis_button_compact">Share</a>'+
    '<a class="addthis_button_facebook"></a>'+
    '<a class="addthis_button_digg"></a>'+
    '<a class="addthis_button_delicious"></a>'+
    '<a class="addthis_button_twitter"></a>'+
    '<a class="addthis_button_google"></a>'+
    '<a class="addthis_button_aim"></a>'+
    '<a class="addthis_button_linkedin"></a>'+
    '<a class="addthis_button_technorati"></a>'+
    '<a class="addthis_button_yahoobkm"></a>'+
    '<a class="addthis_button_myspace"></a>'+
    '<a class="addthis_button_mixx"></a>'+
    '<a class="addthis_button_netvibes"></a>'+
    '<a class="addthis_button_newsvine"></a>'+
    '<a class="addthis_button_orkut"></a>'+
    '<a class="addthis_button_posterous"></a>'+
    '<a class="addthis_button_reddit"></a>'+
    '<a class="addthis_button_stumbleupon"></a>'+
    '<a class="addthis_button_tumblr"></a>'+
    '<a class="addthis_button_email"></a>'+
'</div>'+
'<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c5713383e4cbf7e"></script></div>'+
'<div class="clear"></div></div>';

function addAddThis(before){
var divPwpr=document.getElementById(before);
jQuery(divPwpr).before(txtJAddThis);
}

// fix bottom pages block to page-bottom
function fixCategories(){
var arrPageUp = document.getElementsById("page-up");
var lastPageUp = arrPageUp[arrPageUp.length-1];
jQuery(lastPageUp).attr("id","page-bottom");
}

// fix setting page - add clear after FrameDisplay
function fixSettingPage(){
jQuery("#FrameDisplay").after("<div class='clear'></div>");
}
// fix Video Setting pafe
function fixVideoSetting(){
jQuery(".VSFrameR").after("<div class='clear'></div>");
}

//Add html code of ads-block to tags block on watch page...
function insertAdsToTagsDiv(){
jQuery("#tagArea").before(jQuery("#inc"));
jQuery("#inc").css({"display": "block"});
}
//Add html code of top-list after .download_text block on watch page...
function insertTopToWatch(){
jQuery(".download_text").after(jQuery(".top-watch"));
jQuery(".top-watch").css({"display": "block"});
}

// Add Template Menu
var tmItm='<div class="titleSetting" style="margin-top:5px">Addones Menu</div>'+
'<div class="inner">&raquo; <a id="miTemplate" href="javascript:void(0)">Template Setting</a></div>';
function addTemplateMenu(){
jQuery("#qStatus").before(tmItm);
}


/***************************
**        jQuery popup     **
*****************************/
var popupStatus = 0;

//**** create jQuery popup for Template Menu ***//
function loadPopupTM(){
	if(popupStatus==0){
		jQuery("#backgroundPopup").css({
			"opacity": "0.7"});
		jQuery("#backgroundPopup").fadeIn("slow");
		jQuery("#templateMenu").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopupTM(){
	if(popupStatus==1){
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#templateMenu").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopupTM(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#templateMenu").height();
	var popupWidth = jQuery("#templateMenu").width();
	//centering
	jQuery("#templateMenu").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	jQuery("#backgroundPopup").css({
		"height": windowHeight
	});
}

//**** create jQuery popup for login ***//
function loadPopup(){
	if(popupStatus==0){
		jQuery("#player").css({
			"display": "none"});
		jQuery("#backgroundPopup").css({
			"opacity": "0.7"});
		jQuery("#backgroundPopup").fadeIn("slow");
		jQuery("#popupLogin").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
	jQuery("#player").css({
			"display": "block"});
		jQuery("#backgroundPopup").fadeOut("slow");
		jQuery("#popupLogin").fadeOut("slow");
		
		popupStatus = 0;
	}
}

function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupLogin").height();
	var popupWidth = jQuery("#popupLogin").width();
	//centering
	jQuery("#popupLogin").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	jQuery("#backgroundPopup").css({
		"height": windowHeight
	});
}

// ***** CONTROLLING EVENTS IN jQuery  ******//
jQuery(document).ready(function(){
	
	//LOADING POPUP Tmp Menu
	jQuery("#miTemplate").click(function(){
		centerPopupTM();
		loadPopupTM();
	});
	//LOADING POPUP Login
	jQuery("#popupLoginOpen").click(function(){
		centerPopup();
		loadPopup();
	});
	
	jQuery("#ShowInfoBox").click(function(){
		centerPopup();
		loadPopup();
	});
				
	//CLOSING POPUP Tmp Menu
	jQuery("#miTemplateClose").click(function(){
		disablePopupTM();
	});
	/*
	jQuery("#backgroundPopup").click(function(){
		disablePopupTM();
	});
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopupTM();
		}
	});
	*/
	//CLOSING POPUP Login
	jQuery("#popupLoginClose").click(function(){
		disablePopup();
	});
});
//*************** end jQuery popup ******************// 

// Override functions
//Saving the original func
var org_getMoreTags = window.getMoreTags;
var org_getLessTags = window.getLessTags;
var org_ShowBox = window.ShowBox;

//Assigning proxy fucnc
window.getMoreTags = function(args){
     org_getMoreTags(args); 
     insertAdsToTagsDiv();
};

window.getLessTags = function(args){
     org_getLessTags(args); 
     insertAdsToTagsDiv();
};

window.ShowBox = function(arg1, arg2){
     org_ShowBox(arg1, arg2); 
     centerPopup();
	 loadPopup();
};



/****************************************
*  Search field - show/hide default text *
*****************************************/
var clk=false;
function isEmpty( inputStr ) { 
      if ( null == inputStr || "" == inputStr ) { 
        return true; } 
      return false; 
}

function clkSearch(str){
      clk=true;
      if(str.indexOf("Search videos ...")==-1){return str;}
      else {return "";}
      }

function restSearch(){
    var obj=document.getElementById("s");
    if(isEmpty(obj.value) && !clk)
      obj.value="Search videos ...";
    }

/*************************
**       Bookmark       **
**************************/
function bookmark_us(url, title){
if (window.sidebar) // firefox
    window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();}
else if(document.all)// ie
    window.external.AddFavorite(url, title);
}

