	/*
	* Author: Alex Baskov, 2009
	* NOTE: make sure you include this script *after* jquery.js
	*
	* Feel free to include your custom logic if you want it to be on *every* page.
	*/
	var currentStatement = 1;

	$(document).ready(function () {
		
		// no need for LIVE function [.live("click", function () { ...]
		$("#popupNewsGoToNews").click(function () {
			window.location = $(this).attr("href");
		});
		
		$("#linkSubscribe").click(function (e) {
			e.stopPropagation();
			hideDropDowns();
			$(this).toggleClass("active");
			$("#popupSubscribe").toggle();
			if (!$(this).is("active")) {
				$(".subscribe_label").hide();
				$("#subscribe_info").show();
				$(".subscribe_field").show();
				$("#subscribe_errors .error").remove();
				$("#button_subscribe_close").hide();
				$("#button_subscribe_submit").show();
				$(".subscribe_field input").each(function() {
					$(this).val($(this).attr("title"));
					$(this).removeClass('valid');
				});
			}
		});
		
		$("#button_subscribe_close").click(function () {
			$("#linkSubscribe").click();
		});
		
		$("#subscribe_name")
			.focus(function () { this.value = (this.value == "Your Name") ? "" : this.value; })
			.blur(function () { this.value = (this.value == "") ? "Your Name" : this.value; });
		
		$("#subscribe_email")
			.focus(function () { this.value = (this.value == "Your Email") ? "" : this.value; })
			.blur(function () { this.value = (this.value == "") ? "Your Email" : this.value; });
			
		$.validator.addMethod( 
			"customRequired", 
			function(value, element) { 
				if (element.value == element.title) 
					{ 
						return false; 
					} 
				else return true; 
			}, 
			"Please input something." 
		);
		$.validator.addMethod(
			'customEmail', function(value, element) {
				return this.optional(element) || /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(element.value);
			},"Enter a valid email"
		);
		sub_validator = $("#form_subscribe").validate({
			rules: {
				subscribe_name: {
					required: true,
					customRequired: true,
					minlength: 2,
					maxlength: 200
				},
				subscribe_email: {
					required: true,
					customRequired: true,
					customEmail: true,
					email: true,
					minlength: 5,
					maxlength: 200
				}
			},

			messages: {
				subscribe_name: {
					customRequired: "Missing name. ",
					required: "Missing name. ",
					minlength: jQuery.format("Enter at least {0} characters. "),
					maxlength: jQuery.format("Enter less than {0} characters. ")
				},
				subscribe_email: {
					customEmail: "Enter a valid email. ",
					email: "Enter a valid email. ",
					customRequired: "Missing email. ",
					required: "Missing email. ",
					maxlength: jQuery.format("Enter less than {0} characters. ")
				}
			},
			errorPlacement: function(error, element) {
				$("#subscribe_errors").append(error);
			},
			success: function(label) {
				// set &nbsp; as text for IE
				label.html("&nbsp;").addClass("checked");
				label.remove();
			},
			highlight: function(element, errorClass) {
				$("#subscribe_info").hide();
				$("#subscribe_errors").show();
				$("#subscribe_errors label[for='" + $(element).attr("id") + "']").show();
				$("#form_subscribe .error").show();
				if ($("#subscribe_errors label").size() == 0) 
					$("#subscribe_errors").empty();
			},
			invalidHandler: function() {
				$("#subscribe_errors").show();
				$("#form_subscribe .error").show();
			},
			submitHandler: function() {
				//alert("submitted!");
			}
		});
		
		$(".subscribe_field input").keypress(function(){
			sub_validator.element("#" + $(this).attr('id'));
			sub_validator.focusInvalid();
		});

		runStatementChanging(8000);
	});
	
	function runStatementChanging(interval)
    {
        window.setInterval("changeStatement()",interval);
    }

	//Replace the statement picture by chaging div css property 'background-image'
	function changeStatement()
	{
		$("#his_text").fadeOut(500, function(){
				$(this).css("background-image","url(./img/home/"+currentStatement+".png)");
			}).fadeIn(500);
		currentStatement++;
		if (currentStatement > 9)
		{
			currentStatement = 1;
		}
	}
	
	
	////
	// handle subscribe request
	//
		
	var name_title = "Name";
	var email_title = "Email";
	
	function subscribeRequest(frm)
	{
		if (!(sub_validator.form()))
			return false;
		var requestData = "subscribeURL=" + "http://at2.createsend.com/t/r/s/tdtitr/"
				+ "&cm-name=" + realValueOf("#subscribe_name")
				+ "&cm-tdtitr-tdtitr="+realValueOf("#subscribe_email");
		//var requestData = "name=" + realValueOf("#subscribe_name") + "&email=" + realValueOf("#subscribe_email");
		
		$("#subscribe_info").hide();
		$("#subscribe_errors").hide();
		$("#subscribe_process").show();
		$(".subscribe_field").hide();
		$("#button_subscribe_submit").hide();
		
		$.ajax({
			type: "POST",
			url: "/external/subscription",
			data: requestData,
			success: function(msg) {
				if (msg.indexOf("success") != -1) {
					$("#subscribe_process").hide();
					$("#subscribe_success").show();
					
					$("#button_subscribe_close").show();
				}
				else if (msg.indexOf("error:") != -1) {
					$("#subscribe_process").hide();
					
					var error = msg.substring(msg.indexOf("error:") + "error:".length);
					$("#subscribe_errors").text("Errors appeared during subscription." + error).show();
					$("#button_subscribe_close").show();					
				}
			}
		});
		
		// preventing html form submission
		return false;
	} // /subscribeRequest()

	
	


