$(document).ready(function() {
	
	// arredonda bordas
	$('h1.titulo, h2.titulo').corner('7px');
	$('.box').corner('7px');
	
	//$('.ui-corner-all').corner('12px');
	
	$('#options ul').corner('7px');
	$('#options ul li:first a').corner('7px tl bl');
	$('#options ul li:last a').corner('7px tr br');
			
	$('#novidades #lista ul li').corner('7px');
	$('#novidades #novidades-mais-lidas').corner('7px');
	$('#novidades #receitas').corner('7px');
		
	$('#produtos #description article #options #receitas-que-combinam ul li').corner('7px');
		
	$('#receitas #lista ul li').corner('7px');	
	$('#receitas #description article #options #produtos ul li').corner('7px');
	
	// centraliza as imagens
	$('img.centralize').each(function() {		
		imgsCentralize($(this));
	});
	$('img.centralize').load(function() {
		imgsCentralize($(this));
	});
	$('#category a').click(function() {
		sSubCategory = $(this).attr('href');
		$('img.centralize', sSubCategory).each(function() {
			$(this).load(function() {
				imgsCentralize($(this));
			});
		});		
		
		//$(sSubCategory+' ul').jcarousel();
		
		return false;
	});
	
	/**
	 * EMPRESA
	 */
	// tabs
	$('#empresa #options').tabbed_interface({auto_switch:false, contents_tag: 'div'});
	
	// images
	$('a.thumb').click(function(e) {
		e.preventDefault();
		
		changeImage($(this).children('img').attr('src'), $(this).children('img').attr('alt'), 'small_', $('#img-grande img'));	
	});
	
	
	/**
	 * CONTATO
	 */
	// assunto select
	$('.fg-button').hover(
		function(){ $(this).removeClass('ui-state-default').addClass('ui-state-focus'); },
		function(){ $(this).removeClass('ui-state-focus').addClass('ui-state-default'); }
	);
		
	// send form
	$('#contato form').submit(function(e) {
		e.preventDefault();
		sendContact();		
		return false;
	});
	
	
	/**
	 * NOVIDADES
	 */
	// alterar tamanho da fonte
	$('a.font-size').click(function(e) {
		e.preventDefault();
		
		var nFontSize = parseInt($('#description article').css('font-size'));
		
		if($(this).attr('title') == 'A+') {
			//alert(nFontSize);
			if(nFontSize <= 13) {
				$('#description article').css('font-size', nFontSize + 1 + 'px');
			}			
		} else {
			if(nFontSize >= 10) {
				$('#description article').css('font-size', nFontSize - 1 + 'px');
			}			
		}
	});
	
	// images
	$('#novidades #thumbs a').click(function(e) {
		e.preventDefault();		
		//alert($(this).children('img').attr('src'));
		changeImage($(this).children('img').attr('src'), $(this).children('img').attr('alt'), 'small_', $('#novidades #gallery figure img'));
	});
	
	
	/**
	 * PRODUTOS
	 */
	// Abas
	$('#produtos #options').tabbed_interface({auto_switch:false, contents_tag: 'div', specific_list: '.tabs'});
	
	/*
	// Troca as subcategorias dos produtos
	$('#carousel-12 li.jcarousel-item').load(function() {
		$('.sub-category').css('display', 'none');
		$('#lista-produtos').tabbed_interface({auto_switch:false, contents_tag: 'section.sub-category', specific_list: '.category-list'});
	});
	*/
	
	/*
	// centraliza as imagens das sub-categorias
	$('#category a').click(function(e) {		
		$('.container').each(function() {
			img = $(this).children('img');
			imgsCentralize(img);
		});
	});
	*/
	
	// estilo da tabela de valores nutricionais
	$('#valnutri tr:nth-child(even), #valnutri tr:nth-child(even) td').css({'background': '#E85558', 'color' : '#fff'});
	$('#valnutri tr:nth-child(odd), #valnutri tr:nth-child(odd) td').css({'background': '#fff', 'color' : '#000'});	
	$('#valnutri tr:last').css('background', '#E85558');
	
	
	/**
	 * RECEITAS
	 */
	// Abas
	$('#receitas #options').tabbed_interface({auto_switch:false, contents_tag: 'div', specific_list: '.tabs'});
});

/**
 * change image
 */
function changeImage(sImg, sAlt, sPrefix, ImgTarget) {
	if(typeof(sPrefix) == undefined) {
		sPrefix = 'small_';
	}
	
	sImg = sImg.replace(sPrefix, '');	
	
	ImgTarget.attr('src', sImg).attr('alt', sAlt);	
}

/**
 * show field outro assunto
 */
function showOutroAssunto() {
	$('.elemento-outroassunto').slideDown(700);
}

/**
 * hide field outro assunto
 */
function hideOutroAssunto() {
	$('.elemento-outroassunto').slideUp(400);
}

/**
 * send contact
 */
function sendContact() {
	assunto = $('#menuSelection').html();
	
	//jsonForm = $('#contato form').serialize();
	//alert($('#contato form').serialize() + '&assunto=' + assunto);
	
	$.ajax({
		type: 'POST',
		url:  $('#contato form').attr('action'),
		data: $('#contato form').serialize() + '&assunto=' + assunto,
		dataType: 'json',
		beforeSend: appendLoader($('#alert')),
		complete: function(objData) {
			prependLoader();					
		},
		success: function(objData) {
			/*
			if(objData.type == 'error') {				
				if(typeof objData.errors == 'object') {
					errors = objData.errors;
					ul = $('<ul>');
					for(i in errors) {						
						if(typeof errors[i] != 'object') {
							li = $('<li>' + errors[i] + '</li>');
							ul.append(li);
						} else {
							for(j in errors[i]) {
								li = $('<li>' + errors[i][j] + '</li>');
								ul.append(li);
							}
						}
						
					}					
					$('#errors').html(ul);					
				}				
			}
			*/		
			
			setMessage(objData.message, objData.type);
		},
		error: function(objData) {					
			setMessage(objData.message, objData.type);
		}
	});
	
}

/**
 * insere um texto na barra de mensagem e torna ela visivel
 * 
 * @param string sMessage
 * @param string sType
 */
function setMessage(sMessage, sType) {
	$('#message .texto').html(sMessage);
	$('#message').removeClass().addClass(sType);
	$('#message').animate(
		{'opacity': 'show'},
		500,
		function() {
			setTimeout(function() {
				$('#message').fadeOut(500);
			}, 3500);
		}
	);	
	
	if(sType == 'success') {
		$('#contato form')[0].reset();
	}
}

/**
 * append loader
 * @param objElement elemento que vai abrigar o loader
 */
function appendLoader(objElement) {
	$(objElement).html('<span class="loader" style="display:block"><img src="' + site_url + '/imgs/loader.gif" alt="" /><span>Enviando...</span></span>');	
}

/**
 * prepend loader
 */
function prependLoader() {
	$('.loader').remove();
}

/**
 * centraliza uma imagem
 * @param img
 */
function imgsCentralize(img) {
	sImgHeight = img.outerHeight();
	sImgWidth = img.outerWidth();
	img.css({
		'margin-top': '-' + (sImgHeight / 2) + 'px',
		'margin-left': '-' + (sImgWidth / 2) + 'px'
	});
}

/**
 * 
 * @param sUrl
 * @return
 */
function print(sUrl) {
	var nWidth   = 600;
	var nHeight  = (screen.height / 2);
	var nLeft    = (screen.width / 2) - nWidth / 2;
	var nTop     = 50;
	var styleStr = 'toolbar=NO,location=NO,directories=NO,status=YES,menubar=NO,scrollbars=YES,resizable=NO,copyhistory=YES,width='+nWidth+',height='+nHeight+',left='+nLeft+',top='+nTop+',screenX='+nLeft+',screenY='+nTop;
	window.open(sUrl, "Fricasa", styleStr);
}
