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;
    });		
	

			
	$("#fancybox").fancybox({
		'titlePosition'		: 'none',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	$('#twitter_update_list li span a').click( function() {
		  window.open(this.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();
	});

}
// 	EVENTOS //
function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}
function IsNumeric(value)
{
  var er = /^[0-9]+$/;
  return (er.test(value)) ? true : false;
}

function events(){	
	
	$.get(rootUrl+"event/calendar", function(data){
		
		var loc = window.location.pathname;
		var date = new Date();
		
		loc = loc.substring(loc.lastIndexOf('/')+1,loc.length)
	
		if(IsNumeric(replaceAll(loc,"-",""))){
			var newdate = loc.split("-");
			date = new Date(newdate[2], newdate[1] - 1,newdate[0]);
		}
		
		var events = new Array();		
		if(data != ''){
			data = data.slice(0,  data.length-1);
			events = data.split(",");
		}
		
		calendar(events,date);
		
	});	
}
function calendar(events,date){
	$("#calendar").datepicker({
		defaultDate:date,
		changeMonth: true,
		changeYear: true,
		beforeShowDay: function(e){
			return[($.inArray($.datepicker.formatDate('yymmdd',e),events) >=0),""];
		},
		//onChangeMonthYear: function(year, month, inst) {	
		//},
		onSelect: function(data, inst) {
			data  = replaceAll(data,"/","-");
			document.location.href=rootUrl+"event/index/date/" + data;

		}		
	});
}
function save(year,month){

	var date = new Date(year,month,'1');	
	$.setCookie('calendar', date, {
		duration : 1, // In days
		path : '',
		domain : '',
		secure : false
	});
}

// FIM DE EVENTOS //
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();

		})

	}

}
