// Custom utils by ChaosAD
//var HTTP_SERVER = "http://localhost/project/valueenglish";
var HTTP_SERVER = "http://valueenglish.com";

jQuery.fn.isEmpty = function(){
	var re = /\s/g; //Match any white space including space, tab, form-feed, etc.
	var str = jQuery(this).val().replace(re, "");
	return ( (str.length == 0) ? true : false );
}

jQuery.fn.isEmail = function(){
	var email_reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return email_reg.test(jQuery(this).val());
}

jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++) { jQuery("<img />").attr("src", arguments[i]); }
}

jQuery.fn.loadpage = function(target_url, loading_img_size, loadSuccess){
	var target_obj = this;
	var loading_imgs = { s : "/imgs/layout/loading_s.gif", m : "/imgs/layout/loading_m.gif", l : "/imgs/layout/loading_l.gif" };
	if ( !loading_img_size ) { var loading_img_size = "l"; }
	target_obj.css("height",target_obj.height()+"px");
	target_obj.html("");
	target_obj.css({"background-image":"url("+HTTP_SERVER+loading_imgs[loading_img_size]+")","background-repeat":"no-repeat","background-position":"center"});
	jQuery.ajax({
		url: target_url,
		success: function(rspnse){
			target_obj.css("background-image","none");
			target_obj.css("height","auto");
			target_obj.html(rspnse);
			target_obj.css("height",target_obj.height()+"px");
			if ( jQuery.isFunction(loadSuccess) ) { loadSuccess(); }
		}
	});
}

jQuery.trim12 = function(str){
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

jQuery.in_array = function(needle, haystack, argStrict){
	var found = false, key, strict = !!argStrict;
	for (key in haystack) {
		if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
			found = true;
			break;
		}
	}
	return found;
}

jQuery.array_key_exists = function(key,search){
	if( !search || (search.constructor !== Array && search.constructor !== Object) ){ return false; }
	return key in search;
}

//jQuery.fn.popwin = function(popurl,popwinwidth,popwinheight,appval){
jQuery.fn.popwin = function(popurl,popwinwidth,popwinheight){
	function rePosPopWin(){
		addTopSpc = $(window).scrollTop();
		addLeftSpc = $(window).scrollLeft();
		var popwintopspc = (($(window).height()-popwinheight)/2)+addTopSpc;
		var popwinleftspc = (($(window).width()-popwinwidth)/2)+addLeftSpc;
		$("#popWin").css({"top":popwintopspc+"px","left":popwinleftspc+"px"});
		$("#popWinBgOverlay").css({"width":$(document).width()+"px","height":$(document).height()+"px","top":"0px"});
	}
	$(this).click(function(event){
		var winheight = $(window).height();
		var winwidth = $(window).width();
		var popwintopspc = ((winheight-popwinheight)/2)+$(window).scrollTop();
		var popwinleftspc = (winwidth-popwinwidth)/2;
		var winst = $(window).scrollTop();
		var winsl = $(window).scrollLeft();
		//var targeturl = ( appval === true ) ? popurl+$(this).val() : popurl;
		targeturl = ( popurl == "" ) ? $(this).attr("href") : popurl;
		$("body").prepend('<div id="popWinBgOverlay"></div><div id="popWin"></div>');
		$("#popWinBgOverlay").css({"width":$(document).width()+"px","height":$(document).height()+"px","top":"0px"});
		$("#popWin").css({"width":popwinwidth+"px","height":popwinheight+"px","top":popwintopspc+"px","left":popwinleftspc+"px"});
		jQuery.ajax({ url:targeturl, success: function(rspnse){ $("#popWin").css("background-image","none").html(rspnse); } });
		$("#popWinBgOverlay").click($.exitpopwin);
		$(window).resize(function(){ rePosPopWin(); });
		//$(window).scroll(function(){ $(window).scrollTop(winst); $(window).scrollLeft(winsl); });
		event.preventDefault();
	});
}
jQuery.exitpopwin = function(){
		$(window).unbind("scroll");
		$("#popWinBgOverlay").remove();
		$("#popWin").remove();
}

jQuery.iniHelpPop = function(){
	var help_popups = $(".popHelp");
	$(".helpIcon").remove();
	help_popups.unbind("hover");
	for ( hp = 0; hp < help_popups.length; hp++ ){
		var help_bx_content = ( !$(help_popups[hp]).attr("id") ) ? '<img src="'+HTTP_SERVER+'/imgs/layout/help_top.png" width="200" height="8" /><br /><div class="tWrap">'+$(help_popups[hp]).html()+'</div><img src="'+HTTP_SERVER+'/imgs/layout/help_bottom.png" width="200" height="8" />' : $(help_popups[hp]).html();
		$(help_popups[hp]).attr("id", "helpPop"+hp).before('<a href="" id="helpPop'+hp+'Icon" class="helpIcon"><img src="'+HTTP_SERVER+'/imgs/layout/help.gif" align="absmiddle" border="0" /></a>').html(help_bx_content);
	}
	$(".helpIcon").hover(
		function(){
			var obj_id = "#"+$(this).attr("id").replace('Icon','');
			var obj_pos = $(this).position();
			$(obj_id).css({"left":(obj_pos.left+17), "top":obj_pos.top}).fadeIn(300);
		},
		function(){
			var obj_id = "#"+$(this).attr("id").replace('Icon','');
			var obj_pos = $(this).position();
			$(obj_id).fadeOut(300);
		}
	).click(function(event){ event.preventDefault(); });
}
jQuery.iniHelpPop2 = function(){
	var help_popups = $(".popHelp2");
	for ( hp = 0; hp < help_popups.length; hp++ ){
		$(help_popups[hp]).html( ( !$(help_popups[hp]).attr("id") ) ? '<img src="'+HTTP_SERVER+'/imgs/layout/help_top.png" width="200" height="8" /><br /><div class="tWrap">'+$(help_popups[hp]).html()+'</div><img src="'+HTTP_SERVER+'/imgs/layout/help_bottom.png" width="200" height="8" />' : $(help_popups[hp]).html() );
	}
	$(".popHelpIc").hover(
		function(){
			var obj_id = $("div.popHelp2",this);
			var obj_pos = $(this).position();
			$(obj_id).css({"left":(obj_pos.left+64), "top":obj_pos.top-16}).fadeIn(300);
		},
		function(){
			var obj_id = $("div.popHelp2",this);
			var obj_pos = $(this).position();
			$(obj_id).fadeOut(300);
		}
	);
}


$(document).ready(function(){
	$.iniHelpPop();
	$.iniHelpPop2();
});
