var FLICKRISHAPIKEY = "a1211cdcdc33fc838488f90a55bd5cad";

var lines = new Array();
function Logit(text) {
  lines[lines.length] = text;
}

// *************************************
// Object Jumper Class
// *************************************
function ObjectJumperClass() {
  this.list = new Array();
}

ObjectJumperClass.prototype.AddObject = function(obj) {
  var index = this.list.length;
  for(var i = 0; i < this.list.length; i++) {
    if(this.list[i] == obj) {
      return i;
    }
  }
  this.list[index] = obj;
  return index;
}

ObjectJumperClass.prototype.Add = function(obj) {
  var index = this.list.length;
  for(var i = 0; i < this.list.length; i++) {
    if(this.list[i] == obj) {
      return i;
    }
  }
  this.list[index] = obj;
  return index;
}

ObjectJumperClass.prototype.GetObject = function(index) {
  return this.list[index];
}

ObjectJumperClass.prototype.Get = function(index) {
  return this.list[index];
}

var oJmp = new ObjectJumperClass();

function ObjectJump(index, method, arg) {
  var obj = oJmp.Get(index);
  eval("obj."+method+"(arg);");
}

// *************************************
// Flickrish API-Call
// *************************************
var currentFlickrishAPICall = null;

function FlickrishAPICall() {
  this.callInProgress = false;
  this.callStack = new Array();
  this.callStackIndex = 0;
  this.ojID = oJmp.Add(this);
}

FlickrishAPICall.prototype.Call = function(owner, url, objectID, method) {
  this.callStack[this.callStackIndex] = {'owner':owner,'url':url,'objectid':objectID,'method':method};
  this.callStackIndex++;
  if(this.callInProgress == false) {
    this.DoCall();
  }
}

FlickrishAPICall.prototype.DoCall = function() {
  if(this.callInProgress == false) {
    this.callInProgress = true;
    for(var i = 0; i < this.callStack.length; i++) {
      var callInfo = this.callStack[i];
      if(callInfo != null) {
        currentFlickrishAPICall = callInfo;
        var xscript = document.createElement("script");
        flickrishObject.baseObject.appendChild(xscript);
        var ojid = this.ojID;
        xscript.onload = function() {
          var obj = oJmp.Get(ojid);
          obj.callInProgress = false;
        }
        xscript.src = callInfo['url'];
        this.callStack[i] = null;
        break;
      }
    }
  }
  setTimeout('oJmp.Get('+this.ojID+').DoCall();', 10);
}

var flickrishAPICall = new FlickrishAPICall();

function jsonFlickrApi(result) {
  var obj = oJmp.Get(currentFlickrishAPICall['objectid']);
  eval("obj."+currentFlickrishAPICall['method']+"(result);");
}


// *************************************
// Flickrish Base
// *************************************
function Flickrish() {
  this.baseObject = document.getElementsByTagName('head')[0];
  this.images = new Array();
  this.inPreload = false;
  this.ojID = oJmp.Add(this);
}

Flickrish.prototype.Preload = function(src) {
  this.images[this.images.length] = {'src':src,'image':null};
  this.DoPreload();
}

Flickrish.prototype.DoPreload = function() {
  if(this.inPreload == false) {
    this.inPreload = true;
    var lastIndex = 0;
    for(var i = 0; i < this.images.length; i++) {
      if(this.images[i]['image'] == null) {
        var self = this;
        this.images[i]['image'] = new Image();
        this.images[i]['image'].onload = function() {
          var obj = oJmp.Get(self.ojID);
          eval("obj.DoPreload();");
        }
        this.images[i]['image'].src = this.images[i]['src'];
        break;
      }
      lastIndex = i;
    }
    this.inPreload = false;
  }
}

var flickrishObject = new Flickrish();

// *************************************
// Flickrish Photoset
// *************************************
function FlickrishPhotoset(setID, imageSize, callback) {
  var self = this;
  this.ojID = oJmp.Add(this);
  this.images = new Array();
  if(typeof(imageSize) != 'undefined') {
    this.imageSize = imageSize;
  } else {
    this.imageSize = '';
  }
  this.callback = callback;
  flickrishAPICall.Call(self, "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&format=json&api_key="+FLICKRISHAPIKEY+"&photoset_id="+setID, this.ojID, 'Populate');
}

FlickrishPhotoset.prototype.Populate = function(data) {
  var src;
  var image;
  this.images = new Array();
  if(data["stat"] == "ok") {
    for(var i in data["photoset"]["photo"]) {
      src = 'http://farm'+data["photoset"]["photo"][i]["farm"]+'.static.flickr.com/';
      src += data["photoset"]["photo"][i]["server"]+'/';
      src += data["photoset"]["photo"][i]["id"]+'_';
      src += data["photoset"]["photo"][i]["secret"];
      if(this.imageSize.length > 0) {
        src += '_'+this.imageSize;
      }
      src += '.jpg';

      image = {'src':src, 'title':data["photoset"]["photo"][i]["title"]};
      this.images[this.images.length] = image;
      flickrishObject.Preload(image['src']);
      this.count++;
    }
  }
  if(typeof(this.callback) == 'function') {
    this.callback(this);
  } else if(typeof(this.callback) == 'string') {
    if(this.callback.length > 0) {
      eval(this.callback+'(this);');
    }
  }
}

// *************************************
// Flickrish Display Photoset
// *************************************
function FlickrishDisplayPhotoset(setID, htmlID, imageSize, callback) {
  this.ojID = oJmp.Add(this);
  this.callback = callback;
  this.showEventHandler = null;
  this.showPreEventHandler = null;
  this.htmlID = htmlID;
  this.imageID1 = '';
  this.img1 = null;
  this.imageID2 = '';
  this.img2 = null;
  this.containerID = '';
  this.currentIndex = -1;
  this.step = 5;
  this.stepDelay = 40;
  this.delay = 5000;
  this.active = false;
  this.fadeTimeout = null;
  this.showTimeout = null;
  this.textID = '';
  this.textObject = null;
  this.photoset = new FlickrishPhotoset(setID, imageSize, 'oJmp.Get('+this.ojID+').Draw');
}

FlickrishDisplayPhotoset.prototype.Draw = function(photoset) {
  this.htmlObject = document.getElementById(this.htmlID);
  if(this.htmlObject == null) {
    setTimeout('oJmp.Get('+this.ojID+').Draw('+photoset+');', 10);
    return;
  }
  var id = this.htmlObject.id;
  this.htmlObject.style.position = "relative";
  this.htmlObject.innerHTML = "<img src='nada://none.not/' id='"+id+"_img1' style='position: absolute; top: 0; left: 0; display: none;' /><img src='nada://none.not/' id='"+id+"_img2' style='position: absolute; top: 0; left: 0; display: none;' />"
  this.imageID1 = id+"_img1";
  this.imageID2 = id+"_img2";
  this.containerID = id;
  this.htmlObject.standardWidth = this.htmlObject.style.width;
  this.htmlObject.standardHeight = this.htmlObject.style.height;

  this.Show(0);

  if(typeof(this.callback) == 'function') {
    this.callback(this);
  } else if(typeof(this.callback) == 'string') {
    if(this.callback.length > 0) {
      eval(this.callback+'(this);');
    }
  }
}

FlickrishDisplayPhotoset.prototype.SetTextID = function(textID) {
  this.textID = textID;
  this.textObject = document.getElementById(textID);
  if(this.textObject != null) {
    if(this.currentIndex >= 0) {
      this.textObject.innerHTML = this.photoset.images[this.currentIndex]['title'];
    }
  }
}

FlickrishDisplayPhotoset.prototype.SetShowEventHandler = function(handler) {
  this.showEventHandler = handler;
}

FlickrishDisplayPhotoset.prototype.SetShowPreEventHandler = function(handler) {
  this.showPreEventHandler = handler;
}

FlickrishDisplayPhotoset.prototype.Show = function(index) {
  if(index >= this.photoset.images.length) {
    index = 0;
  } else if(index < 0) {
    index = this.photoset.images.length - 1;
  }
  if(index == this.currentIndex) {
    return;
  }
  var oldIndex = this.currentIndex;
  this.currentIndex = index;
  if(this.img1 == null) {
    this.img1 = document.getElementById(this.imageID1);
  }
  if(typeof(this.img1.SetOpacity) == 'undefined') {
    this.img1.SetOpacity = ImageSetOpacity;
  }
  if(this.img2 == null) {
    this.img2 = document.getElementById(this.imageID2);
  }
  if(typeof(this.img2.SetOpacity) == 'undefined') {
    this.img2.SetOpacity = ImageSetOpacity;
  }
  if(this.img1.src != this.img2.src) {
    this.img1.style.display = 'block';
    this.img1.SetOpacity(100);
  }
  this.img1.src = this.img2.src;
  this.img2.style.display = 'block';
  this.img2.SetOpacity(0);
  this.img2.src = this.photoset.images[index]['src'];
  clearTimeout(this.fadeTimeout);
  this.FadeImages(100);
  if(this.textObject == null) {
    if(this.textID.length > 0) {
      this.textObject = document.getElementById(this.textID);
    }
  }
  if(this.textObject != null) {
    if(this.currentIndex >= 0) {
      this.textObject.innerHTML = this.photoset.images[index]['title'];
    }
  }
  if(this.active == true) {
    clearTimeout(this.showTimeout);
    this.showTimeout = setTimeout('oJmp.Get("'+this.ojID+'").Next();', this.delay);
  }
  if(typeof(this.showEventHandler) == "function") {
    this.showEventHandler(this.currentIndex, oldIndex);
  } else if(typeof(this.showEventHandler) == "string") {
    if(this.showEventHandler.length > 0) {
      eval(this.showEventHandler+'(this.currentIndex, oldIndex);');
    }
  }
}

FlickrishDisplayPhotoset.prototype.Play = function() {
  this.active = true;
  clearTimeout(this.showTimeout);
  this.showTimeout = setTimeout('oJmp.Get("'+this.ojID+'").Next();', this.delay);
}

FlickrishDisplayPhotoset.prototype.Stop = function() {
  this.active = false;
  clearTimeout(this.showTimeout);
}

FlickrishDisplayPhotoset.prototype.Prev = function() {
  this.Show(this.currentIndex - 1);
}

FlickrishDisplayPhotoset.prototype.Next = function() {
  this.Show(this.currentIndex + 1);
}

FlickrishDisplayPhotoset.prototype.SetDelay = function(newDelay) {
  this.delay = newDelay;
}

FlickrishDisplayPhotoset.prototype.FadeImages = function(level) {
  if(level > 0) {
    this.img1.SetOpacity(level);
    this.img2.SetOpacity(100 - level);
    var nextLevel = level - this.step;
    if(nextLevel > 0) {
      this.fadeTimeout = setTimeout('oJmp.Get("'+this.ojID+'").FadeImages('+nextLevel+');', this.stepDelay);
    } else {
      this.fadeTimeout = setTimeout('oJmp.Get("'+this.ojID+'").FadeImages(0);', this.stepDelay);
    }
  }
}



function HTMLEncode(str) {
  var div = document.createElement('div');
  var text = document.createTextNode(str);
  div.appendChild(text);
  return div.innerHTML;
}

// *************************************
// Flickrish Display Photoset Template
// *************************************
function FlickrishDisplayPhotosetTemplate(setID, htmlID, imageSize, template, callback) {
  this.ojID = oJmp.Add(this);
  this.callback = callback;
  this.template = template;
  this.htmlID = htmlID;
  this.photoset = new FlickrishPhotoset(setID, imageSize, 'oJmp.Get('+this.ojID+').Draw');
}

FlickrishDisplayPhotosetTemplate.prototype.Draw = function(photoset) {
  var image;
  var replaceText = '';
  var temp;
  for(var i = 0; i < photoset.images.length; i++) {
    image = photoset.images[i];
    temp = this.template;
    image['index'] = i;
    image['index1'] = (i + 1);
    var txtMatch;
    while((txtMatch = temp.match(/\[mod([0-9]+)\:([^\]]+)\]/)) != null) {
      var text = '';
      var mod = parseInt(txtMatch[1]);
      if(i > 0) {
        if(i % mod == 0) {
          text = txtMatch[2];
        }
      }
      temp = temp.split(txtMatch[0]);
      temp = temp.join(text);
    }
    for(var j in image) {
      temp = temp.split('{'+j+'}');
      temp = temp.join(image[j]);
    }
    while((txtMatch = temp.match(/\[(html|escape)\:([^\]]+)\]/)) != null) {
      if(typeof(image[txtMatch[2]]) == 'undefined') {
        temp = temp.replace(txtMatch[0], '');
      } else {
        if(txtMatch[1] == 'html') {
          temp = temp.replace(txtMatch[0], HTMLEncode(image[txtMatch[2]]));
        } else if(txtMatch[1] == 'escape') {
          temp = temp.replace(txtMatch[0], escape(image[txtMatch[2]]));
        }
      }
    }
    while((txtMatch = temp.match(/\[[\]]+\]/g)) != null) {
      temp = temp.replace(txtMatch[0], '');
    }
    temp = temp.replace(/\{[^\}]+\}/, '');
    temp = temp.replace(/\[[^\]]+\]/, '');
    // temp = temp.split('{index}');
    // temp = temp.join(i);
    replaceText += temp;
  }
  var obj = document.getElementById(this.htmlID);
  obj.innerHTML = replaceText;
  if(typeof(this.callback) == 'function') {
    this.callback(this);
  } else if(typeof(this.callback) == 'string') {
    if(this.callback.length > 0) {
      eval(this.callback+'(this);');
    }
  }
}

// HTMLImageElement.prototype.SetOpacity = function(level) {
function ImageSetOpacity(level) {
  var falseResponse = typeof(this.unsetAttribute);
  if(typeof(this.style.opacity) != falseResponse) {
    this.style.opacity = level / 100;
  } else if(typeof(this.style.filters) != falseResponse) {
    this.style.filter = 'alpha(opacity='+level+')';
  } else if(typeof(this.style.filter) != falseResponse) {
    this.style.filter = 'alpha(opacity='+level+')';
  } else if(typeof(this.style.MozOpacity) != falseResponse) {
    this.style.MozOpacity = level / 100;
  } else if(typeof(this.style.KhtmlOpacity) != falseResponse) {
    this.style.KhtmlOpacity = level / 100;
  } else if(typeof(this.style.MsFilter) != falseResponse) {
    this.style.MsFilter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity='+value+');';
  }
}


