/* Main Javascript file.
*  version 1.0
*/

/*Main variables*/

var serverLocation = location.host == 'localhost' ? location.protocol + '//' + location.host + '/smart/hitskidsteens/www/': location.protocol + '//' + location.host + '/';
var labels;
var user;

/*General use methods*/

// Read a page's URL segments and return them as an associative array.
function getUrlSegments()
{
	var vars = [];
	var hashes = window.location.href.slice(window.location.href.indexOf('http://') + 7).split('/');
	for(var i = 0; i < hashes.length; i++)
	{
		vars.push(hashes[i]);
	}
	return vars;
}

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

/*Document ready general functions*/
$(document).ready(function() 
{
	/*
	 * Init functions
	 **/
	//if(!labels) getButtonLabels();
	var url = getUrlSegments();
        
        $('#news-phone').mask("(99) 9999-9999");
	
	// Logo image click
	$('#logo').click(function()
	{
		$(window.location).attr('href', serverLocation);
	});
	
	/*$(".menu-item").click(function(){
		$(".menu-item").removeClass("selected");
		$(".submenu").hide();
		var id = "#" + $(this).attr("rel");
		$(id).show();
		$(this).addClass("selected");
		return false;
	});*/
	
	$(".menu-item").mouseout(function(){
		if(!$(this).hasClass("selected"))
		{
			var id = "#" + $(this).attr("rel");
			$(id).hide();
		}
	});
	
	$("#news-submit").click(function(){
		if(validate_newsletter())
		{
			$.ajax({
				url: serverLocation + "services/contact/newsletter/",
				type: "POST",
				async: false,
				data: { name: $("#news-name").val(), phone: $("#news-phone").val(), email: $("#news-email").val() },
				success: function(data) {
					if(data == 1)
					{
						alert("Informações enviadas com sucesso.");
						$("#news-name").val("nome");
						$("#news-email").val("e-mail");
						$("#news-phone").val("telefone");
					}else
					{
						alert("Ocorreu um erro no envio de seus dados.");
					}
				}
			});
		}
		return false;
	});
	
	var validate_newsletter = function (){
		if($("#news-name").val() == "" || $("#news-name").val() == "nome")
		{
			alert("Preencha seu nome.");
			$("#news-name").focus();
			return false;
		}
		if(!isEmail($("#news-email").val()))
		{
			alert("Preencha seu e-mail.");
			$("#news-email").focus();
			return false;
		}
		if($("#news-phone").val() == "" || $("#news-phone").val() == "telefone")
		{
			alert("Preencha seu telefone.");
			$("#news-phone").focus();
			return false;
		}
		return true;
	};
	
	var isPhone = function (phoneValue){
		exp = /\(\d{2}\)\\d{4}\-\d{4}/
		return exp.test(phoneValue);
	};
	
	var isEmail = function (value){
		var emailExpression = new RegExp(/^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i);
		return emailExpression.exec(value);
	};
	
});

jQuery(function()
{
        $('input[type="text"], textarea').each(function()	{
                $(this).attr('title', $(this).val()).focus(function() {
                        if ($(this).val() == $(this).attr('title')) {
                                $(this).val('');
                        }
                }).blur(function() {
                        if ($(this).val() == '' || $(this).val() == '') {
                                $(this).val($(this).attr('title'));
                        }
                });
        });

});
