Event.observe(window, 'load', function() { popupWindow() });

function hover(src, func) {
    $('imagemap').src = src;
	// callback
    if (func) func();
}     

var newsWin;
function showNewsWindow() {
    newsWin = new Window({className: "alphacube", top:100, left:50, minWidth:450, zIndex: 100
        , resizable: true, title: 'news', showEffect:Effect.Appear, hideEffect: Effect.SwitchOff, draggable:true, wiredDrag: true});
    newsWin.setAjaxContent('/news', { method: 'get', onComplete: updateWindow })
    newsWin.setCloseCallback(playClose)
}

function updateWindow() {
  soundManager.setVolume('beep',75);
  soundManager.play('beep',1);

  var dimensions = calculateHeight($$('.news_item'));
  newsWin.setSize(450,dimensions.height);
}

function calculateHeight(col) {
  var totalHeight = 0;
  var totalWidth = 0;
  // can't get the height of a display:none div :(
  col.collect(function(el){ totalHeight +=  200; } );
  col.collect(function(el){ totalWidth +=  el.getDimensions().width} );

  return {"width": totalWidth, "height":totalHeight}
}



function showCommsWindow() {
    var win = new Window({className: "alphacube", top:0, left:800, minWidth:300, height:220, zIndex: 99
        , resizable: true, title: 'communicate', showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
    win.setAjaxContent('/communicate', { method: 'get' })
    soundManager.setVolume('beep',75);
    soundManager.play('beep',2);
    win.show();
}
function showPropWindow() {
    var win = new Window({className: "alphacube", top:450, left:550, minWidth:350, height:220, zIndex: 98
        , resizable: true, title: 'propaganda', showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
    win.setAjaxContent('/propaganda', { method: 'get' })
    soundManager.setVolume('beep',75);
    soundManager.play('beep',3);
    win.show();
}

function showDubsWindow() {
    var win = new Window({className: "alphacube", top:100, left:60, minWidth:450, zIndex: 97
        , resizable: true, title: 'dubs', showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
    win.setAjaxContent('/dubs', { method: 'get' })
    soundManager.setVolume('beep',75);
    soundManager.play('beep',3);
    win.show();
}

function showArtistsWindow() {
    var win = new Window({className: "alphacube", top:100, left:70, minWidth:450, zIndex: 96
        , resizable: true, title: 'artists', showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
    win.setHTMLContent('/artists', { method: 'get' })
    soundManager.setVolume('beep',75);
    soundManager.play('beep',3);
    win.show();
}


function showPlayerWindow(track, offset) {
	if (!offset) offset = 0;
    var win = new Window({className: "alphacube", top:150 - offset, left:450 - offset, minWidth:350, height:110, zIndex: 101
        , resizable: true, title: track, showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true});
    win.setAjaxContent('/player?track='+track, { method: 'get' })
    soundManager.setVolume('beep',75);
    soundManager.play('beep',3);
    win.show();
}

function popupWindow() {

  if ($('popup_window')) {
    var win = new Window({className: "alphacube", top:10, left:100, zIndex: 98
        , resizable: true, title: '', showEffect:Effect.Appear, hideEffect: Effect.SwitchOff, draggable:true, wiredDrag: true});
    win.setHTMLContent($('popup_window').innerHTML)
    $('popup_window').hide();
    soundManager.setVolume('beep',75);
    soundManager.play('beep',3);
    win.setSize($('popup_window').getDimensions().width,$('popup_window').getDimensions().height);
    win.setCloseCallback(playClose)
    win.show();
  }

}

function playClose() {
    soundManager.setVolume('page',75);
    soundManager.play('page',1);
    return true;
}

var idleTime    = 5000;
var timeOut     = '';
var resetTimeOut = '';

function init() {
    Event.observe(document.body, 'mousemove', resetIdle, true);
    setIdle();
}

function onIdleFunction(){
                      
	images = ['/images/infrasonics_news.jpg', '/images/infrasonics_propaganda.jpg', '/images/infrasonics_communicate.jpg']
   	// console.log('Your browser has been idle for ' + (idleTime/1000) +' seconds.');

	shuffle(images)
        soundManager.setVolume('select',20);
        soundManager.play('select');
        hover(images[0]);

	window.clearTimeout(resetTimeOut);
	resetTimeOut = window.setTimeout("resetInitImage()", idleTime );

}

function resetInitImage(){        
	hover('/images/infrasonics.jpg');
	resetIdle();
}

function hi() {
    // console.log('CALLBACK');
}

function resetIdle(){
    window.clearTimeout( timeOut );
    setIdle();
}

function setIdle(){
    timeOut = window.setTimeout( "onIdleFunction()", idleTime );
}                  

function shuffle(list)
{
	var i, j, k;
	var temp;

	if (list)
	{
		for (j = 0; j < list.length; j++)
		{
		  k = Math.floor(Math.random() * list.length);
		  temp = list[j];
		  list[j] = list[k];
		  list[k] = temp;
		}
	}
	return list;
}             
 

//MSP don not commit! Event.observe(window, 'load', init, false);

