	var bcExp;
	var modVP;
	var modExp;
	var modContent;
	var modSocial;
	var adModule;
	var filteredItems;
	var searchTerm;
	
	swfobject.addDomLoadEvent(createBcPlayer);
	
    kudos.token = "sO41DP-z_d48omAJKISz-CnQGAbMOvC8-n4iQo3vwXs.";
    kudos.cb = "bcCustomHandleSearch";

	function init()
	{
		//console.log("init", $('#thumb a').attr("id"));	
				 
		applyHover();
				 		 
		// Play first video
		bcCustomLoadById($('#thumb a').attr("id"));
	}
	
	function applyHover()
	{
		// jQuery animation
		var originalBG = $(".hoverItem").css("background-color");
	  	var fadeColor = "#FFFFAA"; 
		
		// Hover
		$(".hoverItem").hover( function () {
		    $(this).animate( { backgroundColor:fadeColor}, 150 );
		  },
		  function () {	  	
		   $(this).animate( { backgroundColor:originalBG}, 150 );
		  }
		 );
	}
	
	function bcCustomLoadBySearch()
	{      
		//console.log("bcCustomLoadBySearch");
                
        filteredItems = new Array();
        searchTerm = $("#vidSearchText").val();
		  
        kudos.get("find_videos_by_text", {	"text": searchTerm,
        									"get_item_count": true,
		        							"page_size": 100,
		        							"page_number": 0 });
                
		$("#bcPlayerTitle").html("Searching '" + searchTerm + "'..."); 
	}

	function bcCustomHandleSearch(response) 
	{
		//console.log("bcCustomHandleTags", response);	
		
		// test the results
		if(response.items.length > 0){
			filterResults(response.items);
			if(response.items.length + (response.page_size*response.page_number) == response.total_count){
				displayResults(filteredItems);
			} else {				  
		        kudos.get("find_videos_by_text", {	"text": searchTerm,
		        									"get_item_count": true,
		        									"page_size": 100,
		        									"page_number": response.page_number+1 
		        									 });
			}
		} else {
			$("#content").html("No results returned");
		}
     }
     
    function filterResults(responseItems)
    {
		//console.log("filterResults");	
		
		/* Filter the results
		 * 
		 * Due to Brightcove inadequacey we cannot filter search results by tags. Therefore we have
		 * to filter and hack the search functions.
		 */		
		for (var i=0; i<responseItems.length; i++) 
		{				
			var tagMatch = false;
			var itemRef;
			
			// search current tags against item tags	
			for (var n=0; n<responseItems[i].tags.length; n++) 	
			{
				itemRef = responseItems[i];
				if(itemRef.tags[n] == "Borders"){
					tagMatch = true;
					break;
				}
			}
			
			// if the any tag matches store
			if(tagMatch == true) filteredItems.push(itemRef); 
		}
    }
    
    function displayResults(displayItems)
    {
		//console.log("displayResults");	
		
    	if(displayItems.length > 0){
			// Display the filtered results
			var newHtml = "";					
	       	var count = 1;
	       	
	       	for(var i=0; i < displayItems.length; i++)
			{
			    var video = displayItems[i];     	        	
				var linkStart = "<a href=\"#\" onclick=\"bcCustomLoadById(" + video.id + ")\" title='" + video.name + "' class=\"thumbnailTooltip\">";
				var linkEnd = "</a>";
				
	              if(count == 1) newHtml += "<div id=\"newRow\" class=\"dottedRow\">";
	                            
	              newHtml +=  "<div id=\"thumbRow\" class=\"hoverItem\">";
	              newHtml +=  "<div id=\"thumb\">";
	              if(video.thumbnailURL) newHtml +=  linkStart + "<img src=\"" + video.thumbnailURL + "\" width=\"142px\" height=\"91px\" />" + linkEnd;
	              newHtml +=  "&nbsp;</div>";
	              newHtml +=  "<div id=\"thumbName\" class=\"title2\">" + linkStart + video.name + linkEnd + "</div>";
	              newHtml +=  "<div id=\"thumbDesc\">" + video.shortDescription + "</div>";
	              newHtml +=  "</div>";
	              
	              if(count == 4 || i == displayItems.length-1) newHtml += "</div>";
	              count = (count < 4)? count = count+1 : count=1;          		
	       	}
	        
	        $("#bcPlayerTitle").html("Search results for '" + $("#vidSearchText").val() + "'..."); 
	        $("#content").html(newHtml);
	        
	        applyHover();
    	} else {
    		$("#content").html("No results returned");
    	}
    }
     
	function createBcPlayer()
	{
		//console.log("createBcPlayer");							
				
		// BC Player
		var bc_config = {};
	
		bc_config["id"] = "myExperience";
		bc_config["bgcolor"] = "#CCC";
		bc_config["width"] = 480;
		bc_config["height"] = 325;
		bc_config["playerId"] = 18696242001;
		bc_config["publisherID"] = 495713462;
		bc_config["@videoPlayer"] = null;
		bc_config["allowScriptAccess"] = "always";
	
		var myPlayer = new BCPlayer(bc_config, "bcPlayer");
	
		myPlayer.create();
	}
	
	// Fired when the BC Player loads
	function onTemplateLoaded(experienceID)
	{
		//console.log("onTemplateLoaded", experienceID);
		
		bcExp = brightcove.getExperience(experienceID);
		
		modVP = bcExp.getModule(APIModules.VIDEO_PLAYER);
		modExp = bcExp.getModule(APIModules.EXPERIENCE);
		modContent = bcExp.getModule(APIModules.CONTENT);
		modSocial = bcExp.getModule(APIModules.SOCIAL);
		
		modExp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
		modExp.addEventListener(BCExperienceEvent.CONTENT_LOAD, onContentLoad);
		
		modContent.addEventListener(BCContentEvent.VIDEO_LOAD, onVideoLoad);	
				
		modVP.addEventListener(BCMediaEvent.BEGIN, onMediaBegin);
		//modVP.addEventListener(BCMediaEvent.CHANGE, onMediaChange);
		//modVP.addEventListener(BCMediaEvent.COMPLETE, onMediaComplete);
		//modVP.addEventListener(BCMediaEvent.MUTE_CHANGE, onMediaMuteChange);
		//modVP.addEventListener(BCMediaEvent.PLAY, onMediaPlay);
		//modVP.addEventListener(BCMediaEvent.PROGRESS, onMediaProgress);
		//modVP.addEventListener(BCMediaEvent.SEEK, onMediaSeek);
		//modVP.addEventListener(BCMediaEvent.STOP , onMediaStop);
		//modVP.addEventListener(BCMediaEvent.VOLUME_CHANGE, onMediaVolumeChange);
		
	}
	  
	function onTemplateReady(evt) 
	{						
		//console.log("onTemplateReady", evt);
		init();
	}
	
	function onContentLoad(evt) 
	{						
		//console.log("onContentLoad", evt);
	}
	
	function onVideoLoad(evt) 
	{						
		//console.log("onVideoLoad", evt);
		
		modVP.loadVideo(evt.video.id);
	}
	
	function onAdStart(evt) 
	{						
		//console.log("onAdStart", evt);
	}
	
	function bcCustomLoadById(id)
	{						
		//console.log("bcCustomLoadById", id)
		
		modContent.getVideoAsynch(id);
	}
	
	/*
	 * BC Player MEDIA EVENTS
	 */
	
	function onMediaBegin(evt) 
	{						
		//console.log("onMediaBegin", evt);
		
		$("#videoTitle").html(evt.media.displayName); 
		$("#videoDesc").html(evt.media.longDescription); 	
		if(evt.media.linkURL) $("#bcPlayerTitle").html("<a href=\"" + evt.media.linkURL + "\">More about this product...</a>"); 
	}
	
	function onMediaChange(evt) 
	{						
		//console.log("onMediaChange", evt);			
	}
	
	function onMediaComplete(evt) 
	{						
		//console.log("onMediaComplete", evt);	
	}
	
	function onMediaMuteChange(evt) 
	{						
		//console.log("onMediaMuteChange", evt);			
	}
	
	function onMediaPlay(evt) 
	{						
		//console.log("onMediaPlay", evt);
	}
	
	function onMediaProgress(evt) 
	{						
		//console.log("onMediaProgress", evt);
	}
	
	function onMediaSeek(evt) 
	{						
		//console.log("onMediaSeek", evt);		
	}
	
	function onMediaStop(evt) 
	{						
		//console.log("onMediaStop", evt);
	}
	
	function onMediaVolumeChange(evt) 
	{						
		//console.log("onMediaVolumeChange", evt);			
	}
	
	/*
	 * Custom Controls Methods
	 */
	 	
	function bcCustomPlay()
	{
		//console.log("bcCustomPlay");
	}
	
	function bcCustomPause()
	{
		//console.log("bcCustomPause");
	}
	
	function bcCustomShare()
	{
		//console.log("bcCustomShare");			
	}
	
	function bcCustomEmail()
	{
		//console.log("bcCustomEmail");		
	}
	
	function bcCustomFullScreen(evt)
	{
		//console.log("bcCustomFullScreen");		
	}
	
	function bcMonitorBuffering()
	{
		//console.log("bcMonitorBuffering", modVP);
	}
	
	function bcCustomSeek(percentage)
	{
		//console.log("bcCustomSeek", percentage);																	
	}
	
	function sendMouseUpToControls()
	{
		//console.log("sendMouseUpToControls");
	}
