var quoteFlag = 0;
var rightFlag = 0;

window.onload = function() {
	//window.resizeTo(1024,768);
	initializeInterface();
	
	initializeMenu();
	
	initializeMySpace();
	addEvent(document.getElementById('MySpaceButton'), 'click', MySpaceSwitch);
	initializeSNPledge();
	addEvent(document.getElementById('SNPledgeButton'), 'click', SNPledgeSwitch);
	
	initializeFlickr();
	addEvent(document.getElementById('flickrButton'), 'click', flickrSwitch);
	initializeNewsRss();
	addEvent(document.getElementById('newsRssButton'), 'click', newsRssSwitch);
	initializeFacebook();
	addEvent(document.getElementById('FacebookButton'), 'click', FacebookSwitch);
	initializeTwitter();
	addEvent(document.getElementById('TwitterButton'), 'click', TwitterSwitch);
	initializeIWill();
	addEvent(document.getElementById('iWillButton'), 'click', iWillSwitch);
	initializeSNSignup();
	addEvent(document.getElementById('SNSignupButton'), 'click', SNSignupSwitch);
	
	initializeMap();
	addEvent(document.getElementById('mapButton'), 'click', mapSwitch);
	initializeKyte();
	addEvent(document.getElementById('kyteButton'), 'click', kyteSwitch);
	initializeLiveBlog();  //done but off
	addEvent(document.getElementById('liveBlogButton'), 'click', liveBlogSwitch);
	initializeWassup();
	addEvent(document.getElementById('wassupButton'), 'click', wassupSwitch);
	initializeQuotes();
	addEvent(document.getElementById('quotesButton'), 'click', quotesSwitch);
	initializeStats();
	addEvent(document.getElementById('statsButton'), 'click', statsSwitch);
	initializeTwitterRSS();
	addEvent(document.getElementById('twitterRSSButton'), 'click', twitterRSSSwitch);
	
	
	initializeHpLogo();
	addEvent(document.getElementById('hpLogoButton'), 'click', hpLogoSwitch);
	initializeBloggers();
	addEvent(document.getElementById('bloggersButton'), 'click', bloggersSwitch);
	initializeScout();
	addEvent(document.getElementById('scoutButton'), 'click', scoutSwitch);
	initializeYesWeCan();
	addEvent(document.getElementById('yesWeCanButton'), 'click', yesWeCanSwitch);
	initializeLoveNow();
	addEvent(document.getElementById('loveNowButton'), 'click', loveNowSwitch);
	initializeMeetup();
	addEvent(document.getElementById('meetupButton'), 'click', meetupSwitch);
	initializeGraph();
	addEvent(document.getElementById('graphButton'), 'click', graphSwitch);
	initializeBackground();
	addEvent(document.getElementById('backgroundButton'), 'click', backgroundSwitch);
	initializeSNAction();
	addEvent(document.getElementById('SNActionButton'), 'click', SNActionSwitch);

	initializeCountdown();
	addEvent(document.getElementById('countdownButton'), 'click', countdownSwitch);
	initializeRight();
	addEvent(document.getElementById('rightButton'), 'click', rightSwitch);
	
	
	initializeZIndex();

}


window.onkeyup=function(e)
{
	//alert(e.keyCode);
	if(e.keyCode==192) // ~ (tilda)
	{
		if( MySpace[0].visibility=='visible')	MySpaceSwitch();
		if( SNPledge[0].visibility=='visible')	SNPledgeSwitch();
		if( SNSignup[0].visibility=='visible') 	SNSignupSwitch();
		if( flickr[0].visibility=='visible') 	flickrSwitch();
		//if( newsRSS[0].visibility=='visible') 	newsRssSwitch();
		if( Facebook[0].visibility=='visible')	FacebookSwitch();
		if( Twitter[0].visibility=='visible') 	TwitterSwitch();
		if( iWill[0].visibility=='visible') 	iWillSwitch();
		if( map[0].visibility=='visible') 		mapSwitch();
		if( kyte[0].visibility=='visible') 		kyteSwitch();
		if( wassup[0].visibility=='visible') 	wassupSwitch();
		if( quotes[0].visibility=='visible') 	quotesSwitch();
		if( stats[0].visibility=='visible') 	statsSwitch();
		if( twitterRSS[0].visibility=='visible') twitterRSSSwitch();
		if( hpLogo[0].visibility=='visible') 	hpLogoSwitch();
		if( bloggers[0].visibility=='visible') 	bloggersSwitch();
		if( scout[0].visibility=='visible') 	scoutSwitch();
		if( yesWeCan[0].visibility=='visible') 	yesWeCanSwitch();
		if( loveNow[0].visibility=='visible') 	loveNowSwitch();
		if( meetup[0].visibility=='visible') 	meetupSwitch();
		if( graph[0].visibility=='visible') 	graphSwitch();
		if( SNAction[0].visibility=='visible') 	SNActionSwitch();
		if( right[0].visibility=='visible') rightSwitch();



		highlight('None');
//if( full[0].visibility=='visible') fullSwitch();
	}
		
	if(e.keyCode==77)MySpaceSwitch(); 	//m
	if(e.keyCode==80)SNPledgeSwitch();	//p
	if(e.keyCode==83)SNSignupSwitch();	//s
	if(e.keyCode==70)flickrSwitch();	//f
	if(e.keyCode==78)newsRssSwitch();	//n
	if(e.keyCode==67)FacebookSwitch();	//c
	if(e.keyCode==89)TwitterSwitch();	//y
	if(e.keyCode==73)iWillSwitch();		//i
	if(e.keyCode==88)mapSwitch();		//x
	if(e.keyCode==75)kyteSwitch();		//k
	if(e.keyCode==87)wassupSwitch();	//w
	if(e.keyCode==81)quotesSwitch();	//q
	if(e.keyCode==56)statsSwitch();		//8
	if(e.keyCode==84)twitterRSSSwitch();//t
	if(e.keyCode==76)hpLogoSwitch();	//l
	if(e.keyCode==71)bloggersSwitch();	//g
	if(e.keyCode==85)scoutSwitch();		//u
	if(e.keyCode==51)yesWeCanSwitch();	//3
	if(e.keyCode==191)loveNowSwitch();	//?
	if(e.keyCode==69)meetupSwitch();	//e
	if(e.keyCode==72)graphSwitch();		//h
	if(e.keyCode==65)SNActionSwitch();	//a
	if(e.keyCode==66)backgroundSwitch();//b
	if(e.keyCode==86)liveBlogSwitch();	//v
	if(e.keyCode==74)rightSwitch();	//j


	
}



//videos listeners////////////////////////////////////////////////////////////

function playerReady(obj) {
	if(obj['id']=="MySpacePlayer")
	{
		document.getElementById(obj['id']).addControllerListener("ITEM","loadMySpaceTitle");
	}
	
	else if(obj['id']=="flickrPlayer")
	{
		document.getElementById(obj['id']).addControllerListener("ITEM","loadFlickrTitle");
	}

	else if(obj['id']=="statsPlayer")
	{
		document.getElementById(obj['id']).addControllerListener("ITEM","loadStatsCategory");
	}
	
}

function loadFlickrTitle(_id){
	var playlist=document.getElementById('flickrPlayer').getPlaylist();
	var index=document.getElementById('flickrPlayer').getConfig().item;
	var comment=playlist[index].title;
	comment=comment.split('[');
	comment=comment[comment.length-1];
	comment=comment.substring(0,comment.length-1);
	tempResponse = comment;
	flickr[1].insertHTML("flickr tag: "+comment);
}

/*
function loadScoutTitle(_id){
	var playlist=document.getElementById('scoutPlayer').getPlaylist();
	var index=document.getElementById('scoutPlayer').getConfig().item;
	var comment=playlist[index].title;
	comment=comment.split('[');
	comment=comment[comment.length-1];
	comment=comment.substring(0,comment.length-1);
	tempResponse = comment;
	//scout[1].insertHTML("scout tag: "+comment);
	scout[1].insertHTML("photos: scout tufankjian");

}
*/


function loadMySpaceTitle(_id){
	var playlist=document.getElementById('MySpacePlayer').getPlaylist();
	var index=document.getElementById('MySpacePlayer').getConfig().item;
	var comment=playlist[index].title;
	

	comment=comment.substring(comment.indexOf('[')+1,comment.indexOf(']'));
	MySpace[0].insertHTML(comment);

	var category=playlist[index].title;
	category=category.substring(category.indexOf('{')+1,category.indexOf('}'));
	highlight(category);	

}

function loadCausecastTitle(_id){
	var playlist=document.getElementById('CausecastPlayer').getPlaylist();
	var index=document.getElementById('CausecastPlayer').getConfig().item;
	var comment=playlist[index].title;
	comment=comment.split('[');
	comment=comment[comment.length-1];
	comment=comment.substring(0,comment.length-1);
	//tempResponse = comment;
	//MySpace[0].insertHTML(comment+"&nbsp;views");
	Causecast[0].insertHTML(comment);
}

function loadStatsCategory(_id){
	var playlist=document.getElementById('statsPlayer').getPlaylist();
	var index=document.getElementById('statsPlayer').getConfig().item;

	var category=playlist[index].title;
	category=category.substring(category.indexOf('[')+1,category.indexOf(']'));
	highlight(category);
}

function getInterfaceObject(_string)
{
var theObject;
for (var i=0; i<d.length; i++)
{
	if ( _string == d[i].id ) theObject = interfaceArray[i];
}
if (theObject) return theObject;
}

/////////////////////////////////////////////////////////////////////////
function rightSwitch()
{
	interfaceToggle(right,getInterfaceObject('rightButton'));
	if(right[0].visibility == 'visible') 
	{
		if (rightFlag == 0)
		{
		var tempString = "of the people by the people for the people";
		var resultArray = tempString.split("");
		right[0].insertHTML('');
		rightFlag = 1;
		arrayRevealRight(right[0], resultArray, 0, 1);
		}	
	}
	
}

function countdownSwitch()
{
	interfaceToggle(countdown,getInterfaceObject('countdownButton'));
	if(countdown[0].visibility == 'visible') fadeIn(countdown[0]);
}

function backgroundSwitch()
{
	var b = getInterfaceObject('backgroundButton');
	interfaceToggle(background,b);
	setTimeout(function(){document.getElementById('backgroundPlayer').sendEvent('PLAY');},10);

}

function graphSwitch()
{
	interfaceToggle(graph, getInterfaceObject('graphButton'));
	if(graph[0].visibility == 'visible')highlight('Community');
	if(graph[0].visibility=='visible')setTimeout(function(){document.getElementById('graphPlayer').sendEvent('NEXT');},10);
	else setTimeout(function(){document.getElementById('graphPlayer').sendEvent('PLAY');},10);
}

function meetupSwitch()
{
	interfaceToggle(meetup,getInterfaceObject('meetupButton'));
	if(meetup[0].visibility == 'visible')highlight('Community');
	setTimeout(function(){document.getElementById('meetupPlayer').sendEvent('PLAY');},10);

}

function loveNowSwitch()
{
	interfaceToggle(loveNow,getInterfaceObject('loveNowButton'));
	if(loveNow[0].visibility == 'visible')highlight('Community');
	setTimeout(function(){document.getElementById('loveNowPlayer').sendEvent('PLAY');},10);
}

function yesWeCanSwitch()
{
	interfaceToggle(yesWeCan,getInterfaceObject('yesWeCanButton'));
	if(yesWeCan[0].visibility == 'visible')highlight('Community');
	setTimeout(function(){document.getElementById('yesWeCanPlayer').sendEvent('PLAY');},10);
}

function bloggersSwitch()
{
	interfaceToggle(bloggers,getInterfaceObject('bloggersButton'));
	if(bloggers[0].visibility == 'visible') highlight('Community');
	if(bloggers[0].visibility=='visible') scrollUp( bloggers[1] );
}

function MySpaceSwitch()
{
	interfaceToggle(MySpace,getInterfaceObject('MySpaceButton'));
	//needs delay in safari!!!
	if(MySpace[0].visibility=='visible')setTimeout(function(){document.getElementById('MySpacePlayer').sendEvent('NEXT');},10);	
	else setTimeout(function(){document.getElementById('MySpacePlayer').sendEvent('PLAY');},10);
}

function wassupSwitch()
{
	interfaceToggle(wassup,getInterfaceObject('wassupButton'));
	//needs delay in safari!!!
	if(wassup[0].visibility=='visible')
	{
	setTimeout(function(){document.getElementById('wassupPlayer1').sendEvent('NEXT');},10);
	setTimeout(function(){document.getElementById('wassupPlayer2').sendEvent('NEXT');},10);
	
	}
	else{
	setTimeout(function(){document.getElementById('wassupPlayer1').sendEvent('PLAY');},10);
	setTimeout(function(){document.getElementById('wassupPlayer2').sendEvent('PLAY');},10);
	}



	if(wassup[0].visibility == 'visible')highlight('New Media');
}

function SNPledgeSwitch()
{
//interfaceToggle(SNPledge,'SNPledgeButton',"makeHttpSNPledge('./php/getSNRSS.php?url=http://cwwang.com/huffingtonPost2/rss/signups.xml',SNPledge[1],SNPledge[2])");
interfaceToggle(SNPledge,getInterfaceObject('SNPledgeButton'),"makeHttpSNPledge('./php/getSNRSS.php?url=http://www.bethechangeaction.org/pledge/signups?code=post_pass',SNPledge[1],SNPledge[2])",10000);
if(SNPledge[0].visibility == 'visible')highlight('Service');

}

function SNSignupSwitch()
{


interfaceToggle(SNSignup,'SNSignupButton',"makeHttpSNSignup('./php/getSNRSS.php?url=http://www.bethechangeaction.org/pledge/signups?code=post_pass',SNSignup[1])", 20000);
//interfaceToggle(SNSignup,getInterfaceObject('SNSignupButton'),"makeHttpSNSignup('./php/getRSS.php?url="+rssRoot+"/rss/bloggerList.atom',SNSignup[1])");
//'./php/getRSS.php?url="+rssRoot+"/rss/bloggerList.atom'
//bottomScroll(SNSignup[1],200);
if(SNSignup[0].visibility == 'visible')highlight('Service');

}




//scout//////////////////////////////////////////////////////////////////////////
function scoutSwitch()
{
	interfaceToggle(scout,getInterfaceObject('scoutButton'));
	setTimeout(function(){document.getElementById('scoutPlayer').sendEvent('PLAY');},10);
}

//flickr//////////////////////////////////////////////////////////////////////////
function flickrSwitch()
{
	interfaceToggle(flickr,getInterfaceObject('flickrButton'));
	setTimeout(flickrFlick,500);
}


function flickrFlick()
{

	var playlist=document.getElementById('flickrPlayer').getPlaylist();
	var index=document.getElementById('flickrPlayer').getConfig().item;
	var comment=playlist[index].title;
	
	
 	//var r = Math.random()*3000;
	//if(r<1500)r=500;
	
	var r = Math.random();
	if (r<.1) r=500;
	else if (r<.2) r=500;
	else if (r<.3) r=500;
	else if (r<.4) r=500;
	else if (r<.5) r=1000;
	else if (r<.6) r=1000;
	else if (r<.7) r=1000;
	else if (r<.8)  r=5000;
	else if (r<.9)  r=10000;
	else r=10000;

	
	document.getElementById('flickrPlayer').sendEvent('NEXT');
	setTimeout(flickrFlick, r);
}
////////////////////////////////////////////////////////////////////////////



function newsRssSwitch()
{
	//interfaceToggle(newsRSS,6);
	//interfaceToggle2(newsRSS,getInterfaceObject('newsRssButton'),"makeHttpNewsRSS('./php/getRSS.php?url=http://rss.news.yahoo.com/rss/us',newsRSS[0])","moveLeft('newsRSS_0')");
	interfaceToggle2(newsRSS,getInterfaceObject('newsRssButton'),"makeHttpNewsRSS('./php/getRSS.php?url=http://feeds.huffingtonpost.com/huffingtonpost/raw_feed',newsRSS[0])","moveLeft('newsRSS_0')");
	//	interfaceToggle2(newsRSS,getInterfaceObject('newsRssButton'),"makeHttpNewsRSS('./php/getRSS.php?url=http://pipes.yahoo.com/pipes/pipe.run?_id=jgHbhK_g3RG7kaxZBR50VA&_render=rss',newsRSS[0])","moveLeft('newsRSS_0')");
	//http://pipes.yahoo.com/pipes/pipe.run?_id=jgHbhK_g3RG7kaxZBR50VA&_render=rss	
}
function FacebookSwitch()
{
	document.getElementById('facebook_sign_2').style.left = '0px';
	var b = getInterfaceObject('FacebookButton');
	interfaceToggleMotion(Facebook,b,"swipeRightOffscreen('facebook_sign_2')",10);
	if(Facebook[0].visibility == 'visible') highlight('Community');	
}
function TwitterSwitch()
{
	document.getElementById('twitter_sign_2').style.left = '0px';
	interfaceToggleMotion(Twitter,getInterfaceObject('TwitterButton'),"swipeRightOffscreen('twitter_sign_2')",10);

}
function SNActionSwitch()
{
	document.getElementById('snAction_sign_2').style.left = '0px';
	interfaceToggleMotion(SNAction,getInterfaceObject('SNActionButton'),"swipeRightOffscreen('snAction_sign_2')",10);
}

function kyteSwitch()
{
	interfaceToggle(kyte,getInterfaceObject('kyteButton'));
	if(kyte[0].visibility == 'visible')
	{
	blinkOut(kyte[5]);
	highlight('Community');
	document.getElementById('Menu_9').style.backgroundColor = 'rgb(255,0,0)';
	}
	else document.getElementById('Menu_9').style.backgroundColor = 'transparent';
}
function liveBlogSwitch()
{
	interfaceToggle(liveBlog,getInterfaceObject('liveBlogButton'));
	if(liveBlog[0].visibility == 'visible')document.getElementById('Menu_8').style.backgroundColor = 'rgb(255,0,0)';
	else document.getElementById('Menu_8').style.backgroundColor = 'transparent';
	if(liveBlog[0].visibility == 'visible')
	{
		blinkOut(liveBlog[1]);
	}
}
function iWillSwitch()
{
	interfaceToggle(iWill,getInterfaceObject('iWillButton'),"makeHttpIWill('./php/getComments2.php',iWill[1],iWill[2])");
	if(iWill[0].visibility == 'visible') highlight('Service');
}
function quotesSwitch()
{
	interfaceToggle(quotes,getInterfaceObject('quotesButton'),"makeHttpQuotes('./php/getRSS.php?url="+rssRoot+"/rss/hpQuotes.atom',quotes[0],quotes[1],quotes[2])", 12000);
	if(quotes[0].visibility == 'visible')
	{
		highlight(quotes[0].category);	
		fadeIn(quotes[0]);
		fadeIn(quotes[1]);

	}

}
function statsSwitch()
{
	//interfaceToggle(stats,17,"makeHttpStats('./php/getRSS.php?url="+rssRoot+"/rss/stats.atom',stats[0],stats[1],stats[2]);");
	interfaceToggle(stats,getInterfaceObject('statsButton'));
	if(stats[0].visibility=='visible')setTimeout(function(){document.getElementById('statsPlayer').sendEvent('NEXT');},10);
	else setTimeout(function(){document.getElementById('statsPlayer').sendEvent('PLAY');},10);

}
function twitterRSSSwitch()
{
	interfaceToggle(twitterRSS,getInterfaceObject('twitterRSSButton'),"makeHttp('./php/getRSS.php?url=http://search.twitter.com/search.atom?q=obama',twitterRSS[2]);");
	if(twitterRSS[0].visibility == 'visible')
	{
	 fadeIn(twitterRSS[2]);
	 highlight('Community');
	}
}
function hpLogoSwitch()
{
	interfaceToggle(hpLogo,getInterfaceObject('hpLogoButton'));
	if(hpLogo[0].visibility == 'visible') fadeIn(hpLogo[0]);
}

function mapSwitch()
{
	interfaceToggle(map,getInterfaceObject('mapButton'));
	setTimeout(function(){document.getElementById('mapPlayer').sendEvent('PLAY');},10);
	if(map[0].visibility == 'visible') highlight('New Media');
}





/*
function interfaceToggle(_dObjectArray,_switchId,_intervalString, _intervalTime)
{

	var intervalTime = 10000;
	if (_intervalTime) intervalTime = _intervalTime;
	for(i = 0; i<_dObjectArray.length; i++)
	{
		_dObjectArray[i].switchVisibility();
	}
	if(_dObjectArray[0].visibility == 'hidden')
	{
		document.getElementById(switchId).style.color = d[switchId].color;
		if(intervalString)
		{
		clearInterval(interfaceArray[switchId].myIntervals[0]);
		}
	}
	else 
	{
		document.getElementById(switchId).style.color = 'rgb(190,190,190)';
		if(intervalString)
		interfaceArray[switchId].myIntervals[0] = setInterval(intervalString,intervalTime);
		//interfaceArray[switchId].myIntervals[0];
	}
}
*/
function interfaceToggle(_dynamicObjectArray, _switchObject, _intervalString, _intervalTime)
{
	var intervalTime = 10000;
	if (_intervalTime) intervalTime = _intervalTime;
	
	
	for(i = 0; i<_dynamicObjectArray.length; i++)
	{
		_dynamicObjectArray[i].switchVisibility();
	}
	if(_dynamicObjectArray[0].visibility == 'hidden')
	{
	//console.log(_switchObject.id);
		document.getElementById(_switchObject.id).style.color = _switchObject.color;
		if(_intervalString)
		{
		clearInterval(_switchObject.myIntervals[0]);
		}
	}
	else 
	{
		document.getElementById(_switchObject.id).style.color = 'rgb(190,190,190)';
		if(_intervalString) _switchObject.myIntervals[0] = setInterval(_intervalString, intervalTime);
	}	
}

function interfaceToggle2(_dynamicObjectArray, _switchObject,_intervalString, _intervalString1)
{

	for(i = 0; i<_dynamicObjectArray.length; i++)
	{
		_dynamicObjectArray[i].switchVisibility();
	}
	if(_dynamicObjectArray[0].visibility == 'hidden')
	{
		document.getElementById(_switchObject.id).style.color = _switchObject.color;
		
		if(_intervalString)
		{
		clearInterval(_switchObject.myIntervals[0]);
		clearInterval(_switchObject.myIntervals[1]);
		}
	}
	else 
	{
		document.getElementById(_switchObject.id).style.color = 'rgb(190,190,190)';
		if(_intervalString) _switchObject.myIntervals[0] = setInterval(_intervalString,60000);
		if(_intervalString1) _switchObject.myIntervals[1] = setInterval(_intervalString1,33);
		//interfaceArray[switchId].myIntervals[0];
	}
}

function interfaceToggleMotion(_dynamicObjectArray, _switchObject, _intervalString, _intervalTime)
{
	//console.log(_switchObject);
	//_switchObject.myIntervals[0]="helloJ";
	//console.log(_switchObject.myIntervals);

	for(i = 0; i<_dynamicObjectArray.length; i++)
	{
		_dynamicObjectArray[i].switchVisibility();
	}
	if(_dynamicObjectArray[0].visibility == 'hidden')
	{
		document.getElementById(_switchObject.id).style.color = _switchObject.color;
		clearInterval(_switchObject.myIntervals[0]);
	}
	else 
	{
		document.getElementById(_switchObject.id).style.color = 'rgb(190,190,190)';
		_switchObject.myIntervals[0] = setInterval(_intervalString,_intervalTime);
	}	
	//console.log("here");
	//console.log(_switchObject.myIntervals[0]);
}




function addEvent( _el, _trigger, _fn ) {
	if (_el.addEventListener)
		_el.addEventListener( _trigger, _fn, false );
	else if (_el.attachEvent) {
		_el["e"+_trigger+_fn] = _fn;
		_el[_trigger+_fn] = function() { _el["e"+_trigger+_fn]( window.event ); }
		_el.attachEvent( "on"+_trigger, _el[_trigger+_fn] );
	}
}

function removeEvent( _el, _trigger, _fn ) {
	if (_el.removeEventListener)
		_el.removeEventListener( _trigger, _fn, false );
	else if (_el.detachEvent) {
		_el.detachEvent( "on"+_trigger, _el[_trigger+_fn] );
		_el[_trigger+_fn] = null;
		_el["e"+_trigger+_fn] = null;
	}
}


function $style(ElementId, CssProperty)
{
    function $(stringId)
    {
        return document.getElementById(stringId);
     }   

    if($(ElementId).currentStyle)
   {
        var convertToCamelCase = CssProperty.replace(/\-(.)/g, function(m, l){return l.toUpperCase()});
        return $(ElementId).currentStyle[convertToCamelCase];
    }
    else if (window.getComputedStyle)
   {
        var elementStyle = window.getComputedStyle($(ElementId), "");
        return elementStyle.getPropertyValue(CssProperty);
    }
}
