/**
 * Arquivo responsável pelo JS GERAL do portfólio Glauber Dorotheu
 *
 * @author: Daniel Lopes - danielmlopes[at]gmail[dot]com
 * @date: 2011/08/01
 *
 * Copyright(c) Todos os direitos reservados à Glauber Dorotheu
 */

if (window.console == null) window.console = { log: function(p) { } };

var fnGeneral;

var GD_Gn = function()
{
	fnGeneral = this;
	fnGeneral.init();
}

GD_Gn.fn = GD_Gn.prototype;
GD_Gn.fn.extend = jQuery.extend;
GD_Gn.fn.extend(
{
    init: function()
	{
		fnGeneral.content = $( "#content" ); // Declara a div#content
		fnGeneral.loader = $( "#loader" ); // Declara a div#loader
		fnGeneral.divView = $( "div.view", fnGeneral.content ); // Declara a div.view - Visualização das Pages
		fnGeneral.divSlide = $( "div.slide", fnGeneral.content ); // Declara a div.slide - Visualização dos destaques
		
		// Ações no Twitter da Home
		if( $( "div.twitter", fnGeneral.content ).length )
			fnGeneral.twitter( $( "div.twitter", fnGeneral.content ) );
			
		// Deleta barra do Admin
		if( $( "#wpadminbar" ).length )
			$( "#wpadminbar" ).remove();
			
		// Destaques da Home
		if( $( "div.slide", fnGeneral.content ).length )
			fnGeneral.slide( $( "div.slide", fnGeneral.content ) );
			
		// Hover nos Jobs
		if( $( "div.jobs", fnGeneral.content ).length )
			fnGeneral.jobs( $( "div.jobs", fnGeneral.content ) );
		
		// Show Page
		fnGeneral.thePages( $( "#header ul li.about a" ) );
		
		$(window).scroll(function()
		{
			fnGeneral.calcScroll()
		});
	},
	
	// Declara a o tempo para opacidade das LIs
	timeLeft: 0,
	
	// Declara a o tempo para voltar à visualização normal das LIs
	timeForChange: 0,
	
	// Declara se as LIs estão com opacidade
	ready: false,
	
	// Ações no Twitter da Home
	twitter: function( obj )
	{
		var hrefAs = $( "" )
		
		$( "a", obj ).attr( "target", "_blank" );
		
		$( "ul li a", obj ).each(function()
		{
			var $this = $(this),
				hrefs = $this.attr( "href" ).split( "" );
			
			if( hrefs[ hrefs.length - 1 ] == ( '”' || '"' || '*' || '+' || '“' ) )
			{
				hrefs[ hrefs.length - 1 ] = "";
				$this.attr( "href", hrefs.join("") );
			}
		})
	},
	
	// Calcular Scroll para exibir goTop
	calcScroll: function()
	{
		var template1 = $( '<div class="buttons1"><a href="#" title="Ir para o topo" class="goTop">Ir para o topo</a></div>' ),
			template2 = $( '<div class="buttons2"><a href="#" title="Ir para o topo" class="goTop">Ir para o topo</a><a href="#" title="Fechar Job em exibição" class="closeJob">Fechar Job em exibição</a></div>' ),
			actions = function()
			{
				$( "div.buttons1 a.goTop, div.buttons2 a.goTop" )
					.unbind()
					.click(function()
					{
						$( "html, body" )
							.animate({
								scrollTop: 0
							}, 800);
						
						return false;
					});
					
				$( "div.buttons2 a.closeJob" )
					.unbind()
					.click(function()
					{
						$( "html, body" )
							.animate({
								scrollTop: 0
							}, 800);
						
						$( "#header ul li a.active" ).removeClass( "active" );
						
						fnGeneral.viewManipulation( false, true, 0 );
						
						return false;
					});
			};
			
		if( Number( $(window).scrollTop() ) >= 250 )
		{
			if( ! $( "div.buttons1" ).length && ! $( "div.buttons2" ).length )
			{
				if( fnGeneral.divView.hasClass( "active" ) )
					template2
						.appendTo( $( "body" ) )
						.css( fnGeneral.content.offset().left == 0 ? "right" : "left", fnGeneral.content.offset().left == 0 ? 0 : fnGeneral.content.offset().left + 900 )
						.fadeIn(function()
						{
							actions();
						});
				else
					template1
						.appendTo( $( "body" ) )
						.css( fnGeneral.content.offset().left == 0 ? "right" : "left", fnGeneral.content.offset().left == 0 ? 0 : fnGeneral.content.offset().left + 900 )
						.fadeIn(function()
						{
							actions();
						});
			}
		}else
			$( "div.buttons1, div.buttons2" ).fadeOut(function()
			{
				$(this).remove();
			});
	},
	
	// Ativa click no ABOUT
	thePages: function( obj )
	{
		obj.unbind().click(function()
		{
			var thiS = $(this);
			
			if( thiS.hasClass( "active" ) )
				return false;
			
			$( "div.jobs ul li.active" ).removeClass( "active" );
							
			thiS.addClass( "active" );
			
			fnGeneral.innerPage( String( thiS.attr( "href" ) ), true );
			
			return false;
		});
	},
	
	// Destaques da Home
	slide: function( obj )
	{
		var tInterval = setInterval(function(){changeHighlights(false, false)}, 4000),
			changeHighlights = function( val, option )
			{
				var itensMenu = $( "ul.menu li", obj ),
					liS = $( "ul.images li", obj ),
					actual = $( "ul.menu li.active", obj ),
					dActive = actual.length >= 1 ? actual : itensMenu.eq(0),
					dNext = "";
				
				if( liS.length > 1 )
				{
					if( ! ( val && option.hasClass( "active" ) ) )
					{
						liS
							.eq( Number( $( "a", dActive ).attr( "class" ).split( "num" )[1] ) - 1)
							.fadeOut(600, function()
							{
								$(this).removeClass( "active" );
							});
								
						if( ! val && ( option == undefined || ! option ) )
						{
							if( dActive.next().length == 0 )
								dNext = $( "ul.menu li", obj ).eq(0);
							else
								dNext = dActive.next();
						}else if( val && ( option != undefined ) )
							dNext = option;
						
						dActive.removeClass( "active" );
						
						liS.eq( Number( $( "a", dNext ).attr( "class" ).split( "num" )[1] ) - 1 ).fadeIn(600, function()
						{
							var _thi = $(this);
		
							_thi.add( dNext ).addClass( "active" );
						});
					}else
						return false;
				}else
					clearInterval( tInterval );
			};
		
		if( $( "ul.images li", obj ).length <= 1 )
			$( "ul.menu", obj ).remove();
		
		$( "ul.menu li a", obj )
			.unbind()
			.click(function()
			{
				clearInterval( tInterval );
				changeHighlights( true, $(this).parent() );
				tInterval = setInterval(function(){changeHighlights(false, false)}, 4000);
				
				return false;
			})

		$( "ul.images li a", obj )
			.unbind()
			.click(function()
			{
				var thiS = $(this),
					hrefThis = thiS.attr( "href" );
				
				$( "p", $( "div.jobs ul li" ) ).each(function()
				{
					var _thi = $(this);
					
					if( _thi.hasClass( hrefThis ) )
						_thi.parent().addClass( "active" );
				})
				
				$( "#header ul li a.active" ).removeClass( "active" );
								
				fnGeneral.innerPage( String( hrefThis ), true );
				
				// fnGeneral.viewManipulation( false, false, 0 );
				
				return false;
			});
	},
	
	// Hover nos Jobs
	jobs: function( obj )
	{
		var liS = $( "ul li", obj );
		
		liS.hover(function()
		{
			var thiS = $(this);
			
			if( thiS.hasClass( "noJobs" ) )
				return false;
				
			clearInterval( fnGeneral.timeLeft );
			clearInterval( fnGeneral.timeForChange );
				
			thiS.addClass( "hover" );
			
			thiS
				.css({
					opacity: 1
				});			
			
			if( ! fnGeneral.ready )
				fnGeneral.timeLeft = window.setInterval(function()
				{
					clearInterval( fnGeneral.timeLeft );
					clearInterval( fnGeneral.timeForChange );
					
					thiS
						.siblings()
						.stop(true, true)
						.animate({
							opacity: 0.6
						}, 1200, function()
						{
							fnGeneral.ready = true;
						});
				}, 1000);
		}, function()
		{
			var thiS = $(this);
			
			if( thiS.hasClass( "noJobs" ) )
				return false;
				
			clearInterval( fnGeneral.timeLeft );
			clearInterval( fnGeneral.timeForChange );
			
			thiS.removeClass( "hover" );
			
			if( fnGeneral.ready )
			{
				if( ! thiS.hasClass( "active" ) )
					thiS
						.css({
							opacity: 0.6
						});
						
				if( ! fnGeneral.divView.hasClass( "active" ) )
					fnGeneral.timeForChange = window.setInterval(function()
					{
						clearInterval( fnGeneral.timeLeft );
						clearInterval( fnGeneral.timeForChange );
						
						fnGeneral.ready = false;
						
						liS
							.stop(true, true)
							.animate({
								opacity: 1
							}, 1200);
					}, 1000);
			}else
			{
				fnGeneral.ready = false;
				
				liS
					.stop(true, true)
					.animate({
						opacity: 1
					}, 1200);
			}
		});
		
		liS
			.click(function()
			{
				var thiS = $(this);
				
				if( thiS.hasClass( "active" ) || thiS.hasClass( "noJobs" ) )
					return false;
				
				$( "#header ul li a.active" ).removeClass( "active" );
				
				thiS
					.addClass( "active" )
					.siblings( ".active" )
					.removeClass( "active" )
					.stop(true, true)
					.animate({
						opacity: 0.6
					}, 200);
				
				fnGeneral.innerPage( String( $( "p", thiS ).attr( "class" ) ), true );
			});
	},
	
	// Manipular abertura e fechamento de página
	innerPage: function( address, act )
	{
		if( act )
		{
			var theTime = 0,
				theInterval = window.setInterval(function()
				{
					if( fnGeneral.divView.hasClass( "active" ) && fnGeneral.loader.is( ":hidden" ) && theTime < 40 )
					{
						clearInterval( theInterval );
						theTime = 0;
					}else if( fnGeneral.loader.is( ":visible" ) && theTime >= 40 )
					{
						clearInterval( theInterval );
						
						theTime = 0;
						
						alert( "Desculpe. Ocorreu um erro no processamento. Tente novamente mais tarde." );
						
						fnGeneral.viewManipulation( false, true, 0 );
						
						fnGeneral.loaderManipulation( false );
					}
					
					theTime++;
				}, 500);
			
			fnGeneral.loaderManipulation( true );
			
			clearInterval( fnGeneral.timeForChange );
			
			if( fnGeneral.divView.hasClass( "active" ) )
				fnGeneral.viewManipulation( false, false, 0 );
				
			$( "iframe", fnGeneral.divView ).attr( "src", address );
				
			$( "html, body" )
				.animate({
					scrollTop: 210
				}, 1000);
		}else
		{
			clearInterval( fnGeneral.timeLeft );
			clearInterval( fnGeneral.timeForChange );
		}
	},
	
	// Manipulação da div View
	viewManipulation: function( act, slider, heighT )
	{
		var liS = $( "div.jobs ul li", fnGeneral.content );
		
		if( act ) // Se for para abrir
		{
			window.setTimeout(function()
			{
				fnGeneral.divView
					.add( $( "iframe", fnGeneral.divView ) )
					.animate({
						height: heighT
					}, 1000, function()
					{
						fnGeneral.loaderManipulation( false );
					});
				
				$( "html, body" )
					.animate({
						scrollTop: 210
					}, 1000);
				
				if( ! slider )
					fnGeneral.divSlide
						.animate({
							height: 0
						}, 1000);
				
				fnGeneral.divView
					.addClass( "active" );
				
				if( ! fnGeneral.ready )
					liS
						.not( ".active" )
						.stop(true, true)
						.animate({
							opacity: 0.5
						}, 1000, function()
						{
							fnGeneral.ready = true;
						});
			}, 1500);
		}else // Se for para fechar
		{
			fnGeneral.divView
				.add( $( "iframe", fnGeneral.divView ) )
				.animate({
					height: 0
				}, 1000, function()
				{
					if( slider )
						$( "iframe", fnGeneral.divView ).attr( "src", "#" );
				});
			
			$( "html, body" )
				.animate({
					scrollTop: 0
				}, 1000);
			
			if( slider ) // Se for para mostrar o slider
			{
				fnGeneral.divSlide
					.animate({
						height: 290
					}, 1000);
				
				fnGeneral.divView
					.removeClass( "active" );
					
				liS
					.removeClass( "active" )
					.stop(true, true)
					.animate({
						opacity: 1
					}, 1000, function()
					{
						fnGeneral.ready = false;
					});
			}else{
				fnGeneral.divSlide
					.animate({
						height: 0
					}, 1000);
			}
		}
	},
	
	// Mudar Height
	changeHeight: function( heighT )
	{
		fnGeneral.divView
			.add( $( "iframe", fnGeneral.divView ) )
			.css( "height", heighT )
	},
	
	// Manipulação do Loader
	loaderManipulation: function( act )
	{
		if( act )
			fnGeneral.loader.fadeIn();
		else
			fnGeneral.loader.fadeOut();
	}
});

// Mensageria
(function()
{
    var $ = jQuery;
    window.Message =
	{
	    show: show,
	    init: init
	};

    var htmlRef = null;

    function init()
    {
        htmlRef = jQuery("<div class='message' style='background-color:#ffffff; border:5px solid #FFA214; display:none; left:50%; margin-left:-245px; padding:20px; position:absolute; top:50%; width:450px; z-index:10000;'><p style='color:#535353; display:block; font-size:14px; line-height:1.3em; text-align:center;'></p></div>").appendTo('body');
    };

    function show(message, type)
    {
        type = type == null ? "" : type;
        htmlRef.fadeIn('slow');
        htmlRef.attr("class", "message " + type);
        htmlRef.find( "p" ).html(message);
		
		if( jQuery.browser.msie && jQuery.browser.version == "6.0" )
			htmlRef.css('top', jQuery(window).scrollTop() + 100);
        
		jQuery('body').mousemove(startHide);

    };

    function startHide()
    {
        jQuery('body').unbind('mousemove', startHide);
        var htmlRef = htmlRef;
        setTimeout(hide, 3500);
    };

    function hide()
    {
        htmlRef.fadeOut('slow');
    };
})();

jQuery( Message.init );

jQuery(function()
{
    jQuery.fn.resetDefaultValue = function()
    {
        function _clearDefaultValue()
        {
            var _$ = jQuery(this);
            if (_$.val() == this.defaultValue) { _$.val(''); }
        };
        function _resetDefaultValue()
        {
            var _$ = jQuery(this);
            if (_$.val() == '') { _$.val(this.defaultValue); }
        };
        return this.click(_clearDefaultValue).focus(_clearDefaultValue).blur(_resetDefaultValue);
    }
});

jQuery( function()
{
	fnGeneral = new GD_Gn();
	
	window.alert = function(msg)
	{
		Message.show(msg.replace(/\n{1}/gi, '<br/>'));
		return null;
	}
});

