﻿<!--
var cleanReturn = 1; //do you want a full youtube return, or just an image list
var inlineVideo = 1; //do you want to redirect to youtube, or play inlinevideo

var timer;
var i =0;
var youtubediv = new Array();

function clearList(ul){
	var list = document.getElementById(ul);
	while (list.firstChild) 
	 {
	    list.removeChild(list.firstChild);
	 }		
}

function hideOverlay(){

	var overlay = $('#youtubeoverlay');
	overlay.hide();
	overlay.innerHTML = "";
	var vid = $('#youtubecontent');
	vid.hide();
	vid.innerHTML = "";
	/*$('#youtubeoverlay').css({
	    display: 'none'
        });
    $('#youtubecontent').css({
	    display: 'none'
        });*/
         
	//vid.show();
	ovr.show();
}


var ovr;
var vid;

function videoOverlay(id){
	
	var objBody = document.getElementsByTagName("body").item(0);
	if(objBody){
    	
	    //ovr.innerHTML = '<div id="youtubecontent"><a href="javascript:hideOverlay()" id="close">Close</a><br /><object width="510" height="420"><param name="movie" value="http://www.youtube.com/v/'+id+'"></param><param name="autoplay" value="1"><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+id+'&autoplay=1" type="application/x-shockwave-flash" wmode="transparent" width="510" height="420"></embed></object></div>';
	    vid.innerHTML = '<div class="youtubecontentvid"><div class="close"><a href="javascript:hideOverlay()" id="galleryClose">Close</a></div><br /><object width="510" height="420"><param name="movie" value="http://www.youtube.com/v/'+id+'&rel=0"></param><param name="autoplay" value="1"><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+id+'&autoplay=1&rel=0" type="application/x-shockwave-flash" wmode="transparent" width="510" height="420"></embed></object></div>';
	    
	    $(vid).show();
	    $(ovr).show();
    	    
        var arrayPageSize = getPageSize();	    
        $('#youtubeoverlay').height(arrayPageSize[1]);
                
        var arrayPageScroll = getPageScroll();
	    var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	    var lightboxLeft = arrayPageScroll[0]; 
	    $('#youtubecontent').css({
	    
          top: lightboxTop + 'px'
        });
        
	}else{
		alert('no body element. please add');
	}

}

function mousOverImage(name,id,nr){

	if(name)
		imname = name;
	//make border orange
	imname.style.border = 	'0px solid orange';

	imname.src = "http://img.youtube.com/vi/"+id+"/"+nr+".jpg";
	nr++;
	if(nr > 3)
		nr = 1;
	timer =  setTimeout("mousOverImage(true,'"+id+"',"+nr+");",1000);

}


function mouseOutImage(name){

	if(name)
		imname = name;
	//make border back to greyish
	imname.style.border = 	'0px solid #333333';
	if(timer)
		clearTimeout(timer)

}

function getVideoId(url){

    //indexOf
    var sPos = url.indexOf("=") + 1;
    var ePos = url.indexOf("&");
    var s = url.substr(sPos, ePos - sPos);
    return s
    
}

function getId(string){

    var match = string.lastIndexOf("Uploads by ");
    if (match != -1) {
      id = string.substring(11);
      return id.toLowerCase();
    }
    
    var match = string.lastIndexOf("'s Videos");
    if (match != -1) {
      id = string.substring(0,match);
      return id.toLowerCase();
    }

    var match = string.lastIndexOf("query");
    if (match != -1) {
      id = string.substring(match+7);
      return id.toLowerCase();
    }

}
function listVideos(json,divid) {
  divid.innerHTML = '';
  var ul = document.createElement('ul');
  ul.setAttribute('id', 'youtubelist');
  if(json.feed.entry){
	  for (var i = 0; i < json.feed.entry.length; i++) {
	    var entry = json.feed.entry[i];
	
	    for (var k = 0; k < entry.link.length; k++) {   
	      if (entry.link[k].rel == 'alternate') {
	        url = entry.link[k].href;
	        break;
	      }
	    }
   	

   	    var thumb = entry['media$group']['media$thumbnail'][1].url;


        var li = document.createElement('li');
        if ((i + 1) % 4 == 0 && i > 0)
        {
            li.setAttribute('class', 'youtubebox end');
        }
        else
        {
            li.setAttribute('class', 'youtubebox');
        }
        
        
        if(cleanReturn == 1){

		    if(inlineVideo == 1){
        	    li.innerHTML = '<a href="javascript:videoOverlay(\''+getVideoId(url)+'\');"><img width="120" height="90" src="'+thumb+'" id="youtubethumb" alt="'+entry.title.$t+'"><br /><span>'+entry.title.$t+'</span></a>';
                
		    }else{

        	    li.innerHTML = '<a href="'+url+'"><img src="'+thumb+'" id="youtubethumb" alt="'+entry.title.$t+'"></a>';
		    }
        }else{
            li.innerHTML = entry.content.$t;
        }

        ul.appendChild(li);
	}
  }else{
  	divid.innerHTML = 'No Results Found';

  }

  document.getElementById(divid).appendChild(ul);
}

function youtubeInit(root) {
  //this hacks the layer for mutiple json queries
    var objBody = document.getElementsByTagName("body").item(0);
    if(objBody){
        ovr = document.createElement('div');
        ovr.setAttribute('id', 'youtubeoverlay');    
        objBody.insertBefore(ovr, objBody.lastChild);
            
        vid = document.createElement('div');
        vid.setAttribute('id', 'youtubecontent');    
        objBody.insertBefore(vid, objBody.lastChild);
    }
        
  id = getId(root.feed.title.$t);
  listVideos(root, youtubediv[id]);

}


function insertVideos(div,typ,q,results,overlay){
	

  inlineVideo = overlay;
  youtubediv[q.toLowerCase()] = div;

  var script = document.createElement('script');
  if(typ == "search")
  	script.setAttribute('src', 'http://gdata.youtube.com/feeds/videos?vq='+q+'&max-results='+results+'&alt=json-in-script&callback=youtubeInit');

  if(typ == "user")
  	script.setAttribute('src', 'http://gdata.youtube.com/feeds/users/'+q+'/uploads?max-results='+results+'&alt=json-in-script&callback=youtubeInit');

  if(typ == "playlist"){
	//doesn't function
	alert('oops.. working on it');
  	script.setAttribute('src', 'http://gdata.youtube.com/feeds/playlists/'+q+'?max-results='+results+'&alt=json-in-script&callback=youtubeInit');
  }

  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

//-->

