// updated 5-10-07

   var quotetext = "";

$(document).ready(function() { 
     var form = document.getElementById("reply");
     var idText = document.getElementById('id_text');
     var preview = document.getElementById('preview'); 
     var quoters = getElementsByClassName(document, "a", "quote");
     
     // ajax call for ratings
     
//post rating
     $("div.postcontrols").find("a.currRating").each(function(i){
 			$(this).attr('href', $(this).attr('href')+ "js/"); 
        $(this).click(function(e)   {
       	    e.preventDefault();
            var thisUrl = this.href;
            var container = this;
            $.post(thisUrl, {rating: $(this).html()}, function(html) {
            alert('posted')
		// format result
        	var result = [html, ' &radic;'];
		// output result
		$(container).html(result.join(''));
            });
	} );
     });
	 $("div.postcontrols").find("a.report").each(function(i){
    	$(this).attr('href', $(this).attr('href')+ "js/");
        $(this).click(function(e)   {
            e.preventDefault();
            var thisUrl = this.href;
            var container = this;
            $.post(thisUrl, {rating: $(this).html()}, function(html) {
                // output result;
                $(container).html('Thank you');
            });
        } );
     });

	// BAD POSTS
	$('ul.postlist li.bad-post h4').append('<span class="note bad-post-message">This comment has been collapsed</span>');
	$('ul.postlist li.bad-post .bad-post-message').click(function() {
		$(this).parent().parent().removeClass('bad-post');
	});

// member rating
     $("div.avatar").find("a.currRating").each(function(i){
    	$(this).attr('href', $(this).attr('href')+ "js/");
        $(this).click(function(e)   {
       	    e.preventDefault();
            var thisUrl = this.href;
            var container = this;
            $.post(thisUrl, {rating: $(this).html()}, function(html) {
		// format result
        	var result = [
	    	    html, 
                    ' &radic;'
		];
		// output result
		$(container).html(result.join(''));
            });
	} );
     });
    
            // dropdown profiles
       $("ul.postlist").find("a.userProfile").each(function(i){
    	$(this).attr('href', $(this).attr('href')+ "js/");
          	var appended = "false";
          	$(this).click(function(e)   {
             	e.preventDefault();
             	var thisUrl = this.href;
             	var container = $(this).parent().find("ul.userinfo");
             	$.post(thisUrl, {rating: $(this).html()}, function(html) {
                 		var p=eval("(" + html  + ")");
                 		var results = '<li><strong>Name:</strong> ' + p.name + '</li>'+
                    		'<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.home_state+'</li>';
                 		}
                 		if (p.website) {
                     		results += '<li><a href="'+ p.website +'">Web Site</a></li>';
                 		}
                 		results += '<li><a href="/members/contact/'+ p.username+'">Contact '+p.name+'</a></li><li><a href="/members/'+p.username+'">View Full Profile</a></li>';
                 		if (appended == "false") {
                    	 	$(container).append(results);
                     		appended = "true";
                 		}
            	});
        	});
    	});

	// js for watched items
  	var watch = $("#watch")
    	$(watch).attr('href', $(watch).attr('href')+ "js/");

      $(watch).click(function(e)   {
      	e.preventDefault();
            var thisUrl = this.href;
            var container = $(this);
            $.post(thisUrl, {rating: $(this).html()}, function(html) {
            	// format result
                	var result = [html,' &radic;'];
                	// output result
                	$(container).html(result.join(''));
         	});
  	});

     // reply box handling
     //
     $("div.postcontrols").find("a.reply").each(function(i){
            $(this).click(function(e)   {
                e.preventDefault();
                theParent = this.parentNode.parentNode.parentNode.parentNode;
                if (theParent.lastChild.nodeName != "FORM") {
                        form.parentNode.removeChild(form);                	
                      	theParent.appendChild(form);
                        idText.style.background = "#ffc";
                        idText.focus();
                        
                        $.highlightFade.defaults.speed = 1700;
		 		$(form).animate({
					height: 'show'
				}, 'slow');
                        $(form).highlightFade('paleyellow');
                  }  
           });
 
        } );
      // previewIt();
       idText.onkeyup = function() {previewIt(idText, preview);}
       quoteIt(form, quoters, idText, preview);      
      
    function previewIt(idText, preview) {
        var html = quotetext + idText.value;
        html = html.replace(/\n/g, "<br \/ >");
	preview.innerHTML = html;
    }
	$("linkerBox").css({ position: "absolute" });   
        $("#linker").click(function(e) {
                e.preventDefault();
		$("#linkerBox").animate({
			height: 'show'
		}, 'fast');              
	});
        $("#linkerBox .closer").click(function(e) {
                e.preventDefault();
		$("#linkerBox").animate({
			height: 'hide'
		}, 'fast'); 
                $("#id_text").val($("#id_text").val() + ' <a href="' + $("#url").val() + '">'+ $("#linktext").val() + '</a>')     
                previewIt(idText, preview);
                $("#id_text").focus();
	});
	//$("#qBox").css({ position: "absolute" });   
	$("#quoter").click(function(e) {
                e.preventDefault();
		$("#qBox").animate({
			height: 'show'
		}, 'fast');              
	});
        $("#qBox .closer").click(function(e) {
                e.preventDefault();
		qWho = "";
		$("#qBox").animate({
			height: 'hide'
		}, 'fast'); 
                if ($("#qWho").val() != "") {
                	qWho = '<em>' + $("#qWho").val() + ' said:</em>\n';
		}
                $("#id_text").val($("#id_text").val() + ' <blockquote><p>' + qWho +  $("#qSaid").val() + 
'</p></blockquote>')     
                previewIt(idText, preview);
                $("#id_text").focus();
	});
//	$("#picBox").css({ position: "absolute" });   
	$("#pixer").click(function(e) {
                e.preventDefault();
		$("#picBox").animate({
			height: 'show'
		}, 'fast');              
	});
        $("#picBox .closer").click(function(e) {
             e.preventDefault();
             picUrl = $("#picUrl").val();
             if (picUrl == "") {
		$("#picBox").animate({
			height: 'hide'
		}, 'fast');  	
             } else if (picUrl.toLowerCase().indexOf(".jpg") != -1 ||  picUrl.toLowerCase().indexOf(".gif") != -1 || picUrl.toLowerCase().indexOf(".png") !=-1) {
               
		$("#picBox").animate({
			height: 'hide'
		}, 'fast'); 
                
                $("#id_text").val($("#id_text").val() + ' <img src="' +  $("#picUrl").val() + '" alt="" />')
                previewIt(idText, preview);     
                $("#id_text").focus();
            } else {
              $("#picBox label").html( $("#picBox label").html() + '<br />Invalid picture format');
           }
	});
                
	$("#helper").toggle(function(e) {
		e.preventDefault();
		$("#helpBox").animate({
			height: 'show'
		}, 'slow');
		$("#helpBox").highlightFade('paleyellow');
	}, function() {
		$("#helpBox").animate({
			height: 'hide'
		}, 'slow');
	});


function quoteIt(form, quoters, idText, preview) {
    for (i=0;i<quoters.length;i++) {
       quoters[i].onclick = function() {
          post = document.getElementById(this.rel).cloneNode(true);
          post.removeChild(post.firstChild);
          quoted = this.title.replace("Quote ", "");
          quotetext = "<blockquote><p>"+ quoted + " wrote: " + post.innerHTML + "</p></blockquote> \n";
          preview.innerHTML = quotetext;
          setFocus(idText);
          return false         
       }
    }
    //form.onsubmit = function() {idText.value=preview.innerHTML;} 
}
 });

    //open links
     function openLinks() {
         $("div.post a").click(function(){window.open(this.href,'','resizable=yes, width=800, height=600, menubar=yes,scrollbars=yes,toolbar=yes'); return false;});
         //$("div.post").find("a").each(function(i){
             //$(this).title += " Opens in new window";
             //link = $(this);
             //$(this).appendChild (document.createTextNode ('»'));
             //newtarget= "window.open('" + link.href + "','','resizable=yes, width=800, height=600, menubar=yes,scrollbars=yes,toolbar=yes'); return false";
             //$(this).click(new Function(newtarget));
        //});
     }
