/*************** Class Methods **************/
function Photo (newPhoto) {
  this.p = newPhoto;
  this.queued = false;
  
  this.addToQueue = function() {
    if (this.s_img.loaded && this.l_img.loaded && !this.queued) {
      Photo.queue.push(this);
      this.queued = true;
      if (Photo.current == null) { Photo.play(); }
    }
  }

  
  if (this.p.url_s) {
  	this.s_img = new Image(parseInt(this.p.width_s),parseInt(this.p.height_s));
  	this.s_img.loaded = false;
		this.s_img.src = this.p.url_s;
		this.s_img.id = "photo";
		this.s_img.parent = this;
		this.s_img.onload = function () { this.loaded = true; this.parent.addToQueue(); }
	}
	
  if (this.p.url_l) {
    this.l_img = new Image(this.p.width_l,this.p.height_l);
    this.l_img.loaded = false;
		
    this.l_img.src = this.p.url_l;
    this.l_img.id = "zoomedPhoto";
    this.l_img.parent = this;
    this.l_img.onload = function () { this.loaded = true; this.parent.addToQueue(); }
    this.s_img.onmouseover = function () { new Effect.Appear('zoombox'); Photo.positionElements();	};		
  }
}

Photo.timer = null;
Photo.timerInterval = 5000;
Photo.current = null;

Photo.processNew = function(responseText) {
	var newPhotos = JSON.parse(responseText);
	newPhotos.each(function(s) { new Photo(s); } );
}

Photo.load = function () {
	if (!Photo.loading) {
		var request = new Ajax.Request('/streams/' + Photo.stream + '.json', { 
			method : 'get',
			onCreate : function () { Photo.loading = true; },
			onSuccess : function(transport) { Photo.processNew(transport.responseText); },
			onComplete : function () { Photo.loading = false; } 
		});
	}
}

Photo.pause = function () {
	clearTimeout(this.timer);
	this.timer = null;
}

Photo.play = function () {
	var photo = Photo.queue.shift();
	if (photo) { photo.show(); }
	Photo.continueShow();
};

Photo.prototype.show = function () {	
	Photo.current = this;
	this.display();
	z = document.getElementById('zoomimg');
	if (z) {
    z.innerHTML = "";
  	z.appendChild(this.l_img);
  }
	this.sizeElements();
};

Photo.continueShow = function () {
	this.timer = setTimeout(Photo.play, this.timerInterval);
};

Photo.positionElements = function () {
  var dims = document.viewport.getFullDimensions();
  var w = dims.width;
  var h = dims.height;
  var center_w = parseInt(w/2.0);
  
  Photo.max = {};
  Photo.max.zh = (h*0.8)-100;
  Photo.max.zw = (w*0.55)-100;

  var m = $("map");
  if (m) { m.style.top = 0; m.style.left = 0;  m.style.width=w; m.style.height=h; }
  
  zb = document.getElementById('zoombox');
	zb.style.top="80px";
	zb.style.width=parseInt(w*0.55) + "px";
	zb.style.left=parseInt(center_w-((w*0.55)/2)) + "px";
	
	if (Photo.current)
	  Photo.current.sizeElements();
}

Photo.launch = function () {
  
  Photo.queue = new Array;
  Photo.timer = null;
  Photo.timerInterval = 5000;
  Photo.current = null;

  Photo.positionElements();

  var matches = window.location.toString().match(/stream=(\S+)&?/);
  Photo.stream = matches ? matches[1] : 'global';
  Photo.load();
  
  $('zoombox').onclick = function () { new Effect.Fade('zoombox'); }
}

window.onresize = Photo.positionElements;

