var base_url = "http://www.choperiacasarao.com.br/";
//var base_url = 'http://localhost/casarao/';

function load_pagination(href, id)
{
	jQuery.ajax({
		type: 'GET',
		url: href,
		dataType: 'json',
		global: false,
		beforeSend: function()
		{
			jQuery('#'+id+' ol').html('<li style="border: none; text-align: center"><span style="display: block;">Carregando...</span></li>');
		},
		success: function(data){
			jQuery('#'+id+' ol').html(data.html);
			jQuery('#'+id+' .pagination-links').html(data.page_links);
		}
	});
	return false;
}

function checkRegexp(o,regexp) {
	if(!(regexp.test(o))) {
		return false;
	}
	else{
		return true;
	}
}

function get_uri(uri)
{
	if(uri)
	{
		return uri.split('/')[4];
	}
}

$(function(){

	jQuery('.notifications .close').live('click', function(){
		jQuery(this).parent('div').fadeOut();
	});

	jQuery('.pagination-links a').live('click', function(){
		a = jQuery(this);
		gethash = a.attr('href');
		hash = gethash.split('#')[1].split('/');
		uri = get_uri(gethash);			
		page = parseInt(hash[1]);
		if(!page)
			{
				page = 0;
			}
		load_pagination(base_url+uri+"/pages/"+page, uri);
	});
	
	getinithash = document.location.hash;
	if(getinithash.length > 0)
	{
		inithash = getinithash.split('#')[1].split('/');
		uri = get_uri(document.location.href);
		if(inithash[0] == 'page')
		{
			initpage = parseInt(inithash[1]);
			if(!initpage)
			{
				initpage = 0;
			}
			load_pagination(base_url+uri+"/pages/"+initpage, uri);
		}
	}
	
	
	jQuery('#newsletter').submit(function(){
		var url = $(this).attr('action');
		var str = $("input[name=name]").val();
		var str2 = $("input[name=email]").val();
		var bValid = true;
		
		if(str.length <= 0){
			alert('Preencha seu nome!');
			return false;
		}
		
		if(str2.length <= 0){
			alert('Informe seu email!');
			return false;
		}
		
		bValid = bValid && checkRegexp(str2,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
		
		if(!bValid)
		{
			alert('E-mail Inválido!');
			return false;
		}
		
		$.ajax({
			type: 'POST',
			url: url,
			dataType: 'html',
			data: {name : str, email : str2},
			global: false,
			beforeSend: function()
			{
				//$('#ajax-loader').show();
			},
			error: function()
			{
				alert('Ops! Ocorreu um erro.');
			},
			success: function(data)
			{
				alert(data);	
			}
		});	
		return false;
	});
 	
	
	if($('.fancy').length > 0)
	{
		function formatTitle(title, currentArray, currentIndex, currentOpts) {
		    return '<div id="fancy-title" style="text-align: left;">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '<span class="info">Imagem ' + (currentIndex + 1) + ' de ' + currentArray.length + '</span><span style="display: block; text-align: right;"><a href="javascript:;" onclick="$.fancybox.close();"><img src="'+base_url+'public/images/closelabel.gif" /></a></span></div>';
		}

		jQuery(".fancy").fancybox({
			'showCloseButton'	: false,
			'titlePosition' 		: 'inside',
			'titleFormat'		: formatTitle
		});
		
	}
	
	if($('#contactForm').length > 0)
	{
		$.metadata.setType('attr', 'validate');	
		jQuery('#contactForm').validate();	
		jQuery('#tel, #cel').mask('(99) 9999-9999',{placeholder:" "});
	}

	//Ativa aba +VIDEOS e adiciona a classe	.brown-tab
	jQuery("ul.tabs li a").live('click',function() {
		$("ul.tabs li").removeClass("active");
		$(this).parents('li').addClass("active");
		rel = $(this).attr('rel');
		if(rel == 'videos')
		{
			$('#imagens').hide();
			$('#videos').show();
		   	return false;	
		}
		else if(rel == 'imagens')
		{
			$('#videos').hide();
			$('#imagens').show();
			return false;
		}
	});


	
/*
	jQuery('#alert-box, #welcome').dialog({
		autoOpen: false,
		modal: true,
		buttons: {
					Ok : function() {
					$(this).dialog('close');
				 	}}
	});*/

 	
});
