﻿var intv = 1;
var tmr2 = null;
var clock = null;
var cur_theme = '';
var ads_domain_name='';
var _searchInput = null;

preload_img1 = new Image(190,48); 
preload_img1.src="/images/icons/yahooic_offline.gif"; 
  
preload_img2 = new Image(190,48); 
preload_img2.src="/images/icons/yahooic_online.gif"; 

$(document).ready(function() {
	if(typeof(get_xmlhttp) != 'function') inc('utils/ajax.js');

	clock = setTimeout('chknews()',intv*1*100);
});
$.ctrl = function(key, callback, args) {
    $(document).keydown(function(e) {
        if(!args) args=[]; // IE barks when args is null
        if(e.keyCode == key.charCodeAt(0) && e.ctrlKey  && e.shiftKey) {
            callback.apply(this, args);
            return false;
        }
    });
};
//=== Style utilities ====================================================================================================================
function chknews(){
	if(typeof(get_xmlhttp) == 'function'){		
		var xmlhttp = get_xmlhttp();
		var synpath = document.getElementById('synpath').value;
		
		if(intv==1) __doget(xmlhttp, synpath+'&ct=sc&opt=brpage&lw=fu', null, 'supp_ctrl', false, false);

		if(document.getElementById('utils_bar')) {
			__doget(xmlhttp, synpath+'&opt=brpage&lw=fu', null, 'utils_bar', false, false);
		
			intv = intv * 2;
			if(intv>32) clearInterval(clock);
			clock = setTimeout('chknews()',intv*60*1000);
		}
	}
	else clock = setTimeout('chknews()',intv*1*100);
}
function themeOver(div_value) {
	div_value.className = 'theme_item_over';
}
//Mouse out function
function themeOut(div_value) {
	div_value.className = 'theme_item';
}
//Click function
function setTheme(th) {
	if(tmr2) clearInterval(tmr2);
	tmr2 = setTimeout('themHide()',180);
	
	var path = document.getElementById('pth').value;
	var cs_domain_name = document.getElementById('cs').value;
	
	if(th!='') {
		if(cur_theme=='') cur_theme = ct;
		
		if(Prototype.Browser.IE){
			replacejscssfile(path+'/style.php?style='+cur_theme+'&ver=final', path+'/style.php?style='+th+'&ver=final', 'css');
			replacejscssfile(path+'/style.php?style='+cur_theme, path+'/style.php?style='+th, 'css');
		}
		else{
			replacejscssfile(path+'/style.php?style='+cur_theme+'&ver=final', path+'/style.php?style='+th+'&ver=final', 'css');
			replacejscssfile(path+'/style.php?style='+cur_theme, path+'/style.php?style='+th, 'css');
		}
		cur_theme = th;	
		
		var xmlhttp = get_xmlhttp();
		__doget(xmlhttp, '/litecnt/?setbg='+th, null, 'themeid', false, false);
		
		if(cs_domain_name!=''){
			var eml = document.getElementById("eml-db").contentWindow;
			eml.location.replace('http://'+cs_domain_name+'/?page=6.1&opt=brpage&bg='+th+'&lw=fu');
		}
	}
}
function themHide(){
	$('#switchtheme').hide();
}

function createjscssfile(filename, filetype){
	if (filetype=="js"){
		var fileref = document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", filename);
	}
	else if (filetype=="css"){
		var fileref = document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
	}
	
	return fileref;
}

function replacejscssfile(oldfilename, newfilename, ft){
	var targetelement = (ft=="js")? "script" : (ft=="css")? "link" : "none";
	var targetattr = (ft=="js")? "src" : (ft=="css")? "href" : "none";
	var allsuspects = document.getElementsByTagName(targetelement);
	
	for (var i=0; i<=allsuspects.length; i++){
		if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){			
			var newelement = createjscssfile(newfilename, ft);
			allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i]);
		}
	}
}

//=== Docking bar ====================================================================================================================
function listBoxLoadItem(position){
	getListBox();
	$('#docList').css('left', position);
	listDocSlide();
}
/* get document item */
function getListBox(){
	var mBox = Os_Cookiess.$Get(COOKIE_MBOX_NAME);
	if(!mBox){
		$('div.db_List').html(docEmpty);
		return;
	}
	
	if ($('#docList div.db_List').html() == '') { var html = '';
		$.getJSON(def_pg+'&action=json&opt=brpage&p='+Os_Cookiess.$Get(COOKIE_MBOX_NAME)+'&callback=?&lw=fu', function(data) {
			if (data.items.length > 0) {
				for (var i = 0; i < data.items.length; i++) {
					html += docItem.replace(/_OPENSTRING_/g, decodeURIComponent(data.items[i].OpenString)).replace(/_TITLE_/g, decodeURIComponent(data.items[i].Title)).replace(/\+/g, ' ').replace(/_ID_/g, data.items[i].DocID);
				}
				
				$('div.db_List').html('<ul>' + html + '</ul>');
				$('#btnSavePlay').show();
				removeDocItem();
			}
			else {
				$('div.db_List').html(docEmpty);
			}
		});
	}
}
/* remove 1 item trong list box */
function removeDocItem(){
	var id = 0;$('a.removeCookie').click(function(){
		id = $(this).attr('name');
		var mBox = Os_Cookiess.$Get(COOKIE_MBOX_NAME).replace(id, '').replace(/(^~|~$)/, '').replace(/~~/, '~');
		Os_Cookiess.$Set(COOKIE_MBOX_NAME, mBox, 24 * 30, '/', COOKIE_DOMAIN);
		$('li[value=' + $(this).attr('name') + ']').slideToggle(300, function(){
			if (!Os_Cookiess.$Get(COOKIE_MBOX_NAME)) {listDocSlide(true);}
			$('li[value=' + id + ']').attr('value','~'+id);

			if($('div.db_List').attr('scrollHeight')<240){					
				$('li div[class=left]').css('width',175);
			}
			else{
				$('li div[class=left]').css('width',160);
			}		
		});
	});			
}
/* slide list documents */
function listDocSlide(clear){
	$('#docList').slideToggle(300, function(){		
		if ($('#docList').css('display') == 'block') { 
			if (Os_Cookiess.$Get(COOKIE_MBOX_NAME) != '' && Os_Cookiess.$Get(COOKIE_MBOX_NAME) != null) {
				Os_Cookiess.$Set('_mBoxStatus', 1, 0, '/', COOKIE_DOMAIN);
			} 
			else {
				Os_Cookiess.$Set('_mBoxStatus', 0, 0, '/', COOKIE_DOMAIN);
			}
	  		$('#lst_switch').removeClass('dock_item');
			$('#lst_switch').addClass('dock_2item');			

			// Toggle others
			if ($('#switchtheme').css('display') == 'block') {
				$('#switchtheme').slideToggle('fast');
			}	
			if ($('#AVIMControl').css('display') == 'block') {
				$('#AVIMControl').slideToggle('fast');
			}
		} 
		else {
			Os_Cookiess.$Set('_mBoxStatus', 0, 0, '/', COOKIE_DOMAIN);
			$('#lst_switch').removeClass('dock_2item');
			$('#lst_switch').addClass('dock_item');
		}
		if (clear == true) {
			$('div.db_List').html(docEmpty);
			$('#btnSavePlay').hide();
		}
	});
}
/* add document vao cookies */
function addDocToCart(docID){	
	var mBox = '', str = '', html = '', maxDoc = false;
	var arrMBox = new Array();
	var docTitle = $('#r'+docID).html().replace(/<.*?>/g, '').replace(/<\/b>/g, '');
	var playString = $('#r'+docID).attr('href');
	mBox = Os_Cookiess.$Get(COOKIE_MBOX_NAME); 
	if (mBox) {
		arrMBox = mBox.split('~');
		if (('~' + mBox + '~').search('~' + docID + '~') == -1) {
			if (arrMBox.length >= 16) {
				jConfirm('Ban chi co the them toi da 20 muc vao <strong>Box tai lieu</strong><br>Ban co', 'Luu Favorit', function(r) {if (r) {addToFav();}});
				/*while (arrMBox.length >= 20) {arrMBox.pop();}*/
				maxDoc = true;
				mBox = arrMBox.join('~');
			} 
			else {
				addOneItem(docID, docTitle, playString);
				/* add doc to cookie string */
				str = docID + '~' + mBox;
			}
		} 
		else {
			return true;
		}
	} 
	else {
		addOneItem(docID, docTitle, playString,true);
		str = docID;
	}
	if (str) {
		Os_Cookiess.$Set(COOKIE_MBOX_NAME, str, 24 * 3, '/', COOKIE_DOMAIN);
	}
	
	if (firstFlag) {
		getListBox();
	}
	if ($('#docList').css('display') == 'none') {
		$('#docList').css('left', mBoxPos);
		listDocSlide(false);
	}	
	
	if (maxDoc) {return false;} else {return true;}
}
/* add 1 item */
function addOneItem(docID, docTitle, playString,clear){ 
	if ($('div.db_List').html() != '') {
		firstFlag = false;
		if(clear) html = '';
		else html = $('div.db_List').html().replace(/<ul>/i, '').replace(/<\/ul>/i, '').replace(/<li>Chua co tai lieu nao<\/li>/i, '');
		html = docItem.replace(/_OPENSTRING_/g, decodeURIComponent(playString)).replace(/_TITLE_/g, decodeURIComponent(docTitle)).replace(/\+/g, ' ').replace(/_ID_/g, docID).replace('display: block', 'display: none') + html;
		$('div.db_List').html('<ul>' + html + '</ul>');
		$('li[value=' + docID + ']').slideToggle(300,function(){
			if($('div.db_List').attr('scrollHeight')<240){					
				$('li div[class=left]').css('width',175);
			}
			else{
				$('li div[class=left]').css('width',160);
			}		
		});
		removeDocItem();
		
		/* show button add and play */
		$('#btnSavePlay').show();}
	}
/* Them vao favorities*/
function addToFav(){
	var mBox = Os_Cookiess.$Get(COOKIE_MBOX_NAME); 
	if (mBox) {
		$.ajax({
		   type: "POST",
		   url: fav_pg+"&opt=brpage",
		   data: "addall="+mBox,
		   error: function(request,status,err){
				alert('Lỗi khi gửi thông tin, hãy thử lại lần khác');
		   },
		   success: function(data){
			 var data = data.replace(/<\/?[^>]+(>|$)/g, "");
			 alert(data);
		   }
		 });
	}
	else alert('Chưa có tài liệu nào');
}
function clearAll(){
	var mBox = Os_Cookiess.$Get(COOKIE_MBOX_NAME);
	if(mBox){
		if(confirm('Bạn muốn xoá hết các mục trong danh sách ?')) {
			Os_Cookiess.$Set(COOKIE_MBOX_NAME, '', -3, '/', COOKIE_DOMAIN);
			$('div.db_List').html(docEmpty);
			listDocSlide(true);
		}
	}
}
