/***********************************************
* CMotion Image Gallery- Ã‚Â© Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for source code
* Last updated Mar 15th, 04'. Added "End of Gallery" message.
* This copyright notice must stay intact for legal use
***********************************************/

var restarea=6 //1) width of the "neutral" area in the center of the gallery in px
var maxspeed=2 //2) top scroll speed in pixels. Script auto creates a range from 0 to top speed.
var endofgallerymsg="" //3) message to show at end of gallery. Enter "" to disable message.
var endofgallerymsg1="" //3) message to show at end of gallery. Enter "" to disable message.

function enlargeimage(path, optWidth, optHeight){ //function to enlarge image. Change as desired.
var actualWidth=typeof optWidth!="undefined" ? optWidth : "600px" //set 600px to default width
var actualHeight=typeof optHeight!="undefined" ? optHeight : "500px" //set 500px to  default height
var winattributes="width="+actualWidth+",height="+actualHeight+",resizable=yes"
window.open(path,"", winattributes)
}

////NO NEED TO EDIT BELOW THIS LINE////////////
var iedom=document.all||document.getElementById
var iedom1=document.all||document.getElementById
var scrollspeed=0
var movestate=""
var movestate1=""
var actualwidth=''
var actualwidth1=''
var cross_scroll, ns_scroll, cross_scroll1
var loadedyes=0
var loadedyes1=0

function changeImage(filename){
   document.mainimage.src = filename;
}

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft: what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function moveleft(){
if (loadedyes){
movestate="left"
if (iedom&&parseInt(cross_scroll.style.left)>(menuwidth-actualwidth+2)){
cross_scroll.style.left=parseInt(cross_scroll.style.left)-scrollspeed+"px"
}
}
lefttime=setTimeout("moveleft()",10)
}

function moveright(){
if (loadedyes){
movestate="right"
if (iedom&&parseInt(cross_scroll.style.left)<0){
cross_scroll.style.left=parseInt(cross_scroll.style.left)+scrollspeed+"px"
}
}
righttime=setTimeout("moveright()",10)
}

function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function stopmotion(e){
if (window.lefttime) clearTimeout(lefttime)
if (window.righttime) clearTimeout(righttime)
movestate=""
}

function fillup(){
if (iedom){
crossmain=document.getElementById? document.getElementById("motioncontainer") : document.all.motioncontainer
menuwidth=parseInt(crossmain.style.width)
mainobjoffset=getposOffset(crossmain, "left")
cross_scroll=document.getElementById? document.getElementById("motiongallery") : document.all.motiongallery
actualwidth=document.all? cross_scroll.offsetWidth : document.getElementById("motiongallery").offsetWidth
leftmain=document.getElementById? document.getElementById("leftbutt") : document.all.leftbutt
rightmain=document.getElementById? document.getElementById("rightbutt") : document.all.rightbutt

leftmain.onmouseover=function(e){
scrollspeed=2
moveright()
}

rightmain.onmouseover=function(e){
scrollspeed=2
moveleft()
}

leftmain.onmouseout=function(e){
stopmotion(e)
}

rightmain.onmouseout=function(e){
stopmotion(e)
}
}

loadedyes=1
if (endofgallerymsg!=""){
creatediv()
positiondiv()
}
cross_scroll.style.left="0px"
}

function start() {
fillup();
}

/**** Top Stories mouse over image change ******/
function mouseOver(imageOver,txt) {
document.getElementById("topstory").src=imageOver
document.getElementById(txt).style.left="15px";
document.getElementById(txt).style.width="250px";
}

function mouseOverOut(imageOver,txt) {
document.getElementById("topstory").src=imageOver
document.getElementById(txt).style.left="15px";
document.getElementById(txt).style.width="250px";
}

function mouseOut(imageOut,txt) {
document.getElementById("topstory").src=imageOut
document.getElementById(txt).style.left="-9999999px";
}

function hideallImage(){
  for (count=1; count<7; count++)
  document.getElementById('image'+count).style.display='none';
}

function showImage(box){
	document.getElementById("imagesContainer").style.left="9px";
	document.getElementById(box).style.display='block';
}
/**** end Top Stories mouse over image change ******/

/**** tabs change content on click ******/
function hideall(thisCount,lastCount){
  for (count=thisCount; count<lastCount; count++) // set "lastcount" to one more than the total images/links
  document.getElementById('box'+count).style.display='none';
}

function showbox(box){
  document.getElementById(box).style.display='block';
}

var lastID = 0;
var thisID = 5;
function SelectImg(id,tabs) {
  if (lastID > 0) {
    document.getElementById(lastID).className = "tabNormal";
  }
  document.getElementById(id).className = tabs;
  lastID = id;
}

function SelectImg2(id,tabs) {
  if (thisID > 5) {
    document.getElementById(thisID).className = "tabNormal";
  }
  document.getElementById(id).className = tabs;
  thisID = id;
}
/**** end tabs change content on click ******/

/**** Web Video Hulu RSS Feed ******/
function rssfeedsetup(){
var feedpointer=new google.feeds.Feed(feedurl) //Google Feed API method
feedpointer.setNumEntries(feedlimit) //Google Feed API method
feedpointer.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
feedpointer.load(displayfeed) //Google Feed API method
}

function displayfeed(result){
	if (!result.error)
	{
		var thefeeds=result.feed.entries
		rssoutput+="<table><tr valign='top'>"
		for (var i=0; i<thefeeds.length; i++)
		{
			var xmlElement = thefeeds[i].xmlNode
			var titleElement = xmlElement.getElementsByTagName("title")[0]
			var title = titleElement.firstChild.nodeValue
			var index = title.indexOf("(")

			if (index > 0 )
			{
				name = title.substr(0,index)
			}
			if (index < 0 )
			{
				name = title.substr(0,35)
			}

			var linkElement = xmlElement.getElementsByTagName("link")[0]
			var eLink = linkElement.firstChild.nodeValue
			var imageElement = xmlElement.getElementsByTagName("media:thumbnail")[0]
			var image = imageElement.getAttribute("url")

			rssoutput+="<td class='HuluBox'><a href='" + eLink + "' target='_blank'>" + "<img src='" + image + "' border=0>"  + "</a>" + "<br/><div class='chartCaption'><a href='" + eLink + "' target='_blank'>" + name + "</a></div></td>"
			feedcontainer.innerHTML=rssoutput
		}
		rssoutput+="</tr></table>"
	}
	else
			alert("Error fetching feeds!")
}
/**** Web Video Hulu RSS Feed ******/

/*** Cookie for default chart ***/
function setDefaultChart(chart) {
    if( chart == "Film" ) { showbox('box6');SelectImg2(6,'tabFilmActive'); }
    if( chart == "TV Ratings" ){ showbox('box7');SelectImg2(7,'tabTVActive');}
    if( chart == "Legit"){showbox('box8');SelectImg2(8,'tabTheaterActive');}
    if( chart == "iTunes"){showbox('box9');SelectImg2(9,'tabiTunesActive');}
    if( chart == "Web Video"){showbox('box10');SelectImg2(10,'tabHuluActive');}
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
	checkingChart=getCookie('v_chartDefault');
	if (checkingChart!=value)
	{
		setCookie2(c_name,value,expiredays);
		alert('Your default chart is now set to ' + value+ '.\n' + value + ' will display as your default chart the next time you visit Variety.');
	}
	else
	{
	expiredays=expiredays - 400;
	setCookie2(c_name,value,expiredays);
  }
}

function setCookie2(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	
}

function checkCookie()
{
	dChart=getCookie('v_chartDefault');
	if (dChart!=null && dChart!="")
	{
	  document.getElementById('box6').style.display='none';
	  setDefaultChart(dChart);
	}
	else
	{
	showbox('box6');
	SelectImg2('6','tabFilmActive');
  }
}

/* for activating chart checkbox */
function setCheckbox(thisTab) {
	 dChart=getCookie('v_chartDefault');
    if( dChart == thisTab ) {
	 	document.write('<div class="chartsFooterCheckbox">My default chart: '+thisTab+' &nbsp;</div>');
	 } else {
		document.write('<div class="chartsFooterCheckbox">Make this my default chart <input style="border-color: rgb(23, 74, 118);" name="chartsDefault" value="1" type="checkbox" onc'+'lick="setCookie(\'v_chartDefault\',\''+thisTab+'\',365);"></div>');
	 }
}

function checkCookieCheckbox(boxID)
{
	dChart=getCookie('v_chartDefault');
	if (dChart!=null && dChart!="")
	{
	setCheckbox(boxID);
	}
	else
	{
	document.write('<div class="chartsFooterCheckbox">Make this my default chart <input style="border-color: rgb(23, 74, 118);" name="chartsDefault" value="1" type="checkbox" onc'+'lick="setCookie(\'v_chartDefault\',\''+boxID+'\',365);"></div>');
  }
}
/*** end Cookie for default chart ***/


/**** font resize ***/
// This Javascript is written by Peter Velichkov (http://blog.creonfx.com)
// and is distributed under the following license : http://creativecommons.org/licenses/by-sa/3.0/
// Use and modify all you want just keep this comment. Thanks

var incdec = 0;
var headID = document.getElementsByTagName("head")[0];
var cssNode = document.createElement("style");
cssNode.type = 'text/css';
cssNode.id="resizingText";

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function loadCss(x){
	try{
		var cssStr = '\
		div.TopStoryBlockFirst #text, div.TopStoryBlock #text2, div.TopStoryBlock #text3, div.TopStoryBlock #text4, div.TopStoryBlock #text5, div.TopStoryBlockLast #text6, .ts_head, .ts_title, .ts_nutgraph, #reviews .deck, #reviews .deck a, #blogs .deck, #FeaturedJobs {font-size:' + (10+x) +'px;}\
		';

if(cssNode.styleSheet){
			cssNode.styleSheet.cssText = cssStr; // for IE
		} else {
			var cssText = document.createTextNode(cssStr);
			cssNode.appendChild(cssText); // breaks ie
			//cssNode.innerHTML = cssStr; // breaks saffari
		}
		if(!document.getElementById("resizingText"))headID.appendChild(cssNode);
	}catch(err){
		// some debugging code
	}
}

function increaseFontSize() {
	if(incdec < 4){
		incdec++;
		loadCss(incdec);
		createCookie('textsize',incdec,1);
	}
}

function decreaseFontSize() {
	if(incdec > 0){
		incdec--;
		loadCss(incdec);
		createCookie('textsize',incdec,1);
	}
}

var x = readCookie('textsize')
if (x && x!=0) {
	x = parseInt(x);
	incdec = x;
	loadCss(x);
}
/**** end font resize ***/

/* CSS browser selector
CSS Browser Selector v0.3.1
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua = u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1;},g='gecko',w='webkit',s='safari',h=document.getElementsByTagName('html')[0],b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3')?g+' ff3':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' '+s+' chrome':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win':is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);
/*** end CSS browser selector ***/


/*** Cross Fader for Big Mama Emmys video ***/
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Timothy Groves | http://www.brandspankingnew.net/
http://javascript.internet.com/css/javascript-css-crossfader.html */
var useBSNns;

if (useBSNns) {
	 if (typeof(bsn) == "undefined")
		  bsn = {}
	   var _bsn = bsn;
} else {
	  var _bsn = this;
}

_bsn.Crossfader = function (divs, fadetime, delay ) {
	 this.nAct = -1;
	 this.aDivs = divs;

	 for (var i=0;i<divs.length;i++) {
	   document.getElementById(divs[i]).style.opacity = 0;
   	document.getElementById(divs[i]).style.position = "absolute";
	   document.getElementById(divs[i]).style.filter = "alpha(opacity=0)";
 	  document.getElementById(divs[i]).style.visibility = "hidden";
	 }

	 this.nDur = fadetime;
  this.nDelay = delay;
  this._newfade();
}

_bsn.Crossfader.prototype._newfade = function() {
  if (this.nID1)
    clearInterval(this.nID1);
	   this.nOldAct = this.nAct;
    this.nAct++;

 	if (!this.aDivs[this.nAct])	this.nAct = 0;

 	if (this.nAct == this.nOldAct)
    return false;

  document.getElementById( this.aDivs[this.nAct] ).style.visibility = "visible";
  this.nInt = 50;
  this.nTime = 0;
  var p=this;
  this.nID2 = setInterval(function() { p._fade() }, this.nInt);
}

_bsn.Crossfader.prototype._fade = function() {
  this.nTime += this.nInt;
 	var ieop = Math.round( this._easeInOut(this.nTime, 0, 1, this.nDur) * 100 ); // change the last number for opacity at start of div load
 	var op = ieop / 50;  // middle opacity in between time change
 	document.getElementById( this.aDivs[this.nAct] ).style.opacity = op;
 	document.getElementById( this.aDivs[this.nAct] ).style.filter = "alpha(opacity="+ieop+")";

  if (this.nOldAct > -1) {
    document.getElementById( this.aDivs[this.nOldAct] ).style.opacity = 1 - op;
  		document.getElementById( this.aDivs[this.nOldAct] ).style.filter = "alpha(opacity="+(100 - ieop)+")";
	 }

	 if (this.nTime == this.nDur) {
		  clearInterval( this.nID2 );

  if (this.nOldAct > -1)
	   document.getElementById( this.aDivs[this.nOldAct] ).style.visibility = "hidden";
    var p=this;
    this.nID1 = setInterval(function() { p._newfade() }, this.nDelay);
  }
}

_bsn.Crossfader.prototype._easeInOut = function(t,b,c,d) {
  return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}
/*** end Cross Fader ***/


function LoadTrigger() {
	hideall('1','11'); // initilize, hide all tabs
	showbox('box1'); // show "All" tab contents for Top Stories
	SelectImg('1','allTabActive'); // select "All" as active tab for Top Stories
        hideallImage();showImage('image1')
	checkCookie(); // check cookie for default chart setting
}

// Multiple onload function created by: Simon Willison
// http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  start();  // photo gallery scroll
  LoadTrigger(); // activate top stories and charts tabs
});

