//------------------------------
$(document).ready(function() {
	//-------
	$(".no_more").each(function() {
		htm=$(this).html();
		if(htm!='more') {
			if($(this).find("a").size()>1) {
				cont=htm.substring(htm.lastIndexOf('<a'));
				$(this).html(cont);
			} else {
				$(this).html('');
			}
		} else {
			$(this).html('');
		}
	});
	//-------
	$("#usrreg_captcha_change").click(function() {
		var randomnumber = Math.floor(Math.random()*9999);
		$("#usrreg_captcha").attr("src","/check/get_captcha.php?ver="+randomnumber);
	});
	//-------
	$(".tail-block").each(function() {
		var it = $(this);
		i=it.find("a[href='']").parent().html();
		if(i!=null) {
			if(i.substring(1,4)=='...') {
				it.remove();
			}
		}
		it.find("a[href^='./view_news']:gt(0)").each(function() {
			it.remove();
		});
		it.show();
	});
	//-------
});
//------------------------------
var top_for_layer = 250;
var tyf_lf_top	  = 94;
//-------
function show_tyf_lf() {
	//-------
	var tyf_lf	= $("#tyf_layerform");
	//-------
	tyf_lf.css('left',-450).fadeIn("fast");
	tyf_lf.animate({left: ((document.body.clientWidth/2)-200)}, 1000);
	tyf_lf.jqDrag("#tyf_layerform_title");
	//-------
}
function show_layer_tell_your_friends() {
	//-------
	var ltyf	= $("#layer_tell_your_friends");
	//-------
	ltyf.css('left',-350).fadeIn("fast");
	ltyf.animate({left: ((document.body.clientWidth/2)-200)}, 1000);
	ltyf.jqDrag();
	//-------
}
//------------------------------
function get_layer_on_index($type) {
	//-------
	if($type == 2) {
		$("#layer_tell_your_friends").remove();
		$("#tyf_layerform").remove();
	}
	//-------
	var COOKIE_GLOI		= 'gloi_cookie';
	var alert_message	= "";
	var cur_pgn			= $("#current_page_name");
	//-------
	$.ajax({
		type:		"POST",
		url:		"get_layer_on_index_page.php",
		data:		"task=get_this_layer",
		success:	function(data) {
			//-------
			cur_pgn.after(data);
			//-------
			var dl 		= $("#dialog");
			var dm 		= dl.find("div");
			var emails	= $(".emails");
			var ltyf	= $("#layer_tell_your_friends");
			var ltyf_x	= $("#layer_tell_your_friends_x");
			var ltyf_ok	= $("#layer_tell_your_friends_ok");
			var tyf_lf	= $("#tyf_layerform");
			var tyf_lf_ok = $("#tyf_layerform .ok");
			var tyf_lf_x  = $("#tyf_layerform .x");
			//-------
			dl.dialog({
				bgiframe: true,
				resizable: false,
				autoOpen: false,
				width:444,
				modal: true
			});
			//-------
			ltyf.css('top',(top_for_layer+'px'));
			tyf_lf.css('top',(tyf_lf_top+'px'));
			if($type == 2) {
				setTimeout('show_tyf_lf()',60);
			} else {
				if($.cookie(COOKIE_GLOI) == "is") {
					return false;
				}
				setTimeout('show_layer_tell_your_friends()',60);
			}
			//-------
			$(window).scroll(function() {
				scroll_y=(document.documentElement) ? document.documentElement.scrollTop : document.body.scrollTop;
				ltyf.css('top',(top_for_layer+scroll_y)+'px');
				tyf_lf.css('top',(tyf_lf_top+scroll_y)+'px');
			});
			//-------
			ltyf.mouseup(function() {
				scroll_y=(document.documentElement) ? document.documentElement.scrollTop : document.body.scrollTop;
				top_for_layer=parseInt($(this).css('top').replace('px',''))-scroll_y;
			});
			//-------
			ltyf_x.mouseover(function() {
				$(this).css('background-color','#660066').css('color','white');
			});
			ltyf_x.mouseout(function() {
				$(this).css('background-color','white').css('color','black');
			});
			ltyf_x.click(function() {
				var date = new Date();
				date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
				$.cookie(COOKIE_GLOI, 'is', { path: '/', expires: date });
				
				ltyf.animate({left: document.body.clientWidth}, 1000).fadeOut('fast');
			});
			//-------
			ltyf_ok.mouseover(function() {
				$(this).css('border-color','red');
			});
			ltyf_ok.mouseout(function() {
				$(this).css('border-color','black');
			});
			ltyf_ok.click(function() {
				ltyf.fadeOut('normal');
				setTimeout('show_tyf_lf()',60);
			});
			//-------
			function tyf_lf_check() {
				var inp = tyf_lf.find('input');
				var can_send = true;
				inp.filter(":lt(2)").each(function() {
					if($(this).val() == "") {
						can_send = false;
					}
				});
				if(can_send == false) {
					alert_message = "Your Name and Email is required."
					return false;
				}
				var name_flag = false;
				inp.filter("[name^='name_']").each(function() {
					if($(this).val() != "") {
						name_flag = true;
					}
				});
				var email_flag = false;
				inp.filter("[name^='email_']").each(function() {
					if($(this).val() != "") {
						email_flag = true;
					}
				});
				if(name_flag == false || email_flag == false) {
					alert_message = "At least one Friend's Name and Email is required."
					return false;
				}
				return true;
			}
			//-------
			function tyf_lf_send() {
				var params = "";
				var it	 = "";
				var name = "";
				var val	 = "";
				tyf_lf.find('input').each(function() {
					it   = $(this);
					name = it.attr('name');
					val  = it.val();
					if(name != "add_me_to_the_ps") {
						params = params + "&" + name + "=" + val;
					} else {
						if(it.attr('checked')) {
							params = params + "&" + name + "=yes";
						}
					}
				});
				$.ajax({
					type:		"POST",
					url:		"get_layer_on_index_page.php?code=kl_4sefsfi_o5p" + params,
					data:		"task=send_email"
				});
				var date = new Date();
				date.setTime(date.getTime() + (60 * 24 * 60 * 60 * 1000));
				$.cookie(COOKIE_GLOI, 'is', { path: '/', expires: date });
			}
			//-------
			function real_email_checking(num) {
				var next = num + 1;
				if(num < emails.size()) {
					var it = emails.eq(num);
					if(it.val() == "") {
						dm.html(dm.html()+ "<hr><b>Checking email: </b>");
						dm.html(dm.html()+ '<span style="color:red">Email is empty!</span>');
						var i = dl.data("incorrect_emails")+1;
						dl.data("incorrect_emails",i);
						it.addClass("incorrect_email");
						real_email_checking(next);
						return false;
					}
					dm.html(dm.html()+ "<hr><b>Checking email: </b>");
					$.ajax({
						type: "POST",
						url: "/engine/validation/email_check/low/er/",
						data: { 'email':it.val() },
						dataType: 'json',
						success: function(data) {
							if(data.errors.length == 0) {
								dm.html(dm.html()+ '<b><span style="color:green;font-size:1.1em">'+it.val()+'</span></b><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:green">This Email is verified.</span>');
							} else {
								var msg = '<b><span style="color:red;font-size:1.1em">'+it.val()+"</span></b><br>";
								msg = msg + '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:red">'+data.errors[1]+"</span><br>";
								msg = msg + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "+data.errors[3]+"<br>";
								msg = msg + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "+data.errors[4]+"<br>";
								msg = msg + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "+data.errors[5]+"<br>";
								msg = msg + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "+data.errors[6]+"<br>";
								dm.html(dm.html() + msg);
								var i = dl.data("incorrect_emails")+1;
								dl.data("incorrect_emails",i);
								it.addClass("incorrect_email");
							}
							real_email_checking(next);
							return true;
						}
					});
				} else {
					if(dl.data("incorrect_emails") > 0) {
						dm.find("center").remove();
						dm.html('<span style="color:red; font-size:1.4em"><center><b>One or more of the email(s) you entered appears to be incorrect. These are highlighted in RED. Please review these email address(es) and either fix them, or if you confirm they are correct, please submit again.</b></center></span>'+dm.html());
						dl.find('img').hide();
						var i = dl.data("check_rounds")+1;
						dl.data("check_rounds",i);
					} else {
						dl.dialog("close");
						$(".incorrect_email").each(function() { $(this).val(""); });
						dl.data("create_links",1);
						tyf_lf_send();
						tyf_lf.animate({left: document.body.clientWidth}, 1000).fadeOut('fast');
					}
				}
				//-------
			}
			//-------
			tyf_lf_x.mouseover(function() {
				$(this).css('border-color','red');
			});
			tyf_lf_x.mouseout(function() {
				$(this).css('border-color','white');
			});
			tyf_lf_x.click(function() {
				var date = new Date();
				date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
				$.cookie(COOKIE_GLOI, 'is', { path: '/', expires: date });
				
				tyf_lf.animate({left: document.body.clientWidth}, 1000).fadeOut('fast');
			});
			//-------
			tyf_lf_ok.mouseover(function() {
				$(this).css('border-color','red');
			});
			tyf_lf_ok.mouseout(function() {
				$(this).css('border-color','black');
			});
			tyf_lf_ok.click(function() {
				if(tyf_lf_check() == true) {
					dl.dialog("open");
					dm.html("<center><b>Please wait while we check the validity of the email addresses you entered.  This may take 30-45 seconds</b></center>");
					dl.find('img').show();
					dl.data("incorrect_emails",0);
					real_email_checking(0);
					//~ tyf_lf_send();
					//~ tyf_lf.animate({left: document.body.clientWidth}, 1000).fadeOut('fast');
				} else {
					alert(alert_message);
				}
			});
			//-------
		}
	});
	//-------
}
//------------------------------
