function VideoSearch() {
	
	this.searching = false;
	this.search_url = SITEURL+'ajax/video/VideoSearch.php';
	this.xmlData;
	this.paramObj;
	
	this.getSearchResult = function(paramObj1, func_name, show_ajax) {
				
		var param_string = '';
		paramObj = paramObj1;
		if (paramObj) {
			for (p in paramObj) {
				if (p != 'hide_nav' && p != 'key' && p != 'code') {
					param_string += '&'+p+'='+paramObj[p];
				}
			}
		}
		
		if ( show_ajax ) VideoSearch.show_ajax = show_ajax;
		
		$.ajax({
			
			dataType: "xml",
			type	: "POST",
			url	: VideoSearch.search_url,
			data	: { key:paramObj.key, video_id:paramObj.video_id, param_string:param_string, code:paramObj.code, width:400 },
			
			success	: function(xmlData) {

			  totalReturned = $('entry', xmlData).size();
    		if ( totalReturned < 1 ) {
    			
    			//VideoSearch.error('<br /><br />Your request did not return any videos.<br />Please try again.<br /><br />');
    			
    		}
    		else
    		{
					VideoSearch.xmlData = xmlData;
					
					paramObj.video_id = $('id', xmlData).text();
					

					//Temp fix
					paramObj.key = VideoSearch.youtube_key;
					VideoSearch.paramObj = paramObj;
					eval( func_name );
				}
				
			}
			
		});
		
	}
	
	this.setContainer = function(id, format) {
		
		VideoSearch.container_id 	= id;
		VideoSearch.format 			= format;
		
		$('<div />').attr('id', 'VideoSearch_searchStatus').addClass('VideoSearch_status').html('<br />Searching...<br /><img src="'+IMAGE_URL+'/ajaxload.gif" /><br />This could take a sec...').appendTo('#'+id);
		
		
		$('#VideoSearch_searchStatus').ajaxStart(function() {
							
			$('#VideoSearch_' + VideoSearch.show_ajax).hide();
			$('#VideoSearch_' + VideoSearch.show_ajax + 'Status').show();
			
		});
		
		$('#VideoSearch_searchStatus').ajaxComplete(function() {
			
			$('#VideoSearch_' + VideoSearch.show_ajax).show();
			$('#VideoSearch_' + VideoSearch.show_ajax + 'Status').hide();
			VideoSearch.show_ajax = null;
			
		});
		
		$('#VideoSearch_searchStatus').ajaxError(function() {
			
			$('#VideoSearch_' + VideoSearch.show_ajax).show();
			$('#VideoSearch_' + VideoSearch.show_ajax + 'Status').hide();
			VideoSearch.show_ajax = null;
			
		});
				
	}
	
	this.preview = function() {
		
		xmlData = VideoSearch.xmlData;
						
		var previewWin = DD.createDraggable({width:420, height:410, title:$('title:first', xmlData).text(), curtain:true, color:'white'});
		
		var html = '<br />' + decodeBase64($('embed_code', xmlData).text()) + '<br />';
		html += '<div style="text-align:center; padding-top:10px;">';
		html += '<a href="#" onClick="DD.destroy(\''+previewWin.attr('id')+'\'); return false;"><img src="'+IMAGE_URL+'/btn_cancel.gif" /></a>&nbsp;&nbsp;';
		html += '<a href="#" onClick="DD.destroy(\''+previewWin.attr('id')+'\', true); VideoSearch.addVideo(); return false;"><img src="'+IMAGE_URL+'/video/btn_addvideo.gif"></a>';
		html += '</div>';
			
		$('#'+previewWin.attr('content_id')).html( html );
		
			
	}
	
	this.per_page = 10;
	this.curr_page = 1;
	
	this.youtube_key = 'youtube';
	this.myspace_key = 'myspace';
	this.videoegg_key = 'videoegg';
	
	this.populateResults = function() {
		xmlData = VideoSearch.xmlData;
		paramObj = VideoSearch.paramObj;
				
		prefix = 'VS';
				
		//dump video_list from xml into an object
		var videoList = $(xmlData);
		//var videoList = $('ut_response > video_list', xmlData);
		
		totalReturned = $('entry', videoList).size();
		if ( totalReturned < 1 ) {
			
			//VideoSearch.error('<br /><br />Your request did not return any videos.<br />Please try again.<br /><br />');
			
		}
		else {
			
			//Remove old search results, if they exist, and make new searchResults div
			$('#VideoSearch_search').remove();
			var searchResults = $('<div />').attr('id', 'VideoSearch_search');
			searchResults.appendTo( '#' + VideoSearch.container_id );
			
			//Make div to hold page nav & result information
			if (totalReturned > VideoSearch.per_page)
			{ 
				totalReturned = VideoSearch.per_page;
			}
			var first = ( (VideoSearch.curr_page - 1) * VideoSearch.per_page ) + 1;
			var last = first + totalReturned - 1;
			var prev = '';
			
			if (VideoSearch.curr_page > 1 && !paramObj.hide_nav) {
				prev = '<a href="#" onClick="VideoSearch.curr_page--; VideoSearch.getSearchResult({method:\''+paramObj.method+'\', tag:\''+paramObj.tag+'\', page:VideoSearch.curr_page, per_page:VideoSearch.per_page, key:\''+paramObj.key+'\'}, \'VideoSearch.populateResults()\', \'search\'); return false;">&laquo;prev</a>';
			}
			
			var next = '';
			if( $("link[rel='next']", videoList).length )
			{
			//if theres a next (fix me with link tag)
				var next = '<a href="#" onClick="VideoSearch.curr_page++; VideoSearch.getSearchResult({method:\''+paramObj.method+'\', tag:\''+paramObj.tag+'\', page:VideoSearch.curr_page, per_page:VideoSearch.per_page, key:\''+paramObj.key+'\'}, \'VideoSearch.populateResults()\', \'search\'); return false;">next&raquo;</a>';
			}
			var nav_div = $('<div />').attr('id', prefix+'_nav').css('font-weight', 'normal').html(prev + '&nbsp;&nbsp;Results '+first+'-'+last+'&nbsp;&nbsp;' + next);
			nav_div.addClass('VideoSearch_nav');
			nav_div.appendTo(searchResults);
			
			
			VideoSearch.resultsList = new Array();
			
			//make a div for each video returned in the result, and append to searchResult div
			$('entry', videoList).each(function(i) {
			  if (i > VideoSearch.per_page) {
					return false;
				}

				if ( $.browser.msie ) 
				{
  				var thumbnail = $('media\\:thumbnail:first', this).attr('url');		  
				}
				else
				{
  				var media = this.getElementsByTagNameNS( 'http://search.yahoo.com/mrss/' ,'thumbnail' );	
  				var thumbnail = media.item(0).getAttribute('url');				  
				}
					var res = $('<div />').attr('id',prefix+'_result_'+i);
					
					var html = '<table cellpadding="0" cellspacing="2" style="border-bottom:#CCC 1px solid; width:100%;">';
					html += '<tr>';
					html += '<td style="width:60px;"><a href="#" onClick="VideoSearch.getSearchResult({method:\'youtube.videos.get_details\', key:\''+paramObj.key+'\', video_id:\''+$('id', this).text()+'\'}, \'VideoSearch.preview()\'); return false;"><img src="'+thumbnail+'" class="VideoSearch_result_img" /></a></td>';
					html += '<td style="padding-left:3px; text-align:left;">';
  				if( $('id', this).text() != 'myb_banned' )
  				{
  					html += '<a href="#" onClick="VideoSearch.getSearchResult({method:\'youtube.videos.get_details\', key:\''+paramObj.key+'\', video_id:\''+$('id', this).text()+'\'}, \'VideoSearch.preview()\'); return false;" class="normaltextlinkbold" style="color:#000;">';
  				}
					html += ( $('title:first', this).text() ).substring(0, 50);
  				if( $('id', this).text() != 'myb_banned' )
  				{
					  html += '</a>';
  				}
					html += '</td>';
					if (VideoSearch.format == 'small') {
						html += '</tr><tr>';
					}
					html += '<td';
					if (VideoSearch.format == 'small') {
						html += ' colspan="2" style="text-align:left;"';
					}
					else if (VideoSearch.format == 'medium') {
						html += ' style="text-align:left; padding:5px;';
						if ( $.browser.msie ) {
							html += ' width:90px;"';
						}
						else {
							html += ' width:80px;"';
						}
					}
					else {
						html += ' style="text-align:left; width:180px;"';
					}
					html += '>';
					if( $('id', this).text() != 'myb_banned' )
					{
					  html += '<img id="VideoSearch_preview'+i+'" src="'+IMAGE_URL+'/btn_preview.gif" onClick="VideoSearch.getSearchResult({method:\'youtube.videos.get_details\', key:\''+paramObj.key+'\', video_id:\''+$('id', this).text()+'\'}, \'VideoSearch.preview()\'); return false;" style="cursor:pointer;">&nbsp;';
					  if (VideoSearch.format == 'medium') html += '<br>';
					  html += '<img id="VideoSearch_add'+i+'" src="'+IMAGE_URL+'/video/btn_addvideo.gif" onClick="VideoSearch.getSearchResult({method:\'youtube.videos.get_details\', key:\''+paramObj.key+'\', video_id:\''+$('id', this).text()+'\'}, \'VideoSearch.addVideo('+i+')\'); return false;" style="cursor:pointer;">';
					}
					html += '</td>';
					
					res.html(html).appendTo(searchResults);
					
				
			});
			
			
			nav_div.clone().appendTo(searchResults);
			
			//fancy animation, yay
			$('#VideoSearch_searchResults div').hide();
			$('#VideoSearch_searchResults div').slideDown('slow');
			
		}
		
	}
	
	this.get_categories_url = SITEURL+'ajax/video/get_categories.php';
	var detailsWin;
	this.addVideo = function(i) {

	  var xmlData = VideoSearch.xmlData;
    if ( $.browser.msie ) 
    {
      var category = $('media\\:category', xmlData).text();
      var duration = $('yt\\:duration', xmlData).attr('seconds');
      var description = $('media\\:description', xmlData).text();
      var keywords = $('media\\:keywords', xmlData).text();
    }
    else
    {
      var category = xmlData.getElementsByTagNameNS( 'http://search.yahoo.com/mrss/', 'category' ).item(0).getAttribute('label');	
      var duration = xmlData.getElementsByTagNameNS( 'http://gdata.youtube.com/schemas/2007', 'duration' ).item(0).getAttribute('seconds');	
      var description = xmlData.getElementsByTagNameNS( 'http://search.yahoo.com/mrss/', 'description' )[0].childNodes[0].nodeValue;
      var keywords = xmlData.getElementsByTagNameNS( 'http://search.yahoo.com/mrss/', 'keywords' )[0].childNodes[0].nodeValue;
    }
    detailsWin = DD.createDraggable({width:420, height:275, title:'Add Video: Please Enter Details', curtain:true, color:'white'});
		var cat_result = VideoSearch.getCategoriesSelect( VideoSearch.youtube_categories[category] );
		var minutes = Math.floor(duration/60);
		var seconds = Math.floor(duration % 60);
		if (seconds < 10) seconds = '0' + seconds;
		duration = minutes + '.' + seconds;
		var title = $('title:first', xmlData).text().replace('"', '');
		var html = '<div style="padding:5px; font-weight:bold;">Adding "' + title + '"';
		html += '<br /><br />';
		html += '<form action="#" onsubmit="return false;">';
		html += '<table cellpadding="0" cellspacing="3">';
		html += '<tr>';
		html += '<td style="text-align:right; width:100px;">Title:</td>';
		html += '<td style="text-align:left; width:280px;">';
		html += '<input type="text" id="addvideo_title" name="addvideo_title" value="' + title + '" style="vertical-align:middle; font-size:12px; width:250px;">';
		html += '</td>';
		html += '</tr><tr>';
		html += '<td style="text-align:right;">Description:</td>';
		html += '<td style="text-align:left;"><textarea id="addvideo_desc" name="addvideo_desc" style="width:250px; height:70px;">' + description + '</textarea>';
		html += '</tr><tr>';
		html += '<td style="text-align:right;">Category:</td>';
		html += '<td style="text-align:left;">'+cat_result+'</td>';
		html += '</tr><tr>';
		html += '<td style="text-align:right;"></td>';
		html += '<td style="text-align:left; padding-top:5px;"><label><input type="checkbox" name="addvideo_bulletin" id="addvideo_bulletin" checked="checked" style="margin:0px; vertical-align:middle;"> Post a myYearbook bulletin</label></td>';
		html += '</table>';
		html += '<br /><br />';
		html += '<a href="#" onClick="DD.destroy(\''+detailsWin.attr('id')+'\', true); VideoSearch.preview(); return false;"><img src="'+IMAGE_URL+'/btn_preview.gif" style="vertical-align:middle; float:left;" /></a>';
		html += '<a href="#" onClick="VideoSearch.submitVideo( \''+paramObj.video_id+'\', \''+duration+'\', \''+paramObj.key+'\', true, $(\'#addvideo_title\').val(), $(\'#addvideo_desc\').val(), $(\'#addvideo_categories\').val(), \'' + escape( keywords ) + '\', $(\'#addvideo_bulletin\').attr(\'checked\') ); return false;"><img src="'+IMAGE_URL+'/btn_submit2.gif" style="vertical-align:middle; float:right; margin-left:3px;" /></a>';
		html += '<a href="#" onClick="DD.destroy(\''+detailsWin.attr('id')+'\'); return false;"><img src="'+IMAGE_URL+'/btn_cancel.gif" style="vertical-align:middle; float:right;" /></a>';
		html += '</form></div>';
			
					
		$('#'+detailsWin.attr('content_id')).html( html );
		
	}	
	
	
	
	this.myb_categories = new Array();
	this.myb_categories[1] = 'Music';
	this.myb_categories[2] = 'Comedy';
	this.myb_categories[3] = 'Sports';
	this.myb_categories[4] = 'Entertainment';
	this.myb_categories[5] = 'Animals';
	this.myb_categories[6] = 'Cars';
	this.myb_categories[7] = 'Video Games';
	this.myb_categories[8] = 'News';
	this.myb_categories[9] = 'Travel';
	this.myb_categories[10] = 'People';
	this.myb_categories[11] = 'Art';
	this.myb_categories[12] = 'Other';
	
	//Contains the YouTube category as the key, and the MYB category_id it should map to.
	this.youtube_categories = new Array();
	this.youtube_categories['Music'] = 1;
	this.youtube_categories['Comedy'] = 2;
	this.youtube_categories['Sports'] = 3;
	this.youtube_categories['Entertainment'] = 4;
	this.youtube_categories['Pets & Animals'] = 5;
	this.youtube_categories['Autos &  Vehicles'] = 6;
	this.youtube_categories['Gadgets & Games'] = 7;
	this.youtube_categories['News & Politics'] = 8;
	this.youtube_categories['Travel & Places'] = 9;
	this.youtube_categories['People & Blogs'] = 10;
	this.youtube_categories['Film & Animation'] = 11;
	
	this.getCategoriesSelect = function(selected) {
		
		if (isNaN(selected)) selected = 12;
		var cat = '<select id="addvideo_categories" name="addvideo_categories">';
		for (i=1; i<=VideoSearch.myb_categories.length; i++) {
			if (VideoSearch.myb_categories[i]) {
				cat += '<option value="'+i+'"';
				if (i == selected) {
					cat += ' selected ';
				}
				cat += '>'+VideoSearch.myb_categories[i]+'</option>';
			}
		}
		cat += '</select>';
		return cat;
		
	}
	
	this.post_action = ' location.href=""; ';
	this.submitVideo_url = SITEURL + 'ajax/video/ajax_upload_video.php';
	this.submitVideo = function(videoPath, videoDuration, videoSource, add_to_playlist, title, desc, cat, tags, bulletin) {
		
		if ( $.trim(videoPath)=='' || $.trim(videoDuration)=='' || $.trim(videoSource)=='' || $.trim(title)=='' || $.trim(desc)=='' || $.trim(cat)=='' ) {
			alert('Please complete all fields.');
		}
		else {
			if (add_to_playlist) {
				add_to_playlist = 2;
			}
			else {
				add_to_playlist = 0;
			}
			
			if (!tags) tags = '';
			
			$('#'+detailsWin.attr('content_id')).html('<br /><br /><br /><br /><img src="' + IMAGE_URL + '/ajaxload_circ2.gif"><br />Please Wait...');

			$.ajax({
				
				dataType: "html",
				type	: "POST",
				url	: VideoSearch.submitVideo_url,
				data	: { path:videoPath, duration:videoDuration, source:videoSource, vp:add_to_playlist, title:title, desc:desc, category:cat, tags:tags, bulletin:bulletin },
				
				success	: function(htmlResult) {
					
					DD.destroy('#'+detailsWin.attr('id')); 
					eval(VideoSearch.post_action);
					
				}
				
			});
		}
		
	}
	
	
	this.error = function(error_str) {
		
		DD.alertBox(error_str);
	
	}
}

var VideoSearch = new VideoSearch();
