var d = document;
var w = window;
function ge(id){return d.getElementById(id)}
function gt(t,o){o=o?o:d;return o.getElementsByTagName(t)}
function gc(c,t,s,o){var r=new Array();var os=gt(t,o);for(var i=0,j=0,l=os.length;i<l;i++){var sc = s + os[i].className + s;if(sc.indexOf(s+c+s)!=-1){r[j] = os[i];j++;}}return r;} // New method for getElementsByTagName + ClassName; string "s" allows pinpointing exact class or if it exists in the string
function gte(e){if (w.event && w.event.srcElement) r = w.event.srcElement;else if (e && e.target) r = e.target;if (!r) return null;if(r.nodeType == 3)r=r.parentNode; /* Safari 1.3 */ return r;}
function ce(t){return d.createElement(t)}
function cf(){return d.createDocumentFragment()}
function ct(s){return d.createTextNode(s)}
function ac(p,c){p.appendChild(c)}
function ib(p,c,e){p.insertBefore(c,e)}
function ae(o,et,fn){if(o.addEventListener)o.addEventListener(et,fn,false);else if(o.attachEvent)o.attachEvent("on"+et,fn);}
function re(o,et,fn){if(o.removeEventListener)o.removeEventListener(et,fn,false);else if(o.detachEvent)o.detachEvent("on"+et,fn);}
//function ad(e,t){while (e.nodeName.toLowerCase() != t && e.nodeName.toLowerCase() != 'html') e=e.parentNode; return (e.nodeName.toLowerCase()=='html') ? null : e;}
function scookie(name, value, days, path, domain) {
	var expires = -1;
	if(typeof days == "number" && days >= 0) {
		var dt = new Date();
		dt.setTime(dt.getTime()+(days*24*60*60*1000));
		expires = dt.toGMTString();
	}
	value = escape(value);
	document.cookie = name + "=" + value + ";"
		+ (expires != -1 ? " expires=" + expires + ";" : "")
		+ (path ? "path=" + path : "")
		+ (domain ? "; domain=" + domain : "")
}
function gcookie(name) {
	var idx = d.cookie.indexOf(name+'=');
	if(idx == -1) { return null; }
	var value = d.cookie.substring(idx+name.length+1);
	var end = value.indexOf(';');
	if(end == -1) { end = value.length; }
	value = value.substring(0, end);
	value = unescape(value);
	return value;
}
function setArtz(mid) {
	ge(mid).className="artz-mb";
}

var acc=gcookie('artz');
//alert(acc);
npn=false;
var nextbutton="Next Video";
var prevbutton="Previous Video";

arrowInt=growvInt=dlInt=false;
arY=!(document.all)?9:0;
function ge(id) {return document.getElementById(id);}
//function gt(el,par) {return par.getElementsByTagName(el);}
function uc(s,t) {t.innerHTML=s.innerHTML;}


function setV(obj) {
	obj.onclick=onClk;
	obj.onmouseover=onOver;
	obj.onmouseout=onOut;
	obj.className='vOut';
}
function reSet() {
	if (npn==true) {
		var oldNode=npnode.parentNode
		oldNode.style.height='20px';
		setV(npnode.nextSibling.childNodes[0]);
		oldNode.removeChild(npnode);	
	};
}
function initV() {
	//alert(acc);
	if (acc!=1) {
		ge('mb-module').className='artz-mb';
		var v=ge('vlist');
		vlinks=gt('h3',v);
		vlength=vlinks.length;
		for (var i=0;i<vlength;i++) {
			setV(vlinks[i].firstChild);
			vlinks[i].firstChild.id=i;
			vlinks[i].firstChild.vid=vlinks[i].firstChild.href;
			vlinks[i].firstChild.removeAttribute('href');
		}
		contextArea();
		createIframe();
	
		defcon=ge('conLinks').cloneNode(ge('conLinks'));
		//alert(defcon.innerHTML);
		 createDL('mbk_dl');
		 createArrow();
		 doClk(vlinks[0].firstChild);
	}
	setAccess(gcookie('artz'));

	
	
}
function onOver() {
	this.className='vOver';
}
function onOut() {
	this.className='vOut';
}
function onClk() {
	doClk(this);
}
function doClk(o) {
	reSet();
	npn=true;
	o.className="vAct";
	actId=parseInt(o.id);
	actNode=o;
	parNode=o.parentNode.parentNode;
	heightV=1.7;
	parNode.style.alpha=0;
	clearTimeout(dlTimeOut)
	if (growvInt) {clearInterval(growvInt)}
	growvInt=setInterval('growV()',60);
	np=ce('h5');
	np.className="now-playing";
	nptxt=document.createTextNode('NOW PLAYING');
	np.appendChild(nptxt);
	npnode=parNode.insertBefore(np,o.parentNode);
	ge('mtitle').innerHTML=o.innerHTML;
	var tNode=o.parentNode.parentNode;
	ge('tct').innerHTML=gt('P',tNode)[0].innerHTML
	vidLink=o.vid;
	setArrow(o);
	o.onmouseover=o.onmouseout=o.onclick=null;
	nexPrev();
	createContext(parNode);
	//var mvt=setTimeout('smovie(vidLink)',500);
 	return false;
}

function growV() {
	if (heightV<3.95) {
		var spd=(4-heightV)*.5;
		heightV+=spd;
	} else {
		//alert('end');
		clearInterval(growvInt);
		heightV=4;
		//setArrow(actNode);
		//alert('grow');
		//smovie(vidLink);
	}
		parNode.style.height=heightV+"em";
		parNode.style.alpha=heightV/4;
		parNode.style.filter="alpha(opacity:"+heightV/4*100+")";
}

function nexPrev() {
	if (ge('NextPrevious')) {
		ge('context').removeChild(bnode);
	};
	bnode=ce('DIV');
	bnode.setAttribute('id','NextPrevious');
	switch (actId) {
		case 0: 
			bnode.appendChild(createButton(nextbutton));
			break;
		case (vlength-1):
			bnode.appendChild(createButton(prevbutton));
			break;
		default: 
			bnode.appendChild(createButton(prevbutton));
			bnode.appendChild(createButton(nextbutton));
	}
	ge('context').appendChild(bnode);
}
function createButton(txt) {
	var dir=(txt==nextbutton)?"next":"prev";
	var button=ce('a');
	var boldbutton=ce('b');
	button.onclick=buttClk;
	button.href="javascript:void(0)"
	button.className='artzBtn';
	button.setAttribute('id',dir);
	var buttxt=document.createTextNode(txt);
	boldbutton.appendChild(buttxt);
	button.appendChild(boldbutton);
	return button;
}
function buttClk() {
	if (this.id=="next") {doClk(vlinks[actId+1].firstChild)} else {doClk(vlinks[actId-1].firstChild)}
}
function createContext (o) {
	var divs=gt('div',o);
	var l=divs.length;
	var cl=ge('conLinks');
	var nc=defcon;
	if (l>0) {
		for (i=0;i<l;i++) {
			if (divs[i].className=='clinks') {
				nc=divs[i];
			} 
		}
	}
	uc(nc,cl);
}
function smovie(v) {
	var pid=v.split('pmmsid=')[1];
	var mpre='http://mp.aol.com/_dl_main.adp?pmmsid='+pid+'&mediatype=video&livestream=0&size=large&skin=grey&referer=http%3A//movies.aol.com/unscripted_sundance_film_fes';
	ge('dalai').src=mpre;
}
function createDL(id) {
	var dl=ge(id);
	dl.className="dl";
	dls=dl.getElementsByTagName('li');
	dll=dls.length;
	for (var i=0; i<dll;i++) {
		dls[i].style.width='0px';
		dls[i].style.zIndex=dll-i;
		var dimg=ce('img');
		dimg.setAttribute('src',dls[i].childNodes[1].href);
		dimg.setAttribute('alt',dls[i].firstChild.firstChild.data)
		dls[i].removeChild(dls[i].childNodes[1]);
		dls[i].firstChild.replaceChild(dimg,dls[i].firstChild.firstChild);
		
	}
	ge("vid").appendChild(dlcontrols())
	dlTimeOut=setTimeout('moveDL()',10);
}
function dlcontrols() {
	var dlc=ce('span');
	dlc.setAttribute('id','dlc')
	var dlrew=ce('a');
	//dlrew.onlick='';
	dlrew.setAttribute('id','dlrew');
	var dlpau=ce('a');
	//dlpau.onclick=dlpause;
	dlpau.setAttribute('id','dlpau');
	var dlfwd=ce('a');
	dlfwd.onclick=moveDL;
	dlfwd.setAttribute('id','dlfwd');
	//dlc.appendChild(dlrew);
	//dlc.appendChild(dlpau);
	dlc.appendChild(dlfwd);
	return dlc;
}

function moveDL(id) {
	clearTimeout(dlTimeOut);
	if(dlInt) {clearInterval(dlInt)}
	topDL=getTopDL();
	currWidth=parseInt(topDL.style.width);
	dlInt=setInterval('goDL()',60);
	
}
function goDL() {
	
	if (currWidth<320) {
		var spd=parseInt((325-currWidth)*.2);
		currWidth=currWidth+spd;
	} else {
		clearInterval(dlInt);
		upDateZ();
		currWidth=320;
		dlTimeOut=setTimeout('moveDL()',8000);
	}
	topDL.style.width=currWidth +"px";
	topDL.style.opacity=currWidth/320;
	topDL.style.filter="alpha(opacity:"+currWidth/320*100+")";
}
function getTopDL() {
	for (i=0;i<dll;i++) {
		if (dls[i].style.zIndex==3) {return dls[i]}
	}
}
function upDateZ() {
	for (i=0;i<dll;i++) {
		if (dls[i].style.zIndex==3) {
				dls[i].style.zIndex=1;
		} else {
			dls[i].style.zIndex++;
			dls[i].style.width='0px';
			dls[i].style.alpha=0;
		}
	}
}
function setArrow(o) {
		if (arrowInt) {clearInterval(arrowInt)}
		arrowTarg=posY(o)-vpos+mpos+arY;
		arrow.style.left=o.offsetLeft+o.offsetWidth-7+'px';
		currArrowY=parseInt(arrow.style.top);
		arrowInt=setInterval('moveArrow()',60);
	}
	vy=0;
function moveArrow() {
		ay=(arrowTarg-currArrowY)*.6;
		vy+=ay;
		vy*=.50;
		currArrowY+=parseInt(vy);
		if (Math.abs(ay)<1) {
			clearInterval(arrowInt);
			smovie(vidLink);
			dlTimeOut=setTimeout('moveDL()',2500);
		}
		arrow.style.top=currArrowY +'px';
}
function createArrow() {
	arrow=ce('div');
	arrow.className='arrow';
	arrow.style.position='absolute';
	arrow.style.top=300+'px';
	vpos=posY(ge('vlist'));
	ge('vlist').appendChild(arrow);
	mpos=posY(ge('mb-module'));
	//mpos=300;
}

//function to determine y-coordinate of target element
function posY(obj) {
    var top=0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            top+=obj.offsetTop;
            obj=obj.offsetParent;
        }
    } else if (obj.y){top+=obj.y}
    return top;
}

function createIframe(id) {
	var ifr=ce('iframe');
	ifr.setAttribute('id','dalai');
	ifr.setAttribute('width',320);
	ifr.setAttribute('height',252);
	ifr.setAttribute('frameborder',0);
	ib(ge('vid'),ifr,ge('mbk_dl'));
}

function setAccess() {
	acc=gcookie('artz');
	var al=ge('accLink');
	al.onclick=accClick;
	var mbm=ge('mb-module');
	if (acc!=1) {
		//alert('boo');
		al.innerHTML='Enable AOL Accessibility';
		//scookie('artz','1',365,'/','aol.com');
		//mbm.className="artz-mb";
	} else {
		al.innerHTML='Disable AOL Accessibility';
		//scookie('artz','0',365,'/','aol.com');
		//mbm.className="artz-mb-acc";
	}
}
function accClick() {
	if (acc!=1) {
		scookie('artz','1',365,'/','artzstudio.com');
	} else {
		scookie('artz','0',365,'/','artzstudio.com');
	}
	document.location.reload();
}
function contextArea() {
	var conArea=ce('div');
	conArea.setAttribute('id','context');
	var h5con=ce('h5');
	var h5txt=ct('NOW PLAYING');
	ac(h5con,h5txt);
	var h3con=ce('h3');
	h3con.setAttribute('id','mtitle');
	var pcon=ce('p');
	pcon.setAttribute('id','tct');
	ac(conArea,h5con );
	ac(conArea,h3con);
	ac(conArea,pcon);
	ib(ge('ctxt'),conArea,ge('adblock'));
}