$(document).ready(function () {
	$("input").focus(function () {
		$(this).addClass('focus').removeClass('error');
	}).blur(function () {
		$(this).removeClass('focus');	
	});
    
    function menuClick() {
        var $this = $(this);
        if ($this.attr('state') == 'opened')
            menuOff($this);
        else
            menuOn($this);
    }
    
	function menuOn($this) {
		if (!$this.hasClass('active') && $this.attr('state') != 'closing' && $this.attr('state') != 'opening') {
            
			$this.addClass('opened');
			if ($this.attr('toggle') != 'false') {
                $("#menu > div[state=opened]").each(function () {
                    //if ($(this).attr('state') == 'opened') {
                        menuOff($(this));
                    //}
                });
				$this.attr('state', 'opening');
				$this.find('h3').addClass('active');
				$this.find('div').slideDown('slow', function() { 
					$this.attr('state','opened');
				});
				$this.find('span').removeClass('default').addClass('icon-active');
			}
		}
	}
	
	function menuOff($this) {
		$this.removeClass('opened');
		if ($this.attr('toggle') != 'false') {
			$this.attr('state','closing');
			
			$this.find('div').slideUp('slow', function () {
				$this.attr('state','closed');
				$this.find('h3').removeClass('active');				
			});	
			$this.find('span').removeClass('icon-active').addClass('icon-default');
		}
	}
	$("#menu div").hide();
	
	$("#menu > h3").prepend($('<span />').addClass('icon').addClass('icon-default')).each(function () {
		var $this = $(this);
		if ($this.attr('toggle') != 'false') {
			var html = $('<div />').click(menuClick).append($('<h3 />').html($this.html())).append($this.next('div'));
            
			//$('<div />').hover(menuOn, menuOff).html($this)
			$this.before(html);
            
            if ($this.hasClass('checked')) {
                html.find('h3').addClass('checked');
                menuOn(html);   
            }
            
			var t = $this.next('div');
			t.remove();
			$this.remove();
		} else {
            $this.click(menuClick);
			//$this.hover(menuOn, menuOff);
		}
	});    
    
    $("#menu h3").click(function () {
        var $href = $(this).find('a').attr('href');
        if ($href != "javascript:void('');") {
            window.location = $href;
        }
    }).hover(function () {
        $(this).addClass('active');
    }, function () {
        $(this).removeClass('active');
    });	
	$("#marco-da-paz-swf").each(function () {
		var offset = $("#marco-da-paz").offset();
		$(this).css({top:offset.top,left:offset.left});
	});

	$("#geral-topo").click(function () {
		if ($("#marco-da-paz").attr('status')=='open') {
			$("#marco-da-paz-swf").animate({width:'1px',height:'1px',top:offset.top, left:offset.left},500,'swing',function () {
				$(this).hide();
			});
			$("#marco-da-paz").attr('status','closed');
		}	
	});
    $("#marco-x").click(function () {
        var $this = $("#marco-da-paz");
        var offset = $this.offset();
        $("#marco-da-paz-swf").animate({width:'1px',height:'1px',top:offset.top, left:offset.left},500,'swing',function () {
			$(this).hide();
	   });
	   $this.attr('status','closed');
    });
	$("#marco-da-paz").click(function () {
		var offset = $(this).offset();
		
		if ($(this).attr('status')!='open') {
			$("#marco-da-paz-swf").show().animate({width:'373px',height:'338px',top:offset.top - 425, left:offset.left + 50},500);
			$(this).attr('status','open');
		} else {
			$("#marco-da-paz-swf").animate({width:'1px',height:'1px',top:offset.top, left:offset.left},500,'swing',function () {
				$(this).hide();
			});
			$(this).attr('status','closed');
		}
		return false;
	});					
});