var deasil = {};

// This function needs to be modified to do any
//  desired per item processing and to add handling for other
//  fields.  The original pipes-pure.js handled only
//  title, link, and a pipe level maximum title attribue.
deasil.doit = function (json, main, num) {
   //var itemorig = $("[@class=deasil_pipe_item]", main);
   // Locate the deasil pipe item template belonging
   //  to the current pipe (main).
   
   	var itemorig = $(".deasil_pipe_item", main);
   
   	if( json.count == 0 ) 
   		{ return; }
   
   	for ( var i = 0; i < json.count; i++ ) 
	{
		var cur = json.value.items[i];
		
		addVideo(i, cur.link, cur.enclosure.url, cur.title, cur.description);
	}    
   
   // OLD CODE
   return;
   
   var itemorig = $(".deasil_pipe_item", main);
   
   if( json.count == 0 ) { return; }
   
   for( var i=0; i<json.count; i++ ) 
   {
	   // Clone the item so we can work with it.
      var item = itemorig.clone(1);
      var id = 'deasil_pipe_' + num + '_' + i;
      
      item.attr('id', id);
      
      // Append the item to the parent of itemorig which
      //  should be a containing DIV.
      itemorig.parent().append(item);

      var title = $('#' + id + ' .deasil_pipe_title');
      var link  = $('#' + id + ' .deasil_pipe_link');

      var cur = json.value.items[i];
      //title.append(cur.title);
      var fixedtitle;
      if( main.attr('deasil_max_title') ) { 
         var max = main.attr('deasil_max_title');
         fixedtitle = cur.title.length > max ? cur.title.substring(0,(max-2)) + '...' : cur.title;
      }
      else {
         fixedtitle = cur.title;
      }

      title.append(fixedtitle);
      link.attr('href', cur.link);
   }
   itemorig.css('display', 'none');
   main.css('display', 'block');
}

deasil.x = function () { alert('ho'); }

// This function finds all the pipes by their class attribute "deasil_pipe"
//  and makes AJAX call for each pipe.
deasil.pipe_init = function () {
   var mains = $('.deasil_pipe');
   deasil.pipe_callbacks = deasil.pipe_callbacks || new Array;
   
   mains.each( function (j) {
         var main = $(this);
         var params = {};
         var deasilre = /^deasil(x?)_(.*)$/;
	 var paramarr = new Array
         for( var i=0; i<this.attributes.length; i++) { 
            if( deasilre.test(this.attributes[i].name) ) {
               if( RegExp.$1 == 'x' ) {
		  paramarr.push(RegExp.$2 + '=' + encodeURIComponent(this.attributes[i].value));
               }
            }
         }

	 deasil.pipe_callbacks['cb' + j] = function (json_result) {
	    deasil.doit( json_result, main, j);
	 }
	 
	 var paramstr = paramarr.join('&');
	 var url = 'http://pipes.yahoo.com/pipes/' + this.attributes['deasil_pipe'].value + '/run?_render=json&_callback=deasil.pipe_callbacks.cb' + j + '&' + paramstr;
	 var script = document.createElement('script');
	 script.type = 'text/javascript';
	 script.src  = url;
	 document.body.appendChild(script);
	 //$('<script type="text/javascript" src="' + url + '"></script>').appendTo("body");
      }
   );
}

// Have the pipe initialization call trigger when the document
//  is ready.
$(document).ready(deasil.pipe_init);

// ================================ 

// Helper function to build the mini table for the submitting user name and notes.
function getMiniTable(videoTitle, youTubeURL, videoDescription)
{
	var tblHtml =
		"<TABLE ALIGN=CENTER VALIGN=TOP CELLPADDING=5 CELLSPACING=5 >\n" +
		"    <TR>\n" +
		"        <TD>\n" +
		"            <P><b>Title: </b><A href='" + youTubeURL + "' >" + videoTitle + "</A></P>\n" +
		"        </TD>\n" +
		"    </TR>\n" +
		"    <TR>\n" +
		"        <TD>\n" +
		"            <P><b>Description:</b> " + videoDescription + "</P>\n" +
		"        </TD>\n" +
		"    </TR>\n" +
		"</TABLE>\n";
		
	return tblHtml;					
} // function getShowLinkMiniTable()

// Makes an OBJECT block from a YouTube URL.
function makeYouTubeVideoObject(youTubeURL)
{
	var S =
		'<object width="425" height="350">'
		+ '<param name="movie" value="' + youTubeURL + '"></param>'
		+ '<param name="wmode" value="transparent"></param>'
		+ '<embed src="' + youTubeURL + '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed>'
		+ '</object>';
	return S;
}

function addVideo(itemNum, youTubeURL, swfURL, videoTitle, videoDescriptionIN)
{
	// Remove HTML from descripton.
	var videoDescription =
		 videoDescriptionIN.replace(/<[^>]*>/g, '');
		 
	// Replace any plain text links with placeholder.
	var videoDescription =
		 videoDescription.replace(/(http:\/\/.*?)(\s|$)/ig, ' (link) ');
		 
	// Truncate it at 512 characters.
	var max = 512;
	
	videoDescription =
		videoDescription.length > max ? videoDescription.substring(0,(max-2)) + '...' : videoDescription;
		 
	var tblPleoVideos = document.getElementById("tblPleoVideos");
	
	if (!tblPleoVideos)
	{
		alert("(addVideo) Unable to find the table for Pleo videos.");
		return;
	}
	
	// Video and description get their own row.
    var trVideo = tblPleoVideos.insertRow(tblPleoVideos.rows.length); 
    
    var tdVideo = trVideo.insertCell(-1);
    
    // Make the YouTube video block.
    // NOTE!:  For some reason IE, at least version 6.x, won't accept
    //  the object unless I encase it on paragraph tags.  Perhaps
    //  other tags would work too.  If this is not done, the
    //  assignment does nothing and innerHTML is empty.  FireFox
    //  does not have this problem.
    tdVideo.innerHTML = "<p>" + makeYouTubeVideoObject(swfURL) + "<p>";
    
    // Build the mini table.
    var miniTableHTML = 
    	getMiniTable(videoTitle, youTubeURL, videoDescription);
    	
    var tdDetails = trVideo.insertCell(-1);
    
	tdDetails.innerHTML = miniTableHTML;
    	
    /*     
	var tdLinkNum = trNew.insertCell(-1);
    tdLinkNum.style.width = "10%";
    tdLinkNum.style.verticalAlign = "top";	 
	tdLinkNum.innerHTML = "<p>" + linkNum + "</p>";
	
	var tdUserMiniTable = trNew.insertCell(-1);
    tdUserMiniTable.style.width = "70%";
    tdUserMiniTable.style.verticalAlign = "top";	 
	tdUserMiniTable.innerHTML = "<p>" + getShowLinkMiniTable(userName, notes) + "</p>";
	
	var tdLinkHtml = trNew.insertCell(-1);
    tdLinkHtml.style.width = "20%";
    tdLinkHtml.style.verticalAlign = "top";	 
	tdLinkHtml.innerHTML = "<p>" + linkHtml + "</p>";
	*/
	
} // function addVideo()


