var clips = {"clips": []};
clips = clips.clips;

var ua = navigator.userAgent.toLowerCase();
var macFF = (ua.indexOf("mac") != -1 && ua.indexOf("firefox") != -1)?true:false;

var emailIsIn = false;
var currentRank = null;

xAddEventListener(window, "load", init, false);

function init() {
	
	if(!xGetElementById('clips'))return;
	
	pageSetup();
	
	clipsSetup();
	
}

function pageSetup() {
	
	var ps = xGetElementById('pageSetup');
	var ss = document.styleSheets[0];
	
	var re = new RegExp("[\n]","gm");
	var setupList = ps.innerHTML.split(re);
	
	xEach(setupList, function(d, i) {
		if(d.indexOf(":") == -1)return;
		var data = d.split(":");
		
		switch (data[0]) {
				
			case "header-size":
				xInsertRule(ss, ".clip h1", "font-size:"+data[1]+"px", -1);
				break;
				
			case "header-color":
				xInsertRule(ss, ".clip h1", "color:"+data[1], -1);
				break;
				
			case "text-size":
				xInsertRule(ss, ".clip .choice a", "font-size:"+data[1]+"px", -1);
				break;

			case "text-color":
				xInsertRule(ss, ".clip .choice a", "color:"+data[1], -1);
				break;
			
			case "delimiter-size":
				xInsertRule(ss, ".clip .choices div.divider", "font-size:"+data[1]+"px", -1);
				break;
			
			case "delimiter-color":
				xInsertRule(ss, ".clip .choices div.divider", "color:"+data[1], -1);
				break;
				
			case "top":
			case "left":
			case "bottom":
			case "right":
			case "width":
			case "height":
			case "font-size":
				xStyle(data[0], data[1]+"px", "clips");
				break;
				
			default:
				xStyle(data[0], data[1], "clips");
				break;
				
		}
	});
	
	try {
		xStyle("left",  clipsPosition.x + "px", "clips");
		xStyle("top", clipsPosition.y + "px", "clips");
	} catch(e) {
		//clips position probably not defined
	}
	
	xStyle("top", Math.round(522-295-5) + "px", "emailFriend");
	
	if(!pcIE) {
		xStyle("visibility", "hidden", "emailFriend");
	}

}

function clipsSetup() {
	
	var cd = xGetElementById('clipData');
	
	xWalkTree2(cd, function(el) {
		//data comes in as
		//line delimited. then,
		//each line is delimited as
		//LABEL;W;H;SRC
		
		var clip = {};
		clip.title = el.id;
		clip.files = new Array();
		
		var re = new RegExp("[\n]","gm");
		var clipList = el.innerHTML.split(re);
		
		xEach(clipList, function(c, i) {
			var ca = c.split(";");
			var clipData = {label: ca[0], width: ca[1], height: ca[2], src: ca[3]};
			clip.files.push(clipData);
		});
		
		clips.push(clip);
		
	}, cd);
	
	loadClipData();

}

function loadClipData() {

	var p = xGetElementById('clips');
	p = xFirstChild('clips');

	p.innerHTML = "";

	xEach(clips, function(c, i) {
		
		var el             = xCreateElement('div');
		xAddClass(el, 'clip');
		
		el.innerHTML      += '<h1>'+c.title+'</h1>';
		
		var choices        = xCreateElement('div');
		xAddClass(choices, 'choices');

		xEach(c.files, function(f, j, l) {
			choices.innerHTML  += '<div class="choice"><a href="javascript:play('+f.width+','+f.height+',\''+f.src+'\');">'+f.label+'</a></div>';
			if(j==(l-1))return;
			choices.innerHTML  += '<div class="divider">|</div>';
		});
		
		el.appendChild(choices);
		p.appendChild(el);
		
	});

}

function emailToggle() {
	//alert("emailToggle: " + emailIsIn);
	if(emailIsIn) {
		emailOut();
	} else {
		emailIn();
	}
}
function emailOut() {
	//alert("emailOut: " + emailIsIn);
	if(emailIsIn) {
		document['emailFriendSWF'].emailOut();
	}
	emailIsIn = false;
}
function emailIn() {
	//alert("emailIn: " + emailIsIn);
	
	if(macFF) {
		popWin('/web/wb_trailers/emailFriend.jsp?id='+id,'emailFriend',300,295,'no','no','no','no','no','no','no',false);
	} else {
	
		hide();
		//$('emailFriend').show();
		//xGetElementById('emailFriend').style.height = '1px';
		xGetElementById('emailFriend').style.visibility = 'visible';
		document['emailFriendSWF'].emailIn();
		//$('emailFriendSWF').emailIn();
		emailIsIn = true;
	
	}
}
function emailOutFinished() {
	//alert('emailOutFinished');

	if(!pcIE) {
		//xGetElementById('emailFriend').style.height = '0px';
		xGetElementById('emailFriend').style.visibility = 'hidden';
	}
	emailIsIn = false;
//	$('emailFriend').hide();
}


function play(w,h,src) {

	try {
		//document['trailer'].Stop();
		xGetElementById('trailer').Stop();
	} catch(e) {
		//alert('no movie');
	}
	
	if(xGetElementById('trailer')) {
		try {
			xGetElementById('trailer').style.display = 'none';
			xGetElementById('trailer').style.visibility = 'hidden';
		} catch(e) {
			//
		}
	}
	
	//hide email form
	emailOut();
	
	
	//xGetElementById(size+'Anchor').onmouseover = null;
	//xGetElementById(size+'Anchor').onmouseout = null;
	//changeImages(size, '/wb_trailers/images/'+size+'-over.gif');

	var moviePlayerEl = xGetElementById("moviePlayer");
	moviePlayerEl.style.visibility = "visible";

	var movieEl = xGetElementById("movie");
	
	var hw = 522/2;
	movieEl.style.top = Math.round((522/2)-(h/2) - 20) + 'px';
	
	movieEl.style.width = w + 'px';
 
	movieEl.innerHTML = "<div><a href=\"javascript:hide();\">close</a></div>";
	movieEl.innerHTML += "<embed id='trailer' name='trailer' src='" + src +"' width='" + w + "' height='" + (h*1+15) + "' autoplay='true' controller='true' pluginspage='http://www.apple.com/quicktime/download/' qtnext1='javascript:hide();' EnableJavaSript='true' />";
}

function hide() {
	
	try {
		//document['trailer'].Stop();
		xGetElementById('trailer').Stop();
	} catch(e) {
		//alert('no movie');
	}
	
	if(xGetElementById('trailer')) {
		try {
			xGetElementById('trailer').style.display = 'none';
			xGetElementById('trailer').style.visibility = 'hidden';
		} catch(e) {
			//
		}
	}
	
	xGetElementById("moviePlayer").style.visibility = 'hidden';
	xGetElementById("movie").innerHTML = "";
	
	/*
	resetRollovers(currentRank);
	*/
}







function embedEmailFriend(standalone) {
	document.write('<object width="300" height="295" id="emailFriendSWF" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">');
	document.write('<param value="always" name="allowScriptAccess"/>');
	document.write('<param value="/wb_trailers/med/email_friend.swf" name="movie"/>');
	document.write('<param value="id='+id);
	if(standalone) {
		document.write('&standalone=true');
	}
	document.write('" name="flashvars"/>');
	document.write('<param value="high" name="quality"/>');
	document.write('<param value="false" name="menu"/>');
	document.write('<param value="#FFFFFF" name="bgcolor"/>');
	document.write('<param value="transparent" name="wmode"/>');
	document.write('<embed name="emailFriendSWF" width="300" height="295" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" bgcolor="#FFFFFF" menu="false" quality="high" flashvars="id='+id);
	if(standalone) {
		document.write('&standalone=true');
	}
	document.write('" src="/wb_trailers/med/email_friend.swf"/>');
	document.write('</object>');
}

function embedBottomNav() {
	document.write('<object width="742" height="26" id="bottomNavButtons" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">');
	document.write('<param value="always" name="allowScriptAccess"/>');
	document.write('<param value="/wb_trailers/med/bottom_nav.swf" name="movie"/>');
	document.write('<param value="id='+id+'&extraLink='+emailFriendParams.extraLink+'&extraText='+emailFriendParams.extraText+'&extraLink2='+emailFriendParams.extraLink2+'&extraText2='+emailFriendParams.extraText2+'&extraLink3='+emailFriendParams.extraLink3+'&extraText3='+emailFriendParams.extraText3+'" name="flashvars"/>');
	document.write('<param value="high" name="quality"/>');
	document.write('<param value="false" name="menu"/>');
	document.write('<param value="#FFFFFF" name="bgcolor"/>');
	document.write('<param value="transparent" name="wmode"/>');
	document.write('<embed name="bottomNavButtons" width="742" height="26" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" bgcolor="#FFFFFF" menu="false" quality="high" flashvars="id='+id+'&extraLink='+emailFriendParams.extraLink+'&extraText='+emailFriendParams.extraText+'&extraLink2='+emailFriendParams.extraLink2+'&extraText2='+emailFriendParams.extraText2+'&extraLink3='+emailFriendParams.extraLink3+'&extraText3='+emailFriendParams.extraText3+'" src="/wb_trailers/med/bottom_nav.swf"/>');
	document.write('</object>');
}








// Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL
// xGetElementById r2, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}

// xAddEventListener r8, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xAddEventListener(e,eT,eL,cap)
{
  if(!(e=xGetElementById(e)))return;
  eT=eT.toLowerCase();
  if(e.addEventListener)e.addEventListener(eT,eL,cap||false);
  else if(e.attachEvent)e.attachEvent('on'+eT,eL);
  else {
    var o=e['on'+eT];
    e['on'+eT]=typeof o=='function' ? function(v){o(v);eL(v);} : eL;
  }
}

// xFirstChild r4, Copyright 2004-2007 Michael Foster (Cross-Browser.com)
function xFirstChild(e,t)
{
  e = xGetElementById(e);
  var c = e ? e.firstChild : null;
  while (c) {
    if (c.nodeType == 1 && (!t || c.nodeName.toLowerCase() == t.toLowerCase())){break;}
    c = c.nextSibling;
  }
  return c;
}

// xWalkTree2 r1, Copyright 2005-2007 Olivier Spinelli
function xWalkTree2( oNode, fnVisit, skip, data )
{
  var r=null;
  if(oNode){if(oNode.nodeType==1&&oNode!=skip){r=fnVisit(oNode,data);if(r)return r;}
  for(var c=oNode.firstChild;c;c=c.nextSibling){if(c!=skip)r  =xWalkTree2(c,fnVisit,skip,data);if(r)return r;}}
  return r;
}

// xEach r1, Copyright 2006-2007 Daniel Frechette
function xEach(c, f, s) {
  var l = c.length;
  for (var i=(s || 0); i < l; i++) {
    f(c[i], i, l);
  }
};

// xAppendChild r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xAppendChild(oParent, oChild)
{
  if (oParent.appendChild) return oParent.appendChild(oChild);
  else return null;
}

// xCreateElement r1, Copyright 2001-2007 Michael Foster (Cross-Browser.com)
function xCreateElement(sTag)
{
  if (document.createElement) return document.createElement(sTag);
  else return null;
}

// xAddClass r3, Copyright 2005-2007 Daniel Frechette - modified by Mike Foster
function xAddClass(e, c)
{
  if ((e=xGetElementById(e))!=null) {
    var s = '';
    if (e.className.length && e.className.charAt(e.className.length - 1) != ' ') {
      s = ' ';
    }
    if (!xHasClass(e, c)) {
      e.className += s + c;
      return true;
    }
  }
  return false;
}

// xHasClass r3, Copyright 2005-2007 Daniel Frechette - modified by Mike Foster
function xHasClass(e, c)
{
  e = xGetElementById(e);
  if (!e || e.className=='') return false;
  var re = new RegExp("(^|\\s)"+c+"(\\s|$)");
  return re.test(e.className);
}

// xStyle r1, Copyright 2007 Michael Foster (Cross-Browser.com)
function xStyle(sProp, sVal)
{
  var i, e;
  for (i = 2; i < arguments.length; ++i) {
    e = xGetElementById(arguments[i]);
    if (e.style) {
      try { e.style[sProp] = sVal; }
      catch (err) { e.style[sProp] = ''; } // ???
    }
  }
}

// xInsertRule r2, Copyright 2006-2007 Michael Foster (Cross-Browser.com)
function xInsertRule(ss, sel, rule, idx)
{
  if (!(ss=xGetElementById(ss))) return false;
  if (ss.insertRule) { ss.insertRule(sel + "{" + rule + "}", (idx>=0?idx:ss.cssRules.length)); } // DOM
  else if (ss.addRule) { ss.addRule(sel, rule, idx); } // IE
  else return false;
  return true;
}


function getSizeFromRank(rank) {
	var size = "";
	switch(rank) {
		case 1:
			size = "small";
			break;
		
		case 2:
			size = "medium";
			break;
			
		case 3:
			size = "large";
			break;
			
		default:
			size = "small"
			break;
	}
	return size;
}
