
var ieenv = navigator.userAgent.toLowerCase();
var is_opera = ieenv.indexOf('opera') != -1 && opera.version();
var is_ie = (ieenv.indexOf('msie') != -1 && !is_opera) && ieenv.substr(ieenv.indexOf('msie') + 5, 3);
var objectclassid;

var menuscript = new Array();
menuscript['objects'] = menuscript['timeout'] = menuscript['insert'] = new Array();

function isvar(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function chgobjview(targetid, defaultview) {
	if(defaultview == 'auto') {
		$("#"+targetid).css("display") == 'none' ? $("#"+targetid).css("display", "") : $("#"+targetid).css("display", "none");
	} else {
		$("#"+targetid).css("display", defaultview);
	}
}

function ajaxloding(targetid, disvar) {
   if (isvar(disvar)) disvar = "";
   
   chgobjview(targetid, disvar);
   $("#"+targetid).html('<span><img src="images/default/loading.gif"> Loading......</span>');
}

function getajax(geturl, targetid) { 
  $.ajax({     
     beforeSend: function(){
        ajaxloding(targetid);
     },
     type: "GET",
     url: geturl,
     cache: false,
     success: function(msg) {
        $("#"+targetid).html(msg);
        chgvote(targetid);
        
        if (targetid == 'form_smilies') smiles_add();

		//add by panghe @2009-2-11
		try{
			var anchor = getanchor();
			if(anchor){
				$(getanchor()).ScrollTo(1);		
			}
		}catch(e){		
		}
		//add end
     },
     error: function(xmlHttpRequest, error) {
        //$("#"+targetid).html("ERROR:"+geturl);
     }
  });
}

function chgvote(showid, targetid) {
   targetid = targetid ? targetid : 'A';
   $("a[href*=actionid]", $("#"+showid)).each(function() { 
      $(this).bind("click", function(){
         getajax($(this).attr("href"), showid, '');
         return false;
      });
   });
}

function smiles_add() {
    
    if (typeof WYSIWYG == 'undefined') WYSIWYG = '';

	$("#form_smilies img").each(function(){
		$(this).click(function(){
			if (($("#preview").text() == WYSIWYG)) {
				if (this.id != '') {
					$("#message").focus();
					if (typeof document.selection != "undefined") {
						var sel = document.selection.createRange();
						sel.text = this.id;
					}
					else {
						$("#message")[0].value = $("#message")[0].value.substr(0, $("#message")[0].selectionStart) + this.id + $("#message")[0].value.substring($("#message")[0].selectionEnd, $("#message")[0].value.length);
					}
				}
			} else {
				if (this.id != '') {
					id = (this.id).replace("fc_", '');
					id = (id).replace("]", '');
					id = (id).replace("[", '');
					href = "images/smilies/default/" + id + ".gif";
					she_zhi('InsertImage', href);
				}
			}
		})
	});

}

function getvoteitem(geturl, showid, disview) {
  if (isvar(disview)) disview = "auto";
  
  $.ajax({     
     beforeSend: function(){
        ajaxloding(showid, disview);
     },
     type: "GET",
     url: geturl,
     cache: false,
     success: function(msg) {
        $("#"+showid).html(msg);
        chgevents(showid);
     },
     error: function(xmlHttpRequest, error) {
        //$("#"+showid).html("ERROR:"+geturl);
     }
  });
}

function chgevents(showid, targetid) {
   targetid = targetid ? targetid : 'A';
   $("a[href*=actionid]", $("#"+showid)).each(function() { 
      $(this).bind("click", function(){
         getvoteitem($(this).attr("href"), showid, '');
         return false;
      });
   });
}

function itemexists(item, arrys) {
	if(typeof item == 'string' || typeof item == 'number') {
		for(var i in arrys) {
			if(arrys[i] == item) {
					return true;
			}
		}
	}
	return false;
}

function getid(id) {
	return document.getElementById(id);
}

function doevent(event) {
	e = event ? event : window.event;
	if(is_ie) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else if(e) {
		e.stopPropagation();
		e.preventDefault();
	}
}

function loadctrl(opobjec, timeout, layer) {
	if(opobjec && !opobjec.initialized) {
		opobjec.initialized = true;
		opobjec.unselectable = true;

		opobjec.outfunc = typeof opobjec.onmouseout == 'function' ? opobjec.onmouseout : null;
		opobjec.onmouseout = function() {
			if(this.outfunc) this.outfunc();
			menuscript['timeout'][opobjec.id] = setTimeout('menuhide(' + layer + ')', timeout);
		}

		opobjec.overfunc = typeof opobjec.onmouseover == 'function' ? opobjec.onmouseover : null;
		opobjec.onmouseover = function(e) {
			doevent(e);
			if(this.overfunc) this.overfunc();
            for(var id in menuscript['timeout']) {
			   if(menuscript['timeout'][id]) clearTimeout(menuscript['timeout'][id]);
			}
		}
	}
}

function loadmenu(ctrlid, menuobj, timeout, layer) {
	if(menuobj && !menuobj.initialized) {
		menuobj.initialized = true;
		menuobj.ctrlkey = ctrlid;
		menuobj.style.position = 'absolute';
		menuobj.onmouseover = function() { clearTimeout(menuscript['timeout'][ctrlid]); }
		menuobj.onmouseout = function() { menuscript['timeout'][ctrlid] = setTimeout('menuhide(' + layer + ')', timeout); }
		menuobj.style.zIndex = 50;
	}
}

function viewdropmenu(ctrlid) {
	var opobjec = getid(ctrlid);
	var click = false;
	var offset = 0;
	var timeout = 0;
	var layer = 0;
	var showid = ctrlid;
	var showobj = getid(showid);
	var menuobj = getid(showid + '_menu');
	var maxh = 400;
    if(!menuobj) return;

	menuhide(layer);

	for(var id in menuscript['timeout']) {
		if(menuscript['timeout'][id]) clearTimeout(menuscript['timeout'][id]);
	}

	loadctrl(opobjec, timeout, layer);
	objectclassid = opobjec.className;
	opobjec.className += ' hover';
	loadmenu(ctrlid, menuobj, timeout, layer);

	menuobj.style.display = '';
	if(!is_opera) {
		menuobj.style.clip = 'rect(auto, auto, auto, auto)';
	}

	menuwzSet(showid, offset);

	if(is_ie && is_ie < 7) {
		if(!menuscript['insert'][layer]) {
			var iframe = document.createElement('insert');
			iframe.style.display = 'none';
			iframe.style.position = 'absolute';
			iframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			getid('append_parent') ? getid('append_parent').appendChild(iframe) : menuobj.parentNode.appendChild(iframe);
			menuscript['insert'][layer] = iframe;
		}
		menuscript['insert'][layer].style.top = menuobj.style.top;
		menuscript['insert'][layer].style.left = menuobj.style.left;
		menuscript['insert'][layer].style.width = menuobj.w;
		menuscript['insert'][layer].style.height = menuobj.h;
		menuscript['insert'][layer].style.display = 'block';
	}

	if(maxh && menuobj.scrollHeight > maxh) {
		menuobj.style.height = maxh + 'px';
		if(is_opera) {
			menuobj.style.overflow = 'auto';
		} else {
			menuobj.style.overflowY = 'auto';
		}
	}

	menuscript['objects'][layer] = menuobj;
}

function menuwzSet(showid, offset) {
	var showobj = getid(showid);
	var menuobj = getid(showid + '_menu');
	if(isvar(offset)) offset = 0;
	if(showobj) {
		showobj.pos = getmenuOffset(showobj);
		showobj.X = showobj.pos['left'];
		showobj.Y = showobj.pos['top'];
		showobj.w = showobj.offsetWidth;
		showobj.h = showobj.offsetHeight;
		menuobj.w = menuobj.offsetWidth;
		menuobj.h = menuobj.offsetHeight;
		menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + 'px';
		menuobj.style.top = offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj.Y - menuobj.h >= 0)) ? (showobj.Y - menuobj.h) + 'px' : showobj.Y + showobj.h + 'px');
		if(menuobj.style.clip && !is_opera) {
			menuobj.style.clip = 'rect(auto, auto, auto, auto)';
		}
	}
}

function menuhide(layer) {
	if(isvar(layer)) layer = 0;
	if(menuscript['objects'][layer]) {
		try {
			getid(menuscript['objects'][layer].ctrlkey).className = objectclassid;
		} catch(e) {}
		clearTimeout(menuscript['timeout'][menuscript['objects'][layer].ctrlkey]);
		menuscript['objects'][layer].style.display = 'none';
		if(is_ie && is_ie < 7 && menuscript['insert'][layer]) {
			menuscript['insert'][layer].style.display = 'none';
		}
		menuscript['objects'][layer] = null;
	}
}

function getmenuOffset(ctrl) {
	var disleft = ctrl.offsetLeft;
	var distop = ctrl.offsetTop;
	while((ctrl = ctrl.offsetParent) != null) {
		disleft += ctrl.offsetLeft;
		distop += ctrl.offsetTop;
	}
	return { 'left' : disleft, 'top' : distop };
}

function refreshCode() {
	var rand = Math.random();
	$('#codeimage').html('<img id="seccode" onclick="refreshCode()" src="seccode.php?update=' + rand + '" class="absmiddle" alt="" />');
}

function checkCode() {
	var codeValue = $('#pubcode').val();
	var handles = $('#codenotices');

	if(!(/[0-9]/.test(codeValue))) {
		noticeMsg(handles, profile_seccode_invalid);
		return;
	}
	checkCodeResponse(handles, 'action=checkseccode&codevalue=' + (is_ie && document.charset == 'utf-8' ? encodeURIComponent(codeValue) : codeValue));
}

function checkCodeResponse(obj, data) {
	geturl = "getajax.php?" + data;
 	$.ajax({
 		type: "GET",
     	url: geturl,
     	cache: false,
     	success: function(msg) {     
     		if (msg == 'succeed') {
     			obj.css("display", "");
     			obj.html('<img src="images/default/check_right.gif" width="13" height="13">');
     		} else {
     			noticeMsg(obj, sub_submit_seccode_invalid);
     		}  
     	},
     	error: function(xmlHttpRequest, error) {
        	//obj.html("ERROR:"+geturl);
     	}
 	});
}

function noticeMsg(obj, msg) {
    //$('#pubcode').select();
    obj.css("display", "");
    obj.html('<img src="images/default/check_error.gif" width="13" height="13">&nbsp;'+ msg);
}

