var browser_IE;
var browser_NS4;
var browser_W3C;
var platForm;
var isMac;
browser_IE  = (document.all && navigator.appName == 'Microsoft Internet Explorer') ? true : false;
browser_NS4 = (document.layers) ? true : false;
browser_W3C = (!document.all && document.getElementById) ? true : false;

platForm=navigator.platform;
isMac=platForm.indexOf("Mac");

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
	rnd.seed = (rnd.seed*9301+49297) % 233280;
	return rnd.seed/(233280.0);
}

function rand(number) {
	return Math.ceil(rnd()*number);
}


function playVideo(video_link_high, video_link_low, clip_start, clip_end, clip_caption, archive, slug, sortdate, clip_id, hub){
	var hubcheck;
	if (hub == '') {
			hubcheck = 'TopStories';
	} else {
			hubcheck = hub;
	}
	var w = 486, h = 603; // adjust width and height here.
	var vUrl = "/servlet/HTMLTemplate/!ctvVideo/"
		+ archive + "/"
		+ slug + "/"
		+ sortdate + "/?"
; // the video url
	var qString = 'hub=' + hubcheck + '&'
		+ 'video_link_high=' + video_link_high + '&'
		+ 'video_link_low=' + video_link_low + '&'
		+ 'clip_start=' + clip_start + '&'
		+ 'clip_end=' + clip_end + '&'
		+ 'clip_caption=' + escape(clip_caption) + '&'
		+ 'clip_id=' + clip_id;

	window.open( vUrl + qString
		+'&subhub=video','ctvVideo','width=' + w + ',height=' + h
		+',status=no,scrollbars,resizable=no,toolbar=no,left=176,screenX=200,top=0,screenY=0');
}


function format_clip_duration(dur){
	// clip end:   00:02:50.43
	// clip_end     0:02:26
	// clip_end has one of the two above formats.

	var matches, format = "";

	var regexFormat1 = /^(\d{2}):(\d{2}):(\d{2}\.\d{2})$/
	var regexFormat2 = /^(\d+):(\d+):(\d+)$/

	if( regexFormat1.test(dur) ){
		matches = dur.match(regexFormat1);
	}
	else if( regexFormat2.test(dur) ){
		matches = dur.match(regexFormat2);
	}
	if( matches == null ) return ''; // didn't match either pattern.
	var durHours = parseInt(matches[1], 10);
	var durMins  = parseInt(matches[2], 10);
	var durSecs  = Math.floor(parseFloat(matches[3]));
	var durSeconds = durHours*60*60 + durMins*60 + durSecs;
	var one_hour = 60*60;
	var formattedSecs = (durSecs < 10) ? '0' + durSecs : durSecs;
	if( durSeconds > one_hour ){
		format = durHours + ':' + (durMins<10 ? '0'+durMins : durMins) + ':' + formattedSecs;
	}
	else{
		format = durMins + ':' + formattedSecs;
	}
	return format;
}


// Trims a string
function trim(str) {
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

var _thisPopWidth = "";
var _thisPopHeight = "";

/**
  * Used to correct the MAC date bug
  */
function fixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0) {  // Except on the Mac - ahead of its time
  	date.setTime (date.getTime() - skew);
	}
}


function setCookie (cookieName, cookieValue, expires, path) {
	fixCookieDate(expires);
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; EXPIRES=' + expires.toGMTString() : '')
		+ (path ? '; PATH=' + path : '');
}

function framesPopUp(filename,winTitle, winName, winX, winY) {
 winName = "__" + winName;
 var regex = /\W+/;
 if( regex.test(winName) ) {
    winName = winName.replace(/\W+/, '_');
 }
 params = "width="+ winX +",height="+winY;
 winExec = winName +"= window.open('','"+winName+"', params)"
 content = "<html><head><title>"+winTitle+"</title><script>window.onload=function(){}</script></head><frameset rows='0,*' frameborder='NO' border='0' framespacing='0'><frame name='topFrame' scrolling='NO' noresize src='about:blank'><frame name='mainFrame' src='"+ filename +"' scrolling=AUTO></frameset><noframes><body bgcolor='#FFFFFF' text='#000000'></body></noframes></html>"
 eval(winExec);
 eval(winName + ".document.open()")
 eval(winName + ".document.write(content)")
 eval(winName + ".document.close()")
}

function _open_popup_window(w, h, link_url, slug, title){
        var regex = /\W+/;
        if( regex.test(slug) ){
                slug = slug.replace(/\W+/, '_');
        }
        if( w != '' && h != '' ) {
                framesPopUp(link_url, title, slug, w, h);
                return false;
        }
        return true;
}

