$(function() {
	
	$("#scraps-form div.radio label:eq(0)").addClass('green2');
	$("#scraps-form div.radio label:eq(1)").addClass('blue2');
	$("#scraps-form div.radio label:eq(2)").addClass('red');
	
	var zIndexNumber = 100;
	$('#mask div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 5;
	});
		
		//cache nav
		var nav = $("#main-nav ul");
		
		//add indicators and hovers to submenu parents
		nav.find("li").each(function() {
			if ($(this).find("div.sub-nav").length > 0) {

			//	$("<span>").text("^").appendTo($(this).children(":first"));

				//show subnav on hover
				$(this).mouseenter(function() {
					$(this).find("div.sub-nav").stop(true, true).show();
				});

				//hide submenus on exit
				$(this).mouseleave(function() {
					$(this).find("div.sub-nav").stop(true, true).hide();
				});
			}
		});	
	
	$('input, textarea').focus(function() {		
	    $(this).addClass('focus');
		if($(this).attr('title')=='Telefone') {
			$(this).mask("(99) 9999-9999", {placeholder:" "});
		}					
	});
	
	$('input, textarea').blur(function() {
	    $(this).removeClass('focus');
		if($(this).attr('title')=='Telefone') {
			$(this).unmask("(99) 9999-9999", {placeholder:" "});
		}
	});
	
	$('#opencomm-btn, #openform-btn').click(function(e){
		var divid = $(this).attr('href');		
		$(divid).slideToggle('normal');			
		e.preventDefault();
	});	
	
	$('.anuncio h4 a').click(function(e){
		var icon = $(this).find('.anuncio-icon');
		var linkid = $(this).attr('id');
		var divid = $(this).attr('href');
		
		$(divid).slideToggle('slow');		
		$(icon).toggleClass('icon-plus');		
		e.preventDefault();
	});
	
	$('input, textarea').labelify();
	
	// "o que fazer"
	$("ul.tabs").tabs("div.panes > div", {initialIndex: diaHoje()});
	
	$(".scrollable, #user-gallery .scrollable").scrollable();
	
	$('#news-highlights').css('visibility', 'visible');
	
	$('form.jqtransform div.select').jqTransform();
	
	// teste galeria
	//$(".scrollable").scrollable();

	$("#scrollable-nav .items a").click(function() {
	
		// see if same thumb is being clicked
		if ($(this).hasClass("active")) { return false; }	
		var url = $(this).attr("href");		
		var title = $(this).attr("title");		
		var titlewrap = $("#title_wrap p");
	
		// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#image_wrap").fadeTo("medium", 0.5);		
		var img = new Image();
		
		titlewrap.parent().slideUp('fast');	
	
		// call this function after it's loaded
		img.onload = function() {
	
			// change the image
			wrap.find("img").attr("src", img.src);
			titlewrap.text(title);
			titlewrap.parent().slideDown();
			
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
	
		};

		// begin loading the image from www.flickr.com
		img.src = url;	
		
		// activate item
		$("#scrollable-nav .items a").removeClass("active");
		$(this).addClass("active");
		
		return false;
	
	// when page loads simulate a "click" on the first image
	}).filter(":first").click();
	
	// Slide com destaques
	$('#mask')
		.cycle({ 
		    fx:    'blindX', 
		    speed:  500,			
		    timeout: 8000,
			pause: 1, 
		    next:   '#news-next', 
		    prev:   '#news-prev',
			pager:	'#news-pager',
			nowrap:	0,
			pagerAnchorBuilder: function(idx, slide) {
				var image = $("#mask div.new-item:eq(" + idx + ")").find("img").attr("rel");
				idx++;
////				idx = idx < 10 ? '0' + (idx + 1) : idx + 1;				
				return '<li><a href="#"><span></span><img src="' + image + '" alt="destaque '+idx+'" /></a></li>';       			
			}
		});		
		
		// envolve img do slide com link, anulando o link "saiba mais"
		$('.new-item').each(function(){
		    var current = $(this);
		    var anchor = current.find('a').first().attr('href');			
			var wrapped = $(current.find('img')).wrap('<a href="' + anchor + '"></a>');			
			current.find('.new-call').hide();		   
		});	
});

function diaHoje(){
	var d=new Date();
	var weekday=new Array(7);
	weekday[0]=6;
	weekday[1]=0;
	weekday[2]=1;
	weekday[3]=2;
	weekday[4]=3;
	weekday[5]=4;
	weekday[6]=5;	
	return weekday[d.getDay()];
}

