/**
* Arquivo responsavel pela chamada de todas as funcionalidades do site Abir
*
* @author: Mateus Moura chagas[dot]mateus[at]gmail[dot]com
* @date: 05/09/2010
*
* Copyright(c) Todos os direitos reservados a 
*/ 

if (window.console == null) window.console = { log: function(p) { }, error: function(p) { } };
		
var oThis;

var Actions = function()
{
	oThis = this;
	
	this.init();
}

Actions.fn = Actions.prototype;
Actions.fn.extend = jQuery.extend;
Actions.fn.extend(
{
    init: function()
	{
		this.content = $( "#content" );
		this.header = $( "#generalHeader" );
		
		$.browser.msie && $.browser.version == "6.0"
			&& DD_belatedPNG.fix( ".png24" );
		
		Cufon.set( 'fontFamily', 'TitilliumText14L');
		Cufon.replace( ".cufonReplace", { hover:true } );
		
		Cufon.set( 'fontFamily', 'TitilliumText14LRegular');
		Cufon.replace( ".cufonReplace_regular", { hover:true } );
		
		Cufon.set( 'fontFamily', 'Stone Inf ITC TT');
		Cufon.replace( ".cufonReplace_stone", { hover:true } );
		$( "#general" ).is( ".home" )
			&& oThis.paginationTweets( $( "div.tweets", oThis.content ) );
		
		oThis.resizeBG();
		oThis.slideMenu( $( "div.navigation", oThis.header ) );
		oThis.slideAffiliates( $( "div.affiliates" ) );
		$( "div.history", oThis.content ).length
			&& $( "div.history div.scroll", oThis.content ).jScrollPane();
			
		$( "div.eventos", oThis.content ).length
			&& oThis.hoverEvents( $( "div.eventos", oThis.content ) );
	},
	
	hoverEvents: function( div )
	{
		$( "div.months ul li:not('.active')", div ).hover( function()
		{
			$( this ).addClass( "active" );
		}, function()
		{
			$( this ).removeClass( "active" );
		});
	},
	
	showBalls: function()
	{
		if( $( "div.affiliates" ).length )
		{
			$( "div.affiliates" ).css(
			{
				visibility: "visible",
				display: "none"
			}).fadeIn( function()
			{
				$( "div.ballOrange, div.sector, div.news" ).css(
				{
					visibility: "visible",
					display: "none"
				}).fadeIn( function()
				{
					$( "h4, p, a", $( this ) ).show().css( "display", "block" );
				});
			});
		}
		else
		{
			$( "div.ballOrange, div.sector, div.news" ).css(
			{
				visibility: "visible",
				display: "none"
			}).fadeIn( function()
			{
				$( "h4, p, a", $( this ) ).show().css( "display", "block" );
			});	
		};
		
		$( "#general.home" ).css( "background", "url(http://abir.org.br/wp-content/themes/abir/img/bg/bg_drink_old.jpg) no-repeat center 140px" );
		$( "div.eventos, div.faleconosco, div.noticias, div.vidasaudavel", oThis.content ).length
			? $( "#general.internal" ).css( "background", "url(http://abir.org.br/wp-content/themes/abir/img/bg/bg_general_internal_2.jpg) no-repeat top center" )
			: $( "#general.internal" ).css( "background", "url(http://abir.org.br/wp-content/themes/abir/img/bg/bg_general_internal.jpg) no-repeat top center" );
		
	},
	
	resizeBG: function()
	{
		var s = $( window.document ).width(),
			bg = $( "div.bg", oThis.header ),
			nu = ( s - 960 ) / 2;
		
		console.log( s )
		
		if( nu <= 0 )
			nu = 10;
		
		bg.css( "width", nu + 10 );
		
		/*s == 1280
			? bg.css( "width", "200" )
			: s == 1024
				? bg.css( "width", "50" )
				: s == 800
					? bg.css( "width", "10" )
					: s == 1680
						? bg.css( "width", "378" )  
						: s == 1920
							? bg.css( "width", "485" )
							: s == 1152
								? bg.css( "width", "165" )
								: [];*/
								
		
			
	},
	
	slideMenu: function( div )
	{
		var subMenus = $( "ul.sub-menu", div );
		
		$( "ul#menu-meet a:first", div ).addClass( "bt_meet" );
		$( "ul#menu-track a:first", div ).addClass( "bt_track" );
		
		subMenus.each( function()
		{
			var $this = $( this ),
				liActive = $( "li.current-menu-item", $this );
			
			$( "li:first", $this ).addClass( "first" );
			$( "<span class='bg'></span>" ).appendTo( $( "li:last", $this ).addClass( "last" ) );
			
			if( liActive.length )
			{
				if( !liActive.is( ".last" ) )
				{
					liActive.prev().length
						&& liActive.prev().addClass( "sibli" );
						
					liActive.next().length
						&& liActive.next().addClass( "sibli" );
				}
			}
		});
		
		/*$( "li:not('.current-menu-item') a", subMenus ).hover( function()
		{
			var li = $( this ).parent();
			
			li.addClass( "current-menu-item" );
			
			if( !li.is( ".last" ) )
			{
				if( li.prev().length )
				{
					li.prev( ".current-menu-item" ).length
						? li.prev( ".first" ).length
							? li.addClass( "hover" )
							: li.prev().addClass( "sibli hover" )
						: li.prev().addClass( "sibli" );
				}
					
				if( li.next().length )
				{
					li.next( ".current-menu-item" ).length
						? li.next().addClass( "sibli hover" )
						: li.next().addClass( "sibli" );
				}
			}
			else
			{
				if( li.prev().length )
				{
					li.prev( ".current-menu-item" ).length
						&& li.addClass( "hover" );
				}
			}
			
			
		}, function()
		{
			var li = $( this ).parent();
			
			li.removeClass( "current-menu-item" );
			if( !li.is( ".last" ) )
			{
				if( li.prev().length )
				{
					li.prev( ".current-menu-item" ).length
						? li.prev( ".first" ).length
							? li.removeClass( "hover" )
							: li.prev().removeClass( "sibli hover" )
						: li.prev().removeClass( "sibli" );
				}
					
				if( li.next().length )
				{
					li.next( ".current-menu-item" ).length
						? li.next().removeClass( "sibli hover" )
						: li.next().removeClass( "sibli" );
				}
			}
		});*/
		
		$( ".bt_meet, .bt_track", div ).unbind().click( function()
		{
			var $this = $( this ),
				li = $this.parent(),
				ulSlide = $( "ul", li ),
				valueLeft = ulSlide.css( "left" );	
			
			$this.is( ".bt_meet" )
				? ulSlide.animate(
				{
					left: valueLeft == "-222px" ? "65px" : "-222px"
				}, 800 )
				: ulSlide.animate(
				{
					left: valueLeft == "-240px" ? "84px" : "-240px"
				}, 800 );
			
			return false;
		});
		
		/*setTimeout( function()
		{
			$( ".bt_meet", div ).click();
			$( ".bt_track", div ).click();
		}, 1000 );*/
	},
	
	slideAffiliates: function( div )
	{
		var ul = $( "ul", div ),
			repeat = $( "li", ul ).length / 3,
			count = 1;
		
		ul.css( "width", $( "li", ul ).length * 84 ); 
		
		var setin = setInterval( function()
		{
			ul.animate(
			{
				left: count == repeat ? "0" : Number( ul.css( "left" ).split( "px" )[ 0 ] ) + ( -252 )
			}, 1000, function()
			{
				if( count == repeat )
					count = 0;
				count++;
			});
		}, 5000 );
	},
	
	paginationTweets: function( div )
	{
		var category = $( "div.category", div ),
			slide = $( "div.slide", div );
		
		$( ".bt_prev, .bt_next", div ).unbind().click( function()
		{
			var $this = $( this ),
				effect = function( fin )
				{
					var cActive = $( "p.active", category ),
						sActive = $( "li.active", slide ),
						activating = cActive[ fin ]().length
										? cActive[ fin ]()
										: fin == "next"
											? $( "p:first", category )
											: $( "p:last", category )
						cl = activating.attr( "class" );
					
					cActive.fadeOut( function()
					{
						cActive.removeClass( "active" );
						activating.fadeIn().addClass( "active" );
						
						sActive.fadeOut( function()
						{
							sActive.removeClass( "active" );
							$( "li." + cl, slide ).fadeIn( function()
							{
								$( this ).addClass( "active" );
							});
						});
					});
				};
			
			$this.is( ".bt_next" )
				? effect( "next" )
				: effect( "prev" );
				
			return false;
		});
	}
});

$( function()
{
	var actions = new Actions();
});
