// JScript File

// How to search through a YouTube channel aka http://www.youtube.com/members

google.load('search', '1');

function OnLoad() {

    // create a search control
    var searchControl = new google.search.SearchControl();

    // So the results are expanded by default
    options = new google.search.SearcherOptions(false, "video_feed");
    options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

    // Create a video searcher
    var videoSearch = new google.search.VideoSearch();

    // Set the result order of the search - check docs for other orders
    videoSearch.setResultOrder(google.search.Search.ORDER_BY_DATE);

    // Add our searcher to the control
    searchControl.addSearcher(videoSearch, options);
    
    // Sets where the vids will load to 
    //searchControl.setLinkTarget(google.search.Search.LINK_TARGET_PARENT);
    
    searchControl.setSearchCompleteCallback(this, youtubeLoaded);

    // Draw the control onto the page
    searchControl.draw(document.getElementById("video_feed"));

    // Search for a YouTube channel
    searchControl.execute("ytchannel:Cogmation");

	// hide crap we don't need
    $("#video_feed table.gsc-search-box").hide();
    $("#video_feed table.gsc-search-box").css("width", "0px");
    $("table.gsc-branding").css("float", "left");
    
    $(".gsc-twiddleRegionCell").hide();
    $("td.gsc-configLabelCell").hide();

    $(".gsc-branding-user-defined").css("width", "152px");
    
    $(".gsc-branding-text").css("text-align", "left");
    $(".gsc-branding-text").css("width", "85px");
    $("td.gsc-branding-img").css("width", "60px");
}

function youtubeLoaded(){
	var count = $(".gs-title").size();
	    
	$("div.gs-videoResult.gs-result").each(function(){

		//title now will popup window instead of redirect to youtube 
		
		$("div.gs-title", this).attr("onClick", "launchPopup( '" + $("a.gs-title", $("div.gs-title", this)).attr("href") + "' )");
		$("div.gs-title", this).text( $("a.gs-title", $("div.gs-title", this)).text() );
		
		$("div.gs-title", this).remove("a.gs-title");
		
		//redirect image link
		
		var div = $("div.gs-image-box", this).append("<div class='videoLink'></div>");
		
		$("div.videoLink", this).append( $("img.gs-image", this) );  
		$(this).remove("a.gs-image");
		
		$("div.videoLink", this).attr("onClick", $("div.gs-title", this).attr("onClick") );

	});
	

	//remove all links to youtube
	$("a.gs-title").attr("href", "");
	$("a.gs-image").attr("href", "");
	$("div.gsc-cursor-box").css("border-top", "none");
}

function linkVideo(URL){
	//populate popup with the youtube video
	
	URL = URL.replace("http://www.youtube.com/watch?v=", "");
	URL = URL.replace("&feature=youtube_gdata", "");
	
	// The video to load.
	var videoID = URL;
	// Lets Flash from another domain call JavaScript
	var params = { allowScriptAccess: "always" };
	// The element id of the Flash embed
	var atts = { id: "flashPlayer" };
	// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
	swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "?version=3&enablejsapi=1&playerapiid=player1", 
	                   "flashPlayer", "480", "295", "9", null, null);                   
    
}

  google.setOnLoadCallback(OnLoad);

  //ugly hack
  setTimeout(OnLoad,650);
