$(document).ready(function(){
	$("<div />")
		.append($("<a />").addClass("ptf_prev").attr({rel: "prev", title: "Previous work"}).text("prev").mouseover(function(){ $(this).css("cursor","pointer"); }))
		.append($("<a />").addClass("ptf_next").attr({rel: "next", title: "Next work"}).text("next").mouseover(function(){ $(this).css("cursor","pointer"); }))
		.append("<span class=\"ptf_img\"></span>")		
		.addClass("ptf_destaque")
		.prependTo(".modelos_slider");
	
	$(".post").each(function(i){

		$("br",this).hide();

		var idpost   = $(this).attr("id");
		var destaque = $(".ptf_destaque .ptf_img",this);
								 
		var imgs = new Array;
		$('.modelos_slider img',this).each(function(i){
			var source = $(this).attr("src");
			imgs[i]    = source;
			$(this).hide();
			$(this).parent().css({height:0, margin:0, padding:0});
			
			alteraAltura($(this).height(),idpost);
						
		});
		destaque.css("backgroundImage","url("+imgs[0]+")").attr("rel","0");
		
		if (imgs.length == 1) {
			$('a.ptf_prev, a.ptf_next').hide();
		}

		$(".ptf_destaque a[rel]",this).click(function(){
			if ($(this).attr("rel") == "next") {
				if ((Number(destaque.attr("rel"))+1) < imgs.length) {
					alteraImg(Number(destaque.attr("rel"))+1,idpost);
				} else {
					if((Number(destaque.attr("rel"))+1) == imgs.length) {
						alteraImg(0,idpost);						
					}
				}
			} else {
				if (destaque.attr("rel") > 0) {
					alteraImg(Number(destaque.attr("rel"))-1,idpost);
				} else {
					if(Number(destaque.attr("rel")) == 0) {
						alteraImg(Number(imgs.length)-1,idpost);
					}
				};
			};
		});
	});
	
	function alteraImg(img,post){
		var novaImg = $("#"+post+" img:eq("+img+")").attr("src");
		$("#"+post+" .ptf_img").css("backgroundImage","url("+novaImg+")").attr("rel",img);
//		alert(novaImg);
		alteraAltura($("#"+post+" img:eq("+img+")").height(), post);
	}
	
	function alteraAltura(altura,post){
		$("#"+post+" .ptf_destaque, #"+post+" .ptf_img").height(altura);
		$("#"+post+" .ptf_destaque a").css("top",	(altura-20)/2+"px")
	}	
	$('.post .fade').hover(function(){
		$(this).find('img').fadeTo(300,0.6);
		$(this).find('h3 a').animate({backgroundColor: '#66CAFF'},{duration:300});
		}, function(){
		$(this).find('img').fadeTo(300,1);
		$(this).find('h3 a').animate({backgroundColor: '#0675C6'},{duration:300});		
	});

});



























