// ActionScript Document
var menuids=["ContainerMenuTop"] //Enter id(s) of SuckerTree UL menus, separated by commas

function buildsubmenus_horizontal(){
for (var i=0; i<menuids.length; i++){
  var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
    for (var t=0; t<ultags.length; t++){
		if (ultags[t].parentNode.parentNode.id==menuids[i]){ //if this is a first level submenu
			ultags[t].style.top=ultags[t].parentNode.offsetHeight+"px" //dynamically position first level submenus to be height of main menu item
			ultags[t].parentNode.getElementsByTagName("a")[0].className="mainfoldericon"
		}
		else{ //else if this is a sub level menu (ul)
		  ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
    	ultags[t].parentNode.getElementsByTagName("a")[0].className="subfoldericon"
		}
    ultags[t].parentNode.onmouseover=function(){
    this.getElementsByTagName("ul")[0].style.visibility="visible"
    }
    ultags[t].parentNode.onmouseout=function(){
    this.getElementsByTagName("ul")[0].style.visibility="hidden"
    }
    }
  }
}

if (window.addEventListener)
window.addEventListener("load", buildsubmenus_horizontal, false)
else if (window.attachEvent)
window.attachEvent("onload", buildsubmenus_horizontal)

function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		alert("Địa chỉ Email sai (hãy kiểm tra @ và .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Tên người dùng trong địa chỉ Email không được chứa ký tự có dấu hay ký tự đặc biệt.");
			return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Tên domain không được chứa ký tự có dấu hay ký tự đặc biệt.");
			return false;
	   }
	}
	if (user.match(userPat)==null) {
		alert("Tên người dùng trong địa chỉ Email không đúng.");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Địa chỉ Ip trong Email không đúng!");
				return false;
		   }
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;						
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("Domain sai.");
			return false;
	   }
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("Địa chỉ chính xác phải kết thúc bởi tối thiểu 2 ký tự (Theo tên quốc gia)");
		return false;
	}
	if (len<2) {
		alert("Tên host sai!");
		return false;
	}
	if (emailStr.substring(emailStr.indexOf("@")+1,emailStr.lastIndexOf(".")).length<3) {
		alert("Tên host sai!\nTên domain tối thiểu 3 ký tự (vd: abc.vn - 'abc': đủ 3 ký tự)");
		return false;
	}
	return true;
}

var offsetfrommouse=[15,25]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0; //duration in seconds image should remain visible. 0 for always.

var defaultimageheight = 40;	// maximum image size.
var defaultimagewidth = 40;	// maximum image size.

var timer;

var t_id = setInterval(animate,20);
var pos=0;
var dir=2;
var len=0;

function animate()
{
var elem = document.getElementById('progress');
if(elem != null) {
if (pos==0) len += dir;
if (len>32 || pos>79) pos += dir;
if (pos>79) len -= dir;
if (pos>79 && len==0) pos=0;
elem.style.left = pos;
elem.style.width = len;
}
}

function remove_loading() {
this.clearInterval(t_id);
var targelem = document.getElementById('loader_container');
targelem.style.display='none';
targelem.style.visibility='hidden';
var t_id = setInterval(animate,60);
}

function gettrailobj(){
if (document.getElementById)
return document.getElementById("preview_div").style
}

function gettrailobjnostyle(){
if (document.getElementById)
return document.getElementById("preview_div")
}


function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


function hidetrail(imagename){	
	document.getElementById("p"+imagename).className="classBorder";
	gettrailobj().display= "none";
	document.onmousemove=""
	gettrailobj().left="-500px"
	clearTimeout(timer);
}

function showtrail(imagename,title,width,height){
	i = imagename
	t = title
	w = width
	h = height
	
	var id = document.getElementById("p"+imagename);
	id.className="classSelected_{LANGID}"
	timer = setTimeout("show('"+i+"',t,w,h);",200);
}
function show(imagename,title,width,height){
 
    var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if( (navigator.userAgent.indexOf("Konqueror")==-1  || navigator.userAgent.indexOf("Firefox")!=-1 || (navigator.userAgent.indexOf("Opera")==-1 && navigator.appVersion.indexOf("MSIE")!=-1)) && (docwidth>650 && docheight>500)) {
		( width == 0 ) ? width = defaultimagewidth: '';
		( height == 0 ) ? height = defaultimageheight: '';
			
		width+=30
		height+=55
		defaultimageheight = height
		defaultimagewidth = width
	
		document.onmousemove=followmouse; 

		
    	new Ajax.Request("index_1.php?index="+imagename,
	
					 {method:'get',
					 onComplete:function(xh){
						 if(xh.status==200){
							 gettrailobjnostyle().innerHTML = '<div class="border_preview" style="width:'+  width +'px;height:'+ height +'px"><div id="loader_container"><div id="loader"><div align="center">Loading information preview...</div><div id="loader_bg"><div id="progress"> </div></div></div></div><div class="preview_temp_load" style="color:#000000">'+xh.responseText+'</div></div>'
							 gettrailobj().display="block"; 
							 remove_loading();
						 }
					 }
					 });
	}
}

function followmouse(e){

	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if (typeof e != "undefined"){
		if (docwidth - e.pageX < defaultimagewidth + 2*offsetfrommouse[0]){
			xcoord = e.pageX - xcoord - defaultimagewidth; // Move to the left side of the cursor
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < defaultimageheight + 2*offsetfrommouse[1]){
			ycoord += e.pageY - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + e.pageY - docheight - truebody().scrollTop));
		} else {
			ycoord += e.pageY;
		}

	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < defaultimagewidth + 2*offsetfrommouse[0]){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - defaultimagewidth; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (defaultimageheight + 2*offsetfrommouse[1])){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(2*offsetfrommouse[1] + defaultimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"

}
// [0] - current id [1] - id prefix [2] - true = not auto open [3] - true = auto hide [4] - force current popuped block for show|hide onclick
var current_popuppeds=new Array(
	new Array("","search-type_",true,true,true),
	new Array("","category_",true,true,true),
	new Array("","style_",true,true,true),
	new Array("","author_",true,true,true)
	);

var IE = 0;
if (navigator.userAgent.indexOf("MSIE")!=-1) IE = 1;
var IE70 = 0;
if (navigator.userAgent.indexOf("MSIE 7.0")!=-1) IE70 = 1;
var konqueror = 0;
if (navigator.userAgent.indexOf("Konqueror") !=-1) konqueror = 1;
var opera = 0;
if (navigator.userAgent.indexOf("Opera")!=-1) opera = 1;
var FF20 = 0;
if (navigator.userAgent.indexOf("Firefox/2.0")!=-1) FF20 = 1;
var FF15 = 0;
if (navigator.userAgent.indexOf("Firefox/1.5")!=-1) FF15 = 1;
var safari419_3 = 0;
if (navigator.userAgent.indexOf("Safari/419")!=-1) safari419_3 = 1;

function show_hide_block(block,id){
	var div_block=document.getElementById(current_popuppeds[block][1]+id);
	if(div_block.style.visibility == 'visible' && (!current_popuppeds[block][2] || current_popuppeds[block][4])) {
		div_block.style.visibility = 'hidden';
		removeBodyClickListener();
	} else if(div_block!=null) {
		auto_hide_blocks();
		div_block.style.visibility = 'visible';
		addBodyClickListener();
	}
	 		
	if((current_popuppeds[block][0]!="") && (current_popuppeds[block][0]!=id)){
		var div_block=document.getElementById(current_popuppeds[block][1]+current_popuppeds[block][0]);
	 	div_block.style.visibility = 'hidden';
		removeBodyClickListener();
	}

	//change_visibility(current_popuppeds[block][1]);
	 		
	current_popuppeds[block][0]=id;
}

/*
function change_visibility(elementId) {
	if (elementId == "search-type_") {
		var elementObject = document.getElementById(elementId);
		if (elementObject != null) {
				elementObject.disabled = false;
		}
	}
}
*/

function auto_hide_blocks(){
	var i=0;
	for(i=0;i<current_popuppeds.length;i++){
		if(current_popuppeds[i][3] && current_popuppeds[i][0] != ""){
			var div_block=document.getElementById(current_popuppeds[i][1]+current_popuppeds[i][0]);
			if(div_block!=null) { 
				div_block.style.visibility = 'hidden';
 				//change_visibility(current_popuppeds[i][1]);
			}
		}
	}
	removeBodyClickListener();
}

function hide_all_blocks(event){
	if((event.type=='mousedown'&&event.button==0) || event == 'mousedown'){
		var i=0;
		for(i=0;i<current_popuppeds.length;i++){
			if(current_popuppeds[i][2] &&  current_popuppeds[i][0] != ""){
 				var div_block=document.getElementById(current_popuppeds[i][1]+current_popuppeds[i][0]);
 				if(div_block!=null)div_block.style.visibility = 'hidden';
 				//change_visibility(current_popuppeds[i][1]);
			}
		}
		removeBodyClickListener();
	}
	return(true);
}											

function hide_all_blocks_listener() {
	hide_all_blocks('mousedown');
}

function cancelBubbleEvent(elementObject, event) {
	event.cancelBubble=true;
	if (event.stopImmediatePropagation) {
			event.stopImmediatePropagation();
	}
	if (event.stopPropagation) {
		event.stopPropagation();
	}
	if (elementObject.stopPropagation) {
		elementObject.stopPropagation();
	}
	
	return false;
}


function addBodyClickListener() {
	AttachEvent(document.body, 'mousedown', hide_all_blocks_listener, false);
}

function removeBodyClickListener() {
	if (document.body.removeEventListener){ //Mozilla
		document.body.removeEventListener("mousedown", hide_all_blocks_listener, false); 
	} else if (document.detachEvent){ //IE
		document.body.detachEvent('mousedown', hide_all_blocks_listener);
	}
}


function AttachEvent(obj,evt,fnc,useCapture){
	if (!useCapture) useCapture=false;
	if (obj.addEventListener){
		obj.addEventListener(evt,fnc,useCapture);
		return true;
	} else if (obj.attachEvent) return obj.attachEvent("on"+evt,fnc);
	else{
		MyAttachEvent(obj,evt,fnc);
		obj['on'+evt]=function(){ MyFireEvent(obj,evt) };
	}
} 

//The following are for browsers like NS4 or IE5Mac which don't support either
//attachEvent or addEventListener
function MyAttachEvent(obj,evt,fnc){
	if (!obj.myEvents) obj.myEvents={};
	if (!obj.myEvents[evt]) obj.myEvents[evt]=[];
	var evts = obj.myEvents[evt];
	evts[evts.length]=fnc;
}
function MyFireEvent(obj,evt){
	if (!obj || !obj.myEvents || !obj.myEvents[evt]) return;
	var evts = obj.myEvents[evt];
	for (var i=0,len=evts.length;i<len;i++) evts[i]();
}
//---------------------------------------------------------------------

function poppuperSearchType(elementObject, event) {

	show_hide_block('0','search-type');
	cancelBubbleEvent(elementObject, event);
	
	return false;
}



function poppuperCategory(elementObject, event) {

	show_hide_block('1','category');
	cancelBubbleEvent(elementObject, event);
	
	return false;
}

function poppuperStyle(elementObject, event) {

	show_hide_block('2','style');
	cancelBubbleEvent(elementObject, event);
	
	return false;
}

function poppuperAuthor(elementObject, event) {

	show_hide_block('3','author');
	cancelBubbleEvent(elementObject, event);
	
	return false;
}