document.write('<script type="text/javascript" src="/js/jsonparser.js"></script>');

var GoogleAPI = new Object();

GoogleAPI.SearchVideos = function()
{
	//GoogleAPI.setResultSetSize(8);
	GoogleAPI.callback = GoogleAPI.onVideosSearched;
	var gateway = "/service/gateway.php?feed="+ escape("http://ajax.googleapis.com/ajax/services/search/video?v=1.0&key=ABQIAAAANzgCX9vTDc9i5tHSNWpG3xR6bna0IOKjjDf3pjEGlCLTVUhDhhTkBeFeX3SOre69BEzf9hZyOBCfcA&rsz=small&start="+ GoogleAPI.Index +"&q=")+ GoogleAPI.Query;
	GoogleAPI.makeRequest(gateway);
	
}

GoogleAPI.onVideosSearched = function(request)
{	
	
	var divVideos= document.getElementById('gvideo');
	divVideos.innerHTML = '';
	
	if(this.request.responseText)
	{
		var gvideos = this.request.responseText.parseJSON();
		var results = gvideos.responseData.results;
		
		
		if(results.length > 0)
		{	
			var videoPlayer = document.createElement('div');
			

			videoPlayer.id = "videoPlayer";
			divVideos.appendChild(videoPlayer);
			for(var i=0; i<results.length; i++)
			{
				if(i == 0) GoogleAPI.loadVideo(results[i].playUrl);
				
				var videoItem = document.createElement('div');
				videoItem.className = 'videoItem';
				videoItem.videoUrl = results[i].playUrl;
				videoItem.onclick = function() { GoogleAPI.loadVideo(this.videoUrl); }
				videoItem.innerHTML = '<div class="videoImage" style="background: url('+ results[i].tbUrl +') center;"></div>';
				videoItem.innerHTML += '<h1 class="title">'+ results[i].titleNoFormatting +'</h1>';
				videoItem.innerHTML += '<em class="date">'+ results[i].published +'</em>';
				
				divVideos.appendChild(videoItem);
			}
			
			var moreBtn = document.createElement('a');
			moreBtn.href = "#"+(GoogleAPI.Index+4);
			moreBtn.innerHTML = "<font color=yellow><b>More Videos &gt;&gt;<b></font>&nbsp;";
			moreBtn.onclick = function() { GoogleAPI.Index+=4; GoogleAPI.SearchVideos(); }
			divVideos.appendChild(moreBtn);
		}
		else GoogleAPI.ThrowError("There were no video results");
		
	}
	else GoogleAPI.ThrowError("There was an error receiving the videos feed");
}

GoogleAPI.loadVideo = function(videoUrl)
{
	var videoPlayer = document.getElementById('videoPlayer');
	videoPlayer.innerHTML = '<object width="635" height="420" style="position:relative;left:-35px;"><param name="movie" value="'+videoUrl+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+videoUrl+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="635" height="420"></embed></object>' +
	                        '<div style="height:2px;"></div><center>' + 
	                       /*  '<iframe name="headerad" width="468" height="60" src="/includes/adserver/google/468x60.php" frameBorder="0" marginWidth="0" marginHeight="0" scrolling="no">' +  */
	                        '</center><br>';

	                        
}

GoogleAPI.makeRequest = function(gw)
{
	this.request = (window.XMLHttpRequest) ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); 
	this.request.onreadystatechange = function() { GoogleAPI.checkReadyState(); };
	this.request.open('GET', gw, true);
	this.request.send(gw);
}

GoogleAPI.ThrowError = function(msg)
{

  var videoPlayer = document.getElementById('videoPlayer');
	videoPlayer.innerHTML = '<div style="height:50%;width:50%;">' + msg + '</div>';
}
	
GoogleAPI.checkReadyState = function()
{
	switch(this.request.readyState)
	{
		case 1: break;
		case 2: break;
		case 3: break;
		case 4:
			this.callback(this.request);
	}
}

