/*
FastcastMedia

Author: Unknown
Updated: Jonathan Mangano
Dependencies: CookieLibrary.js
*/


function MakeArray(str, sep) {
	strArray = new Array();
	var pipe = 0; var start = 0; var count = 0;
	var sepLength = sep.length;

	while (pipe > -1) {
		pipe = str.indexOf(sep, pipe + 1);
		if (pipe == -1) {
			strArray[count] = str.substring(start, str.length)
		} else {
			strArray[count] = str.substring(start, pipe);
			start = pipe+sepLength;
			count++;
		}
	}  
	return strArray;
}

function SetFCCookie( cookieName, cook, x ) {
	WM_setCookie (cookieName, cook + '|' + x, 365*24);
	WM_setCookie (cookieName + 'C', '2');	
}

function ShowFastCast(amount, attribs, cookieName, cgiNumbers, FastcastUrl, PlayerType, RealURL, takeOverURL, takeOverLocation) {
	var qs = location;
	var strQs = location.toString();
	
	if ( strQs.indexOf("pp=1") == -1 ) {
		var link = "";
		var newCookie = "";
		var cook = WM_readCookie(cookieName);
		if (!cook) {
			cook = null;
		}
		var cginumber;
		var x = 1;
		var go = true;
		
		var finalUrl = getFinalURL( PlayerType, FastcastUrl, RealURL );
		
		cgis = MakeArray(cgiNumbers, ";");
	
		if (finalUrl == null) return false;		
	
		if (go == true) {
			var qsVideoNumber = getQueryVariable("video");

			if (qsVideoNumber == "") {
				while ( x <= amount ) {
					if (cook == null || cook.indexOf(strQs.toString()) == -1) {
						if ( ShowFcPopup ( x, cook, attribs, cgis[x-1], finalUrl, cookieName, amount, takeOverURL, takeOverLocation, false ) ) {
							break;
						}
						x++;	
					} // cook == null
				} // x <= amount
			} else if (!isNaN(qsVideoNumber)) {
				ShowFcPopup ( qsVideoNumber, cook, attribs, cgis[qsVideoNumber-1], finalUrl, cookieName, amount, takeOverURL, takeOverLocation, true )
			}	
		} // go == true
	} // pp=1
}

function ShowFcPopup(x, cook, windAttribs, cginum, fastcastUrl, cookieName, amount, takeOverURL, takeOverLocation, forcePopup) {
	if ( (( cook == null || cook.indexOf(x.toString()) == -1 ) && WM_readCookie( "fcshown" ) != "1") || forcePopup ) {
		var ref = fcPageType;
		var takeOverShown = false;	

		WM_setCookie ('fcshown', '1', 0.25, '');

		var finalUrl = 'http://ww3.peerviewpress.com/gpc.nsf/doc?CreateDocument&n=' + cginum + '&id=48DDE4A73E09A969852568880078C249&t=00-00-00;AM&l=PVP&u=' + fastcastUrl + '&s=y&ref=' + ref + '&video=' + x + "&dist=" + distType + "&num=" + distNumber;
		takeOverCookie = WM_readCookie(cookieName + "_takeOver");

		if (takeOverURL != null && takeOverCookie == false && (takeOverLocation == ref.toLowerCase() || takeOverLocation == 'both')) {
			var win2 = window.open(takeOverURL + "?u=" + finalUrl, 'wndFastcast', windAttribs); 		
			WM_setCookie(cookieName + "_takeOver" , 'y', 24 * 365);		
			takeOverShown = true;
			fcshown = true;		
		} else {		
			var win2 = window.open(finalUrl, '', windAttribs);
			win2.blur();
			window.focus();	
			fcshown = true;		
		}
	
		if (takeOverShown == false) {
			if ( amount > 1 ) {
				WM_setCookie(cookieName, cook + "|" + x, 365*24);
			} else {
				if ( WM_readCookie( cookieName + "C" ) == "1" ) {
					SetFCCookie(cookieName,cook,x)
				} else {
					WM_setCookie(cookieName + 'C', '1', 365*24);
					setTimeout( "SetFCCookie('" + cookieName + "','" + cook + "','" + x + "')", 30000 );
				}
			}
		}

		return true;
	} else {
		return false;
	}
}
