
$(document).ready(function(){
	$("#search li:last").css({marginRight: '0px'});
	$(".footercontent_copyright_nav a:last").css({borderRight: '0px'});
	$('#slideshowplugin').coinslider({ width: 960, height:230, navigation: false, delay: 5000 });
	$(".lastarticles_box:last:last").css({borderBottom: '0px'});

	$('.agenda_box:last').css({ padding: 0, margin:0, border: 0});
	$('.trabalhosacademicos_box:last').css({ borderBottom: 0});
	
	$('#formsearch').submit(function() {
		location.href = 'http://www.google.com.br/search?q='+escape($('#formsearch_text').val())+'+site%3Amodaecompanhia.com.br';
		return false;
	});
	
	// ROTATOR DA HOME
	if ($('div.rotator').length > 0)
	{
	



	
		function theRotator() {
			//Set the opacity of all images to 0
			$('div.rotator ul li').css({opacity: 0.0});
			
			//Get the first image and display it (gets set to full opacity)
			$('div.rotator ul li:first').css({opacity: 1.0});
				
			//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds

			setInterval('rotate()',6000);
			
		}



		
			//Load the slideshow
			if ($('div.rotator ul li').length > 1)
			{
				theRotator();
				$('div.rotator').fadeIn(1000);
				$('div.rotator ul li').fadeIn(1000); // tweek for IE
			}
			
		}
	
	
	
	if ($('#morecomments_button').length > 0)
	{
		var pagi = 0;
		var next;
				$('#morecomments_button').hide();
				pagi+=8;
				$.ajax({
						   url: "/mural/ajax/"+pagi,
						   success: function(msg){
							next = msg; 

							if (next != '')
							 {
								$('#morecomments_button').show('fast');

							}
						   }
						 });
						

				$('#morecomments_button').click(function(){

						$( "#mural_list" ).append( next );
						
						$('.opniaonamoda_box').show('slow');
						pagi+=0;
						
						$.ajax({
						   url: "/mural/ajax/"+pagi,
						   success: function(msg){
							next = msg;
							 if (next == '')
							 {
								$('#morecomments_button').hide('slow');
								
							 }
						   }
						 });
						
						 
			return false;
		});
	}
	
	
	
	if ($('#internalnews_morecomments').length > 0)
	{
		var pag = 0;
		var next;
				$('#internalnews_morecomments').hide();
				pag+=10;
				$.ajax({
						   url: "/comentarios/listar/"+escape($('#modulo').val())+"/"+escape($('#mid').val())+"/"+pag,
						   success: function(msg){
							next = msg; 
							if (next != '')
							 {
								$('#internalnews_morecomments').show('fast');
							}
						   }
						 });
						

				$('#internalnews_morecomments').click(function(){

						$( "#comment_list" ).append( next );
						$('.internalnews_comments_box').show('slow');
						pag+=10;
						$.ajax({
						   url: "/comentarios/listar/"+escape($('#modulo').val())+"/"+escape($('#mid').val())+"/"+pag,
						   success: function(msg){
							next = msg;
							 if (next == '')
							 {
								$('#internalnews_morecomments').hide('slow');
							 }
						   }
						 });
						
						 
			return false;
		});
	}
	
	if ($('#write_comment').length > 0)
	{

		var nome = $( "#nome" ),
			email = $( "#email" ),
			mensagem = $( "#mensagem" ),
			save = $( "#save" ),
			mid = $( "#mid" ),
			site = $( "#site" ),
			modulo = $( "#modulo" ),
			allFields = $( [] ).add( nome ).add( email ).add( mensagem ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( n + " precisa ter entre " +
					min + " e " + max + " caracteres." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		$( "#internalnews_form" ).dialog({
			autoOpen: false,
			height: 350,
			width: 350,
			modal: true,
			buttons: {
				"Postar comentário": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( nome, "nome", 3, 64 );
					bValid = bValid && checkLength( email, "email", 6, 128 );
					bValid = bValid && checkLength( mensagem, "mensagem", 5, 512 );

					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Coloque um email válido." );
			
					if ( bValid ) {

						
						$.ajax({
						   type: "POST",
						   url: "/comentarios/enviar",
						   data: "nome="+escape($( "#nome" ).val())+"&email="+escape(email.val())+"&mensagem="+escape(mensagem.val())+"&save="+escape(save.val())+"&mid="+escape(mid.val())+"&modulo="+escape(modulo.val())+"&site="+escape(site.val()),
						   success: function(msg){
							 $( "#internalnews_msg" ).html = msg;
						   }
						 });
						 $( this ).dialog( "close" );
						$( "#internalnews_msg" ).dialog( "open" );
					}
				},
				Cancelar: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		$( "#internalnews_msg" ).dialog({
				autoOpen: false,
				modal: true,
				buttons: {
					Ok: function() {
						$( this ).dialog( "close" );
					}
				}
			});

		$( "#write_comment" )
			.click(function() {
				$( "#internalnews_form" ).dialog( "open" );
				return false;
			});
		
	}
	
	
if ($('#desuaopniao').length > 0)
	{

		var nome = $( "#nome" ),
			email = $( "#email" ),
			mensagem = $( "#mensagem" ),
			save = $( "#save" ),
			mid = $( "#mid" ),
			site = $( "#site" ),
			modulo = $( "#modulo" ),
			allFields = $( [] ).add( nome ).add( email ).add( mensagem ),
			tips = $( ".validateTips" );

		function updateTips( t ) {
			tips
				.text( t )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				tips.removeClass( "ui-state-highlight", 1500 );
			}, 500 );
		}

		function checkLength( o, n, min, max ) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass( "ui-state-error" );
				updateTips( n + " precisa ter entre " +
					min + " e " + max + " caracteres." );
				return false;
			} else {
				return true;
			}
		}

		function checkRegexp( o, regexp, n ) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass( "ui-state-error" );
				updateTips( n );
				return false;
			} else {
				return true;
			}
		}
		
		$( "#opiniao_form" ).dialog({
			autoOpen: false,
			height: 350,
			width: 350,
			modal: true,
			buttons: {
				"Postar opinião": function() {
					var bValid = true;
					allFields.removeClass( "ui-state-error" );

					bValid = bValid && checkLength( nome, "nome", 3, 64 );
					bValid = bValid && checkLength( email, "email", 6, 128 );
					bValid = bValid && checkLength( mensagem, "mensagem", 5, 512 );

					// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
					bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "Coloque um email válido." );
			
					if ( bValid ) {

						
						$.ajax({
						   type: "POST",
						   url: "/mural/enviar",
						   data: "nome="+escape($( "#nome" ).val())+"&email="+escape(email.val())+"&mensagem="+escape(mensagem.val())+"&save="+escape(save.val()),
						   success: function(msg) {
						   var id=Math.floor(Math.random()*31);
						   
						var mydate=new Date();
						var year=mydate.getYear();
						var hrs = mydate.getHours();
						var min = mydate.getMinutes();
						if (year<2000);
						year += (year < 1900) ? 1900 : 0;
						var day=mydate.getDay();
						var month=mydate.getMonth();
						var daym=mydate.getDate();
						if (daym<10)
						daym="0"+daym;
						if (min<10)
						min="0"+min;
						if (hrs<10)
						hrs="0"+hrs;
						
						var dayarray=new Array("Dom","Seg","Ter","Qua","Qui","Sex","Sáb");
						var montharray=new Array(" de Jan de "," de Fev de "," de Mar de ","de Abr de ","de Mai de ","de Jun de","de Jul de ","de Ago de ","de Set de "," de Out de "," de Nov de "," de Dez de ");
						var data = dayarray[day]+", "+daym+" "+montharray[month]+year+" ás "+hrs+":"+min;
						   
							var post = '<div class="opniaonamoda_box" id="op_'+id+'" style="display:none;"><div class="opniaonamoda_box_left"><div class="opniaonamoda_verticalalign">';
							post += '<span class="opniaonamoda_box_left_author">'+$( "#nome" ).val()+'</span><br />';
							post += '<span class="opniaonamoda_box_left_date">'+data+'</span>';
							post += '</div></div><div class="opniaonamoda_box_right">';
							post += $( "#mensagem" ).val();
							post += '</div></div>';
							 $( "#mural_list" ).prepend( post );
							 $( "#mural_null" ).hide( 'slow' );
							 $( "#op_"+id ).show( 'slow' );
							 $( "#opiniao_form" ).dialog( "close" );
						   }
						 });
						 

					}
				},
				Cancelar: function() {
					$( this ).dialog( "close" );
				}
			},
			close: function() {
				allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});


		$( "#desuaopniao" )
			.click(function() {
				$( "#opiniao_form" ).dialog( "open" );
			});
		
	}	
	
	
	if ($('.gallery_box').length > 0)
	{
		if ($('.b_small').length > 0)
		{
			$('.b_small').each(function(index) {
				if ((index + 1) % 6 == 0) $('.b_small').eq(index).css({ marginRight: 0});
			});
		}
		else
		{
			$('.gallery_box').each(function(index) {
				if ((index + 1) % 3 == 0) $('.gallery_box').eq(index).css({ marginRight: 0});
			});
		}
	}
	
	// se tiver select de estados entao ele puxa os estados pelo ajax
	if ($('#estado').length > 0)
	{
		buscaEstados();
		$('#estado').change(function() {

			var value = $(this).val();
			buscaCidades(value);
		});
	}
	
	if ($("#gender").length > 0)
	{
				
			$("#gender").change(function() {
				var value = $(this).val();
					if (value != "0") {
					location = value;
					}
			});
		
	}

	if ($("a[rel^='prettyPhoto']").length > 0)
	{
		$("a[rel^='prettyPhoto']").prettyPhoto({social_tools: '<button onclick="iopen()">Salvar imagem</button>'});

	}
	$('#sidebar_nav li:first').css('border-top','0');
	$('#sidebar_nav li:last').css('border-bottom','0');

	
	
	$("#sidebar_nav li a").mouseover(function(){$(this).stop().animate({ fontSize: 14 }, 'fast');});
	$("#sidebar_nav li a").mouseout(function(){$(this).stop().animate({ fontSize: 13 }, 'fast');});


	
	if ($('#mycarousel').length > 0) {
	// Iniciando o carousel ( gallery )
	 jQuery('#mycarousel').jcarousel({
    	wrap: 'circular'
    });
	}
	
	// GALERIA INLINE
	
		//To switch directions up/down and left/right just place a "-" in front of the top/left attribute
				//Vertical Sliding
				$('.boxgrid.slidedown').hover(function(){
					$(".cover", this).stop().animate({top:'-260px'},{queue:false,duration:300});
				}, function() {
					$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
				});
				//Horizontal Sliding
				$('.boxgrid.slideright').hover(function(){
					$(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300});
				}, function() {
					$(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300});
				});
				//Diagnal Sliding
				$('.boxgrid.thecombo').hover(function(){
					$(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300});
				}, function() {
					$(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300});
				});
				//Partial Sliding (Only show some of background)
				$('.boxgrid.peek').hover(function(){
					$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
				}, function() {
					$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});
				});
				//Full Caption Sliding (Hidden to Visible)
				$('.boxgrid.captionfull').hover(function(){
					$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
				}, function() {
					$(".cover", this).stop().animate({top:'260px'},{queue:false,duration:160});
				});
				//Caption Sliding (Partially Hidden to Visible)
				$('.boxgrid.caption').hover(function(){
					$(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
				}, function() {
					$(".cover", this).stop().animate({top:'220px'},{queue:false,duration:160});
				});
			});	

		function rotate() {	
			//Get the first image
			var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

			if ( current.length == 0 ) current = $('div.rotator ul li:first');

			//Get next image, when it reaches the end, rotate it back to the first image
			var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));
			
			//Un-comment the 3 lines below to get the images in random order
			
			//var sibs = current.siblings();
				//var rndNum = Math.floor(Math.random() * sibs.length );
				//var next = $( sibs[ rndNum ] );
					

			//Set the fade in effect for the next image, the show class has higher z-index
			next.css({opacity: 0.0})
			.addClass('show')
			.animate({opacity: 1.0}, 1000);

			//Hide the current image
			current.animate({opacity: 0.0}, 1000)
			.removeClass('show');
			
		}
function iopen(){
    open( $("#fullResImage").attr("src") );
}

