// JavaScript Document
jQuery.noConflict();
jQuery(function($) {

  function matchService(siteURL){
  	if ( (siteURL.indexOf('youtube.com',0) > -1))
  	 return "http://www.youtube.com/oembed?url="+siteURL+"&format=json";    
  	else if ((siteURL.indexOf('flicker.com',0) > -1))
  	 return "http://flickr.com/services/oembed?url="+siteURL+"&format=json";     
  	else if ((siteURL.indexOf('vimeo.com',0) > -1))  	 
  	 return "http://vimeo.com/api/oembed.json?url="+siteURL; 	 
  	else 
  	 return false; 
  }

	jQuery('#makeLive').click(function() {
  	var url =  jQuery("input#url").val();
  	var userid =  jQuery("input#userid").val();
  	/* var site = matchService(url); */
  	var title = jQuery("input#title").val();  	  	
  	if (title == "" && url == "") {
      alert("No go captain");  	
  	} else {
      jQuery.ajax({
        type: "GET",
        data: "task=proxy&url="+url+"&title="+title+"&userid="+userid,
        url: "/wp-content/plugins/showboxv1.0/showboxv1.0.php",
        dataType: "json",
        success: function (data) {
/*           alert(data.result); */
/*           $("td#determine").html(data.result); */
        if (data == "An error has occurred. Can't read the website provided") {
/*           $("td#determine").html(data.result); */
            alert("An error has occured");
        } else {
          $("td#liveTable").prepend(data.result);
          $("td#determine").html("");  
        }
       }
       });    
      }
  });
  
  	jQuery('a.feedActionDIV').click(function(e) {
  	  e.preventDefault();
    	var rowID = $(this).attr('bar');
 /*    	$(this).previous('#feedtable').remove(); */
      jQuery.ajax({
        type: "GET",
        data: "task=delete&rowID="+rowID,
        url: "/wp-content/plugins/showboxv1.0/showboxv1.0.php",
        dataType: "json",
        success: function (data) {
          $("td#determine").html(data); 
          location.reload(true); 
         }
       });    
    });
  
	jQuery('a.comment').live("click", function(){
    var postID = jQuery(this).attr('postid'); 
    jQuery('div').remove('.inline-1').remove('.latestComments');
    var comments = "";
    jQuery.ajax({
        type: "GET",
        data: "task=message&postID="+postID,
        url: "/wp-content/plugins/showboxv1.0/showboxv1.0.php",
        dataType: "json",
        success: function (data) {
          jQuery.each(data.result, function(i,item) {	  
            comments += "<div style='padding: 5px;' class='latestComments'>&raquo; "+item.value+"</div>"
          });	            
        },

        async: false
     });    
    jQuery("div.prevComment_"+postID).append(comments);
    
    //must be logged in to add a comment
    if (jQuery("div.commentBox_"+postID).length != 0)
    {
      jQuery("div.commentBox_"+postID).append('<div class="inline-1 content" style="display: none;"><form id="commentForm"><div class="comments"></div><table id="dataCap" style="width: 300px"><input type="hidden" value="'+postID+'" id="postid"/><tr><td colspan="2"><textarea cols="42" rows="5" id="message">have your say!</textarea></td></tr><tr><td><input type="submit" id="commentSave" value="Express yourself!"/></td><td><span id="inline-close">hide this</span></td><tr></table></form></div>').slideDown(); 
    }
    else
    {
      var top = jQuery('#rightcolumn #table'+postID).position().top;
      var left = 120;
      var offsetTop = jQuery('#table'+postID).offset().top;
      if (offsetTop < 410)
        top += 70;
      else
        top -= 10;
      $('#loginNeededContainer').html('You need to be logged in to comment on this post').show().css({ left: left+'px', top: top+'px'});
      jQuery(this).oneTime(2000, function() {
        $('#loginNeededContainer').fadeOut(2000);
      });
    }
    jQuery(".inline-1").animate({ 
      height: 'toggle', 
      opacity: 'toggle' 
    }, "slow", function() {
      if (jQuery('#rightcolumn').length != 0)
        jQuery('#rightcolumn').jScrollPane();    
    });

  });  
  
  
  	/*jQuery('a.like').live("click", function(){
    var text = jQuery(this).text(); 
    var todo = text;
    var postID = jQuery(this).attr('postid'); 
    var comments = "";
    jQuery.ajax({
        type: "GET",
        data: "action=like&postID="+postID+'&todo='+todo,
        //url: "/wp-admin/admin-ajax.php",
         url: "/wp-content/plugins/showboxv1.0/showboxv1.0.php",
        dataType: "json",
        success: function (data) {
          alert('test');
          //alert(data);
          if (action == 'like')
          {
            alert('You have liked this post');
            jQuery(this).text('unlike');
          }
          else
          {
            alert('You have unliked this post'); 
            jQuery(this).text('like');
          }
         },

        async: false
     });    
  }); */
  
  
  jQuery('#commentForm').live("submit",function(event){    
      event.preventDefault(); 
      var form = jQuery(this);
      var message = form.find( ":input#message" ).val();
      var postID = form.find( ":input#postid" ).val();
      if (message != ""){
        jQuery.ajax({
          type: "GET",
          data: "task=comment&message="+message+"&postID="+postID,
          url: "/wp-content/plugins/showboxv1.0/showboxv1.0.php",
          dataType: "json",
          success: function (data) {
//            form.parent().animate({ 
//              height: 'toggle', opacity: 'toggle' 
//            }, "slow", function() {
              if (jQuery('#rightcolumn').length != 0)
                jQuery('#rightcolumn').jScrollPane();
//            });
            jQuery("div.prevComment_"+postID).append("<div style='padding: 5px;' class='latestComments'>&raquo; "+message+"</div>");         
            jQuery("span#totalComments_"+postID).text(data.result);
          }
        });        
      } else {
        alert("Please enter a comment.");
      }
   });  
   
   $("textarea#message").live("click", function() {
     $(this).html("");
   });
   
   $("#inline-close").live("click", function() {
     jQuery('div').remove('.inline-1').remove('.latestComments');
     if (jQuery('#rightcolumn').length != 0)
     {           
       jQuery('#rightcolumn').jScrollPaneRemove();
       jQuery('#rightcolumn').jScrollPane();
     }
   });
   
	jQuery("a[rel^='prettyPhoto']").prettyPhoto();
 	
});