$(document).ready(function() 
{
	
	$("#facebook").hover(function(){ $(this).stop().animate({marginLeft:0}, 200); },function(){ $(this).stop().animate({marginLeft:-10}, 200);;});

	setInterval( "slideSwitch()", 4000 );
	
	facebox_prod();
	 
	$('#menu .normal').mouseover(function(){
			$(this).children().children(".left").css({backgroundPosition:"0px 0px"});
			$(this).children().children(".right").css({backgroundPosition:"0px 0px"});
			$(this).children().children(".center").css({backgroundPosition:"0px 0px"});
		})
		.mouseout(function(){
			
			$(this).children().children(".left").css({backgroundPosition:"-3px 0px"});
			$(this).children().children(".right").css({backgroundPosition:"-3px 0px"});
			$(this).children().children(".center").css({backgroundPosition:"-150px 0px"});
		});

	$('#menu .sel').children().children(".left").css({backgroundPosition:"0px 0px"});
	$('#menu .sel').children().children(".right").css({backgroundPosition:"0px 0px"});
	$('#menu .sel').children().children(".center").css({backgroundPosition:"0px 0px"});


	$('a[rel*=facebox]').click(function(event){
	
		event.preventDefault();
		
		var news_id = ($(this).attr("href")).split("-");
		
		$.get("http://"+$website_patch+"/processa.php?w=news_info&id="+news_id[1], function(data) {
		
			var news_info = data.split("<|>");
			$("#facebox_title_pt").html(news_info[0]);
			$("#facebox_intro_pt").html(news_info[2]);
			$("#facebox_image").attr("src", "http://"+$website_patch+"/site_files/"+news_info[4]);
			
			$.facebox({ div: '#facebox_news' });
		});
	});	
	
	
	$("#bt_send").click(function()
	{
		$.post("http://"+$website_patch+"/processa.php?w=contacta",
		{
			nome:$('#nome').val(),
			apelido:$('#apelido').val(),
			rua:$('#rua').val(),
			cidade:$('#cidade').val(),
			cpostal:$('#cpostal').val(),
			pais:$('#pais').val(),
			telefone:$('#telefone').val(),
			email:$('#email').val(),
			mensagem:$('#mensagem').val()
		},
		function(data)
		{
		
			var result = data.split("|");
		
			if(result[0] == "ok")
			{
				$("#msg_info").fadeOut(500, function()
				{ 
					$("#bt_send").fadeOut(500);
					$("#msg_info").text(result[1]).fadeIn(500);
				});
			}
			else
			{
				$("#msg_info").fadeOut(500, function()
				{ 
					$("#msg_info").text(result[1]).fadeIn(500);
				});
			}
		});
		return false;
	});
	
	$("#paging_button li").click(function(){
		
		var page_id = ($(this).attr("id")).split('-');
		
		if(!page_id[1]){page_id[1]=1;}
		
		items_page = page_id[1];
	
		$("#block-produtos").load("http://"+$website_patch+"/processa.php?w=list_produtos&p="+ page_id[1], 
		function(data)
		{	
		});
		
		$("#paging_button li").removeClass("page_sel");
		$(this).addClass("page_sel");
			
	});
});


function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);

		$(this).removeAttr("title").mouseover(function(){
				my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
		}).mousemove(function(kmouse){
				my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
		}).mouseout(function(){
				my_tooltip.fadeOut(400);
		});
	});
}


function facebox_prod()
{
	$('a[rel*=box_prod]').click(function(event){
	
		event.preventDefault();

		
		var prod_id = ($(this).attr("href")).split("-");
		
		$.facebox({ ajax: 'template/produto_info.php?id='+prod_id[1] });
	});	
	
}



function slideSwitch() 
{
    var $active = $('#slideshow IMG.active');
	

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
