/*
Copyright 2005, Internet Broadcasting Systems. All Rights Reserved.
Version:   $Name $
ID:        $Id $
*/

// AD MANAGEMENT
adTagParam = '';
leaderboard = false;
lastAdStatus = '';
adtracked = false;

function fnAdCall(t, tracker) {
	if(!leaderboard) {
		videoAdTag = (t)? t+';':'';
		var rand = Math.floor((Math.random()*1000000000));
		var adTag = 'http://ad.doubleclick.net/adi/ibs.' + siteinfo.sitekey.lc + '.interactive/' + window.adZone + ';kw=video+'+myPlaylist.currentCategory+'+'+myPlaylist.currentSubcategory+'+banner1+' + myPlaylist.currentVideoID + ';ad=true;pgtype=detail;sz=728x90;' + videoAdTag + 'ord='+rand+'?';

		if(getEl("banner1") && getEl("banner1").parentElement)
		{
			getEl('banner1').parentElement.innerHTML = '<iframe id="banner1" src="' + adTag + '" width="728" height="90" marginwidth="0" marginheight="0" scrolling="no" frameborder="0"></iframe>';
		}

		//getEl('banner1').document.location.replace(adTag);
		// Create Tracker if ad has tracking pixel url
		if(typeof tracker != 'undefined' && tracker.indexOf('http') != -1) {
			var trackingimage = new Image();
			trackingimage.src=tracker;
		}
		leaderboard = true;
	}
}
function logAdStatus(s) {
	var rand = Math.floor((Math.random()*1000000000));
	//var adTag = 'http://ad.doubleclick.net/adi/ibs.' + siteinfo.sitekey.lc + '.interactive/;kw=' + videoplayer.videoid + '+' + s + ';ad=true;tile=1;pgtype=detail;sz=1x1;' + videoAdTag + ';ord='+rand+'?';
	//if(lastAdStatus != s)commframe.location.href = adTag;
	//lastAdStatus = s;
}

// PLAYER CONTROLS
function fullScreen() {
	// first, check to see if video player is playing or paused
	if (typeof videoplug != "undefined" && (videoplug.playState == 2 || videoplug.playState == 3)){
		if(confirm("You are about to go to Full Screen mode. Press ESC to return to normal mode. Do you wish to continue?"))videoplug.fullScreen = 'true';
	}
}
function showHelp(){
	if(typeof myPlaylist == 'undefined') {
		window.open('/surveypopup/3369893/detail.html', 'help', 'width=320,height=400,toolbar=no,location=no,status=no,menuBar=no,scrollBars=1,resizable=yes,top=50,left=50');
	} else {
		window.open('/surveypopup/4813639/detail.html', 'help', 'width=320,height=400,toolbar=no,location=no,status=no,menuBar=no,scrollBars=1,resizable=yes,top=50,left=50');

	}
}
function emailVideo() {
		//pageProps={envhost:"www.kcra.com"};
		if(typeof pageProps != 'undefined') hostElems = pageProps.envhost.split('.');
		else hostElems = new Array('www','ibsys','com');
		emailPage = 'http://cf.' + hostElems[1] + '.' + hostElems[2] + '/' + siteinfo.sitekey.lc + '/sh/toafriend/index.cfm?page=' + hostElems[0] + '.' + hostElems[1] + '.' + hostElems[2] + '/video/' + myPlaylist.currentVideoID + '/index.html';
		//emailPage = 'http://cf.' + sitehost + '/' + sitekey + '/sh/toafriend/index.cfm?page=' + siteDomain + '/video/' + videoplayer.videoid + '/detail.html';
		window.open(emailPage,'emailafriend','width=450,height=225');
	}

var omitformtags='input,textarea,select';
function disableselect(e){
	return false;
}
function reEnable(){
	return true
}
if (typeof document.onselectstart != "undefined") {
	document.onselectstart = checkEl;
}else{
	document.onmousedown=disableselect;
	document.onmouseup=reEnable;
}
function checkEl() {
	return false;
}

function getEl(el) {
	return document.getElementById(el);
}


// FIND THE TRUE POSITION OF AN ELEMENT
function getRealPos(i,which) {
	iPos = 0;
	while (i!=null) {
		iPos += i["offset" + which];
		i = i.offsetParent;
	}
	return iPos
}
isMacIE = (navigator.userAgent.toLowerCase().indexOf('mac') != -1 && navigator.userAgent.toLowerCase().indexOf('msie') != -1);
dragElement = null;

function ibsPlaylist() {
	if(document.getElementById && document.createElement) {
		myPlaylist = this;
		this.currentCategory = '';
		this.currentSubcategory = '';
		this.currentVideoAction = null;
		this.orientation = 'horizontal';
		this.dragonid = 0;
		this.insertAt = 0;
		this.nextVideoID = 0;
		this.previousVideoID = 0;
		this.currentVideoID = 0;
		this.idList = new Array();
		this.crumbs = new Array();
		this.entries = new Array();
		this.pairs = document.cookie.split('; ');
		if(this.pairs){
			for (c in this.pairs) {
				if(typeof this.pairs[c] != 'function'){
					cookieHolder = this.pairs[c].split('=');
					this.crumbs[cookieHolder[0]] = unescape(cookieHolder[1]);
				}
			}
		}
		this.idList = (this.crumbs['ibsPlaylist'])?this.crumbs['ibsPlaylist']:'';

		dply = document.createElement('div');
		dply.setAttribute('id', 'playlistDIV');
		if(window.ActiveXObject) dply.setAttribute('className', 'playlistDIV');
		else dply.setAttribute('class', 'playlistDIV');
		if(getEl('playlist')) this.displayElement = getEl('playlist').appendChild(dply);
		//else this.displayElement = document.body.appendChild(dply);
		myPlaylist.displayElement.onmouseover = function(e){
			thelist = getEl('playlist');
			if (window.event) e = window.event;
			if(navigator.userAgent.toLowerCase().indexOf('gecko') == -1) {
				pageLeft = e.clientX;
			} else {
				pageLeft = e.pageX;
			}
			mousePosition = pageLeft - getRealPos(thelist,'Left');
			if(mousePosition < 100 && myPlaylist.dragonid){
				//scrollLeft
				thelist.scrollLeft = thelist.scrollLeft - 50;
			} else if(mousePosition > 450 && myPlaylist.dragonid){
				//scrollRight
				thelist.scrollLeft = thelist.scrollLeft + 50;
			};
		}
		myPlaylist.displayElement.onmouseout = function(){
			window.status = '';
		}
		this.currentList = '';
		// fetchXML function gets the XML from the site MIGHT HAVE TO ENSURE THAT I CAN DO THIS WITHIN THE DOMAIN
		this.fetchXML = function() {
			if (window.ActiveXObject) {
				 //WINDOWS
				myPlaylist.xmldata = new ActiveXObject("Microsoft.XMLHTTP");
			} else if (document.implementation && document.implementation.createDocument) {
				//MOZILLA
				myPlaylist.xmldata = new XMLHttpRequest();
			}
			myPlaylist.xmldata.onreadystatechange = myPlaylist.writePlaylist;
			myPlaylist.xmldata.open("GET", '/_objectxml/' + this.idList + '/detail.html', true);
			myPlaylist.xmldata.send(null);
		}
		this.writePlaylist = function() {
			if (myPlaylist.xmldata.readyState == 4){
				if(typeof myPlaylist.xmldata.responseXML == 'undefined') {
					thedata = myPlaylist.xmldata;
				} else {
					thedata = myPlaylist.xmldata.responseXML;
				}
				myPlaylist.displayElement.innerHTML = '';
				cos = thedata.getElementsByTagName("video");
				if(cos.length > 0) {
					if(getEl('PLinstructions')) getEl('PLinstructions').style.display = 'none';
					for(a=0; a<cos.length; a++) {
						imagesrc = 'http://images.ibsys.com/sh/images/videoplayer/genericvideo_8060.jpg';
						if(cos[a].getElementsByTagName("ibs_image").length != 0) {
							imagesrcs = cos[a].getElementsByTagName("ibs_image")[0].getElementsByTagName("src");
							for(i=0; i<imagesrcs.length; i++) {
								if (imagesrcs[i].getAttribute('width') == '60' && imagesrcs[i].childNodes[0].nodeValue != '') imagesrc = 'http://images.ibsys.com'+imagesrcs[i].childNodes[0].nodeValue;
							}
						}
						myPlaylist.entries[a] = new entry(cos[a].getAttribute('id'), cos[a].getElementsByTagName('headline')[0].childNodes[0].nodeValue,1,imagesrc, 'playlist');
						if(getEl('playlistlink'+cos[a].getAttribute('id'))) getEl('playlistlink'+cos[a].getAttribute('id')).style.visibility = 'hidden';
						myPlaylist.displayElement.appendChild(myPlaylist.entries[a].getDisplayElement(a))
					}
				} else {
					if(getEl('PLinstructions')) getEl('PLinstructions').style.display = 'block';
				}
				myPlaylist.updateCookie();
				myPlaylist.setNextEntry();
				myPlaylist.highlight('current');
				if(myPlaylist.orientation == 'horizontal') myPlaylist.displayElement.style.width = (myPlaylist.entries.length*160)+'px';
			}
		}
		this.refreshPlaylist = function() {
			myPlaylist.displayElement.innerHTML = '';
			if(myPlaylist.entries.length > 0){
				if(getEl('PLinstructions')) getEl('PLinstructions').style.display = 'none';
				for(a=0; a<myPlaylist.entries.length; a++) {
					myPlaylist.displayElement.appendChild(myPlaylist.entries[a].getDisplayElement(a))
					if(getEl('playlistlink'+myPlaylist.entries[a].id)) getEl('playlistlink'+myPlaylist.entries[a].id).style.visibility = 'hidden';
				}
			} else {
				if(getEl('PLinstructions')) getEl('PLinstructions').style.display = 'block';
			}
			//myPlaylist.updateCookie();
			//myPlaylist.setNextEntry();
			//myPlaylist.highlight('current');
			if(myPlaylist.orientation == 'horizontal') {
				myPlaylist.displayElement.style.width = (myPlaylist.entries.length*160)+'px';
			}
		}
		this.insert = function(id,rank) {
			if(typeof rank != 'undefined' && rank <= myPlaylist.entries.length) myPlaylist.insertAt = rank;
			else myPlaylist.insertAt = myPlaylist.entries.length;
			if(myPlaylist.idList.indexOf(id) == -1) {
				if (window.ActiveXObject) {
					// WINDOWS
					myPlaylist.entryXML = new ActiveXObject("Microsoft.XMLHTTP");
				} else if (document.implementation && document.implementation.createDocument) {
					// MOZILLA
					myPlaylist.entryXML = new XMLHttpRequest();
				}
				myPlaylist.entryXML.onreadystatechange = myPlaylist.addNewEntry;
				myPlaylist.entryXML.open("GET", '/_objectxml/' + id + '/detail.html', true);
				myPlaylist.entryXML.send(null);
			} else {
				//myPlaylist.highlight('last');
				//myPlaylist.setNextEntry();
			}
		}
		this.addNewEntry = function(){
			if (myPlaylist.entryXML.readyState == 4) {
				if(typeof myPlaylist.entryXML.responseXML == 'undefined') {
					theentry = myPlaylist.entryXML;
				} else {
					theentry = myPlaylist.entryXML.responseXML;
				}
				co = theentry.getElementsByTagName("video");
				imagesrc = 'http://images.ibsys.com/sh/images/videoplayer/genericvideo_8060.jpg';
				if(co[0].getElementsByTagName("ibs_image").length != 0) {
					imagesrcs = co[0].getElementsByTagName("ibs_image")[0].getElementsByTagName("src");
					for(i=0; i<imagesrcs.length; i++) {
						if (imagesrcs[i].getAttribute('width') == '60' && imagesrcs[i].childNodes[0].nodeValue != '') imagesrc = 'http://images.ibsys.com'+imagesrcs[i].childNodes[0].nodeValue;
					}
				}
				listlength = myPlaylist.entries.length;
				thisvideo = new entry(co[0].getAttribute('id'), co[0].getElementsByTagName('headline')[0].childNodes[0].nodeValue,1,imagesrc, 'playlist');
				myPlaylist.entries.splice(myPlaylist.insertAt, 0, thisvideo);
				myPlaylist.updateCookie();
				myPlaylist.refreshPlaylist();
				myPlaylist.highlight('last');
				myPlaylist.setNextEntry();
				if(typeof updateChannelList != 'undefined') updateChannelList('current');
			}
		}
		this.setNextEntry = function() {
			for(a=0; a<myPlaylist.entries.length; a++) {
				if(myPlaylist.entries[a].id == myPlaylist.currentVideoID) {
					if(myPlaylist.entries[a+1]) myPlaylist.nextVideoID =  myPlaylist.entries[a+1].id;
					else myPlaylist.nextVideoID = 0;
					if(myPlaylist.entries[a-1])myPlaylist.previousVideoID =  myPlaylist.entries[a-1].id;
					else myPlaylist.previousVideoID = 0;
				}
			}
			window.status = myPlaylist.nextVideoID;
		}
		this.getNextEntry = function() {
			return myPlaylist.nextVideoID;
		}
		this.move = function(from, to){
			pickup = myPlaylist.entries.splice(from, 1);
			myPlaylist.entries.splice(to, 0, pickup[0]);
			myPlaylist.dragonid = '';
			dragElement = null;
			getEl('dragon').style.display = 'none';
			getEl('dragon').innerHTML = '';
			myPlaylist.refreshPlaylist();
			myPlaylist.highlight(false);
			myPlaylist.updateCookie();
			myPlaylist.setNextEntry();
		}
		this.deleteID = function(id) {
			for(a=0; a<myPlaylist.entries.length; a++) {
				if(myPlaylist.entries[a].id == id) {
					myPlaylist.entries.splice(a, 1);
					//el = getEl('entry'+id);
					//myPlaylist.displayElement.removeChild(el);
				}
			}
			if(getEl('playlistlink'+id)) getEl('playlistlink'+id).style.visibility = 'visible';
			if(typeof videoplayer != 'undefined' && id == videoplayer.videoid) nextVideo();
			myPlaylist.refreshPlaylist();
			myPlaylist.highlight(false);
			myPlaylist.updateCookie();
			myPlaylist.setNextEntry();
			if(typeof videoplayer != 'undefined') updateChannelList('current');
		}
		this.getCurrentVideo = function() {
			currEntry == -1;
			for(a=0; a<myPlaylist.entries.length; a++) {
				if(myPlaylist.entries[a].id == videoPlayer.videoid) currEntry = a;
			}
			if (currEntry > -1) return myPlaylist.entries[currEntry];
		}
		/* setCurrentVideo()
		 *  @v 	= string		ID of video asset to play
		 *  @act = string		string that represents the calling method ('search', 'mostpopular', etc) 
		 *  					This value is passed in the WT tracking pixel as ib_video_action
		 *  
		 *  calls Controls object to set current vidoe item in the player
		 *  sets myPlaylist.currentVideoID = v
		 *  Requests the data for the current video calling updatePlayer() to handle the response
		 */
		this.setCurrentVideo = function(v, act){
			myPlaylist.currentVideoAction = (act)?act:null;
			Controls.setCurrentVideo(v);
			myPlaylist.currentVideoID = v;
			if (window.ActiveXObject) {
				// WINDOWS
				myPlaylist.videoinfo = new ActiveXObject("Microsoft.XMLHTTP");
			} else if (document.implementation && document.implementation.createDocument) {
				// MOZILLA
				myPlaylist.videoinfo = new XMLHttpRequest();
			}
			myPlaylist.videoinfo.onreadystatechange = myPlaylist.updatePlayer;
			myPlaylist.videoinfo.open("GET", '/_objectxml/' + v + '/detail.html', true);
			myPlaylist.videoinfo.send(null);
			adTagParam = '';
			leaderboard = false;
		}
		this.updatePlayer = function() {
			if(myPlaylist.videoinfo.readyState == 4){
				if(typeof myPlaylist.videoinfo.responseXML == 'undefined') {
					Info = myPlaylist.videoinfo;
				} else {
					Info = myPlaylist.videoinfo.responseXML;
				}
				co = Info.getElementsByTagName("video");
				getEl('videoheadline').innerHTML = '';
				getEl('videoteaser').innerHTML = '';
				if(co.length > 0){
					myPlaylist.currentCategory = co[0].getAttribute('categoryname');
					myPlaylist.currentSubcategory = co[0].getAttribute('subcategoryname');
					var id = co[0].getAttribute('id');
					if(co[0].getElementsByTagName('headline')[0].childNodes.length > 0) {
						getEl('videoheadline').innerHTML = co[0].getElementsByTagName('headline')[0].childNodes[0].nodeValue;
						document.title = co[0].getElementsByTagName('headline')[0].childNodes[0].nodeValue;
					}
					if(co[0].getElementsByTagName('teaser')[0].childNodes.length > 0) {
						getEl('videoteaser').innerHTML = co[0].getElementsByTagName('teaser')[0].childNodes[0].nodeValue;
					}
				}
				myPlaylist.highlight('current');
				myPlaylist.setNextEntry();
				// TRACK THIS EVENT WITH WEBTRENDS (put here at the end to not interrupt normal video play)
				if (co.length > 0) {
				  var cgs = co[0].getAttribute('topic_path');
					if (cgs != null)  {
						var pattern = /\/([^\/]*)\//g;
						cgs = cgs.replace(pattern, "$1");
					} else {
						cgs = myPlaylist.currentCategory;
					}
					// grab coid, owner and livestream
					var coid = co[0].getAttribute('id');
					var pool = co[0].getAttribute('owner');
					var vtype = co[0].getAttribute('livestream');
					if (vtype != null && vtype == 'true') {
						vtype = 'Livestream';
					} else {
						vtype = 'Clickplay';
					}
					dcsMultiTrack('WT.ti',document.title,'DCSext.mtype','Video','DCSext.vtype',vtype,'WT.cg_s',cgs,'DCSext.ib_coid',coid,'DCSext.ib_pool',pool,'DCSext.ib_video_action',myPlaylist.currentVideoAction,'DCSext.ib_video_category', myPlaylist.currentCategory);
				}
			}
		}
		this.highlight = function(show) {
			for(a=0; a<myPlaylist.entries.length; a++) {
				if(myPlaylist.entries[a].id == myPlaylist.currentVideoID) {
					getEl('slide'+myPlaylist.entries[a].id).className = 'slideon';
					//if(show && show == 'current') getEl('playlist').scrollLeft = a*150;
				} else {
					getEl('slide'+myPlaylist.entries[a].id).className = 'slide';
				}
			}
			//if(show && show == 'last') getEl('playlist').scrollLeft = a*150;
		}
		this.updateCookie = function() {
			myPlaylist.idList = '';
			for(a=0; a<myPlaylist.entries.length; a++) {
				if(a!=0) myPlaylist.idList += ',';
				myPlaylist.idList += myPlaylist.entries[a].id;
			}
			path = '/';
			domain = document.domain;
			secure = false;
			var expires = new Date();
			expires.setTime(expires.getTime() + (240 * 3600000)); // 10 days in the future
			var curCookie =  "ibsPlaylist=" + escape(myPlaylist.idList) +
				((expires) ? "; expires=" + expires.toGMTString() : "") +
				((path) ? "; path=" + path : "") +
				((domain) ? "; domain=" + domain : "") +
				((secure) ? "; secure" : "");
				document.cookie = curCookie;
		}
	} else {
		return false;
	}
}

function entry(id, h, t, i, a) {
	this.id = id;
	this.teaser = t;
	this.headline = h;
	this.image = i;
	this.action = a;
	this.getDisplayElement = function(p) {
		this.displayElement = document.createElement('div');
		this.displayElement.setAttribute('id', 'slide'+id);
		this.displayElement.setAttribute('className', 'slide');
		this.displayElement.setAttribute('class', 'slide');
		this.displayElement.setAttribute('coid', id);
		this.displayElement.onmouseover = function() {
			window.status = this.className;
			if(dragElement && dragElement == getEl('dragon') && this.className != 'slideon' && this.className != 'selectedslide') {
				this.className = 'slideover';
			} else if(!dragElement && this.className == 'slide') {
				this.className = 'slidehover';
			} else {
				//alert('3');
			}
		}
		this.displayElement.onmouseup = function() {
			if(myPlaylist.dragonid) {
				myPlaylist.deleteID(myPlaylist.dragonid);
				myPlaylist.insert(myPlaylist.dragonid, p);
				getEl('dragon').style.display = 'none';
				if(getEl('dragon').childNodes.length > 0) getEl('dragon').removeChild(getEl('dragon').childNodes[0]);
				if(myPlaylist && myPlaylist.dragonid) {
					// If this came from the clip list we need to reset the activeDIV class
					if(typeof activeDIV != 'undefined') activeDIV.className = 'slide';
					myPlaylist.dragonid = 0;
				}
			}
		}
		this.displayElement.onmouseout = function() {
			if(this.className != 'selectedslide' && this.className != 'slideon') {
				this.className = 'slide';
			}
		}
		del = document.createElement('a');
		del.setAttribute('class', 'delete');
		del.setAttribute('className', 'delete');
		del.setAttribute('videoid', id);
		del.appendChild(document.createTextNode('Remove'));
		del.onclick = function() {
			myPlaylist.deleteID(this.getAttribute('videoid'));
		}
		play = document.createElement('a');
		play.setAttribute('class', 'play');
		play.setAttribute('className', 'play');
		play.setAttribute('videoid', id);
		play.appendChild(document.createTextNode('Play'));
		play.onclick = function() {
			myPlaylist.setCurrentVideo(id, a);
		}
		handle = document.createElement('div');
		handle.setAttribute('class', 'handle');
		handle.setAttribute('className', 'handle');
		handle.setAttribute('drag','slide');
		handle.setAttribute('coid', id);
		handle.appendChild(document.createTextNode(' '))
		handle.onmousedown = function() {
			var floater = this.parentNode.cloneNode(true);
			if(getEl('dragon').childNodes.length > 0) getEl('dragon').innerHTML = '';
			getEl('dragon').appendChild(floater);
			getEl('dragon').style.display = 'block';
			if(typeof myPlaylist != 'undefined') myPlaylist.dragonid = this.getAttribute('coid');
			this.parentNode.className = 'slideactive';
			activeDIV = this.parentNode;
		}
		this.displayElement.appendChild(handle);
		img = document.createElement('img');
		img.setAttribute('src', i);
		img.setAttribute('alt', h);
		img.setAttribute('width', 60);
		img.setAttribute('height', 45);
		img.setAttribute('coid', id);
		img.onmousedown = function() {
			var floater = this.parentNode.cloneNode(true);
			if(getEl('dragon').childNodes.length > 0) getEl('dragon').innerHTML = '';
			getEl('dragon').appendChild(floater);
			getEl('dragon').style.display = 'block';
			if(typeof myPlaylist != 'undefined') myPlaylist.dragonid = this.getAttribute('coid');
			this.parentNode.className = 'slideactive';
			activeDIV = this.parentNode;
		}
		br = document.createElement('br');
		this.displayElement.appendChild(img);
		this.displayElement.appendChild(br);
		hdln = document.createElement('div');
		hdln.setAttribute('class', 'headline');
		hdln.setAttribute('className', 'headline');
		hdln.innerHTML = this.headline;
		this.displayElement.appendChild(hdln);
		this.deleteButton = this.displayElement.appendChild(del);
		this.playButton = this.displayElement.appendChild(play);
		return this.displayElement
	}

}
// NEW DRAGGABLE BEHAVIOUR
function StartDrag(e){
	if (window.event) {
		if (document.body && document.body.scrollTop) {
		   winY = document.body.scrollTop;
		   winX = document.body.scrollLeft;
		} else if (document.documentElement && document.documentElement.scrollTop) {
		   winY = document.documentElement.scrollTop;
		   winX = document.documentElement.scrollLeft;
		} else if (window.pageYOffset) {
		   winY = window.pageYOffset;
		   winX = window.pageXOffset;
		} else {
			winY = 0;
			winX = 0;
		}
		e = window.event;
		e.which = window.event.button;
		e.pageX = window.event.clientX+winX;
		e.pageY = window.event.clientY+winY;
		e.target = event.srcElement;
	}
	if(e.target.tagName.toLowerCase() != 'input'){
		if(e.which > 1) {
			return false;
		} else {
			srcElement = e.target;
			dragTarget = srcElement.getAttribute("drag", false);
			if(dragTarget == 'parent'){
				dragElement = (srcElement.parentElement)? srcElement.parentElement : srcElement.parentNode;
				InitLeft = e.clientX;
				InitTop = e.clientY;
				InitElTop = parseInt(dragElement.style.top);
				InitElLeft = parseInt(dragElement.style.left);
				dragElement.style.pixelTop = InitElTop;
				dragElement.style.pixelLeft = InitElLeft;
				if (activeElement) activeElement.style.zIndex=9;
				dragElement.style.zIndex=10;
				activeElement=dragElement
			}
		}
		xoff = (getEl('dragon').offsetParent)? getEl('dragon').offsetParent.offsetLeft:0;
		yoff = (getEl('dragon').offsetParent)? getEl('dragon').offsetParent.offsetTop:0;
		moveTo(getEl('dragon').style,e.pageX-xoff-78,e.pageY-yoff+10);
		return false;
	} else {
		return true;
	}
}

function moveTo(obj,x,y) {
	obj.left = x+'px'
	obj.top = y+'px'
}

function Drag(e) {
	if (window.event) {
		if (document.body && document.body.scrollTop) {
		   winY = document.body.scrollTop;
		   winX = document.body.scrollLeft;
		} else if (document.documentElement && document.documentElement.scrollTop) {
		   winY = document.documentElement.scrollTop;
		   winX = document.documentElement.scrollLeft;
		} else if (window.pageYOffset) {
		   winY = window.pageYOffset;
		   winX = window.pageXOffset;
		} else {
			winY = 0;
			winX = 0;
		}
		e = window.event;
		e.which = window.event.button;
		e.pageX = window.event.clientX+winX;
		e.pageY = window.event.clientY+winY;
		e.target = event.srcElement;
	}
	if(dragElement){
		moveTo(dragElement.style,e.pageX+InitElLeft-InitLeft,e.pageY+InitElTop-InitTop);
		//moveTo(getEl('inviterDIV').style,e.pageX+InitElLeft-InitLeft-198,e.pageY+InitElTop-InitTop+20);
	}
	if(getEl('dragon')){
		xoff = (getEl('dragon').offsetParent)? getEl('dragon').offsetParent.offsetLeft:0;
		yoff = (getEl('dragon').offsetParent)? getEl('dragon').offsetParent.offsetTop:0;
		moveTo(getEl('dragon').style,e.pageX-xoff-78,e.pageY-yoff+10);
	}
}

function StopDrag(e){
	if (window.event) {
		if (document.body && document.body.scrollTop) {
		   winY = document.body.scrollTop;
		   winX = document.body.scrollLeft;
		} else if (document.documentElement && document.documentElement.scrollTop) {
		   winY = document.documentElement.scrollTop;
		   winX = document.documentElement.scrollLeft;
		} else if (window.pageYOffset) {
		   winY = window.pageYOffset;
		   winX = window.pageXOffset;
		} else {
			winY = 0;
			winX = 0;
		}
		e = window.event;
		e.which = window.event.button;
		e.pageX = window.event.clientX+winX;
		e.pageY = window.event.clientY+winY;
		e.target = event.srcElement;
	}
	if(getEl('selection')) {
		getEl('selection').innerHTML = '';
		getEl('selection').style.visibility = 'hidden';
	}
	getEl('dragon').style.display = 'none';
	if(getEl('dragon').childNodes.length > 0) getEl('dragon').removeChild(getEl('dragon').childNodes[0]);
	if(typeof myPlaylist != 'undefined' && myPlaylist.dragonid) {
		// If there we are dragging a clip we need to make sure it gets back where it belongs.
		if(typeof activeDIV != 'undefined') activeDIV.className = 'slide';
		myPlaylist.dragonid = 0;
	}
	if(!dragElement) return;
	activeDIV = null;
	dragElement=null;
}



// PAGE STUFF FOR INLINE PLAYERS
	     function showClips(cat) {
		categorydisplay.innerHTML = '';
		  for(var c=0; c<myClips.length; c++) {
			clipcat = (myClips[c].getAttribute('videocategory').toLowerCase() == 'news_-_no_ads')?'news':myClips[c].getAttribute('videocategory').toLowerCase();
			if(clipcat == cat){
				myClips[c].style.display = 'block';
				var copy = myClips[c].cloneNode(true);
				copy.style.display = 'block';
				copy.getElementsByTagName('div')[0].onmousedown = function() {
					if(typeof myPlaylist != 'undefined'){
						var floater = this.parentNode.cloneNode(true);
						if(getEl('dragon').childNodes.length > 0) getEl('dragon').removeChild(getEl('dragon').childNodes[0]);
						getEl('dragon').appendChild(floater);
						getEl('dragon').style.display = 'block';
						if(typeof myPlaylist != 'undefined') myPlaylist.dragonid = this.parentNode.getAttribute('coid');
						this.parentNode.className = 'slideactive';
						activeDIV = this.parentNode;
					}
				}
				if(copy.getElementsByTagName('img').length > 0) {
					copy.getElementsByTagName('img')[0].onmousedown = function() {
						if(typeof myPlaylist != 'undefined'){
							var floater = this.parentNode.cloneNode(true);
							if(getEl('dragon').childNodes.length > 0) getEl('dragon').removeChild(getEl('dragon').childNodes[0]);
							getEl('dragon').appendChild(floater);
							getEl('dragon').style.display = 'block';
							if(typeof myPlaylist != 'undefined') myPlaylist.dragonid = this.parentNode.getAttribute('coid');
							this.parentNode.className = 'slideactive';
							activeDIV = this.parentNode;
						}
						return false;
					}
				}
				copy.onmouseover = function() {
					if(typeof myPlaylist != 'undefined' && myPlaylist.dragonid == 0 && this.className == 'slide'){
						this.className = 'slideover';
					}
				}
				copy.onmouseout = function() {
					if(typeof myPlaylist != 'undefined' && myPlaylist.dragonid == 0 && this.className != 'slideactive') {
						this.className = 'slide';
					}
				}
				categorydisplay.appendChild(copy);
			} else {
				myClips[c].style.display = 'none';
			}
		  }
		}
	// ONLOAD PARSE THE CONTENT IN BETA AND CREATE AN ARRAY OF SLIDE ELEMENTS
	addEvent(window, "load",
	    function() {
		myPlaylist = new ibsPlaylist();
		myPlaylist.fetchXML();
		categorydisplay = getEl('slides');
		searchdisplay = getEl('searchresults');
		populardisplay = getEl('mostpopularslides');
		activeDIV = null;
		getEl('playlist').onmouseup = function() {
			if(typeof myPlaylist != 'undefined' && myPlaylist.dragonid) myPlaylist.insert(myPlaylist.dragonid);
		}
		myClips = new Array();
		clipGroup = document.getElementById('beta').getElementsByTagName('div');
		catOptions = document.getElementById('videocategories');
		vidcats = new Array();
		if(clipGroup.length > 0) {
			//WALK ALL THE DIVS IN THE BETA CONTAINER AND FIND THE SLIDES
			for(var c=0; c<clipGroup.length; c++) {
				// IF ITS A SLIDE REGISTER IT WITH THE DOCUMENT
				if(clipGroup[c].className == 'slide') {
					myclipslength = myClips.length;
					vidcat = (clipGroup[c].getAttribute('videocategory').toLowerCase() == 'news_-_no_ads')?'news':clipGroup[c].getAttribute('videocategory').toLowerCase();
					// ADD THIS SLIDE TO THE LIST OF CLIPS
					myClips[myclipslength] = clipGroup[c];
					// IF NOT ALREADY THERE ADD THIS CATEGORY TO THE PULLDOWN
					if(!vidcats[vidcat]) {
						if (catOptions.tagName == "SELECT") {
							vidcats[vidcat] = document.createElement('option');
						}
						else {
							vidcats[vidcat] = document.createElement('a');
							vidcats[vidcat].href = "#";
							vidcats[vidcat].onclick = function() {
								showClips(this.value);
							}
						}
						vidcats[vidcat].setAttribute('value', vidcat);
						vidcats[vidcat].className = "catoff";
						vidcats[vidcat].appendChild(
							document.createTextNode(vidcat.toUpperCase().replace(/_/g, " ")));
						catOptions.appendChild(vidcats[vidcat]);
					}
				}
			}

		}
		mostpop = new Array();
		mostpopclips = document.getElementById('gamma').getElementsByTagName('div');
		if(mostpopclips.length > 0) {

			//WALK ALL THE DIVS IN THE GAMMA CONTAINER AND FIND THE SLIDES
			for(var m=0; m<mostpopclips.length; m++) {
				if(mostpopclips[m].className == 'slide') {
					mostpop[mostpop.length] = mostpopclips[m];
					var copy = mostpopclips[m].cloneNode(true);
					copy.style.display = 'block';
					copy.getElementsByTagName('div')[0].onmousedown = function() {
						if(typeof myPlaylist != 'undefined'){
							var floater = this.parentNode.cloneNode(true);
							if(getEl('dragon').childNodes.length > 0) getEl('dragon').removeChild(getEl('dragon').childNodes[0]);
							getEl('dragon').appendChild(floater);
							getEl('dragon').style.display = 'block';
							if(typeof myPlaylist != 'undefined') myPlaylist.dragonid = this.parentNode.getAttribute('coid');
							this.parentNode.className = 'slideactive';
							activeDIV = this.parentNode;
						}
					}
					if(copy.getElementsByTagName('img').length != 0) {
						copy.getElementsByTagName('img')[0].onmousedown = function() {
						if(typeof myPlaylist != 'undefined'){
							var floater = this.parentNode.cloneNode(true);
							if(getEl('dragon').childNodes.length > 0) getEl('dragon').removeChild(getEl('dragon').childNodes[0]);
							getEl('dragon').appendChild(floater);
							getEl('dragon').style.display = 'block';
							if(typeof myPlaylist != 'undefined') myPlaylist.dragonid = this.parentNode.getAttribute('coid');
							this.parentNode.className = 'slideactive';
							activeDIV = this.parentNode;
						}
						return false;
						}
					}
					copy.onmouseover = function() {
						if(typeof myPlaylist != 'undefined' && myPlaylist.dragonid == 0 && this.className == 'slide'){
							this.className = 'slideover';
						}
					}
					copy.onmouseout = function() {
						if(typeof myPlaylist != 'undefined' && myPlaylist.dragonid == 0 && this.className != 'slideactive') {
							this.className = 'slide';
						}
					}
					populardisplay.appendChild(copy);
				}
			}

		}
		showClips('news');
		PlayerFactory.createPlayer(320,240,"playerparent");
		PlayerFactory.createControls();
		Controls.onLastEntry = function() {
			Controls.closeAll();
			if(myPlaylist.nextVideoID != 0) myPlaylist.setCurrentVideo(myPlaylist.nextVideoID);
		}
		if(pageProps.coid){
			myPlaylist.setCurrentVideo(pageProps.coid);
			myPlaylist.setNextEntry();
		} else {
			Controls.closeAll();
		}
	     }
	);

	function showCategory()
	{
		if(!window.location.href.match(/[0-9]{6,}/))
			return;
		window.setTimeout(function()
		{
			var sel = document.getElementById("videocategories");
			if(sel == null || !myPlaylist)
				return;
			for(var j=0; j < sel.length; j++)
			{
				var testval = myPlaylist.currentCategory.replace(/_\-_no_ads/,"");
				var testval2 = myPlaylist.currentSubcategory;
				if(sel[j].value == testval || sel[j].value == testval2)
				{
					sel[j].selected = true;
					showClips(sel[j].value);
					break;
				}
			}
		}, 1800);
	}

	addEvent(window,"load",showCategory);


PlayerFactory.createControlsOnLoad = false;
document.onmousedown=StartDrag;
document.onmouseup=StopDrag;
document.onmousemove=Drag;
document.ondragstart = function() {
	if(dragElement) drag();
	return false;
}






