var uri;
var channelId;

var escapeChannel = "channels/206132";
var kyteplayer;
var kyteBadge;
var kyteServer = "www.kyte.tv";
var apiKey = "1HP9EY7ylxj2NAkRgG7U";


function player_load(uri) {
	kyteplayer = new Kyte.Player(uri, { layoutMode:"tall", minChatHeight: 320, premium: false, producerMode: "never", hideShowsTab: true}, "player");
	//kyteplayer.addEventListener("Navigation.ShowChanged", listener_showchg);
	//kyteplayer.addEventListener("Navigation.ChannelChanged", listener_channelchg);
	show_play(uri);
}

function getParameter ( parameterName ) {
	var framesetQuery = "";
	if ( window.top.location.search != 0 ) {
		framesetQuery = window.top.location.search;
	} else {
		framesetQuery = "?OpenFrameset";
	}		
	
	// Add "=" to the parameter name (i.e. parameterName=value)
	var parameterName = parameterName + "=";
	if ( framesetQuery.length > 0 ) {
	// Find the beginning of the string
		begin = framesetQuery.indexOf ( parameterName );
	// If the parameter name is not found, skip it, otherwise return the value
		if ( begin != -1 ) {
	// Add the length (integer) to the beginning
			begin += parameterName.length;
	// Multiple parameters are separated by the "&" sign
			end = framesetQuery.indexOf ( "&" , begin );
			if ( end == -1 ) {
				end = framesetQuery.length
			}
	// Return the string
			return unescape ( framesetQuery.substring ( begin, end ) );
		}
	// Return "null" if no parameter has been found
		return "null";
	}
}

function pageSetup() {
	uri = getParameter('uri');
	var play = getParameter('play');
	var psa = "default";
	if (play == "true") psa = 'autoplay';
	
	if (uri == "null") uri = escapeChannel;
	var uriAsArray = uri.split("/");
	channelId = uriAsArray[1];
	
	switch (channelId) {
		case ("206133"):
			$('submitButton').show();
			$('pagedesc').innerHTML = "<b>Email photos from your cell phone</b> while in Phoenix to <a href=\"mailto:allstarscene@me.com\">allstarscene@me.com</a> . Post them for the world to see! Or just family and friends. Plus, <b>comment</b> or chat on all of our pics and videos. You are now a part of the All-Star Scene. View this channel on your mobile phone. Go to <a href=\"http://wap.nba.com/allstarscene\">wap.nba.com/allstarscene</a> and click on Fans. <span class=\"lawyer\"><a href=\"faq.html\">(NBA All-Star Scene Policy)</a></span>";
			break;
		case ("206132"):
			$('pagedesc').innerHTML = "<b>Watch the players</b> up close throughout the weekend. From arrival to practices to courtside, catch NBA Stars as they mingle and prepare. View this channel on your mobile phone. Go to <a href=\"http://wap.nba.com/allstarscene\">wap.nba.com/allstarscene</a> and click on Players.";
			break;
		case ("206134"):
			$('pagedesc').innerHTML = "Get your <b>custom NBA picture or video</b> taken in Phoenix? Check here during the All-Star and up to a week afterwards to see it and <b>share</b> it with your family and friends. View this channel on your mobile phone. Go to <a href=\"http://wap.nba.com/allstarscene\">wap.nba.com/allstarscene</a> and click on HP Jam Session/Block Party.";
			 $('headerImageImg').src = "http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/AllStar_Scene_Header2.jpg";
			break;
		case ("206107"):
			$('pagedesc').innerHTML = "Go behind the scenes with <b>Legends</b>, the <b>Commissioner</b>, and a parade of Guests and <b>Celebrities</b> that make the All-Star scene the place to be.  View this channel on your mobile phone. Go to <a href=\"http://wap.nba.com/allstarscene\">wap.nba.com/allstarscene</a> and click on Celebrities and VIP.";
			break;
		case ("206136"):
			$('pagedesc').innerHTML = "<h2>Check out the best moments from each day of All-Star week.</h2> View this channel on your mobile phone. Go to <a href=\"http://wap.nba.com/allstarscene\">wap.nba.com/allstarscene</a> and click on Featured.";
			break;											
	}
	
	// load Player 
	kyteplayer = new Kyte.Player(uri, { layoutMode:"tall",  width: 425, premium: false, producerMode: "never", hideShowsTab: true ,preShowAction: psa, wmode: "transparent"  }, "player");
	//kyteplayer.setURI(uri);
	kyteBadge = new KyteBadge(channelId, { nextPageDeact:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/nextDe.png", nextPageAct:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/next.png", prevPageDeact:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/prevDe.png", prevPageAct:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/prev.png", cssClass:"nbaKyteBadgeCh1", cssFile:"http://i.cdn.turner.com/nba/nba/.element/css/1.0/allstar/allstarscene/styleCh1.css", padding:11, footerHeight:40, metaWidth:298, metaDescriptionLength:80, metaComments: true, metaRating: true, metaRatingLength: 3, metaViews: true, metaDescription: true, metaProducer: true, metaTitle: true, filterSearch: true, filterBestRated: true, filterMostWatched: true, filterJustProduced: true, defaultSortMode:"createdTime-d", onClickThumb:"player", pagingControl: true, layout:"vertical", kyteServer:kyteServer, ak:apiKey, showsPerPage: 6 }, 'kyteBadge');
}

function promptForProduction() {
	$('playerProducePrompting').show();
}

function produce(produce) {
	if (produce && uri == "channels/206133") {
		kyteplayer = new Kyte.Player(uri, { layoutMode:"tall",  width: 425, premium: false, hideShowsTab: true, wmode: "transparent"  }, "player");
		kyteplayer.display('Producer');
	} 
	$('playerProducePrompting').hide();
}

function searchDo() {
	var searchKey = escape($('searchKey').value);
	window.location = "search.html?searchKey="+ searchKey;
}

function searchSetup() {
	var searchKey = unescape(getParameter('searchKey'));
	
	kyteBadge = new KyteBadge(12345, { searchAccount: true, filterSearchKeyword: searchKey, nextPageDeact:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/nextDe.png", nextPageAct:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/next.png", prevPageDeact:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/prevDe.png", prevPageAct:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/prev.png", cssClass:"nbaKyteBadgeCh1", cssFile:"http://i.cdn.turner.com/nba/nba/.element/css/1.0/allstar/allstarscene/styleSearch.css", padding:11, footerHeight:40, metaWidth:298, metaComments: true, metaRating: true, metaRatingLength: 3, metaViews: true, metaDescription: true, metaDescriptionLength:100, metaProducer: true, metaTitle: true, filterSearch: true, badgeTitle: "Search Results", defaultSortMode:"createdTime-d", onClickThumb:"player", pagingControl: true, layout:"vertical", kyteServer:kyteServer, ak:apiKey, showsPerPage: 6 }, 'kyteBadge');
}

function loadThumbs() {
	getJSON(206132);
	getJSON(206134);
	//getJSON(206135);
	getJSON(206107);
	getJSON(206133);
	//getJSON(206136);
	
	kyteBadge = new KyteBadge(206136, { nextPageDeact:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/nextDe.png", nextPageAct:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/next.png", prevPageDeact:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/prevDe.png", prevPageAct:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/prev.png", cssClass:"nbaKyteBadgeCh1", cssFile:"http://i.cdn.turner.com/nba/nba/.element/css/1.0/allstar/allstarscene/styleBFront.css", padding:11, footerHeight:0, headerHeight: 0, metaWidth:136, metaComments: true, metaRating: true, metaRatingLength: 3, metaViews: true, metaDescription: true, metaProducer: true, metaTitle: true, defaultSortMode:"createdTime-d", onClickThumb:"player", layout:"vertical", kyteServer:kyteServer, ak:apiKey, showsPerPage: 5 }, 'kyteBadge');
	kyteBadge = new KyteBadge(12345, {metaRating: true, headerHeight: 0, metaRatingLength: 3, metaDescriptionLength:35, searchAccount: true, nextPageDeact:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/nextDe.png", nextPageAct:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/next.png", prevPageDeact:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/prevDe.png", prevPageAct:"http://i.cdn.turner.com/nba/nba/.element/img/1.0/allstar/allstarscene/prev.png", cssClass:"nbaKyteBadgeCh1", cssFile:"http://i.cdn.turner.com/nba/nba/.element/css/1.0/allstar/allstarscene/styleSearch.css", padding:11, footerHeight:40, metaWidth:130, metaTime: false, metaViews: true, metaDescription: true, metaProducer: true, metaTitle: true, filterSearch: true,  defaultSortMode:"totalWatches-d", onClickThumb:"player", pagingControl: true, layout:"horizontal", kyteServer:kyteServer, ak:apiKey, showsPerPage: 4 }, 'kyteBadgePop');
}

function serviceCallback(json) {
  try {
		var channelUri = "thumb_channels/"+ json["result"]["items"][0]["uri"];
		var uriAsArray = channelUri.split("/");
		thumbId = "thumb_channels/"+ uriAsArray[2];
		$("thumb_channels/"+ uriAsArray[2]).src = json["result"]["items"][0]["thumb120x90Url"];
		$("produced_channels/"+ uriAsArray[2]).innerHTML = "Last Update: "+ timeCalculator(json["result"]["items"][0]["createdTime"]);
		$("views_channels/"+ uriAsArray[2]).innerHTML = json["result"]["totalSize"] +" Shows";
	} catch (err) {
	}
}

function getJSON(channelId) {
	var url = 'http://'+ kyteServer +'/services/rest/channels/'+ channelId +'.fetchShows?ak='+ apiKey +'&frt=0&mrs=1&callback=serviceCallback';

	var id = "KB_SCR_"+ channelId;
	oScript = document.getElementById(id);
	var head = document.getElementsByTagName("head")[0];
	if(oScript) {
		head.removeChild(oScript);
	}    
	oScript = document.createElement("script");
	oScript.type = "text/javascript";
	oScript.setAttribute("src", url);
	oScript.setAttribute("id",id);
	head.appendChild(oScript);
}

function timeCalculator(ISO8061) {

                var timeDiffInHours = 7;
				var showDate = new Date();
				showDate.setFullYear(ISO8061.substring(0,4));
				showDate.setMonth(ISO8061.substring(4,6)-1);
				showDate.setDate(ISO8061.substring(6,8));
				showDate.setHours(ISO8061.substring(9,11));
				showDate.setMinutes(ISO8061.substring(11,13));
				showDate.setSeconds(ISO8061.substring(13,15));
				showDate.setHours(showDate.getHours() - timeDiffInHours);
                var month;
                switch (showDate.getMonth()) {
                               case (0): month = "Jan"; break;
                               case (1): month = "Feb"; break;
                               case (2): month = "Mar"; break;
                               case (3): month = "Apr"; break;
                               case (4): month = "May"; break;
                               case (5): month = "Jun"; break;
                               case (6): month = "Jul"; break;
                               case (7): month = "Aug"; break;
                               case (8): month = "Sep"; break;
                               case (9): month = "Oct"; break;
                               case (10): month = "Nov"; break;
                               case (11): month = "Dec"; break;
                }

                var time;
                var hour = showDate.getHours();
                if (hour > 12) time = (hour - 12) +":"+ showDate.getMinutes() +"pm";
                else time = hour +":"+ showDate.getMinutes() +"am";
                var cd = month + " " + showDate.getDate() + " " + time;
               
                return cd;

}           


var schedule = new Array();
schedule[0] = new Array("Jam Session", "Wednesday, Feb. 11th", "4:00PM MT", "Convention Center");
schedule[1] = new Array("TNT Rehearsals - TV Talent and Stand Ins", "Wednesday, Feb. 11th", "6:00PM MT", "US Airways Center");
schedule[2] = new Array("TNT Rehearsals - TV Talent", "Thursday, Feb. 15th", "1:00PM MT", "US Airways Center");
schedule[3] = new Array("Jam Session", "Thursday, Feb. 14th", "4:00PM MT", "Convention Center");

//scheduleLoad();
function scheduleLoad() {
	$('event1').innerHTML = scheduleCellFill(0);
	$('event2').innerHTML = scheduleCellFill(1);
	$('event3').innerHTML = scheduleCellFill(2);
	$('event4').innerHTML = scheduleCellFill(3);
	
}

function scheduleCellFill(key) {
	var html = '<div class="scheduleEntry">';
	html 		+= '<div>'+ schedule[key][0] +'</div>';
	html 		+= '<div>'+ schedule[key][1] +'</div>';
	html		+= '<div>'+ schedule[key][2] +'</div>';
	html		+= '<div>'+ schedule[key][3] +'</div>';
	html 		+= '</div>';
	return html;
}