jQuery(function(){
	
    // window.reqStack = 0;
    // 
    // $(document).bind('ajaxSend', function(event){
    //  if (window.reqStack === 0){
    //      $("#progress").show();
    //  }
    //  window.reqStack = window.reqStack + 1;
    // });
    // 
    // $(document).bind('ajaxComplete', function(event){
    //  window.reqStack = window.reqStack - 1;
    //  if (window.reqStack === 0){
    //      $('#progress').hide();
    //  }
    // });
	
	loadWidgets();
	
	$('.widgt').bind('loaded', function(event){
		$(this).animate({opacity:1},1500);
	});
	
	// -----------
	// OVERLAY
	//------------
	overlay = function overlay(info){
		var msg = $('#overlay .msg');
		msg.html(info);
		$('#overlay').show();
		$('#overlay').trigger('invoked');
	};
	window.overlay = overlay;
	$('#overlay').live('click', function(event){
		$('#overlay .msg').html("");
		$('#overlay').hide();
		$('#overlay').trigger('closed');
	});
	$('#overlay .msg').live('click', function(event){
		event.stopPropagation();
		return false;
	});
	
	
	//------------
	// ACTION BUTTONS
	//------------
	if ($('#action-buttons').length > 0) {
		
        $('#action-buttons .see-comments').bind('click', function(event){
			var comment_url = document.location.pathname + 'comments/';
			$('#extra-space .see-comments').load(comment_url);
			$
			return false;
        });
		
		$('#action-buttons > a.extra').bind('click', function(event){
			var klass = $(this).attr('class').split(" ")[3];
			$('#extra-space > div').hide();
			$('#extra-space .'+klass).show();
			$('#extra-space').slideDown(400);
			return false;
		});
		
		$('#comment-form').bind('submit', function(event){
			var data = $(this).serialize();
			var url = document.location.pathname + 'comment/';
			$.post(url, data, function(data){
				if (data === 'ok'){
					$('#comment-form')[0].reset();
					var msg = "<p class='msg'>Obrigado por comentar, sua opinião está aguardando moderação.</p>";
					$('#extra-space .comment').html(msg);
				} else {
					$('#comment-form formset').html(data);
				}
			});
			return false;
		});
		
		$('#email-form').bind('submit', function(event){
			var link = document.location;
			$('#email-form #id_link').val(link);
			var data = $(this).serialize();
			$.post('/mail/recommend/', data, function(data){
				if (data === 'ok'){
					var msg = "<p class='msg'>Obrigado. Seu email já foi enviado.</p>";
					$("#extra-space .email").html( msg );
				} else {
					$('#email-form formset').html(data);
				}
			});
			return false;
		});
    }
	
	//-------------
	// TOCAE
	//-------------
	$("a[href=#tocae]").bind('click', function(event){
		overlay( $('#tocae-form-placeholder').html() );
		
		$('#tocae-form input[type=submit]').bind('click', function(event){
			var data = $('#tocae-form').serialize();
			$.post('/mail/tocae/', data, function(data){
				if (data === 'ok'){
					$('#tocae-form')[0].reset();
					var msg = "<p>Sua música já foi encaminhada e está em boas mãos.";
					msg = msg + " Continue ouvindo a Rádio Cidade JF, 100,1</p>";
					$('#overlay .msg').html(msg);
				} else {
					$('#tocae-form formset').html(data);
				}
			});
			return false;
		});
		
		return false;
	});
	
	//------------
	// CATEGORY SELECT
	//------------
	if ($('#select').length > 0){
		$('#select').bind('click', function(event){
			// $(this).find('.options').slideDown(600);
			$(this).find('.options').slideToggle();
		});
	}
	
	//------------
	// SUBMENU
	//-------------
	$('ul.menu > li:has(.submenu) > a').bind('click', function(){
		$(this).parents('li').find('.submenu').toggle();
		return false;
	});

	//------------
	// TICKER
	//-------------
	(function(){
        var container_width = 0;
	    $('.ticker .container div').each(function(i,el){
            container_width += $(el).outerWidth();
		});
        $('.ticker .container').width(container_width);
        var visible_width = $('.ticker').width();
        var target =  container_width;
        var target = parseInt( "-" + target );
        var time = (container_width) * 16;
        $('.ticker .container').animate({'left':target}, time);
        setInterval(function(){
         $('.ticker .container').css('left',570);
         $('.ticker .container').animate({'left':target}, time);
        }, time+100);
	})();
	
	//----------
	// WIDGTS header as link
	//-----------
	$('.widgt .header').css({'cursor':'pointer'});
	$('.widgt .header').bind('click', function(){
	    var section = $(this).parents('.widgt').attr('section');
	    window.location.pathname = "/" + section;
	    return false;
	});
	//----------
	// SEC Title as Link
	//-----------
	$('.sec-title').bind('click', function(){
	    document.location.pathname = "falae/sua-voz/";
	    return false;
	});
	
	//----------
	// Special case for .imgtop widgets
	//-----------
	$('.widgt.imgtop').live('loaded', function(){
		var imgspot = $("<div class='imgspot'><a><img /></a></div>");
		$(this).find('.body').prepend( imgspot );
		$(this).find('.body .title:visible:first').trigger('slideon');
	});
	$('.widgt.imgtop .title').live('hover slideon', function(){
		var img = $(this).parents('p').find('.image').attr('src');
		var href = $(this).parents('p').find('a').attr('href');
		$(this).parents('.body').find('.imgspot img').attr('src',img);
		$(this).parents('.body').find('.imgspot a').attr('href', href);
	});
	
	//----------
	// Hiding flash elements on overlay invocation and showing them uppon closing
	//-----------
	$('#overlay').live('invoked', function(event){
	    $('object').hide();
	    hiding_flash = setInterval(function(){
	        $('object').addClass('weak-hide');
	    }, 600);
	});
	$('#overlay').live('closed', function(event){
	    clearInterval(hiding_flash);
	    $('object').removeClass('weak-hide');
	});
	
	
});