var iImg = 0;



$(document).ready(function()
{		
	$.fn.image = function(src, f){
		return this.each(function(){
			var i = new Image();
			i.src = src;
			i.onload = f;
			this.appendChild(i);
		});
	}
	if ( !IS_MOBILE ){
		$("#ImagesY").bind("mouseenter",function(){
			$("#ScrollView").slideDown();
			setTimeout(function(){$("#ScrollView").slideUp();},3500);	
		}).bind("mouseleave",function(){
			$("#ScrollView").slideUp();
		});
	}
});
function clickit(thiis){
		$("#captchaDiv img[src='img/reload.png']").hide();
		$("#captchaDiv img[src='img/captcha-loader.gif']").show();
		//alert("try to load");
		$("#captchaDiv div").empty();
		$("#captchaDiv div").image("view/captcha/"+HASH+"/"+iImg+"/",function(){
			$("#captchaDiv img[src='img/reload.png']").show();
			$("#captchaDiv img[src='img/captcha-loader.gif']").hide();
		});
		iImg++;   
	}		
var myForm = new Object();
/*
 * SHOW EMAILING FORM
 */
function InitForm(thiss)
{
			var newX = document.getElementById("ImagesY").scrollLeft;
			$("#TestS center form").remove();
			$("#myCloneForm form").clone().appendTo("#TestS center").attr("id", "MailForm");	
			$("#ImagesY").attr({style: "overflow:hidden"});
			$("#SendMailForm").attr({style: "left:"+newX+"px"});
			$("#TestS").attr({style: "left:"+newX+"px"});
			$("#SendMailForm").show();
			$("#TestS").find("#divCaptcha").attr("id", "captchaDiv");
			$("#TestS").fadeIn("slow");
			clickit();
			return false;			
}
/*
 * CLEAR INPUT FIELDS
 */
function ClearInputFields(thiis)
{
	if ($(thiis).val() == 'Your Email' || $(thiis).val() == 'Enter Code' || $(thiis).val() == 'Custom Title for PDF'  || $(thiis).val() == "Recepient's Email" || $(thiis).val() == 'Cc' || $(thiis).val() == 'Bcc' || $(thiis).val() == 'Subject' || $(thiis).val() == 'Message')
	{
		$(thiis).val("");
	}
}
		
/*
 * CLOSE EMAILING FORM
 */	
function CloseMailForm()
{
	$("#SendMailForm").hide();
	$("#TestS").hide();
	$("#ImagesY").attr({style: IS_MOBILE ? "overflow-x:hidden;" : 'overflow-x:scroll'});				
}	

	/*
 * SEND EMAIL
 */		
function Emailing()
{			
	//CHECK INPUT FIELDS
	var ml = $("#MailForm input[name=youremail]").val();
	if(ml == 'Your Email' || ml.length < 3)
	{
		alert("Error: Field Your Email is required! Please fill it in.");
		return;
	}
	
	$("#MailForm input[type=text]").each(function(){
		if ($(this).val() == 'Your Email' || $(this).val() == 'Enter Code' || $(this).val() == "Recepient's Email" || $(this).val() == 'Cc' || $(this).val() == 'Bcc' || $(this).val() == 'Subject')
		{
			$(this).val("");
		}				
	});	

	//CHECK TEXTAREA FIELD
	if ($("#MailForm textarea").val() == 'Message')
	{
		$("#MailForm textarea").val("");
	}	
	
	var postObject = new Object();
	$("#MailForm input[type=text]").each(function(i){
		postObject[$(this).attr("name")] = $(this).val();
	});	
	postObject[$("#MailForm input[type=hidden]").attr("name")]=$("#MailForm input[type=hidden]").val();
	postObject[$("#MailForm textarea").attr("name")]=$("#MailForm textarea").val();

	$("#TestS center form table").find("tr").each(function(i)
	{
		if(i < 2)
		{
			$(this).find("td[colspan=2]").attr("align", "center");
			$(this).find("input").remove();		
			$(this).find("div").toggle();								
		}else
		{
			$(this).remove();
		}				
	});	

	$.post("__ajax.php?handler=sendmail", postObject,
	function(data)
	{
		$("#SendMailLoader").empty();	
		$("#SendMailLoader").text(data);	
		$("#MailForm #closeMailForm").show();				
	}
	);

	return false;		
};
