$( document ).ready( function() {
	
	// ADS REPOSITIONING
	if( $( '.banners .right-bar' ).hasClass( 'right-bar' ) )
	{
		function position_right_banners()
		{
			var $pub_width = $( '.banners .right-bar img' ).width();
			if( $pub_width == undefined )
				$pub_width = $( '.banners .right-bar object' ).attr('width');
			var ww   = $( window ).width();
			var diff = ( ww-$('.container_16').width() ) / 2;
			if( diff > 0 )
			{
			  $( '.right-bar' ).css( { 'padding-right': (diff+2)+'px', 'right' : '0' } );
			} else
			{
			  $( '.right-bar' ).css( { 'padding-right': '40px', 'right' : '-62px' } );
		  }

			$( '.right-bar' ).fadeTo( 1500 , '1.0' );

		}
		
		setTimeout( function(){ position_right_banners() } , 1500 );
		$( window ).resize( function(){
			position_right_banners();
		});
	}
	if( $( '.banners .left-bar' ).hasClass( 'left-bar' ) )
	{
		function position_left_banners()
		{
			var $pub_width = $( '.banners .left-bar img' ).width();
			if( $pub_width == undefined )
				$pub_width = $( '.banners .right-bar object' ).attr('width');
			var $left_offset = $( '.universalHeader' ).offset().left;
			var $needed_left_margin = $left_offset + 108 - $pub_width;
			if( $needed_left_margin < 0 )
				$( '.left-bar' ).css( 'marginLeft' , $needed_left_margin ).fadeTo( 1500 , '1.0' );
			else
				$( '.left-bar' ).css( 'paddingLeft' , $needed_left_margin ).fadeTo( 1500 , '1.0' );
		}
		setTimeout( function(){ position_left_banners() } , 1500 );
		$( window ).resize( function(){
			position_left_banners();
		});
	}

	// IMAGES
	if($('div.image').hasClass('image')){
		$('div.image').each(function(i,el){
			var $img = $(el).find("img");
			var $caption = $(el).find("p.caption");
			if($img.width() > 30 ) $caption.css('width',parseInt($img.width())+'px');
		});
	}

	// GALLERY
	if( $( '.image-gallery' ).hasClass( 'image-gallery' ) )
	{
		// calculando o tamanho do slide para adicionar ao thumb-slider
		var $widthTotal  = 0;
		var $gutterTotal = 0;
		var $diffMargin  = $( '.thumb-slider' ).find( 'div:first' ).css( 'margin-left' );
		$( '.thumb-slider' ).find( 'div:first' ).addClass( 'alpha curr' );
		$( '.thumb-slider' ).find( 'div:last' ).addClass( 'omega' );
		$( '.thumb-slider' ).find( 'div' ).each( function( i , el ) {
			$widthTotal += 108;
			$gutterTotal = i*12;
			$( el ).attr( 'index' , i );
		} );
		$( '.thumb-slider' ).width( $widthTotal + $gutterTotal + $diffMargin );
		$( '.active-image' ).attr( 'index' , '0' );
		
		// acao de troca de imagens
		$( '.image-gallery' ).find( '.thumb-scroll' ).find( '.grid_2' ).click( function() {
			showImage( $( this ) );
			return false;
		});
		
		// prev
		$( '#prev_btn' ).click( function() {
			var $indexActive = $( '.active-image' ).attr( 'index' );
			if( $indexActive > 0 ){
				$indexActive--;
				showImage( $( '.thumb-slider' ).find( 'div[index='+$indexActive+']' ) );
			}
			return false;
		} );
		
		// next
		$( '#next_btn' ).click( function() {
			var $indexActive = $( '.active-image' ).attr( 'index' );
			var $slideElTot = parseInt( $( '.thumb-slider' ).find( 'div' ).length )-1;
			if( $indexActive < $slideElTot ){
				$indexActive++;
				showImage( $( '.thumb-slider' ).find( 'div[index='+$indexActive+']' ) );
			}
			return false;
		} );
		
		// metodo realizar o slide
		function slideTo( $index )
		{
			$( '.thumb-slider' ).find( 'div.curr' ).removeClass( 'curr' );

			if( $( '#marginIni' ).attr( 'id' ) == undefined ) {
				var $marginLeft = parseInt( $( '.thumb-slider' ).css( 'margin-left' ) );
				$( 'body' ).append( '<input type="hidden" id="marginIni" value="'+$marginLeft+'" />');
			} else
				var $marginLeft = parseInt($('#marginIni').attr('value'));
				
			var $totMargin  = 120 * $index;
			var $currIndex  = $('.active-image').attr('index');
			
			$totMargin = $totMargin * (-1);

			$totMargin += $marginLeft;
			$( '.thumb-slider' ).stop().animate( { marginLeft: $totMargin + "px" } , 500 , function() {
				$( this ).find( 'div[index='+$index+']' ).addClass( 'curr' );
			} );
		}
		
		// metodo para mostrar a imagem desejada
		function showImage( $this )
		{
			//$( '.active-image' ).find( 'p' ).fadeOut( 500 );
			$( '.image-gallery' ).find( '.loading' ).fadeIn( 250 );
			//$( '.active-image' ).append( '<span>carregando...</span>' );
			var $index = $this.attr( 'index' );
			slideTo( $index );
			$( '.active-image' ).attr( 'index' , $index );
			var $src = $this.find( 'a' ).attr( 'href' );
			var $legend = $this.find( 'span' ).html();
			$( '.active-image' ).find( 'img' ).remove();
			$( '.active-image' ).find( 'p' ).fadeOut( 100 ).html( $legend );
			
			var image = new Image();
			$(image).load(function() {
				$( '.active-image' ).find( 'p' ).fadeIn( 100 );
				$( '.image-gallery' ).find( '.loading' ).fadeOut( 250 );
				$( '.active-image' ).prepend( $( this ) );
			}).error( function() {	
				$( '#next_btn' ).click();
			}).attr( 'src' , $src );
		}
	}
	
	
	// TEXT+IMAGES ADJUST FOR MINHA CIDADE
	/*
	if ( $( '.text' ).parents( '.minhacidade').hasClass( 'minhacidade' ) )
	{
		var $images_height = 0;
		var $num_images = $( '.text' ).find( 'img' ).length;
		//var $num_txts = $( '.text > p:has( span:not(.note_text) )' ).length;
		var $num_txts = $( '.text > p' ).length;
		var $new_img_parent_top = $( '<div style="overflow: hidden; float: left; width: 288px;"></div>');
		var $new_txt_parent_top = $( '<div style="overflow: hidden; float: right; width: 228px;"></div>');
		var $new_txt_parent_mid = $( '<div class="grid_9 alpha omega"></div>');
		var $new_img_parent_bot = $( '<div style="overflow: hidden; float: right; width: 288px;"></div>');
		var $new_txt_parent_bot = $( '<div style="overflow: hidden; float: left; width: 228px;"></div>');
		$( '.text' ).find( 'img' ).each( function( i , el ){
			var $img_parent = $( el ).parents( '.image' );
			if( ! $img_parent.hasClass( 'full_image' ) )
			{
				$images_height = $images_height + $( el ).height();
				if ( i <= ($num_images/2) )
					$new_img_parent_top.append( $img_parent.width( 288 ) );				
				else
					$new_img_parent_bot.append( $img_parent.width( 288 ) );
			}
		});
		$( '.text > p' ).each( function( i , el ){
		//$( '.text > p:has( span:not(.note_text) )' ).each( function( i , el ){
			if ( $( el ).html() == ('' || '&nbsp;') )
				$( el ).remove();
			else
			{
				if ( i <= ($num_txts/3) )
					$new_txt_parent_top.append( $( el ).width( 228 ) );
				else if ( i > ($num_txts/3) && i <= ($num_txts*2/3) )
					$new_txt_parent_mid.append( $( el ) );
				else
					$new_txt_parent_bot.append( $( el ).width( 228 ) );
			}
		});
		$( '.text' ).append( $new_img_parent_top ).append( $new_txt_parent_top ).append( $new_txt_parent_mid ).append( $new_img_parent_bot ).append( $new_txt_parent_bot ).append( '<br class="clear" />');
	}
	*/
	
	// ALL ARTICLES FUNCTIONALITIES
	$( '.abstracts_trigger' ).click( function(){
		$( '.abstracts' ).slideToggle( 500 );
	});
	$( '.citation_trigger' ).click( function(){
		$( '.how_to_cit' ).slideToggle( 500 );
	});
		
	// COVERS
	$( '.entry' ).hover( function(){
			$article_id = $( this ).attr( 'id' ).replace('article_', '');
			$image = $( '#image_article_'+$article_id );
			// for arquiteturismo second highlight
			$image_2 = $( '#_image_article_'+$article_id );
			$lead = $( '#lead_article_'+$article_id );
			if ( $lead.attr( 'id' ) != undefined )
				$lead.css( 'display' , 'block' );
			$lead_2 = $( '#lead_2_article_' + $article_id );
			if ( $lead_2.attr( 'id' ) != undefined )
				$lead_2.css( 'display' , 'block' );
			if ( $image.attr( 'id' ) != undefined )
			{
				$( '.selected-entry > img.original' ).css( 'display' , 'none' );
				$image.css( 'display' , 'block' );
			}	
			if ( $image_2.attr( 'id' ) != undefined )
				$image_2.css( 'display' , 'block' );
		}, function(){
			$article_id = $( this ).attr( 'id' ).replace('article_', '');
			$image = $( '#image_article_'+$article_id );
			// for arquiteturismo second highlight
			$image_2 = $( '#_image_article_'+$article_id );
			$lead = $( '#lead_article_'+$article_id );
			if ( $lead.attr( 'id' ) != undefined )
				$lead.css( 'display' , 'none' );
			$lead_2 = $( '#lead_2_article_' + $article_id );
			if ( $lead_2.attr( 'id' ) != undefined )
				$lead_2.css( 'display' , 'none' );
			if ( $image != undefined )
			{
				$( '.selected-entry > img.original' ).css( 'display' , 'block' );
				$image.css( 'display' , 'none' );
			}
			if ( $image_2.attr( 'id' ) != undefined )
				$image_2.css( 'display' , 'none' );
	});
	
	$( '.selected-entry' ).hover( function(){
			$original_id = $( this ).find( 'img.original' ).attr( 'id' ).replace('original_', '');
			$image = $( this ).find( '#image_article_' + $original_id );
			if( $image.attr('id') == undefined)
				$image = $( this ).find( '#_image_article_' + $original_id );
			$lead = $( this ).find( '#lead_article_' + $original_id );
			if ( $lead.attr( 'id' ) != undefined )
				$lead.css( 'display' , 'block' );
			$lead_2 = $( '#lead_2_article_' + $original_id );
			if ( $lead_2.attr( 'id' ) != undefined )
				$lead_2.css( 'display' , 'block' );
			if ( $image.attr( 'id' ) != undefined )
			{
				$( '.selected-entry > img.original' ).css( 'display' , 'none' );
				$image.css( 'display' , 'block' );
			}
		}, function(){
			$original_id = $( this ).find( 'img.original' ).attr( 'id' ).replace('original_', '');
			$image = $( this ).find( '#image_article_' + $original_id );
			if( $image.attr('id') == undefined )
				$image = $( this ).find( '#_image_article_' + $original_id );
			$lead = $( this ).find( '#lead_article_' + $original_id );
			if ( $lead.attr( 'id' ) != undefined )
				$lead.css( 'display' , 'none' );
			$lead_2 = $( '#lead_2_article_' + $original_id );
			if ( $lead_2.attr( 'id' ) != undefined )
				$lead_2.css( 'display' , 'none' );
			if ( $image.attr( 'id' ) != undefined )
			{
				$( '.selected-entry > img.original' ).css( 'display' , 'block' );
				$image.css( 'display' , 'none' );
			}
	});
	
	// arquiteturismo
	// edition image slide
	$( '.arquiteturismo .image-major' ).hover( function() {
		if( $( "#image-major_content" ).css("margin-left") == "-348px" )
		{
			$( '.arrow_left' ).fadeIn( 750 );
		}
		else
		{
			$( '.arrow_right' ).fadeIn( 750 );
		}
	}, function() {
		$( '.arrow_left' ).fadeOut( 250 );
		$( '.arrow_right' ).fadeOut( 250 );
	});
	
	$( '.arquiteturismo .image-major' ).click( function() {
		$( '.arrow_left' ).fadeOut( 250 );
		$( '.arrow_right' ).fadeOut( 250 );
		if( $( "#image-major_content" ).css("margin-left") == "-348px" )
		{
			$( '.arrow_right' ).fadeIn( 1500 );
			$( '.line-block' ).find( '.grid_6:first' ).removeClass( 'omega' );
				$( '.line-block' ).find( '.grid_3:first' ).removeClass( 'alpha' );
			$( "#image-major_content" ).animate( { marginLeft: 0 } , 750 );
		}
		else
		{
			$( '.arrow_left' ).fadeIn( 1500 );
			$( '.line-block' ).find( '.grid_6:first' ).addClass( 'omega' );
			$( "#image-major_content" ).animate( { marginLeft: '-348px' } , 750, function(){
				$( '.line-block' ).find( '.grid_3:first' ).addClass( 'alpha' );
			});
		}
	});
	
	// DROPS COVERS
	// 
	var upper_max_h = 0;
	$( '.upper-titles' ).find( '.drops-entry-title' ).each( function( i , title ){
		if ( $( title ).height() > upper_max_h )
		{
			upper_max_h = $( title ).height();
		}	
	});
	$( '.upper-titles' ).find( '.drops-entry-title' ).each( function( i , title ){
		$( title ).height( upper_max_h );
	});
	var lower_max_h = 0;
	$( '.lower-titles' ).find( '.drops-entry-title' ).each( function( i , title ){
		if ( $( title ).children().height() > lower_max_h )
		{
			lower_max_h = $( title ).children().height();
		}	
	});
	$( '.lower-titles' ).find( '.drops-entry-title' ).each( function( i , title ){
		$( title ).height( lower_max_h );
	});
	$( '.cover-entry , .drops-entry-title' ).hover( 
		function() {
			// replace highlighted image
			var article_id = $( this ).attr( 'article_id' );
			if ( article_id != 'undefined' )
			{
				// switch pb / color images
				$( '.entries' ).find( '.cover-entry' ).each( function( i , entry ){
					if ( $( entry ).attr( 'article_id' ) == article_id )
					{
						$( entry ).find( 'img' ).each( function( j , entry_img ){
							$( entry_img ).toggleClass( 'hidden_image' );
						});
					}
				});
				// set text as active
				$( '.drops-entry-title' ).each( function( k , entry_title ){
					if ( $( entry_title ).attr( 'article_id' ) == article_id )
					{
						$( entry_title ).addClass( 'ativo' );							
					}
				});
				// show article lead
				$( '.lead[article_id='+article_id+']').show();										
			}
		},
		function() {
			var article_id = $( this ).attr( 'article_id' );
			if ( article_id != 'undefined' )
			{
				// switch pb / color images
				$( '.entries' ).find( '.cover-entry' ).each( function( i , entry ){
					if ( $(entry).attr( 'article_id' ) == article_id )
					{
						$(entry).find( 'img' ).each( function( j , entry_img ){
							$(entry_img).toggleClass( 'hidden_image' );
						});
					}
				});
				// set text as active
			}	
			// set text as inactive
			$( '.drops-entry-title' ).each( function( k , entry_title ){
				$( entry_title ).removeClass( 'ativo' );							
			});
			// hide article lead
			$( '.lead[article_id='+article_id+']').hide();
		}
	);
});

