// updated 1-31-08


function linkerclose(closer) { //formatting helper for links
	var id_text = $("#id_text");
	alert('test');
	var link = $(closer).parent().find("#url").val()
	if (link != "") {
		AtCursor('id_text', ' <a href="' + link + '">'+ $(closer).parent().find("#linktext").val() + '</a>')
	    livePreview($(id_text));
    	$(id_text).focus();
    }
}; 

function picclose(closer) { //formatting helper for images
	var id_text = $("#id_text");	
	var picUrl = $(".TB_modal #picUrl").val();
    if (picUrl != "") {
     	pname = picUrl.toLowerCase()
		if (pname.indexOf(".jpg") != -1 ||  pname.indexOf(".gif") != -1 || pname.indexOf(".png") !=-1) {
			AtCursor('id_text', ' <img src="' +  picUrl + '" alt="" />')
            livePreview($(id_text));
            $(id_text).focus();
        } else { 
			$(".TB_modal #picBox label").html($(".TB_modal #picBox label").html() + '<br />Invalid picture format');
        }
 //   } else {
    	//$(".formatHelper #id_post_image_file").val($(closer).parent().find("#id_post_image_file").val());
    
    }
    
}; 
function qclose(closer) { //formatting helper for links
	var id_text = $("#id_text");
	var quote = $(closer).parent().find("#qSaid").val()
	var speaker = $(closer).parent().find("#qWho").val()
	if (quote != "") {
		if (speaker != "") {
			AtCursor('id_text', ' <blockquote><cite>'+speaker+' said:</cite>'+quote+'\n</blockquote>')
		} else {
			AtCursor('id_text', ' <blockquote>'+quote+'\n</blockquote>')
		}
	    livePreview($(id_text));
    	$(id_text).focus();
    }
};

//open links
function openLinks() {
	$("div.post a").click(function(){window.open(this.href,'',''); return false;});
}

function livePreview(thePost) {
	//if (thePost.value != null) {
	//	thePost = thePost.value.replace(/\n/g, "<br \/ >");
	//	$("#preview").html(thePost);
	//}
}

function AtCursor(Field, myValue) {
  //IE support
  myField = document.getElementById(Field)
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  //MOZILLA/NETSCAPE support
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
                  + myValue
                  + myField.value.substring(endPos, myField.value.length);
  } else {
    myField.value += myValue;
  }
}

$(document).ready(function() {
	//wrap pics
	$(".post img:not('.smiley')").wrap("<div class='picwrap'></div>");

	//live preview
	var id_text = $("#id_text");
	var form = $("#reply");
	$("#preview").html($(id_text).val())
	
	$(id_text).keyup(function() {
		livePreview(this);
	});
	// QUOTING
	$("#quoter").click(function(e) {
      	e.preventDefault();
      	$(".formatHelper").animate({height: 'hide'}, 'fast');
		$("#qBox").animate({height: 'show'}, 'fast'); 
		$(".postlist").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});
		$("#previewText").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});
		$("#id_text").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});
	});
      $("#qBox .closer").click(function(e) {
      	e.preventDefault();
		qWho = "";
        if ($("#qWho").val() != "") {
           qWho = '<em>' + $("#qWho").val() + ' said:</em>\n\n';
		}
        //$(id_text).val($(id_text).val() + '<blockquote>\n<p>' + qWho +  $("#qSaid").val() + '</p>\n</blockquote>')
        AtCursor('id_text', '<blockquote>\n<p>'+qWho+  $("#qSaid").val() + '</p>\n</blockquote>\n');
        $("#qBox").animate({height: 'hide'}, 'fast');
        livePreview($(id_text));
        $(id_text).focus();
	});
	// linking
	$("#linker").click(function(e) {
      	e.preventDefault();
      	$(".formatHelper").animate({height: 'hide'}, 'fast');
		$("#linkerBox").animate({height: 'show'}, 'fast'); 
		$(".postlist").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});
		$("#previewText").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});
		$("#id_text").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});             
	});
      $("#linkerBox .closer").click(function(e) {
      	e.preventDefault();
		$("#linkerBox").animate({height: 'hide'}, 'fast'); 
        //$(id_text).val($(id_text).val() + '<a href="'+ $("#linkerBox #url").val() +'">'+ $("#linkerBox #linktext").val() + '</a>')     
        AtCursor('id_text', '<a href="'+ $("#linkerBox #url").val() +'">'+ $("#linkerBox #linktext").val() + '</a>');
        livePreview($(id_text));
        $(id_text).focus();
	}); 
	// ADD PICTURES
	$("#pixer").click(function(e) {
      	e.preventDefault();
      	$(".formatHelper").animate({height: 'hide'}, 'fast');
		$("#picBox").animate({height: 'show'}, 'fast');
		$(".postlist").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});
		$("#previewText").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});
		$("#id_text").click(function(e) {
		    $(".formatHelper").animate({height: 'hide'}, 'fast');
		});              
	});
      $("#picBox .closer").click(function(e) {
      	e.preventDefault();
      	$("#picBox").animate({height: 'hide'}, 'fast'); 
        //$(id_text).val($(id_text).val() + '<img src="' + $("#picUrl").val() +'" />\n')
        AtCursor('id_text', '<img src="' + $("#picUrl").val() +'" />\n');
     
        livePreview($(id_text));
        $(id_text).focus();
	});
	// DROPDOWN REPLY BOX
	$("ul.postcontrols").find("a.reply").each(function(i){
            $(this).click(function(e)   {
                e.preventDefault();
                theParent = this.parentNode.parentNode.parentNode;
                if (theParent.lastChild.nodeName != "FORM") {
                        $(form).remove();                	
                      	$(theParent).append($(form));
                      	$(id_text).val('In reply to '+ $(theParent).find('.author_name').html()+ ':\n\n');    

                        $(id_text).focus();
                        $.highlightFade.defaults.speed = 3000;
		 		$(form).animate({height: 'show'}, 'slow');
                        $(form).highlightFade('paleyellow');
                  }
           	});
 	});
	// BAD POSTS
	$('ul.postlist li.bad-post h4').append('<em class="note bad-post-message">- This comment has been collapsed</em>');
	$('ul.postlist li.bad-post h4').click(function() {
		//alert ($(this).parent().class())
		$(this).parent().removeClass('bad-post');
		
	});
	// ADD TO WATCHLIST
      	$('a.watch').click(function(e)   {
		this.href = this.href + 'js/';
      		e.preventDefault();
            	var container = $(this);
            	$.post(this.href, {rating: $(this).html()}, function(html) {
            		// format result
            	    	var result = [html,' &radic;'];
            	    	// output result
            	    	$(container).html(result.join(''));
         	});
  	});
  	$("ul.postlist li a.avatar").each(function(i){
         	//$(this).href( $(this).href() + "js/");
          	var appended = "false";
          	$(this).click(function(e)   {
             	e.preventDefault();
             	$("div.profile-info").hide();
             	var thisUrl = this.href + 'js/';
             	var container = $(this).parent().find("div.profile-info");
             	$(container).animate({opacity: 'show' }, "fast");
             	$.post(thisUrl, {rating: $(this).html()}, function(html) {
                 		var p=eval("(" + html  + ")");
                 		var results = '<ul><li><strong>Posts:</strong> ' + p.posts +'</li>' +
                    		'<li><strong>Joined:</strong> '+ p.joined +'</li>';
                 		if (p.city || p.state) {
                     		results += "<li><strong>Location:</strong> " + p.city +", " + p.state+'</li>';
                 		}
                 		if (p.rides) {
                 			results += "<li><strong>Rides:</strong><ul>"+ p.rides + "</ul></li>";
                 		}
                 		results +='<li><a href="/contact/'+p.username+'/">Contact</a></li>';
                 		results +='<li><a href="/community/'+p.username+'/">View full profile</a></li>';
                 		results +="</ul>"
                 		if (appended == "false") {
                    	 	$(container).append('<span class="closer">X</span>');
                    	 	$(container).append(results);
                     		appended = "true";
                     		$(container).click(function()   {
        						$(container).hide();
        					});
                 		}
                 		
            	});
            	
        	});
        	
    	});
});


