$(document).ready(function () {								
	function feiraClick() {
		var $this = $(this).parent();
		if ($this.attr('status') == 'open') {
			$this.attr('status','closed').removeClass('active').next("ul").slideUp('fast');					
		} else {
			$this.parent().find('.active').attr('status','closed').removeClass('active').next("ul").slideUp('fast');	
			$this.attr('status','open').addClass('active').next("ul").slideDown('fast');
		}
		return false;
	}
							
	$("#feiras h3 a").click(feiraClick);
	
	$("#feiras ul > li > a").click(function () {		
		var $bloco = $('<div/>');
		return false;
	});

    $opened_dialog = $('<div />');
    $mask = $("<div />").css({width:$(window).width(),height:$(document).height(),top:0,display:'none',position:'absolute'}).click(function () {
        $(this).fadeOut('fast', function () {
            $opened_dialog.animate({width:1, height:1,top: $(window).height() / 2 - 0.5 + $(window).scrollTop(),left: $(window).width() / 2 - 0.5 },500,function () {
                $(this).hide();
            }); 
        });
    }).appendTo('body'); 
    
    $(window).scroll(function () {
        $opened_dialog.animate({top:$(window).height() / 2 - 200 + $(this).scrollTop()},{duration:100,queue:false});
        $mask.css('top',$(this).scrollTop());
    });
    
    //seleciona os elementos a com atributo name="modal"
    var box_c = 0;
    $('.feiras-links a').click(function(e) {        
        //armazena o atributo href do link
        var href = $(this).attr('href');        
        var winH = $(window).height();
        var winW = $(window).width();
        
        if (new Number($(this).attr('box_id')) > 0) {
            $box = $("#b"+$(this).attr('box_id'));
            $box.show().animate({width:500, height:$box.attr('oHeight'), left: winW / 2 - 250, top: winH / 2 - 200 + $(window).scrollTop()},500); 
            $opened_dialog = $box;      
            $mask.fadeIn(1000).fadeTo("slow",0.8);  
        } else {        
            box_c++;
            $(this).attr('box_id', box_c);
            
    
            
            $box = $('<div />').attr('id','b'+box_c).addClass('dialog_box').css({
                width:1,
                height:1,
                position:'absolute',
                display:'none',
                top: winH / 2 - 0.5 + $(window).scrollTop(),
                left: winW / 2 - 0.5        
            }).css('background-color','white').html($('<img />').attr('src',IMAGES_PATH+'site/ajax-loader-big.gif').css({'margin-top':173,'margin-left':223}));  
            
            $box.appendTo('body'); 
            $box.show().animate({width:500, height:400, left: winW / 2 - 250, top: winH / 2 - 255 + $(window).scrollTop()},500);   
            $opened_dialog = $box;      
            $mask.fadeIn(1000).fadeTo("slow",0.8);       
            $.ajax({
               type:'POST',
               url: href,
               success: function (obj) {   
                    var data = obj.split('|');
                    /*
                     nome = 0
                     descricao = 1
                     info = 2
                     link = 3
                    
                    */
                
                    var html = '<div class="box-inteira"><h3>'+data[0]+'</h3><div class="descricao">'+
'<div><a href="http://www.augustus.com.br/" target="_blank"><img src="'+IMAGES_PATH+'site/banner-pt-br.jpg" alt="" style="margin-bottom:5px;" /></a></div><div align="justify">'+data[1]+'</div><br /><h4>Informação:</h4><div align="justify">'+data[2]+'</div><br />'+
'<div class="botao-vermelho-escuro"><strong>Para saber mais, acesse: <a href="http://'+data[3]+'" target="_blank"><span>'+data[3]+'</span></a></strong></div></div></div>';
                
                    $box.html(html);
                    $box.prepend($('<span />').addClass('x').attr('align','center').click(function () {
                        $mask.fadeOut('fast', function () {
                            $opened_dialog.animate({width:1, height:1,top: $(window).height() / 2 - 0.5 + $(window).scrollTop(),left: $(window).width() / 2 - 0.5 },500,function () {
                                $(this).hide();
                            }); 
                        });
                    }).html('x'));
                    //console.log($box.find(".box-inteira").height());
                    $box.attr('bheight', $box.find('.box-inteira').height());
                    $box.animate({height:$box.find('.box-inteira').height()},300);
                    $box.attr('oHeight',$box.find('.box-inteira').height());
               } 
            });
        }
        
        return false;
    });

		
});