
//Verify / Set some parameters
if (!PromoType) var PromoType="TwoTallThinVertical";
//TwoTallThinVertical
//ThreeTallThinVertical
//TwoTallWideVertical
//ThreeTallWideVertical

if (!Filter) var Filter="";
if (!VideoPromoDiv) var VideoPromoDiv="promo1";
if (!ExtrasCount) var ExtrasCount = 6;	//Max count of videos to load

//Call JQuery Plugin 
document.write("<sc" + "ript src='http://www.suntimes.com/javascript/version2/jquery-jcarousellite.min.js' type='text/javascript'></sc" + "ript>\n");

document.write("<link href='http://www.suntimes.com/css/version2/VideoPromos.css' rel='stylesheet' type='text/css' />");
document.write("<div id='VideoPromoDiv-"+VideoPromoDiv+"' class='VideoPromo"+PromoType+" VideoPromoVertical'></div>");



/// Time Converter --------------------------------
function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}
function time(ms) {
		var sec = Math.floor(ms/1000)
		ms = ms % 1000
		t = three(ms)
		
		var min = Math.floor(sec/60)
		sec = sec % 60
		//t = two(sec) + ":" + t
		t = two(sec)
		
		var hr = Math.floor(min/60)
		min = min % 60
		t = two(min) + ":" + t
		
		var day = Math.floor(hr/60)
		hr = hr % 60
		if (hr>0){
			t = two(hr) + ":" + t
		}
		
		return t
}


// Wide Tall Vertical with Time
function VideoPromoVertical(QueryString,Div,Count){

	$("#VideoPromoDiv-"+Div+"").html("<br/><br/>Loading Videos...");

	//Get domain and pass sub pub path if exists
	var URLPath = new Object;
	var href    = document.location.href;
	var string  = href.split( "/" );
	URLPath.parts = new Array(string.length);
	for ( var i = 2; i < string.length; i++ ) {
		URLPath.parts[i-2] = string[i];
	}
	
	var PubPath = "";
		//SCN Daily Pub check
		if (URLPath.parts[0] == "www.suburbanchicagonews.com"){
			if (URLPath.parts[1] == "beaconnews" || URLPath.parts[1] == "couriernews" || URLPath.parts[1] == "heraldnews" || URLPath.parts[1] == "napervillesun" || URLPath.parts[1] == "newssun"){
				PubPath = "/"+URLPath.parts[1];
			}
		}


	//Determine from PromoType, how many thumbs to show
	var VideoPromoScrollCount = 3;
	if ((PromoType=="TwoTallThinVertical") || (PromoType=="TwoTallWideVertical")){
			VideoPromoScrollCount = 2;
	}

	//Results auto scroll function
	function VideoPromoCarousel(){
					$("#VideoPromoDiv-"+Div+" .VideoPromoScroll").jCarouselLite({
						btnNext: ".next",
						btnPrev: ".prev",
						visible: VideoPromoScrollCount,
		    		auto: 3000,
  				  speed: 500,
						circular: true,
						vertical: true
					});
		}


    $.getJSON("http://feeds.theplatform.com/ps/JSON/PortalService/2.1/getReleaseList?PID=EOTZ7DCbM_x35MTaWqvVJzi43yisMlOp&startIndex=1&endIndex="+Count+"&"+QueryString+"&field=title&field=description&field=length&field=thumbnailURL&field=contentID&field=URL&field=PID&callback=?",
        function(data){
					
					//Pull out Category and pass to full video player in special way ----------------------
					var qsParams = new Array();
					var querystring = QueryString;
					var params = querystring.split('&');
						for (var i=0; i<params.length; i++) {
						var pos = params[i].indexOf('=');
						if (pos > 0){
							var key = params[i].substring(0,pos);
							var val = params[i].substring(pos+1);
							qsParams[key] = val;		
							if (key=="query"){
								var qpos = val.indexOf('|');
								if (qpos > 0) {
									var qterm = val.substring(0,qpos);
									var qval = val.substring(qpos+1);						
									if (qterm=="Categories"){
										QueryString = QueryString+"&cat="+qval;
									}
								}
							}
						}
					}
					////////////////////// ---------------------------------------------------------------

					var html = "<span class='VideoPromoTopper'><a href='"+PubPath+"/video' title='Go to all videos'>Video</a></span>";
					html += "<div class='VideoPromoScroll'>";
					html += "<ul>";
					$.each(data.items, function(i,item){
						html += "<li id='rel"+i+"'>";
						html += "<a href='"+PubPath+"/video/index.html?"+QueryString+"&pid="+item.PID+"' title='"+item.title+" :: "+item.description+"'>";
						if (item.thumbnailURL!=""){
							html += "<img src='"+item.thumbnailURL +"' alt='"+item.title+"' width='45' height='25' border='0'/>";
						}
						html += "</a>";
						html += "<a  href='"+PubPath+"/video/index.html?"+QueryString+"&pid="+item.PID+"' title='"+item.title+" :: "+item.description+"' class='Title'>";
						html += item.title.substr(0,21);
						if (item.title.length>21){
							html += "...";
						}
						html += "</a> ";
						
						html += "<span class='ElapsedTime'>"+time(item.length)+"</span>";
						html += "</li>";
					});
					
					html += "</ul>";
					html += "</div>";
					$("#VideoPromoDiv-"+Div+"").html(html);
					
				//If we don't wait, it renders it at a wrong size
				setTimeout(VideoPromoCarousel, 400);
					
	});



}





$(document).ready(function(){
	VideoPromoVertical(Filter,VideoPromoDiv,ExtrasCount);
});

