TimeToFade = 500.0;

if(typeof teaserArray === 'undefined') {
	var teaserArray = [];
}

var sbhtml;

function fillTeaser(JSONData, JSONFeed, search) {
	var playFirst = true;
	var teaser = new teaserObject(JSONData, JSONFeed);
	if(search != undefined) {
		teaser.search = true;
	}
	teaserArray[teaser.teaser_id] = teaser;
	teaser.drawWidget();
	teaser.setCSS();
	teaser.activate_page_btns();
	teaser.setCSS2();
	teaser.fillContent();
	if(teaser.search == undefined)
	{
		for(x in teaserArray)
		{
			if(teaser.teaser_id != teaserArray[x].teaser_id && teaser.play_player == teaserArray[x].play_player) {
				playFirst = false;
				break;
			}
		}
		if(playFirst)
		{
			teaser.setFirstVideo();
		}
	}
	if(playFirst)
	{
		if(sbhtml!=undefined)
		{
			if(sbhtml.check)
			{
				teaser.setFirstVideo();
			}
		}
	}

}

function fillTeaserSearch(JSONData, JSONFeed) {
	fillTeaser(JSONData, JSONFeed, true);
}

function dynamicTag(player_id, http_root_path) {
	var request = http_root_path + "/teaser/teaser_service/" + player_id + "/json/fillTeaser/";
    var head = document.getElementsByTagName("head").item(0);
    var script = document.createElement("script");
    script.setAttribute("type", "text/javascript");
    script.setAttribute("src", request);
	script.async = true;
    head.appendChild(script);
}

function widgetSearch(player_id, http_root_path) {
	
	if(document.getElementById('sb_search_input_' + player_id).value == "" || document.getElementById('sb_search_input_' + player_id).value == "SEARCH VIDEOS") {
		dynamicTag(player_id, http_root_path);
	} else {
	    var request = http_root_path + "/teaser/teaser_service/" + player_id + "/json/fillTeaserSearch/" + document.getElementById('sb_search_input_' + player_id).value;
	    var head = document.getElementsByTagName("head").item(0);
	    var script = document.createElement("script");
	    script.setAttribute("type", "text/javascript");
	    script.setAttribute("src", request);
	    head.appendChild(script);
	}
}

function teaserObject(JSONData, JSONFeed) {
	
	this.JSONFeed = JSONFeed;
	this.list_path = JSONData[0].list_path;
	this.referrer = JSONData[0].referrer;							//Target video page url, on different page
	this.partner_id = JSONData[0].partner_id; 						//partner id
	this.http_root_path = JSONData[0].http_root_path;				//root path
	this.shared_file_system = JSONData[0].shared_file_system;		//shared file system, storage location
	this.master_player = JSONData[0].master_player;					//master player location
	this.ro_cms_url = JSONData[0].ro_cms_url;
	this.lightbox = JSONData[0].player_lightbox;
	this.html5Support = JSONData[0].player_html5Support;
	this.useOutline = JSONData[0].player_useOutline;				//player useOutline
	this.continousPlay = JSONData[0].player_continousPlay;			//player continousPlay
	this.environment = JSONData[0].environment;
	this.autoPlay = JSONData[0].player_autoPlay;
	this.listContent = JSONData[0].listContent;					//content type of list widget
	this.listSize = JSONData[0].listSize;							//list size of list widget
	this.listTagName = JSONData[0].listTagName;					//tag keyword used for Google SEO
	this.playlist_id = JSONData[0].playlist_id;					//id of a playlist for list widgets
	this.channel_id = JSONData[0].channel_id;						//channel id
	this.search;
	this.gorillaNationPlayerId = '';
	
	this.sb_player_orig_width = JSONData[0].sb_player_orig_width;
	this.sb_player_orig_height = JSONData[0].sb_player_orig_height;
	this.sb_player_width = JSONData[0].sb_player_width;
	this.sb_player_height = JSONData[0].sb_player_height;
	
	this.teaser_id = JSONData[0].player_id; 						//teaser id - id of the teaser widget (e.g. sova007)
	this.playsOnSamePage = JSONData[0].playsOnSamePage;				//plays on same page
	this.play_player = JSONData[0].play_player; 					//Used player to play videos - player that will be used to generate video player (e.g sova006)
	this.horizontal_alignment = JSONData[0].horizontalAlignment;	//horizontal alignment
	this.search_value = JSONData[0].search_value;					//search_value
	this.searchBar = JSONData[0].searchBar;							//search Bar value
	this.colors_array = JSONData[0].fiveColors.split(";");
	
	this.colorPrimary = this.colors_array[0].replace('#', '');
	this.colorSecondary = this.colors_array[4].replace('#', '');
	this.arrColors  = createGradient(this.colorPrimary, this.colorSecondary, 10);
	this.hover_color = this.arrColors[8];
	this.basic_color = this.colors_array[0];
	this.div_gradient_color_1 = this.arrColors[9];
	this.div_gradient_color_2 = this.arrColors[6];
	this.div_gradient_color_3 = this.arrColors[8];
	
	this.total_items = 0;
	this.items_per_page = 0;
	this.total_pages = 1;
	this.current_page = 1;
	
	this.width = JSONData[0].width;
	this.height = JSONData[0].height;
	
	if(this.width < 250) {
		this.width = 250;
	}
	
	if(this.height < 95) {
		this.height = 95;
	}
	
	this.placeholder_height = this.height - 49;
	if(this.searchBar == 1) {
		this.placeholder_height = this.height - 24;
	}
	
	//functions
	this.drawWidget = drawWidget;
	this.setCSS = setCSS;
	this.activate_page_btns = activate_page_btns;
	this.setCSS2 = setCSS2;
	this.fillContent = fillContent;
	this.createItem = createItem;
	this.getVideoPlayer = getVideoPlayer;
	this.setFirstVideo = setFirstVideo;
}

function drawWidget() {
	
	var innerDivHtml = '';
	
	innerDivHtml += '<div id="list_widget_container_' + this.teaser_id + '" style="position: relative;">\n';
	innerDivHtml += '  <div id="sb_search_div_' + this.teaser_id + '" class="sb_search">\n';
	innerDivHtml += '    <div style="height: 20px; line-height: 18px; font-size: 16px;">\n';
	innerDivHtml += '      <form class="widget_form" onsubmit="return widgetSearch(\'' + this.teaser_id + '\', \'' +this.http_root_path+ '\');" method="get" action="javascript:void(0);">\n';
	innerDivHtml += '        <div style="height: 20px; float: left">\n';
	innerDivHtml += '        <input type="text" style="color: #000000; margin: 2px 0px 0px 4px; font-size: 10px; background-image: url(' +this.http_root_path+ '/img/sb_search_input_bg.png)" class="sb_search_input" id="sb_search_input_' + this.teaser_id + '" value="';
	if(this.search_value != '') {
		innerDivHtml += this.search_value;
	} else {
		innerDivHtml += 'SEARCH VIDEOS';
	}
	innerDivHtml += '" onblur="if(this.value == \'\'){this.value = \'SEARCH VIDEOS\';}" onfocus="if(this.value == \'SEARCH VIDEOS\'){this.value = \'\';}" />\n';
	innerDivHtml += '        </div>\n';
	innerDivHtml += '        <div style="height: 20px; float: left">\n';
	innerDivHtml += '        <div class="sb_go_btn widget_links" id="sb_go_btn_id_' + this.teaser_id + '" onclick="widgetSearch(\'' + this.teaser_id + '\', \'' +this.http_root_path+ '\'); return false;" >GO</div>\n';
	innerDivHtml += '        </div>\n';
	innerDivHtml += '      </form>\n';
	innerDivHtml += '    </div>\n';
	innerDivHtml += '  </div>\n';
	
	innerDivHtml += '  <div class="placeholder" id="placeholder_id_' + this.teaser_id + '">\n';
	innerDivHtml += '    <div id="main_widget_' + this.teaser_id + '" class="main_widget_content">\n';
	innerDivHtml += '      <div style="display:none;" id="no_result_' + this.teaser_id + '" class="no_result">No matching videos.</div>\n';
	innerDivHtml += '      <div id="loading_' + this.teaser_id + '" class="loading">loading...</div>\n';
	innerDivHtml += '      <div id="item_collection_' + this.teaser_id + '" style="padding-left: 2px;">\n';
	innerDivHtml += '        <!-- content -->\n';
	innerDivHtml += '      </div>\n';
	innerDivHtml += '    </div>\n';
	innerDivHtml += '  </div>\n';
	
	innerDivHtml += '  <div id="list_widget_bottom_' + this.teaser_id + '" class="list_widget_bottom">\n';
	innerDivHtml += '    <div class="prev_btn" id="scroll_left_' + this.teaser_id + '" onclick="change_page(\'prev\', \'' + this.teaser_id + '\')">&#9668;</div>\n';
	innerDivHtml += '    <div class="prev_btn" id="prev_btn_placeholder_' + this.teaser_id + '" style="display: none; cursor: default;">&#9668;</div>\n';
	innerDivHtml += '    <div class="next_btn" id="scroll_right_' + this.teaser_id + '" onclick="change_page(\'next\', \'' + this.teaser_id + '\')">&#9658;</div>\n';
	innerDivHtml += '    <div class="next_btn" id="next_btn_placeholder_' + this.teaser_id + '" style="display: none; cursor: default;">&#9658;</div>\n';
	innerDivHtml += '    <div id="grab_embed_link_' + this.teaser_id + '" class="grab_embed_link">\n';
	if(this.playsOnSamePage != 0 || this.lightbox == 1) {
		innerDivHtml += '      <div onClick="javascript:grabEmbed(\'' +this.teaser_id+ '\');" class="list_widget_bottom_text" id="list_widget_bottom_text_' + this.teaser_id + '">GET EMBED CODE</div>\n';
	}
	innerDivHtml += '    </div>\n';
	innerDivHtml += '  </div>\n';
	
	
	innerDivHtml += '<div id="list_widget_embed_container_' + this.teaser_id + '" style="display: none;" class="list_widget_embed_container">\n';
	innerDivHtml += '  <div class="list_widget_embed_title" id="list_widget_embed_title_' +this.teaser_id+ '">\n';
	innerDivHtml += '    <textarea onclick="this.select();" id="sb_embed_textarea_' + this.teaser_id + '" class="embed_textarea">loading embed code...</textarea>\n';
	innerDivHtml += '    <br />\n';
	innerDivHtml += '    <input class="close_btn_style" style="border-width: 1px; width: 100px; padding: 0px 0px 2px 0px; color: #FFFFFF; font-size: 12px; line-height: normal; margin: 5px 0px 0px 0px; font-family: Verdana;" type="button" value="close" onclick="document.getElementById(\'list_widget_embed_container_' + this.teaser_id + '\').style.display=\'none\';">\n';
	innerDivHtml += '  </div>\n';
	innerDivHtml += '</div>\n';
	
	
	
	innerDivHtml += '</div>';
	
	
	
	document.getElementById("teaser_container_" + this.teaser_id).innerHTML = innerDivHtml;
}

function setCSS() {
	document.getElementById("list_widget_container_" + this.teaser_id).style.width = (this.width - 2)+'px';
	document.getElementById("list_widget_container_" + this.teaser_id).style.border = '1px solid ' + this.colors_array[3];
	document.getElementById("list_widget_container_" + this.teaser_id).style.backgroundColor = this.colors_array[0];
	
	document.getElementById("sb_search_div_" + this.teaser_id).style.borderTop = '1px solid ' + this.colors_array[0];
	document.getElementById("sb_search_div_" + this.teaser_id).style.borderLeft ='1px solid ' +  this.colors_array[0];
	document.getElementById("sb_search_div_" + this.teaser_id).style.borderRight = '1px solid ' + this.colors_array[0];
	document.getElementById("sb_search_div_" + this.teaser_id).style.borderBottom = '1px solid ' + this.colors_array[1];
	if(this.searchBar == 1) {
		document.getElementById("sb_search_div_" + this.teaser_id).style.display = 'none';
	}
}

function createGradient(color1, color2, steps) {
	var i = parseInt(steps);
	var vn = parseInt(steps) - 1;
	var a = [];
	r1 = hex2dec( color1.substring(0,2) ),    
	g1 = hex2dec( color1.substring(2,4) ),    
	b1 = hex2dec( color1.substring(4) ),    
	rs = ( hex2dec( color2.substring(0,2) ) - r1 ) / vn,    
	gs = ( hex2dec( color2.substring(2,4) ) - g1 ) / vn,    
	bs = ( hex2dec( color2.substring(4) ) - b1 ) / vn;  
	while(i--) {
		a[i] = '#' + dec2hex( r1 ) + dec2hex( g1 ) + dec2hex( b1 );    
		r1 += rs;    g1 += gs;    b1 += bs;
	}
	return a;
}
function dec2hex( s ) {
	return ( s<15.5 ? '0' : '' ) + Math.round( s ).toString( 16 ); 
}

function hex2dec( s ) {
	return parseInt( s, 16 ); 
}

function activate_page_btns() {
	if(this.current_page != 1) {
		document.getElementById('scroll_left_' + this.teaser_id).style.display = '';
		document.getElementById('prev_btn_placeholder_' + this.teaser_id).style.display = 'none';
	} else {
		document.getElementById('scroll_left_' + this.teaser_id).style.display = 'none';
		document.getElementById('prev_btn_placeholder_' + this.teaser_id).style.display = '';
	}
	
	if(this.current_page == this.total_pages || this.total_pages == 0) {
		document.getElementById('scroll_right_' + this.teaser_id).style.display = 'none';
		document.getElementById('next_btn_placeholder_' + this.teaser_id).style.display = '';
	} else {
		document.getElementById('scroll_right_' + this.teaser_id).style.display = '';
		document.getElementById('next_btn_placeholder_' + this.teaser_id).style.display = 'none';
	}
	document.getElementById("list_widget_bottom_" + this.teaser_id).style.backgroundColor = this.colors_array[0];
	setGradient('list_widget_bottom_' + this.teaser_id, this.div_gradient_color_1 , this.div_gradient_color_2 ,0);
	setGradient('sb_search_div_' + this.teaser_id, this.div_gradient_color_2 , this.div_gradient_color_3 ,0);
}

var setGradient = (function() {
	
	//private variables;
	var p_dCanvas = document.createElement('canvas');
	var p_useCanvas =  !!(typeof(p_dCanvas.getContext) == 'function');
	var p_dCtx = p_useCanvas?p_dCanvas.getContext('2d'):null;
	var p_isIE = /*@cc_on!@*/false;
    //test if toDataURL() is supported by Canvas since Safari may not support it
	try {
		p_dCtx.canvas.toDataURL()
	} catch(err) {
		p_useCanvas = false;
	};
         
	if(p_useCanvas) {
	   return function (dEl , sColor1 , sColor2 , bRepeatY ){
			if(typeof(dEl) == 'string') dEl =  document.getElementById(dEl);
			if(!dEl) return false;
			var nW = dEl.offsetWidth;
			var nH = dEl.offsetHeight;
			nH = 23;
			p_dCanvas.width = nW;
			p_dCanvas.height = nH;
		
			var dGradient;
			var sRepeat;
			// Create gradients
			if(bRepeatY){
				dGradient = p_dCtx.createLinearGradient(0,0,nW,0);
				sRepeat = 'repeat-y';
			}else{
				dGradient = p_dCtx.createLinearGradient(0,0,0,nH);
				sRepeat = 'repeat-x';
			}		
			
			dGradient.addColorStop(0,sColor1);
			dGradient.addColorStop(1,sColor2);				
			
			p_dCtx.fillStyle = dGradient ; 
			p_dCtx.fillRect(0,0,nW,nH);
			var sDataUrl = p_dCtx.canvas.toDataURL('image/png');
			
			with(dEl.style){
				backgroundRepeat = sRepeat;
				backgroundImage = 'url(' + sDataUrl + ')';
				backgroundColor = sColor2;    
			};
	   }
	} else if(p_isIE) {
		p_dCanvas = p_useCanvas = p_dCtx =  null;		
		return function (dEl , sColor1 , sColor2 , bRepeatY){
			if(typeof(dEl) == 'string') dEl =  document.getElementById(dEl);
			if(!dEl) return false;
			dEl.style.zoom = 1;
			var sF = dEl.currentStyle.filter;
			dEl.style.filter += ' ' + ['progid:DXImageTransform.Microsoft.gradient(	GradientType=',  +(!!bRepeatY ),',enabled=true,startColorstr=',sColor1,', endColorstr=',sColor2,')'].join('');
		};
	} else {
		p_dCanvas = p_useCanvas = p_dCtx =  null;
		return function(dEl , sColor1 , sColor2  ) {
			
			if(typeof(dEl) == 'string') dEl =  document.getElementById(dEl);
			if(!dEl) return false;
			with(dEl.style) {
				 backgroundColor = sColor2; 
			};
			//alert('your browser does not support the gradient effect');
		}
	}
})();

function setCSS2() {
	/* SET CSS */
	document.getElementById("sb_search_input_" + this.teaser_id).style.border = '1px solid ' + this.colors_array[3];
	document.getElementById("sb_search_input_" + this.teaser_id).style.width = (this.width-45) + 'px';
	document.getElementById("sb_go_btn_id_" + this.teaser_id).style.color = this.colors_array[4];
	
	document.getElementById("placeholder_id_" + this.teaser_id).style.backgroundColor = this.colors_array[0];
	document.getElementById("placeholder_id_" + this.teaser_id).style.height = this.placeholder_height+ 'px';

	document.getElementById("main_widget_" + this.teaser_id).style.height = this.placeholder_height;
	document.getElementById("main_widget_" + this.teaser_id).style.backgroundColor = this.colors_array[0];

	document.getElementById("item_collection_" + this.teaser_id).style.width = (this.width - 3) + 'px';
	document.getElementById("item_collection_" + this.teaser_id).style.height = this.placeholder_height;
	document.getElementById("no_result_" + this.teaser_id).style.color = this.colors_array[4];
	document.getElementById("loading_" + this.teaser_id).style.color = this.colors_array[4];

	document.getElementById("scroll_left_" + this.teaser_id).style.color = this.colors_array[4];
	document.getElementById("prev_btn_placeholder_" + this.teaser_id).style.color = this.colors_array[0];
	document.getElementById("scroll_right_" + this.teaser_id).style.color = this.colors_array[4];
	document.getElementById("next_btn_placeholder_" + this.teaser_id).style.color = this.colors_array[0];
	if(this.playsOnSamePage != 0 || this.lightbox == 1) {
		document.getElementById("list_widget_bottom_text_" + this.teaser_id).style.color = this.colors_array[3];
	}
	/* SET CSS */
}

function fillContent() {
	document.getElementById("loading_" + this.teaser_id).style.display = 'none';
	document.getElementById("item_collection_" + this.teaser_id).innerHtml = '';
	
	for(x in this.JSONFeed) {
		if(typeof(this.JSONFeed[x].id) == 'number') {
			this.total_items = this.total_items + 1 ;
		}
	}
	
	if(this.horizontal_alignment == 1) {
		//Horizontal
		this.items_per_page_vertical = Math.floor(this.placeholder_height/72);
		this.items_per_page_horizontal = Math.floor((this.width-3)/214);
		this.items_per_page = this.items_per_page_vertical * this.items_per_page_horizontal;
		this.total_pages = Math.ceil(this.total_items/this.items_per_page);
		difference = (this.width-3) - (214*this.items_per_page_horizontal);
		this.width_add_on = Math.floor(difference/this.items_per_page_horizontal);
	} else {
		//Vertical
		this.items_per_page = Math.floor(this.placeholder_height/70);
		this.total_pages = Math.ceil(this.total_items/this.items_per_page);
	}
	
	if(this.total_items < 1) {
		document.getElementById("no_result_" + this.teaser_id).style.display='';
	} else {
		item_id = 1;
		for(x in this.JSONFeed) {
			if(typeof(this.JSONFeed[x].id) == 'number') {
				this.createItem(item_id, this.JSONFeed[x].id, this.JSONFeed[x].title, this.JSONFeed[x].blurb, this.JSONFeed[x].length, this.JSONFeed[x].image);
				item_id++;
			}
		}
	}
	
	this.activate_page_btns();
}

function secondsToTime(secs)
{
    var divisor_for_minutes = secs % (60 * 60);
    var minutes = Math.floor(divisor_for_minutes / 60);
 
    var divisor_for_seconds = divisor_for_minutes % 60;
    var seconds = Math.ceil(divisor_for_seconds);
   
    return Math.round(minutes,2)+':'+Math.round(seconds,2);
}

function rectime(sec) {
	var hr = Math.floor(sec / 3600);
	var min = Math.floor((sec - (hr * 3600))/60);
	sec -= ((hr * 3600) + (min * 60));
	sec += ''; min += '';
	while (min.length < 2) {min = '0' + min;}
	while (sec.length < 2) {sec = '0' + sec;}
	hr = (hr)?hr+':':'';
	return hr + min + ':' + sec;
}

function createItem(id, video_id, title, blurb, length, image) {
	var play_url = 'location.href = \'' + this.referrer + '?sb_video_id=' + video_id + '&sb_partner_id=' + this.partner_id + '\'';
	var error_img = this.http_root_path + '/img/video_thumb.png';
	
	if(this.html5Support == 1 && this.playsOnSamePage == 0) {
		play_url = 'changeVideo(' +this.partner_id+ ', ' +video_id+ ', ' +id+ ', \'' +this.teaser_id+ '\');';
	} else if(this.lightbox == 1) {
	  	play_url = 'start_lightbox_' + this.play_player + '(\'' + this.ro_cms_url + '/embed_code_lightbox/index/' +
	  	this.partner_id + '/single/' + video_id + '/' + this.play_player + '/10/' + this.sb_player_width + '/' + this.sb_player_height+ '/' + this.useOutline + '/' + 
	  	this.continousPlay + '/source/' + sbDocumentLocation + '\', \''+ this.sb_player_orig_width + '\', \'' + this.sb_player_orig_height + '\');';
	} else if(this.playsOnSamePage == 0) {
		play_url = 'playVideo(' + video_id + ', ' + this.partner_id + ', ' +id+ ', \''+this.teaser_id+'\');';
	}
	if(this.horizontal_alignment == 1) {
		title = stripslashes(title);
		blurb = stripslashes(blurb);
		if(title.length > 14) { title = title.substring(0, 14)+'...'; }
		if(blurb.length > 50) { blurb = blurb.substring(0, 50)+'...'; }
		
		var widget_single_item_right_width = (112 + this.width_add_on);
		div_element = '';
		div_element +='<div id=sb_item_' +this.teaser_id+ '_' +video_id+ ' style="border: 1px solid ' + this.colors_array[2];
		div_element += '" onMouseOver="this.style.backgroundColor=\''+this.hover_color+'\';" onMouseOut="outMouseOn(\'' +this.colors_array[0]+ '\', \'' +this.teaser_id+ '\', ' +video_id+ ');" class="widget_single_item">';
		div_element +='  <div style="padding:2px;">';
		div_element +='    <div class="widget_single_item_left" onclick="' + play_url + '" style="cursor: pointer;  height: 64px;">';
		div_element +='      <img border="0" width="87px" height="64px" src="'+ image +'" onerror="this.error = null; this.src = \''+error_img+'\'"/>';
		div_element +='    </div>';
		
		div_element +='    <div id="id_' +video_id+ '" class="widget_single_item_right" style="text-align: left; width: ' + widget_single_item_right_width + 'px; margin-left: 2px; height: 64px; margin-top: 1px; cursor: pointer;" onclick="' + play_url + '">';
		div_element +='      <div class="single_item_duration" style="color: ' + this.colors_array[2] + '">';
		div_element +='        <div>';
		div_element +='        	<span style="padding: 0 2px; font-size: 9px; font-weight: bold; background-color: ' + this.colors_array[4] + '; color: ' + this.colors_array[1] + ';">PLAY VIDEO</span>';
		
		
		
		div_element +='        	<span style="margin-left: 3px; color: ' + this.colors_array[2] + ';">'+rectime(length)+'</span>';
		div_element +='        </div>';
		div_element +='      </div>';
		div_element +='      <div class="single_item_title" style="color: ' + this.colors_array[4] + '">'+ title +'</div>';
		div_element +='      <div class="single_item_body" style="color: ' + this.colors_array[3] + '">' + blurb + '</div>';
		div_element +='    </div>';
		div_element +='  </div>';
		div_element +='</div>';
		
		var newDiv = document.createElement('div');
		newDiv.id = 'sb_item_' + this.teaser_id + '_' + id;
		newDiv.innerHTML = div_element;
		newDiv.style.width = (212 + this.width_add_on) + 'px';
		newDiv.style.height = '71px';
		newDiv.style.cssFloat = 'left';
		newDiv.style.styleFloat = 'left';
		newDiv.style.marginRight = '1px';
		newDiv.style.marginBottom = '1px';
		if(id > this.items_per_page) {
			newDiv.style.display = 'none';
		}
		
	} else {
	
		title = stripslashes(title);
		blurb = stripslashes(blurb);
		if(title.length > 20) { title = title.substring(0,20)+'...'; }
		if(blurb.length > 85) { blurb = blurb.substring(0,85)+'...'; }
		
		div_element = '';
		div_element +='<div id=sb_item_' +this.teaser_id+ '_' +video_id+ ' style="border-bottom: 1px solid ' + this.colors_array[2];
		div_element +='" onMouseOver="this.style.backgroundColor=\''+this.hover_color+'\';" onMouseOut="outMouseOn(\'' +this.colors_array[0]+ '\', \'' +this.teaser_id+ '\', ' +video_id+ ');" class="widget_single_item">';
		div_element +='  <div style="padding:2px;">';
		div_element +='    <div class="widget_single_item_left" onclick="' + play_url + '" style="cursor: pointer; height: 64px;">';
		div_element +='      <img border="0" width="87px" height="64px" src="'+ image +'" onerror="this.error = null; this.src = \''+error_img+'\'"/>';
		div_element +='    </div>';
		
		div_element +='    <div id="id_' +video_id+ '" class="widget_single_item_right" style="text-align: left; width: ' + (this.width-105) + 'px; height: 65px; cursor: pointer;" onclick="' + play_url + '">';
		div_element +='      <div class="single_item_duration" style="color: ' + this.colors_array[2] + '">';
		div_element +='        <div>';
		div_element +='        	<span style="padding: 0 2px; font-size: 9px; font-weight: bold; background-color: ' + this.colors_array[4] + '; color: ' + this.colors_array[1] + ';">PLAY VIDEO</span>';
		div_element +='        	<span style="margin-left: 3px; color: ' + this.colors_array[2] + ';">'+rectime(length)+'</span>';
		div_element +='        </div>';
		div_element +='      </div>';
		div_element +='      <div class="single_item_title" style="color: ' + this.colors_array[4] + '">'+ title +'</div>';
		div_element +='      <div class="single_item_body" style="color: ' + this.colors_array[3] + '">' + blurb + '</div>';
		div_element +='    </div>';
		div_element +='  </div>';
		div_element +='</div>';
		
		newDiv = document.createElement('div');
		newDiv.id = 'sb_item_' + this.teaser_id + '_' + id;
		newDiv.innerHTML = div_element;
		if(id > this.items_per_page) {
			newDiv.style.display = 'none';
		}
	}
	
	document.getElementById("item_collection_" + this.teaser_id).appendChild(newDiv);
}

function stripslashes(str) {
	return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}

function outMouseOn(color, widget_player_id, video_id) {
	var current_teaser = teaserArray[widget_player_id];
	if(current_teaser.current_video == undefined || current_teaser.current_video != video_id) {
		document.getElementById('sb_item_' +widget_player_id+ '_' +video_id).style.backgroundColor = color;
	}
}

function change_page(direction, widget_player_id) {
	
	var current_teaser = teaserArray[widget_player_id];
	document.getElementById('scroll_left_' + widget_player_id).style.display = 'none';
	document.getElementById('scroll_right_' + widget_player_id).style.display = 'none';
	document.getElementById('prev_btn_placeholder_' + widget_player_id).style.display = '';
	document.getElementById('next_btn_placeholder_' + widget_player_id).style.display = '';
	
	//fadeOut
	document.getElementById('item_collection_' + widget_player_id).style.opacity = '0';
	document.getElementById('item_collection_' + widget_player_id).style.filter = 'alpha(opacity=0)';
	document.getElementById('item_collection_' + widget_player_id).FadeState = -2;
	
	if(direction == 'next'){ current_teaser.current_page++; }
	if(direction == 'prev'){ current_teaser.current_page--; }
	
	var active_begin = ((current_teaser.current_page - 1) * current_teaser.items_per_page) + 1;
	var active_end = current_teaser.current_page * current_teaser.items_per_page;
	
	for (i = 1; i <= current_teaser.total_items; i++) {
		if(i < active_begin || i > active_end) {
			document.getElementById('sb_item_' + widget_player_id + '_' + i).style.display = 'none';
		} else {
			document.getElementById('sb_item_' + widget_player_id + '_' + i).style.display = '';
		}
	}
	
	//fadein
	setTimeout('fade(\'item_collection_' + widget_player_id + '\', \'' + widget_player_id + '\');', 1000);
}

function fade(eid, widget_player_id) {
	var current_teaser = teaserArray[widget_player_id];
	
	var element = document.getElementById(eid);
	if(element == null)
		return;
   
	if(element.FadeState == null) {
		if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1'
        ) {
			element.FadeState = 2;
		} else {
			element.FadeState = -2;
		}
	}
   
	if(element.FadeState == 1 || element.FadeState == -1) {
		element.FadeState = element.FadeState == 1 ? -1 : 1;
		element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
	} else {
		element.FadeState = element.FadeState == 2 ? -1 : 1;
		element.FadeTimeLeft = TimeToFade;
		setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "','" + widget_player_id + "')", 33);
	}
}

function animateFade(lastTick, eid, widget_player_id) {
	var current_teaser = teaserArray[widget_player_id];
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;

	var element = document.getElementById(eid);

	if(element.FadeTimeLeft <= elapsedTicks) {
		element.style.opacity = element.FadeState == 1 ? '1' : '0';
		element.style.filter = 'alpha(opacity = '
			+ (element.FadeState == 1 ? '100' : '0') + ')';
		element.FadeState = element.FadeState == 1 ? 2 : -2;
		current_teaser.activate_page_btns();
		return;
	}

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft/TimeToFade;
	if(element.FadeState == 1)
		newOpVal = 1 - newOpVal;

	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

	setTimeout("this.animateFade(" + curTick + ",'" + eid + "','" + widget_player_id + "')", 33);
}

function getVideoPlayer(video_id, partner_id, initialItem) {
	switch(this.listContent) {
		case 'latestOnSite': 
			sb_content_flash_url = 'latest_' + this.listSize;
			break;
		case 'latestInChannels':
			sb_content_flash_url = 'latest_in_channel_' + this.channel_id;
			break;
		case 'latestInPlaylist':
			sb_content_flash_url = 'playlist_' + this.playlist_id;
			break;
		case 'latestByTag':
			sb_content_flash_url = 'tag_' + this.listTagName + "_" + this.listSize;
			break;
		default:
			sb_content_flash_url = this.listSize;
	}
	if(this.html5Support == 0) {
		//swf object params
		var test_param = '';
		var autostart_param = '';
		var initialItem_param = '';
		var file_param = '';
		var teaserReporting_param = '';
		var pageUrl_param = '&pageUrl='+escape(document.location);
		if (this.environment == 'development') {
			test_param = '&testing=true';
		}
		if(this.autoPlay == 1) {
			autostart_param = '&autostart=true';
		} else {
			autostart_param = '&autostart=false';
		}
		if(this.continousPlay == 1 && this.search == undefined) {
			if(initialItem == 0) initialItem = 1;
			initialItem_param = '&initialItem='+initialItem;
			file_param = '&file='+this.http_root_path +"/"+ this.list_path;
		} else {
			file_param = '&file='+this.http_root_path +"/xml_feeds_advanced/index/"+partner_id+"/3/"+video_id+"/0/0/0/0/0/0/0";
		}
		teaserReporting_param = '&teaserReporting='+this.teaser_id;
		var so = new SWFObject(this.master_player+"?pid="+this.play_player+"&siteId="+this.partner_id+test_param+autostart_param+initialItem_param+file_param+teaserReporting_param+pageUrl_param, "mplayer", this.sb_player_orig_width, this.sb_player_orig_height, "8", "#000000");
		so.addParam("wmode","transparent");
		so.addParam("swliveconnect", "true");
		so.addParam("allowfullscreen", "true");
		so.addParam("allowscriptaccess", "always");
		so.write("sb_video_player_" + this.play_player);
	} else {
		var initItemContent = '';
		var fileContent = '';
		var autostartContent = '';
		var testContent = '';
		if(this.continousPlay == 1 && this.search == undefined) {
			if(initialItem == 0) initialItem = 1;
			initItemContent = '&initialItem='+initialItem;
			fileContent = '&file='+this.http_root_path +'/'+ this.list_path + '/';
		} else {
			fileContent = '&file='+this.http_root_path +'/xml_feeds_advanced/index/'+partner_id+'/3/'+video_id+'/';
		}
		if(this.autoPlay == 1) {
			autostartContent = '&autostart=true';
		} else {
			autostartContent = '&autostart=false';
		}
		if (this.environment == 'development') {
			testContent = '&testing=true';
		}
		var teaserReport_content = '&teaserReporting='+this.teaser_id;
		
		this.gorillaNationPlayerId ='sbp_'+this.teaser_id+'_pid_'+this.partner_id+'_'+this.play_player+'_pl_'+this.playlist_id+'_teaserWidget';
		
		var player_code = '<object style="display:block;" width="'+this.sb_player_orig_width+'" height="'+this.sb_player_orig_height+'" id="'+this.gorillaNationPlayerId+'" class="SpringboardSwitcher" type="application/x-shockwave-flash" data="'+this.master_player+'?width='+this.sb_player_orig_width+'&height='+this.sb_player_orig_height+'&pid='+this.play_player+initItemContent+fileContent+autostartContent+testContent+teaserReport_content+'">';
		player_code += '<param name="wmode" value="transparent">';
		player_code += '<param name="swliveconnect" value="true">';
		player_code += '<param name="allowfullscreen" value="true">';
		player_code += '<param name="allowScriptAccess" value="always">';
		player_code += '<param name="movie" value="'+this.master_player+'?width='+this.sb_player_orig_width+'&height='+this.sb_player_orig_height+'&pid='+this.play_player+initItemContent+fileContent+autostartContent+testContent+teaserReport_content+'">';
		player_code += '<param name="flashvars" value="wmode=transparent';
		player_code += '</object>';
		document.getElementById("sb_video_player_"+this.play_player).innerHTML = player_code;
		if(sbhtml!=undefined)
		{
			if (this.continousPlay == 1)
			{
				sbhtml.createSpringboards(this.gorillaNationPlayerId,[{"continuousPlay":"1"}],this.JSONFeed);
			}
			else
			{
				sbhtml.createSpringboards(this.gorillaNationPlayerId,[{"continuousPlay":"0"}],this.JSONFeed);
			}
		}
		else
		{
			alert('Please import html5fallback.js library to run html5 player.');
		}		
	}
}

function setFirstVideo() {
	if(this.playsOnSamePage == 0 && this.lightbox == 0) {
		if(this.continousPlay == 1) {
			this.getVideoPlayer(0, 0, 0)
		} else {
			//this.getVideoPlayer(this.JSONFeed[0].real_id, this.JSONFeed[0].site_id, 0);
			this.getVideoPlayer(this.JSONFeed[0].id, this.JSONFeed[0].site_id, 0);
		}
	}
}

function playVideo(video_id, widget_partner_id, id, player_id) {
	
	var current_teaser = teaserArray[player_id];
	
	current_teaser.getVideoPlayer(video_id, widget_partner_id, id);
}

function teaserReport(videoId, teaserId) {
	var current_teaser = teaserArray[teaserId];
	document.getElementById('sb_item_' +teaserId+ '_' +videoId).style.backgroundColor = current_teaser.hover_color;
	if(current_teaser.current_video != undefined && current_teaser.current_video != videoId) {
		document.getElementById('sb_item_' +teaserId+ '_' +current_teaser.current_video).style.backgroundColor = current_teaser.basic_color;
	}
	current_teaser.current_video = videoId;
}

function grabEmbed(widget_player_id) {
	
	var current_teaser = teaserArray[widget_player_id];
	
	var embed_div = document.getElementById('list_widget_embed_container_' + widget_player_id);
	embed_div.style.width = current_teaser.width + 'px';
	embed_div.style.height = current_teaser.height + 'px';
	//sb_embed_textarea
	document.getElementById('sb_embed_textarea_' + widget_player_id).style.width = (current_teaser.width/4*3) + 'px';
	document.getElementById('sb_embed_textarea_' + widget_player_id).style.height = (current_teaser.height-88) + 'px'; //28 pixels for close button
	var container_div = document.getElementById('list_widget_container_' + widget_player_id);
	
	if(current_teaser.height < 126) {
		document.getElementById('sb_embed_textarea_' + widget_player_id).style.height = (current_teaser.height-58) + 'px'; //28 pixels for close button
		document.getElementById('list_widget_embed_title_' +widget_player_id).style.paddingTop = '15px';
		document.getElementById('list_widget_embed_title_' +widget_player_id).style.paddingBottom = '15px';
	}
	
	embed_div.style.display = '';
	getListWidgetCode(current_teaser.width, current_teaser.height, widget_player_id, current_teaser.http_root_path, current_teaser.shared_file_system, current_teaser.play_player);
}

function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent) {
		while(1) {
			curleft += obj.offsetLeft;
			if(!obj.offsetParent){ break; }
			obj = obj.offsetParent;
		}
	}
	else if(obj.x){ curleft += obj.x; }
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent) {
		while(1){
			curtop += obj.offsetTop;
			if(!obj.offsetParent){ break; }
			obj = obj.offsetParent;
		}
	}
	else if(obj.y){ curtop += obj.y; }
	return curtop;
}

function getListWidgetCode(widget_width, widget_height, widget_player_id, http_root_path, shared_file_system, play_player) {
	var current_teaser = teaserArray[widget_player_id];
	
	var embed_iframe_code = '<!-- SPRINGBOARD VIDEO LIST WIDGET -->\n';
	
	if(current_teaser.lightbox == 1) {
		embed_iframe_code += '<script language="javascript" type="text/javascript" src="' +shared_file_system+ '/lightbox_code/static/companion_ads.js"></script>\n';
		embed_iframe_code += '<script language="javascript" type="text/javascript" src="' +shared_file_system+ '/lightbox_code/' +play_player+ '.js"></script>\n\n';
	}
	
	embed_iframe_code += '<script src="' +shared_file_system+ '/js/list_widget/teaser_object.js" type="text/javascript"></script>\n';
	embed_iframe_code += '<script type="text/javascript">dynamicTag(\'' + widget_player_id+ '\', \'' +http_root_path+ '\');</script>\n';
	embed_iframe_code += '<style type="text/css">\n';
	embed_iframe_code += '  @import url(' +shared_file_system+ '/css/widget.css);\n';
	embed_iframe_code += '</style>\n';
	embed_iframe_code += '<div id="teaser_container_' + widget_player_id + '" class="teaser_container"></div>\n';
	embed_iframe_code += '<!-- SPRINGBOARD VIDEO LIST WIDGET -->\n';
	
	document.getElementById("sb_embed_textarea_" + widget_player_id).value = embed_iframe_code;
}

function changeVideo(partnerId, videoId, initItem, widget_player_id) {
	var current_teaser = teaserArray[widget_player_id];
	var object_string = document.getElementById('sb_video_player_'+current_teaser.play_player).innerHTML;
	if(object_string.indexOf("<video ") != -1) {
		if(current_teaser.continousPlay == 1) {
			if(current_teaser.search == undefined) {
				sbhtml.refreshVideoDataWidget(current_teaser.gorillaNationPlayerId,initItem - 1);
			} else {
				sbhtml.refreshVideoDataWidget(current_teaser.gorillaNationPlayerId, initItem - 1);
			}
		} else {
			if(current_teaser.search == undefined) {
				sbhtml.refreshVideoDataWidget(current_teaser.gorillaNationPlayerId,initItem - 1);
			} else {
				sbhtml.refreshVideoDataWidget(current_teaser.gorillaNationPlayerId, initItem - 1);
			}
		}
	} else {
		current_teaser.getVideoPlayer(videoId, partnerId, initItem);
	}
}


