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();
		});
	}

    $('[rel=sealSign]').click(function(){
        var newX = document.getElementById("ImagesY").scrollLeft;
        $("#TestS center form").remove();
        $("#TestS div").remove();
        $("#sealInfo>div").clone().appendTo("#TestS center").attr("id", "sealInfoBlock");
        $('#sealInfoBlock').show();
        $("#ImagesY").attr({style: "overflow:hidden"});
        $("#SendMailForm").attr({style: "left:"+newX+"px"});
        $("#TestS").attr({style: "left:"+newX+"px"});
        $("#SendMailForm").show();
        $("#TestS").show();
    });
});


function clickit(thiis){
		$("#captchaDiv img[src='img/reload.png']").hide();
		$("#captchaDiv img[src='img/captcha-loader.gif']").show();
		$("#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();
		});


		$("#captchaDiv2 img[src='img/reload.png']").hide();
		$("#captchaDiv2 img[src='img/captcha-loader.gif']").show();
		$("#captchaDiv2 div").empty();
		$("#captchaDiv2 div").image("view/captcha/"+HASH+"/"+iImg+"/",function(){
			$("#captchaDiv2 img[src='img/reload.png']").show();
			$("#captchaDiv2 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();
			$("#TestS div").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() == 'Custom Title for PDF' || $(thiis).val() == 'Enter Code'  || $(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'});			
}

function CloseSealBlock()
{
    $("#SendMailForm").hide();
	$("#TestS").hide();
	$("#ImagesY").attr({style: IS_MOBILE ? "overflow-x:hidden;" : 'overflow-x:scroll'});
}

/*
 * SEND EMAIL
 */		
function Emailing()
{			
	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 false;
	}
	
	$("#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;		
};
function ShowSimilar()
{
	if ($("#SimilarLocations").text()!=''){
		$("#ImagesY").scrollTo($("#TSLocations"), 700 );
	}else{
		alert("Currently no results");
	}
}	


function SendPdf()
{
	var email = $("input[name='tomail']").val();
	var ccc = $("input[name='ccc']").val();
	var bccc = $("input[name='bccc']").val();
	var code = $("#code_check2").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=emailpdf&id="+catIdIs+"&to="+email+"&cc="+ccc+"&bcc="+bccc+"&code_check="+code, $('#MyEmailPdfForm').serialize(), function(data){
        $("#SendMailLoader").empty();
        $("#SendMailLoader").text(data);
        $("#MailForm #closeMailForm").show();}
    );
}

function EmailPdfForm()
{
	var newX = document.getElementById("ImagesY").scrollLeft;
	$("#TestS center form").remove();
	$("#TestS div").remove();
	$("#myEmailPdf 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"});
	$("#TestS").find("#divCaptcha2").attr("id", "captchaDiv2");
	$("#SendMailForm").show();
	$("#TestS").show();
	clickit();
	return false;
}
