var fetch_timeout;
var counter = 0;
var $min=0;

$(function() {	
	// var myomama = $("a.my-omama-link2");
	// var clone = $(myomama).clone().prependTo('div.chat-rooms-grid.mounted');
	// console.debug(clone);
	
	var options = {
		beforeSubmit: function() {
			$.blockUI('Posting your comment...');
		},
		success: function(responseText) {
			if(responseText.match('no-auth')) {
				$.blockUI('You must be logged in before you can comment in the chat rooms');
				window.location = $("div#basedir").attr('rel')+'login';
			} else {
				$("#PostContent").val('').focus();
				$.unblockUI();
				clearTimeout(fetch_timeout);
				fetch_new_posts();
			}
		}
	};
	$("div.posts div.submit form").ajaxForm(options);
	
	$("a.reply.inactive").livequery("click", function() {
		$.blockUI('You must be logged in before you can comment');
		window.location = $("div#basedir").attr('rel')+'login';
	});
	$("a.reply.active").livequery("click", function() {
		var replyLink = this;
		var wrapper = $("<div class='form_wrap'></div>").css({backgroundColor: 'white', padding: '25px'}).appendTo('#wrapper');
		var clone = $("#PostAddForm").clone();
		clone.find('#PostThreadId').val($(this).attr('rel'));
		clone.appendTo(wrapper);
		overlayApi = wrapper.overlay({
		    expose: { 

		        // you might also consider a "transparent" color for the mask 
		        color: '#022E55', 

		        // load mask a little faster 
		        loadSpeed: 200, 

		        // highly transparent 
		       // opacity: 0.7 
		        opacity: 0
		    }, 


		    // we want to use the programming API 
		    api: true 
		}).load();
		
		var options = {
			beforeSubmit: function() {
				overlayApi.close();
				$.blockUI('Posting your comment...');
			},
			success: function(responseText) {
				if(responseText.match('no-auth')) {
					$.blockUI('You must be logged in before you can comment');
					window.location = $("div#basedir").attr('rel')+'login';
				} else {
					// $.get('/articles/posts/'+$("#PostArticleId").val()+'/1', function(data){
					//  	$('#posts_full').html(data);
					// 	$.unblockUI();
					// });
					
					
					// window.location.reload();
					// clearTimeout(fetch_timeout);
					fetch_new_posts();
				}
			}
		};
		$(wrapper).find("form").ajaxForm(options);
	
	
		return false;
	});

	fetch_new_posts();
	// $("#PostContent").focus();
	
	$("#FilterSubjectId").change(function() {
		clearTimeout(fetch_timeout);
		$("ul.post_content").html('');
		fetch_new_posts();
		$("#PostSubjectId").val($(this).val());
		
		// $("ul.post_content").fadeOut(250, function() {
		// 	$("ul.post_content").html('');
		// 	fetch_new_posts();
		// });
	});
	
	$("div.subject a.comment").livequery('click', function() {
		$("#PostSubjectId").val($(this).attr('rel'));
		$("#PostContent").focus();
		return false;
	});
});

function fetch_new_posts($min) {
	if($min==null) $min = $("ul.post_content div.post").get(0);
	// $min = $($min).attr('rel');
	$min = null;
	var $filter = $("#FilterSubjectId").val();
	$.ajax({
		url: $("div#basedir").attr('rel')+"chat_rooms/posts/",
		type: "POST",
		dataType: "html",
		data: {room: $("#PostChatRoomId").val(), filter: $filter, min: $min},

		complete: function(data) {
		},

		success: function(data) {
			if(!$min) {
				$("ul.post_content").html(data);
				$("div.post.new").removeClass('new');
			} else {
				$("ul.post_content").prepend(data);
				$("div.post.new").hide().fadeIn().removeClass('new');
			}
			// fetch_timeout = setTimeout(function() {
			// 	if (counter%2==0) fetch_new_posts(0);
			// 	else fetch_new_posts();
			// }, 8000);
			$.unblockUI();
			fetch_timeout = setTimeout(function() {
				fetch_new_posts(0);
			}, 60000);
			
			counter++;
		},

		error: function() {
			//called when there is an error
		}
	});


}

$(function() {
	
	var $style = {
		width: 200,
		padding: 5,
		tip: 'topMiddle',
		name: 'red',
		color: '#fff',
		background: '#6F1100',
		border: {
		         width: 5,
		         radius: 0,
		         color: '#990000'
		      }
	};
	// O-MAMA Buttons
	$(".general-hot-button").qtip({
		content: {
			text: 'Have a "hot button" issue you want all moms to discuss?'
		},
		show: {
			delay: 10,
			effect: {
				length: 50
			}
		},
		hide: 'mouseout',
		position: {
			corner: {
				target: 'leftMiddle',
				tooltip: 'rightMiddle'
			}
		},
		show: {
			effect: {
				length: 600,
				type: 'fade'
			}
		},		
		style: {
		width: 125,
		padding: 5,
		tip: 'rightMiddle',
		name: 'blue',
		color: '#fff',
		background: '#4485C4',
		border: {
		         width: 5,
		         radius: 5,
		         color: '#4485C4'
		      }
	}
	});
});