jQuery(document).ready( function() {
	$(document).pngFix();
	newsletter();
	flash();
	if (jQuery("#about").length) aboutAccordion();
	if (jQuery("#associate").length) associate();
	if (jQuery("#useful").length) useful();
	if (jQuery("#login").length) login();
	if (jQuery("#gallery-details").length) galleryDetails();
	if (jQuery("#top-of-midia").length) topOfMidia();
	if (jQuery("#events").length) events();
	showPoll();
	
		
	$('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
});

function newsletter() {
	jQuery("#box-newsletter label").each( function() {
		var txt = jQuery(this).text();
		jQuery(this).hide().next('input').attr('value',txt).bind('mouseout', function() {
			var val = jQuery(this).attr('value');
			if (val=='') jQuery(this).attr('value',txt);
		}).bind('click', function() {
			var val = jQuery(this).attr('value');
			if (val==txt) jQuery(this).attr('value','');
		});
	});
}

function flash() {
	/* logo w3 */
	var flashvars = {color:"666666", animation:"false"};
	var params = {wmode:"transparent",menu:"false"};
	swfobject.embedSWF("http://www.w3haus.com.br/swf/w3haus.swf", "go_to_w3haus", "50", "14", "8.0.0","../swf/expressInstall.swf", flashvars, params);
	swfobject.createCSS("#go_to_w3haus","outline:none");
}

function aboutAccordion() {
	var el = jQuery("#about .accordion");
	el.find('h3').wrapInner('<a href=""></a>');
	var options = { header: 'h3', autoHeight:false }
	el.accordion(options);
}


function associate() {
	
	var btAgree = jQuery("#associate input#associate_agree");
	var btClean = jQuery("#associate input.clean");
	var btSubmit = jQuery("#associate input[type='submit']");
	var btCancel = jQuery("#associate input[type='button'].cancel");
	var form = jQuery("#associate form.new");
	var els = form.find("fieldset");
	$(btCancel).click(function(){
		window.location = rootUrl;
	});
	if (btAgree.length){
		jQuery("#associate form.new input[type='text'], #associate form.new select, #associate form.new input[type='submit'], #associate form.new input[type='button'], #associate form.new input[type='password']").attr("disabled", "disabled");
		els.addClass('opacity50');
		btAgree.click(function(){
		if($(this).attr("checked")){
			jQuery("#associate form.new input[type='text'], #associate form.new select, #associate form.new input[type='submit'], #associate form.new input[type='button'], #associate form.new input[type='password']").attr("disabled", "");
			els.removeClass("opacity50");
		}else{
			jQuery("#associate form.new input[type='text'], #associate form.new select, #associate form.new input[type='submit'], #associate form.new input[type='button'], #associate form.new input[type='password']").attr("disabled", "disabled");
			els.addClass("opacity50");
		}
		jQuery(form).submit(function(){
		if(!btAgree.attr("checked")){
			return false
		}
	});
	});
	}
	//btSubmit.attr('disabled','disabled');
	
	btClean.bind('click', function() {
		resetForm(jQuery(form));
	});
}

function useful(){
	$("ul.local-menu li:first-child").addClass("first");
}

function galleryDetails(){
	$("div.photos ul.clearfix li:nth-child(4n+4)").addClass("last");
	var thumbs = $("div.photos ul.clearfix li a");
	//mostra a primeira imagem e marca o thumb
	$("#photo-detail")[0].src =	$("div.photos ul.clearfix li:first a").attr("href");
	$("div.photos ul.clearfix li:first").addClass("selected");
	
	$(thumbs).click(function(){
		$("div.photos ul.clearfix li.selected").removeClass("selected");
		$(this).parent().addClass("selected");
		$("#photo-detail")[0].src = this.href;
		return false;
	});
	$("a.next").click(function(){
		if($("div.photos ul.clearfix li.selected").next().length){
			$("#photo-detail")[0].src = $("div.photos ul.clearfix li.selected").removeClass("selected").next().addClass("selected").find("a").attr("href");
		}
	});
	$("a.prev").click(function(){
		if($("div.photos ul.clearfix li.selected").prev().length){
			$("#photo-detail")[0].src = $("div.photos ul.clearfix li.selected").removeClass("selected").prev().addClass("selected").find("a").attr("href");
		}
	});
}

function resetForm(form){
	$(':input', form).each(function() {
	var type = this.type;
	var tag = this.tagName.toLowerCase(); // normalize case
	if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = "";
    else if (tag == 'select')
		this.selectedIndex = 0;
    });
}

function topOfMidia(){
	$(".winners .content").hide();
	$(".years li:first-child").addClass("selected");
	$(".winners .content:first").show();
	
	$(".years li a").click(function(){
		$(".winners .content").hide();
		$(".years li").removeClass("selected");
		$(this).parent().addClass("selected");
		$("div."+$(this).text()).show();
	});
}

function events(){
	if ($("div.calendar").length){
		$("div.calendar").hide();
		calendarLoad();
	}
}

function calendarLoad(month, year){
	$("div.calendar").fadeOut("slow");
	if(month && year){
		$("div.calendar").load(rootUrl+"event/calendar/month/"+month+"/year/"+year, function(){
			mountLinks();
		});
	}else{
		$("div.calendar").load(rootUrl+"event/calendar", function(){
			mountLinks();
		});
	}
}

function mountLinks(){
	$("div.calendar, p.all").fadeIn("slow");
	$("a.prev_year").click(function(){
		var prevYear = year-1;
		calendarLoad(month, prevYear);
		$(this).unbind("click");
		return false;
	});
	$("a.prev_month").click(function(){
		if (month == 1) {
			var prevMonth = 12;
			year = year-1;
		}else{
			var prevMonth = month-1;
		}
		calendarLoad(prevMonth, year);
		$(this).unbind("click");
		return false;
	});
	$("a.next_year").click(function(){
		var nextYear = year+1;
		calendarLoad(month, nextYear);
		$(this).unbind("click");
		return false;
	});
	$("a.next_month").click(function(){
		if (month == 12) {
			var nextMonth = 1;
			year = year+1;
		}else{
			var nextMonth = month+1;
		}
		calendarLoad(nextMonth, year);
		$(this).unbind("click");
		return false;
	});
}

function postPoll(){
	jQuery("#box-poll").bind('submit', function() {
		$.ajax({
		   type: "POST",
		   url: rootUrl + "pollVote/vote/",
		   data: "vote="+jQuery("input[name='vote']:checked").val(),
		   success: function(msg){
		   	jQuery("#box-poll").html(msg);
		   }
		 });
		 return false;
	});
}

function showResultPoll(){
	jQuery("#showResult").click(function() {
		$.ajax({
		   type: "POST",
		   url: rootUrl + "pollVote/showResult/",
		   data: "answerId="+jQuery("input[name='vote']").val(),
		   success: function(msg){
		   	jQuery("#box-poll").html(msg);
			$("#box-poll a#back").click(function(){
				$(this).unbind("click");
				showPoll();
			});
		   }
		 });
		 $(this).unbind("click");
		 return false;
	});
}
function showPoll(){
	$("#box-poll").load(rootUrl+"index/poll", function(){
		postPoll();
		showResultPoll();
	});
}
function login(){
	if($("#forgot-password").length){
		$("#forgot-password").hide();
		$("#forgotPassword").click(function(){
			$("#forgot-password").show();
		})
	}
}