var ctvWidgetHome = (typeof ctvWidgetHome != 'undefined') ? ctvWidgetHome : 'http://widgets.current.com';

function ctvGetGroupWidget(group, type) {
	if(group == 'rt-rt') group = 'groups|rt-rt'; 
	var count = (typeof ctvWidgetCount != 'undefined') ? ctvWidgetCount : 4;
	var sort = (typeof ctvWidgetSort != 'undefined') ? ctvWidgetSort : '';
	var callback = (typeof ctvWidgetCallback != 'undefined') ? ctvWidgetCallback : 'ctvSearchCallback';
	var apiUrl = ctvWidgetHome + '/widgets/api/?channel=group:'+group+'/type:'+type+'/sort:'+sort+'&count='+count+'&callback='+callback;
//	document.write('<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>');
	document.write('<script type="text/javascript" src="' + apiUrl + '"></script>');
}

function ctvGetUserWidget(username) {
	var count = (typeof ctvWidgetCount != 'undefined') ? ctvWidgetCount : 4;
	var callback = (typeof ctvWidgetCallback != 'undefined') ? ctvWidgetCallback : 'ctvSearchCallback';
	var apiUrl = ctvWidgetHome + '/widgets/api/?channel=username:'+username+'&count='+count+'&callback='+callback;
	document.write('<script type="text/javascript" src="' + ctvWidgetHome + '/widgets/v1/omniture.js"></script>');
	document.write('<script type="text/javascript" src="' + apiUrl + '"></script>');
}

function ctvSearchCallback(obj) {
	if(!obj || !obj.results) return;

	var imageBaseUrl = ctvWidgetHome + '/widgets/v1/img';

	var title = (typeof ctvWidgetTitle != 'undefined') ? ctvWidgetTitle : '';
	var width = (typeof ctvWidgetWidth != 'undefined') ? ctvWidgetWidth : 300;
	var linkColor = (typeof ctvWidgetLinkColor != 'undefined') ? ctvWidgetLinkColor : '414141';
	var linkFontSize = (typeof ctvWidgetLinkFontSize != 'undefined') ? ctvWidgetLinkFontSize : 12;
	var linkFontWeight = (typeof ctvWidgetLinkFontWeight != 'undefined') ? ctvWidgetLinkFontWeight : 'bold';
	var textColor = (typeof ctvWidgetTextColor != 'undefined') ? ctvWidgetTextColor : '666666';
	var headerColor = (typeof ctvWidgetHeaderColor != 'undefined') ? ctvWidgetHeaderColor : 'ffffff';
	var headerBgColor = (typeof ctvWidgetHeaderBgColor != 'undefined') ? ctvWidgetHeaderBgColor : '333333';
	var bgColor = (typeof ctvWidgetBgColor != 'undefined') ? ctvWidgetBgColor : 'ffffff';
	var lineColor = (typeof ctvWidgetLineColor != 'undefined') ? ctvWidgetLineColor : 'cccccc';
	var lineStyle = (typeof ctvWidgetLineStyle != 'undefined') ? ctvWidgetLineStyle : 'dashed';
	var borderColor = (typeof ctvWidgetBorderColor != 'undefined') ? ctvWidgetBorderColor : '333333';
	var playIconTop = (typeof ctvWidgetPlayIconTop != 'undefined') ? ctvWidgetPlayIconTop : 25;
	var playIconLeft = (typeof ctvWidgetPlayIconLeft != 'undefined') ? ctvWidgetPlayIconLeft : 40;
	var itemMinHeight = (typeof ctvWidgetItemMinHeight != 'undefined') ? ctvWidgetItemMinHeight : 45;
	var itemMarginTop = (typeof ctvWidgetItemMarginTop != 'undefined') ? ctvWidgetItemMarginTop : 10;
	var itemMarginLeft = (typeof ctvWidgetItemMarginLeft != 'undefined') ? ctvWidgetItemMarginLeft : 10;

	var clickUrl = (typeof ctvWidgetClickUrl != 'undefined') ? ctvWidgetClickUrl : '';
	if(clickUrl.indexOf('http://') != 0) clickUrl = '';

	if(!title && obj.title) {
		title = obj.title;
		if(obj.sub_title) title += ' <span class="ctvHeaderSubTitle">' + obj.sub_title + '</span>';
	}

	var css = [
'<style type="text/css">',
'#ctvWidget {margin:0; padding:0; text-align:left; background-color:#' + bgColor + '; color:#' + textColor + '; font-size:11px; font-family:\'Helvetica Neue\',Helvetica,Arial,sans-serif; border:1px solid #' + headerBgColor + '; width:' + (width-2) + 'px;}',
'#ctvWidget a.ctvTitle, #ctvWidget a.ctvTitle:visited, #ctvWidget a.ctvTitle:active {text-decoration:none;color:#' + linkColor + '; font-weight:'+linkFontWeight+'; font-size:'+linkFontSize+'px;}',
'#ctvWidget a:hover {text-decoration: underline;}',
'#ctvWidget #ctvHeader {height:40px; margin:0; overflow:hidden; padding:3px 0 0; color:#' + headerColor + '; background-color:#' + headerBgColor + ';}',
'#ctvWidget #ctvHeader .ctvHeaderLeft {width:180px; height:40px; float:left; padding-left:10px;}',
'#ctvWidget #ctvHeader .ctvHeaderRight {width:90px; float:right;}',
'#ctvWidget #ctvHeader .ctvHeaderTitle {margin:0 0 2px; padding-bottom:0; font-size:18px; font-weight:bold;}',
'#ctvWidget #ctvHeader .ctvHeaderSubTitle {color:#' + textColor + ';}',
'#ctvWidget .ctvItem {position:relative; margin:'+itemMarginTop+'px '+itemMarginLeft+'px 0; padding-bottom:10px; clear:both; color:#' + textColor + '; border-bottom:1px '+lineStyle+' #' + lineColor + '; min-height:'+itemMinHeight+'px;}',
'#ctvWidget .ctvAsset {float:left; position:relative; height:45px;}',
'#ctvWidget a.ctvThumb {margin: 0 5px 5px 0; border:0;}',
'#ctvWidget .ctvMore {text-align:left; padding:6px 10px; clear:both;}',
'#ctvWidget .ctvMore a {font-size: 11px;}',
'#ctvWidget .ctvLogo {background:transparent url(' + imageBaseUrl + '/logo-small.gif) no-repeat scroll left top; float:right; height:15px; margin:10px 10px 10px 0; width:75px;}',
'#ctvWidget .ctvPlay {background:transparent url(' + imageBaseUrl + '/play.gif) no-repeat; height:17px; width:17px; position:absolute; left:'+playIconLeft+'px; top:'+playIconTop+'px; z-index:1001; cursor: pointer}',
'#ctvWidget #ctvOverlay {display:none; position:absolute; left:0px; top:0px; width:470px; height:400px; z-index:1002;}',
'#ctvWidget #ctvOverlayContainer {position:relative;width:470px;height:400px;}',
'#ctvWidget #ctvOverlayBorder {position:absolute;top:0;left:0;width:460px; height:390px; -webkit-border-bottom-left-radius:5px; -webkit-border-bottom-right-radius:5px; -webkit-border-top-left-radius:5px; -webkit-border-top-right-radius:5px; -moz-border-radius-bottomleft:5px; -moz-border-radius-bottomright:5px; -moz-border-radius-topleft:5px; -moz-border-radius-topright:5px; opacity:0.75; -moz-opacity:0.75; filter:alpha(opacity=75); border:10px solid #' + borderColor + '; background-color:#' + bgColor + '; color:#' + textColor + '}',
'#ctvWidget #ctvOverlayMain {position:absolute;top:10px;left:10px;width:460px;height:390px;overflow:hidden;background-color:#' + bgColor + ';}',
'#ctvWidget #ctvOverlay .ctvClose {text-align:right;margin:5px;}',
'#ctvWidget #ctvOverlay .ctvClose a {color:#' + textColor + '; text-decoration:none;}',
'#ctvWidget #ctvOverlayBody {margin:0 30px;}',
'#ctvWidget #ctvVideoPlayer {width:400px; height:300px;}',
'</style>'
	].join("\n");

	var html = [
		'<div id="ctvHeader">',
		'<div class="ctvHeaderLeft">',
		'<div class="ctvHeaderTitle">' + title + '</div>',
		'<img src="' + imageBaseUrl + '/logo-ccn.gif" width="133" height="5" border="0"/>',
		'</div>',
		'<div class="ctvHeaderRight">',
		'<a title="current.com" target="_blank" class="ctvLogo"',
		' href="' + clickUrl + 'http://current.com/?xid=widget"',
		' onclick="ctvClickLogo(\'' + obj.channel +  '\');"></a>',
		'</div>',
		'</div>'
	].join("\n");

	var videoPlay = (typeof ctvWidgetVideoPlay != 'undefined') ? ctvWidgetVideoPlay : 'embed';
	for (var i=0; i<obj.results.length; i++) {
		var item = obj.results[i];
		var itemLink = clickUrl + item.link + '?xid=widget';

		var onclick = '';
		if(item.asset_type == 'V' && videoPlay == 'embed') {
			onclick = ' onclick="ctvPlayVideo(' + item.id
				+ ', \'' + itemLink + '\''
				+ ', \'' + escape(item.title) + '\''
				+ ', ' + item.comment_count
				+ ', \'' + obj.channel + '\');'
				+ 'return false;"';
		} else {
			onclick = ' onclick="ctvClickStory(\'' + obj.channel + '\', \'' + item.id + '\');"'; 
		}

		html += '<div class="ctvItem">' + "\n";
		if(item.thumbnail) {
			html += '<div class="ctvAsset"><a href="' + itemLink + '" class="ctvThumb" target="_blank"' + onclick + '>';
			if(item.asset_type == 'V')  html += '<div class="ctvPlay"></div>';
			html += '<img src="' + item.thumbnail + '" width="60" height="45" border="0"/></a></div>' + "\n";
		}
		html += '<a href="' + itemLink + '" class="ctvTitle" target="_blank"'
			+ onclick + '>' + item.title + '</a>' + "\n"
			+ '<br/>' + item.description + "\n"
			+ '</div>' + "\n";
	}
	
	if(obj.more_url) {
		html += '<div class="ctvMore">'
			+ '<a href="' + clickUrl + obj.more_url + '" target="_blank"'
			+ ' onclick="ctvClickMore(\'' + obj.channel + '\')">see more</a>'
			+ '</div>' + "\n";
	}
	html += '<div id="ctvTrackingHolder" style="display:none;"></div>' + "\n";

	html += [
		'<div id="ctvOverlay">',
		'<div id="ctvOverlayContainer">',
		'<div id="ctvOverlayBorder"></div>',
		'<div id="ctvOverlayMain">',
		'<div class="ctvClose"><a href="#" onclick="ctvStopVideo();return false;">',
		'<img src="' + imageBaseUrl + '/close.gif" border="0"/></a></div>',
		'<div id="ctvOverlayBody"></div>',
		'</div>',
		'</div>',
		'</div>'
	].join("\n");

	document.write(css);
	document.getElementById('ctvWidget').innerHTML = html;
	ctvTrackAction({act: 'widget-load', cat: obj.channel})
}

function ctvClickLogo(channel) {
	ctvTrackAction({act: 'click-logo', cat: channel});
}

function ctvClickStory(channel, contentId) {
	ctvTrackAction({act: 'click-story', cat: channel, cid: contentId});
}

function ctvClickMore(channel) {
	ctvTrackAction({act: 'click-more', cat: channel});
}

function ctvStopVideo() {
	var ctvOverlayBody = document.getElementById('ctvOverlayBody');
	ctvOverlayBody.innerHTML = '';
	var ctvOverlay = document.getElementById('ctvOverlay');
	ctvOverlay.style.display = 'none';
	return false;
}

function ctvPlayVideo(contentId, link, title, comments, channel) {
	var videoWidth = 400;
	var videoHeight = 300;

	var videoSrc = 'http://i2.current.com/swf/current/veep.swf'
		+ '?contentId=' + contentId + '&autoplay=true';

	var html = '<div id="ctvVideoPlayer">'
		+ '<object id="ce_' + contentId + '"'
		+ ' width="' + videoWidth + '"'
		+ ' height="' + videoHeight + '"'
		+ ' data="' + videoSrc + '">'
		+ '<param name="movie" value="' + videoSrc + '"></param>'
		+ '<param name="wmode" value="transparent"></param>'
		+ '<param name="allowfullscreen" value="true"></param>'
		+ '<param name="allowscriptaccess" value="always"></param>'
		+ '<embed type="application/x-shockwave-flash" src="' + videoSrc + '"'
		+ ' width="' + videoWidth + '"'
		+ ' height="' + videoHeight + '"'
		+ ' wmode="transparent" allowfullscreen="true" allowscriptaccess="always">'
		+ '</embed>'
		+ '</object>'
		+ '</div>';

	html += '<div style="margin-top:10px;"><a href="' + link 
		+ '" class="ctvTitle" target="_blank">' 
		+ unescape(title) + '</a>';
	if(comments > 0) {
		html += '<br/>' + comments + ' responses';
	}

	var ctvOverlayBody = document.getElementById('ctvOverlayBody');
	ctvOverlayBody.innerHTML = html;

	var ctvWidget = document.getElementById('ctvWidget');
	var widgetPos = ctvFindPos(ctvWidget);

	var ctvOverlay = document.getElementById('ctvOverlay');

	var overlayLeft = widgetPos.left;
	if(overlayLeft >= 490) {
		overlayLeft -= 490;
	} else {
		overlayLeft += 305;
	}
	ctvOverlay.style.left = overlayLeft + 'px'; 
	ctvOverlay.style.top = widgetPos.top + 'px';
	ctvOverlay.style.display = 'block';

	var flashvars = {
		contentId: contentId,
		trackingBucket: 'curvembeds',
		autoplay: 'true',
		externalContext: 'widget',
		context: 'widget',
		permalink: link,
		allowFullScreen: 'true',
		allowscriptaccess: 'always'
	};
	ctvTrackAction({act: 'click-video', cat: channel, cid: contentId});

	return false;
}

function ctvGetStyle(element, style) {
	var value = element.style[style];
	if(value) return value;

 	if (document.defaultView && document.defaultView.getComputedStyle) {
 		var css = document.defaultView.getComputedStyle(element, null);
 		value = css ? css.getPropertyValue(style) : null;
	} else if (element.currentStyle) {
		value = element.currentStyle[style];
	}
	return value;
}

function ctvFindPos(obj) {
	var curtop = 0;
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent && ctvGetStyle(obj, 'position') != 'relative') {
			curtop += obj.offsetTop
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.y && obj.x) {
		curtop += obj.y;
		curleft += obj.x;
	}

	return {'top' : parseInt(curtop), 'left' : parseInt(curleft)};
}

function ctvTrackAction(params) {
	try { 
		var url = '';
		for (p in params) {
			url += '/' + p + '[' + params[p] + ']';
		}

//		var pageTracker = _gat._getTracker("UA-6771039-9"); 
//		pageTracker._trackPageview(url);

		var tparams = {objectType:14};
		if(params['act'] == 'widget-load') {
			tparams['eventType'] = 1;
		} else if(params['act'] == 'click-video') {
			tparams['eventType'] = 40;
			tparams['objectId'] = params['cid'];
		} else if(params['act'] == 'click-story') {
			tparams['eventType'] = 41;
			tparams['objectId'] = params['cid'];
		} else if(params['act'] == 'click-more') {
			tparams['eventType'] = 42;
		} else if(params['act'] == 'click-logo') {
			tparams['eventType'] = 43;
		}

		tparams['url'] = window.location.href;
		if (document.referrer && document.referrer != "") {
			tparams['referer'] = document.referrer;
		}

		var args = '?img=1';
		for (p in tparams) {
			args += '&' + p + '=' + encodeURIComponent(tparams[p]);
		}

		var t = '<img width="0" height="0" src="http://current.com/tracking.htm' + args + '"/>';
		document.getElementById('ctvTrackingHolder').innerHTML = t;
	} catch(err) {
	}
}

var ctvWidgetGroup = (typeof ctvWidgetGroup != 'undefined') ? ctvWidgetGroup : '';
var ctvWidgetType = (typeof ctvWidgetType != 'undefined') ? ctvWidgetType : '';
if(ctvWidgetGroup) {
	ctvGetGroupWidget(ctvWidgetGroup, ctvWidgetType);
}

