/**
 * Arquivo responsável pelo estilo geral do Site RodakSul
 *
 * @author: Daniel Lopes danielmlopes[at]gmail[dot]com
 * @date: 2010/09/20
 *
 * Copyright(c) Todos os direitos reservados a RodakSul
 */

if (window.console == null) window.console = { log: function(p) { } };

var $ = jQuery,
	$object;

var Functions = function()
{
	$object = this;
	$object.init();
}

Functions.fn = Functions.prototype;
Functions.fn.extend = $.extend;
Functions.fn.extend(
{
    init: function()
	{
		$object.header = $( "#header" );
		$object.content = $( "#content" );
		
		if( $( "div.highlights" ).length )
			$object.highlights();
		
		$object.actions();
		
		$("ul.menu li").first().click();
	},
	
	// Método para alternamento entre os destaques
	highlights: function()
	{
		var divHighlights = $( "div.highlights" ),
			tInterval = setInterval(function(){$object.changeHighlights(false, false)}, 10000);
		
		$( "ul.menu li", divHighlights )
			.unbind()
			.click(function()
			{
				var tempoDestaque = $(this).attr('data-tempo');
				clearInterval( tInterval );
				$object.changeHighlights( true, $(this) );
				tInterval = setInterval(function(){$object.changeHighlights(false, false)}, tempoDestaque);
				
				return false;
			})
	},

	// Método auxiliar para alternamento entre os destaques
	changeHighlights: function( val, option )
	{
		
		var divHighlights = $( "div.highlights" ),
			itensMenu = $( "ul.menu li", divHighlights ),
			liS = $( "ul.items li", divHighlights ),
			actual = $( "ul.menu li.active", divHighlights ),
			dActive = actual.length >= 1 ? actual : itensMenu.eq(0);
		
		if( ! ( val && option.hasClass( "active" ) ) )
		{
			liS.eq(Number( dActive.text() ) - 1).fadeOut(250, function()
			{
				var _this = dActive,
					dNext = "";
					
				if( ! val && ( option == undefined || ! option ) )
				{
					if( _this.next().length == 0 )
						dNext = $( "ul.menu li", divHighlights ).eq(0);
					else
						dNext = _this.next();
				}else if( val && ( option != undefined ) )
				{
					dNext = option;
				}
				
				_this.add( dActive ).removeClass( "active" );
				liS.eq(Number( dNext.text() ) - 1).fadeIn(250, function()
				{
					var _thi = $(this);
					_thi.add( dNext ).addClass( "active" );
					_thi.add( dNext ).click();
				});
			});
		}else
			return false;
	},
	
	// Método para retorno do valor padrão nos Inputs
	defaultValue: function( tElem )
	{
        function clearDefaultValue()
        {
            var _this = $(this);
			
			_this.val() == this.defaultValue
				&& _this.val("");
        };
        function resetDefaultValue()
        {
            var _this = $(this);
			
			_this.val() == ""
				&& _this.val( this.defaultValue );
        };
		
        return tElem.click( clearDefaultValue ).focus( clearDefaultValue ).blur( resetDefaultValue );
	},
	
	// Método de ações simples
	actions: function()
	{
		var automotiveParts = $( "div.automotiveParts", $object.content );
		
		if( automotiveParts.length )
		{
			var imageBox = $( "div.imageBox", automotiveParts );
			
			if( ! $( "ul", imageBox ).length )
				automotiveParts.addClass( "noGalery" );
		}
	}
});

// Área de Mensagem - Message Show
(function()
{
    window.Message =
	{
	    show: show,
	    init: init
	};

    var htmlRef = null;

    function init()
    {
        htmlRef = $("asdasdasd<div class='message' style='background-color:#ffffff; border:5px solid #CEE7FF; 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( $.browser.msie && $.browser.version == "6.0" )
			htmlRef.css('top', $(window).scrollTop() + 100);
        
		$('body').mousemove(startHide);

    };

    function startHide()
    {
        $('body').unbind('mousemove', startHide);
        var htmlRef = htmlRef;
        setTimeout(hide, 3500);
    };

    function hide()
    {
        htmlRef.fadeOut('slow');
    };
})();

$( Message.init );


$( function()
{
	functions = new Functions();
	
	window.alert = function(msg)
	{
		// inserido botão OK para fechar a janela
		msgOk = "<div style='float:left;'>" + msg + "</div><div id='msgOk' style='float:right; padding:2px 8px; border: #000 1px solid; cursor:pointer;' >Fechar</div>";
		Message.show(msgOk.replace(/\n{1}/gi, '<br/>'));
		$("#msgOk").click(function() {
			$(".message").hide();
		});
		return null;
	}
});

