var slCtl, playlistCtl, initVid, u, t, d;
var curSlLinkID = 124807;
var curSlBadgeID = 0;
var useAutoDirect = true;
var xapW = '512'; //16:9 512x320, 4:3 512x416
var xapH = '416'; //if 4:3, be sure to changeAspect('L')
var initDims = false;
var installPage = '/';
var ua = navigator.userAgent;
var splashScreen = 'http://www.smartandsimpleevents.com/ClientBin/SplashScreen.xaml';
var pInitParams = 'loaderslate=http://www.smartandsimpleevents.com/ClientBin/loaderslate.xaml,settingsurl=http://www.smartandsimpleevents.com/ClientBin/Settings.xml';
var curArtGroup = 0;
var curQuote = 0;
var curPromo = 0;
var curArtGroup = 0;
var clip = null;
var quoteSpeed = 4; //seconds
var promoSpeed = 4; //minutes
$(document).ready(function(){
	$("#quotebox").fadeOut("fast");
	if(xapH=='416'){changeAspect('L');}
	parseHash();
	retryArticles();
	getQuotes();
	getPromotions();
	/* -- sharebox begin */
	$(".sharetab").click(function(){
		$(this).next(".sharebox").slideToggle(600);
		$(".sharetab").hide();
		$("#quotebox").fadeTo(100,0.3);
		setTimeout("showShareClose()",600);
	});
	$("#shareclose").click(function(){
		$(".sharebox").hide();
		$(".sharetab").fadeIn("fast");
		$("#quotebox").fadeTo(200,1);
		$("#shareclose").hide();
	});
	/* end sharebox -- */
	/* -- articlebox begin */
	$("#aboxleftclick").click(function(){
		if($("#aboxheadgroupsinner").css("margin-left")>(0-120)){
		}
	});
	$("#aboxrightclick").click(function(){
		if($("#aboxheadgroupsinner").css("margin-left")<0){
		}
	});
	/* articlebox end -- */
});
function parseHash(){
	if(window.location.hash){
		var hashP = window.location.hash.split('|');
		initVid = hashP[0].replace(/\#/g,'');
		logEntryFlow();
		setTimeout("playUrl('"+initVid+"',true)",1000);
	}else{
		//pInitParams = 'settingsurl=http://www.smartandsimpleevents.com/ClientBin/Settings.xml,media1=http://www.smartandsimpleevents.com/slate.asx,rate1=100K,defaultrate=100K,AutoPlay=true';
		logEntryFlow();
	}
}
function randomVideo(){
	var v = 0; var vv = 0;
	$.ajax({
		type: "GET",
		url: "/videos.xml",
		dataType: "xml",
		cache: false,
		success: function(xml){
			$(xml).find("video").each(function() {
				v++;
			});
			var r = Math.floor(Math.random()*(v+1));
			$(xml).find("video").each(function() {
				vv++;
				if(r==vv){
					initVid = $(this).attr('id'); /*alert(r+': '+initVid);*/
					playUrl(initVid,false);
				}
			});
		}
	});
}
function playUrl(id,autoplay) {
	initVid = id;
	$.ajax({
		type: "GET",
		url: "/ClientBin/mediaFeed.xml",
		dataType: "xml",
		cache: false,
		success: function(xml) {
			playUrlBegin(xml,autoplay);
		}
	});
}
function playUrlBegin(mediafeed,autoplay){
	var start = 0;
	var id = initVid;
	var url = '';var mediaurl = '';var media1 = '';var rate1 = '';var media2 = '';var rate2 = '';var media3 = '';var rate3 = '';var defaultrate = '';
	var title = '';
	var description = '';
	var aspect = 'W';
	$(mediafeed).find("item").each(function() {
		if ($(this).find("itemID").text() == id) {
			if ($(this).find('aspect')) {aspect = $(this).find('aspect').text();}
			if ($(this).find('itemMediaURI').find('midBitrate').attr('rate') == 'adaptive')
				mediaurl = $(this).find('itemMediaURI').find('midBitrate').text();
			else {
				media2 = $(this).find('itemMediaURI').find('midBitrate').text();
				rate2 = $(this).find('itemMediaURI').find('midBitrate').attr('rate');

				if ($(this).find('itemMediaURI').find('lowBitrate') != null) {
					media1 = $(this).find('itemMediaURI').find('lowBitrate').text();
					rate1 = $(this).find('itemMediaURI').find('lowBitrate').attr('rate');
				}

				if ($(this).find('itemMediaURI').find('highBitrate') != null) {
					media3 = $(this).find('itemMediaURI').find('highBitrate').text();
					rate3 = $(this).find('itemMediaURI').find('highBitrate').attr('rate');
				}
			}

			if ($(this).find('itemMediaURI').attr('start') != null){
				start = $(this).find('itemMediaURI').attr('start');
			}else{start = 0;}

			id = $(this).find('itemID').text();

			var url = $(this).find("itemMediaURI").find("midBitrate").text();
			title = $(this).find("itemTitle").text();
			description = $(this).find("itemAbstract").text();
			setEmbedCode(title,description,url);
		}
	});
	location.href = '#'+id+'|'+title+'|'+start
	changeAspect(aspect);
	getVideoQuote();
	//if(autoplay==true){
		if(slCtl){
			//alert('url: '+mediaurl+'\n'+id+'\n'+start+'\n'+media1+'\n'+rate1+'\n'+id+'\n'+media2+'\n'+rate2+'\n'+id+'\n'+media3+'\n'+rate3+'\n'+id+'\n'+rate2+'\n'+title+'\n'+description+'\nautoplay: '+autoplay);
			slCtl.Content.mainV.PlayUrl(mediaurl, id, start, media1, rate1, id, media2, rate2, id, media3, rate3, id, rate2, title, description, 'true');
		}
	//}
}

function setEmbedCode(t,d,u){
	var code = '&lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="512" height="'+$("#sbePlayer").css("height").replace(/px/g,'')+'"&gt;&lt;param name="source" value="http://www.smartandsimpleevents.com/ClientBin/MSFT.SBE.UI.xap" /&gt;&lt;param name="enableHtmlAccess" value="true" /&gt;&lt;param name="allowHtmlPopupWindow" value="true" /&gt;&lt;param name="background" value="transparent" /&gt;&lt;param name="windowless" value="true" /&gt;&lt;param name="minRuntimeVersion" value="3.0.40624.0" /&gt;&lt;param name="initParams" value="splashscreen=http://www.smartandsimpleevents.com/ClientBin/SplashScreen.xaml,settingsurl=http://www.smartandsimpleevents.com/ClientBin/Settings.xml,title='+t+',description='+d+',mediaurl='+u+'" /&gt;&lt;/object&gt;';
	/*alert(code);*/
	$('#embedcode').html(code);
}
function changeArtGroup(){
	curArtGroup++;
	retryArticles();
}
function retryArticles(){
	$("#aboxcontentinner").fadeOut("normal", function() {
		$.ajax({
			type: "GET",
			url: "articles.xml",
			dataType: "xml",
			cache: false,
			success: parseArticles,
			error: function (xhr, ajaxOptions, thrownError){
				//alert('XHR Status: '+xhr.status+'\nError:\n'+thrownError);
			}
		});	
	});
}
function parseArticles(xml){
	var articleBoxContent = '';
	var articleBoxGroup = '';
	var aBlocks = 0;
	var a = 0;
	var articleCount = 0;
	var aArray = new Array();
	$(xml).find("articleGroup").each(function(){
		aArray[a] = $(this).attr('id');
		a++;
	});
	if(curArtGroup>=aArray.length){curArtGroup = 0;}
	$(xml).find("articleGroup").each(function(){
		if($(this).attr('id') == aArray[curArtGroup]){
			aBlocks++;
			articleBoxGroup = $(this).find("articleGroupTitle").text();
			articleBoxContent += '<div class="aboxcontents" id="aboxcontents'+$(this).attr("id")+'">\n';
			articleBoxContent += '	<table>';
			if($(this).find("articleGroupThumb").text()!=''){
				articleBoxContent += '	<tr height="6"><td colspan="2"><img src="/images/articles/'+$(this).find("articleGroupThumb").text()+'" border="0" alt="'+$(this).find("articleGroupTitle").text()+'"/></td></tr>';
			}
			$(this).find("article").each(function(){
				articleCount++;
				articleBoxContent += '	<tr height="6"><td colspan="2" height="6"></td></tr>';
				articleBoxContent += '	<tr><td valign="top" width="47"><img src="/images/articlepad.png" border="0"/></td>';
				articleBoxContent += '	<td valign="top">';
				articleBoxContent += '	'+$(this).find("aTitle").text()+'<br/>';
				var t = $(this).find("aAbstract").text();
				if(t.length>1){
					articleBoxContent += '	<span class="artAbstract">'+$(this).find("aAbstract").text()+'</span><br/>';
				}
				articleBoxContent += '	<img src="/images/offsite-tiny-1a457a.png" border="0" alt="Go" align="right" style="margin-top:2px;"/>';
				articleBoxContent += '	<a target="articleNew" href="'+$(this).find("aLink").text()+'">Read the article</a>';
				articleBoxContent += '	</td></tr>';
			});
			//alert(articleCount);
			articleBoxContent += '	</table>\n';
			articleBoxContent += '</div>\n';
			$("#aboxheadgroupsinner").css("width",(120*aBlocks)+"px");
			$("#aboxcontentinner").css("width",(235*aBlocks)+"px");
		}
	});
	//alert(articleCount);
	$("#aboxheadleft").html('Articles: '+articleBoxGroup);
	$("#aboxcontentinner").html(articleBoxContent);
	$("#aboxcontentinner").fadeIn("normal");
}
function getPromotions(){
	$("#promobox").fadeOut("normal", function() {
		$.ajax({
			type: "GET",
			url: "promos.xml",
			dataType: "xml",
			cache: false,
			success: parsePromotions,
			error: function (xhr, ajaxOptions, thrownError){
				//alert('XHR Status: '+xhr.status+'\nError:\n'+thrownError);
			}
		});	
	});
}
function parsePromotions(xml){
	var promoContent = '';

	var pCount = new Array();
	var p = 0;
	$(xml).find("promo").each(function(){
		pCount[p] = $(this).attr("id"); p++;
	});
	if(curPromo>=pCount.length){ curPromo=0; }
	//alert(curPromo+': '+pCount[curPromo]);
	$(xml).find("promo").each(function(){
		if($(this).attr("id")==pCount[curPromo]){
			promoContent = $(xml).find("data").text();
		}
	});
	$("#promobox").html(promoContent);
	$("#promobox").fadeIn("normal");
	curPromo++;
	setTimeout("getPromotions()",(promoSpeed*60*1000));
}
function getVideoQuote(){
	$("#quotebox").fadeOut("normal", function() {
		$.ajax({
			type: "GET",
			url: "videoquotes.xml",
			dataType: "xml",
			cache: false,
			success: showVideoQuote,
			error: function (xhr, ajaxOptions, thrownError){
			//	alert('XHR Status: '+xhr.status+'\nError:\n'+thrownError);
			}
		});	
	});
}
function showVideoQuote(xml){
	$(xml).find("quote").each(function(){
		//alert($(this).attr("videoid")+'\n'+initVid+'\n'+$(this).find("quotetext").text());
		if($(this).attr("videoid")==initVid){
			$("#quotebox").html('<div id="videoquote">'+$(this).find("quotetext").text()+'<br/><em>-- '+$(this).find("quoter").text()+'</em></div>');
			$("#quotebox").fadeIn("fast");
		}
	});
}
function getQuotes(){
	$("#randquote").fadeOut("normal", function() {
		$.ajax({
			type: "GET",
			url: "quotes.xml",
			dataType: "xml",
			cache: false,
			success: parseQuotes,
			error: function (xhr, ajaxOptions, thrownError){
				//alert('XHR Status: '+xhr.status+'\nError:\n'+thrownError);
			}
		});	
	});
}
function parseQuotes(xml){
	var quoteBoxContent = '';
	var qCount = new Array();
	var q = 0;
	$(xml).find("quote").each(function(){
		qCount[q] = $(this).attr("id"); q++;
	});
	if(curQuote>=qCount.length){ curQuote=0; }
	$(xml).find("quote").each(function(){
		//alert(curQuote+'\n'+$(this).attr("id")+'\n'+qCount[curQuote]);
		if($(this).attr("id")==qCount[curQuote]){
			if($(this).find("data").text()!=''){ quoteBoxContent += '<div class="quotetext">'+$(this).find("data").text()+'</div>\n'; }
			if($(this).find("name").text()!=''){ quoteBoxContent += '<div class="quotename"><span class="qinner"><em>'+$(this).find("name").text()+'</em></span></div>\n'; }
			if($(this).find("title").text()!=''){ quoteBoxContent += '<div class="quotetitle"><span class="qinner"><em>'+$(this).find("title").text()+'</em></span></div>\n'; }
			if($(this).find("siteName").text()!='' && $(this).find("siteLink").text()!=''){ 
				quoteBoxContent += '<div class="quotesite"><a href="'+$(this).find("siteLink").text()+'" target="_blank">'+$(this).find("siteName").text()+'</a></div>\n'; 
			} else if($(this).find("siteLink").text()!=''){ 
				quoteBoxContent += '<div class="quotesite"><a href="'+$(this).find("siteLink").text()+'" target="_blank">'+$(this).find("siteLink").text()+'</a></div>\n'; 
			}
			if($(this).find("blogLink").text()!=''){ quoteBoxContent += '<div class="quoteblog"><a href="'+$(this).find("blogLink").text()+'" target="_blank">'+$(this).find("blogLink").text()+'</a></div>\n'; }
		}
	});
	$("#randquote").html(quoteBoxContent);
	$("#randquote").fadeIn("fast");
	curQuote++;
	setTimeout("getQuotes()",(quoteSpeed*1000));
}
function showShareClose(){
	$("#shareclose").fadeIn("fast");
}
function slLoaded(sender, args) {
	slCtl = sender.getHost();
//	if(!window.location.hash){
//		setTimeout("randomVideo()",1000);
//	}
}
function playlistLoaded(sender, args) {
	playlistCtl = sender.getHost();
}
function changeAspect(aspect){
	var baseH = 320;
	if(aspect=='L'){ baseH = 416; }
	$("#silverlightControlHost").css('height',baseH+'px');
	$("#silverlightPlayer").css('height',baseH+'px');
	$("#sbePlayer").css('height',baseH+'px');
	$(".sharebox").css('top',(baseH+20)+'px');
	$(".sharetab").css('top',(baseH+20)+'px');
	$("#quotebox").css('top',(baseH+24)+'px');
	$("#columns").css('marginTop',(baseH-400)+'px'); //320
}
function getICS(id) {
	datahole.location.href = id+'.ics';
}
function addThis_goto(to){
	dcsMultiTrack('DCS.dcsuri','/silverlight/video_share/'+to+'/'+document.title,'WT.ti','Link: '+to+': '+document.title,'WT.z_element','Video_share','WT.clip_n',document.title,'WT.z_share',to,'WT.dl','7');
	//WT.z_tickername=WT.z_tickerclick=WT.z_tabname=WT.clip_n=WT.clip_ev=WT.z_share='';
	window.open('http://www.addthis.com/bookmark.php?v=250&winname=addthis&pub=AddThis&source=max-250&lng=en&s='+to+'&url=http://www.smartandsimpleevents.com/?vid='+window.location.hash.replace('#','')+'&title='+document.title);
}
function dcsClearVars(){
	WT.z_tickername=WT.z_tickerclick=WT.z_tabname=WT.clip_n=WT.clip_ev=WT.z_share='';
}
function register(url) {
	window.open(url);
}
function copyEmbed(){
	var text = $("#embedcode").html();
	text = text.replace(/&gt\;/g,'>');
	text = text.replace(/&lt\;/g,'<');
	clip.setText('Boom');
	clip.glue( 'd_clip_button' ); 
}

function changeEvent(title) {
	playlistCtl.Content.main.ChangeEvent(title);
}
