String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
var cFadeDistance = 75;
var cMoveDelay = 250;
var cTop = 175;
var cLeft = 500;
var cWidth = 175;
var cHeight = 60;
var insertAtTop = true;
var inProcess = false;
var forceOpen = true;
var isLeft = true;

var cOff = new Array();
cOff[0] = new Array();
cOff[0].left = 0;
cOff[0].top = 0;
cOff[1] = new Array();
cOff[1].left = 50;
cOff[1].top = 100;
cOff[2] = new Array();
cOff[2].left = 75;
cOff[2].top = 200;
cOff[3] = new Array();
cOff[3].left = 50;
cOff[3].top = 300;
cOff[4] = new Array();
cOff[4].left = 0;
cOff[4].top = 400;


var commCycle = new Array('1 this is the first comment', '2 second comment', '3 thirddddd','4 fourth comment','5 how many of these are there?','6 these are supposed to simulate an ajax call','7 weeeeeee','8 what?');
var cTopInd = commCycle.length-1;
var cBotInd = cTopInd - cOff.length + 1;


function orientComments(left) {
	if( (left && !isLeft)  || (!left && isLeft) ){
		flipOffsets();	
		
	}
}

function flipOffsets() {
	var cOffLeft = new Array();
	for(var i = 0; i < cOff.length; i++) {
		cOffLeft[i] = new Array();
		cOffLeft[i].top = cOff[i].top;
		cOffLeft[i].left = 	-1 * (cOff[i].left) - cWidth;
	}
	cOff = cOffLeft;
	if(!isLeft) { isLeft = true; } else { isLeft = false; }
	
}

function positionComments(top, left) {
	$(".comment_post").css("top",top+cOff[0].top - cHeight-20).css("left",left+cOff[0].left);
	var comments = $(".comment_bubble");
	comments.each(function() {
		$(this).css("top",top+cOff[comments.index(this)].top).css("left",left+cOff[comments.index(this)].left);
	});
	//positionNewComment(top,left,true);
}

function positionNewComment(top, left, first) {
	if(first) {
		$(".new_comment_bubble").css("top",top+cOff[0].top).css("left",left+cOff[0].left);
	}
	else {
		//alert(cOff.length);
		$(".new_comment_bubble").css("top",top+cOff[cOff.length-1].top).css("left",left+cOff[cOff.length-1].left);
	}
}

function initComments() {
	for(var i = 0; i < cOff.length; i++) {
		var cInd = commCycle.length-cOff.length+i;
		$("body").prepend('<div class="comment_bubble"><div class="comm_text">'+ commCycle[cInd] +'</div></div>');
	}
	$("body").prepend('<div class="new_comment_bubble"><div class="comm_text"></div></div>');
	$(".comment_bubble, .new_comment_bubble").corner("12px");
}

function resetToTopComments(callback,comment) {
	if(!inProcess) {
		inProcess = true;
		var comments = $(".comment_bubble");
		comments.animate({opacity: 0},cMoveDelay/3,function() { 
			var cInd = commCycle.length-comments.index(this) - 1;
			cTopInd = commCycle.length-1;
			cBotInd = cTopInd - cOff.length + 1;
			$(this).find("div.comm_text").html(commCycle[cInd]);
			comments.animate({opacity:1},cMoveDelay/3,function() {
				inProcess = false;
				if(typeof(callback) == "function" && cInd==commCycle.length-cOff.length && comments.index(this)==cOff.length-1) {
					callback(comment);
				}	
			});
		} );
	}
}

function postComment(comment) {
	if(comment.trim() != "") {

		//ajax post comment to db here
		commCycle.splice(cTopInd+1,0,comment);
		shiftDown(0);
	}
}

function shiftDown(i) {
	if(!inProcess) {
		cTopInd++;
		cBotInd++;
		if(cBotInd > commCycle.length) {
			cBotInd = 0;
		}
		if(cTopInd >= commCycle.length) {
			cTopInd = 0;
		}
		var comment = commCycle[cTopInd];
		rotateDown(comment);
	}
}
function shiftUp(i) {
	if(!inProcess) {
		cTopInd--;
		cBotInd--;
		if(cTopInd < 0) {
			cTopInd = commCycle.length-1;
		}
		
		if(cBotInd < 0) {
			cBotInd = commCycle.length-1;
		}
		var comment = commCycle[cBotInd];
		rotateUp(comment);
	}
}

function rotateUp(comment) {
	if(!inProcess) {
		inProcess = true;
		var comments = $(".comment_bubble")
		$(".comment_bubble:not(:first)").each(function() {
				var tmove = cOff[comments.index(this)-1].top - cOff[comments.index(this)].top;
				var lmove = cOff[comments.index(this)-1].left - cOff[comments.index(this)].left;
				$(this).animate({top:'+='+tmove, left:'+='+lmove},cMoveDelay);
		});
		positionNewComment(cTop,cLeft,false);
		$(".new_comment_bubble").html('<div class="comm_text">' + comment + '</div>').corner("12px").animate({opacity: 1, width: cWidth, height: cHeight},200,function() {
			$(this).attr("class","comment_bubble").insertAfter($(".comment_bubble:last"));
		});
		$(".comment_bubble:first").animate({top: '-='+cFadeDistance,opacity: 0},cMoveDelay,function() {
			$(this).css("top",cTop).attr("class","new_comment_bubble").insertAfter($(".comment_bubble:last"));
			inProcess = false;
		});
	}
}

function rotateDown(comment) {
	if(!inProcess) {
		inProcess = true;
		var comments = $(".comment_bubble")
		$(".comment_bubble:not(:last)").each(function() {
				var tmove = Math.abs(cOff[comments.index(this)+1].top - cOff[comments.index(this)].top);
				var lmove = cOff[comments.index(this)+1].left - cOff[comments.index(this)].left;
				$(this).animate({top:'+='+tmove, left:'+='+lmove},cMoveDelay);
		});
		positionNewComment(cTop,cLeft,true);
		$(".new_comment_bubble").html('<div class="comm_text"">' + comment + '</div>').corner("12px").animate({opacity: 1, width: cWidth, height: cHeight},200,function() {
			$(this).attr("class","comment_bubble").insertBefore($(".comment_bubble:first"));
		});
		$(".comment_bubble:last").animate({top: '+='+cFadeDistance,opacity: 0},cMoveDelay,function() {
			$(this).css("top",cTop).attr("class","new_comment_bubble").insertBefore($(".comment_bubble:first"));
			inProcess = false;
		});
	}
}

function expandPost(els){
	inProcess = true;
	$(".comment_post").animate({opacity: 1, width:cWidth,height:cHeight},100, function() {
		$(this).find("input").keypress(function(e) {
			if((e.which == 13) && !inProcess) {
				forceOpen=false;
				if( e.which==13 ) {
					//alert(resetToTopComments());
					if(cTopInd != commCycle.length-1 && insertAtTop) {
						
						resetToTopComments(postComment,$(this).val());
						
					}
					else {
						postComment($(this).val());
					}
				}
			//	else {
				//	rotateDown($(this).val());
				//}
				$(this).val('');
				//$(this).parents(".comment_post").hide().css({opacity: .1, height:1,width:1});
				//shrinkBubble();
			}
		}).focus().blur(function() {
			//forceOpen = false;
			//$(this).parents(".comment_post").hide().css({opacity: .1, height:1,width:1});
			//shrinkBubble();
		});
		forceOpen = true;
		expandCascade(els);
	});
	//$('#lightbox-container-image-box').animate({ width: intWidth, height: intHeight },settings.containerResizeSpeed,function() { _show_image(); });
}

function shrinkBubble(flip) {
	if(!forceOpen && !inProcess) {
		var flipDone = false;
		$(".comment_bubble, .comment_post").animate({opacity:.1,width:1,height:1},250,function() {
			$(this).hide();
			if(flip && !flipDone) { flipOffsets(); flipDone=true; positionComments(cTop,cLeft); }
		});
	}
	
}

function expandCascade(els) {
	if(els.length > 0) {
		els.eq(0).animate({opacity: 1, width:cWidth,height:cHeight},250,function() {
			expandCascade(els.slice(1))
		});
	}else {
		inProcess = false;
	}
}
/*
var step = 75;
var topBox = 175;
var botBox = 400;

function rotateComments(comment) {
	//inProcess = true;
	//$(".comment_bubble:eq(2)").animate({top: 175},250);
	//$(".comment_bubble:eq(2)").animate({top: 175},250);
	//$(".comment_bubble:eq(0)").hide();
	//$(".comment_bubble:eq(2)").animate({top: 250,opacity: 0},250).html(comment).animate({top:175,opacity: 1}, 250);
	//$(".comment_bubble:eq(0)").animate({top: 250, left: 575},250);
	//$(".comment_bubble:eq(1)").animate({top: 325, left: 500},250);
	
	if(!inProcess) {
		inProcess = true;
		var comments = $(".comment_bubble")
		$(".comment_bubble:not(:last)").each(function() {
			if( comments.index(this) < Math.floor(comments.length/2) ){
				$(this).animate({top:'+='+step, left:'+='+step},250);
			}
			else {
				$(this).animate({top:'+='+step, left:'-='+step},250);
			}
		});
		$(".new_comment_bubble").html('<div style="padding: 10px; font-size:13px; color:#333; line-height:15px;">' + comment + '</div>').animate({opacity: 1, width: 175, height: 45},200,function() {
			$(this).attr("class","comment_bubble").insertBefore($(".comment_bubble:first"));
		});
		$(".comment_bubble:last").animate({top: botBox,opacity: 0},250,function() {
			$(this).css("top",topBox).attr("class","new_comment_bubble").insertBefore($(".comment_bubble:first"));
			inProcess = false;
		});
	}
}
*/

