Application = Class.create();
Application.prototype = {
	initialize : function() {
		Event.observe(window,"load", this.onload.bindAsEventListener(this) );
	},
	onload : function() {
		App.Menu.jumpto("home");
		

		CSBfleXcroll('scrollarea');	
		Event.observe("gallery","mouseover",this.Gallery.menuover.bindAsEventListener(this.Gallery) );
		Event.observe("gallery-hover","mouseover",this.Gallery.menuover.bindAsEventListener(this.Gallery) );
		Event.observe("gallery-hover","mouseout",this.Gallery.menuout.bindAsEventListener(this.Gallery));		
		Event.observe("gallery","mouseout",this.Gallery.menuout.bindAsEventListener(this.Gallery));		
	},
	Menu : {
		current : "home",
		jumpto : function(section) {
			$('container').className = section;			
			var template = $(section + "-template").innerHTML;
			switch(section) {
			case "contact":
				$('frame').hide();
				new Ajax.Request("page/get/2",{method:"get",onSuccess:App.Content.load_cb});
				killSimpleSlide();
				break;			
			case "about":
				$('frame').hide();
				new Ajax.Request("page/get/1",{method:"get",onSuccess:App.Content.load_cb});
				killSimpleSlide();
				break;			
			case "yourvisit":
				$('frame').hide();
				new Ajax.Request("page/get/4",{method:"get",onSuccess:App.Content.load_cb});
				killSimpleSlide();
				break;			
			case "testimonials":
				$('frame').hide();
				new Ajax.Request("page/get/3",{method:"get",onSuccess:App.Content.load_cb});
				killSimpleSlide();
				break;
			case "gallery":
				var html = SXOOP.template.parse(template);
				$("frame").innerHTML = html;
				killSimpleSlide();
				break;
			case "home":
			default:	
				$('frame').hide();	
				
				var html = SXOOP.template.parse(template);
				$("frame").innerHTML = html;	
				initSimpleSlide();						
				Loader.cacheOrLoad($$('#simpleslide-1 img')[0].src,{onComplete: function(){				
						new Effect.Appear('frame',5);
				}});
			}
			
			this.current = section;
			

		}
		
	},
	Content : {
		load_cb : function(data) {
			var template = $(App.Menu.current + "-template").innerHTML;
			this.data = eval(data.responseText.strip());
			var html = SXOOP.template.parse(template,this.data);
			
			$("frame").innerHTML = html;
			setTimeout(function() { Effect.Appear('frame',3); },200);
			
			if (document.all) { return; } //skip custom scroll bar for IE.
			CSBfleXcroll('scrollarea');			
		}
	},
	Gallery : {
		keepmenu : "",
		current : "", //current gallery
		page : 0,
		thumbLength : 6,
		next : function() {
			this.loadPage(App.Album.current,this.page + 1);
		},
		back : function() {
			this.loadPage(App.Album.current,this.page - 1);
		},
		menuover : function() {
			Element.show("gallery-hover");
			clearInterval(this.keepmenu);
		},
		menuout : function() {
			this.keepmenu = setInterval("App.Gallery.menuclose()",300);
		},		
		menuclose : function() {
			Element.hide("gallery-hover");				
		},
		populateThumb : function(albumdata,page) {
			
			var divArray = $$('#thumbnail .thumbnail');
			divArray.each(Element.hide);
			var offset = page * this.thumbLength;
			for(var i=0;offset < (page + 1) * this.thumbLength && offset < albumdata.length;i++) {
/*				alert(albumdata[offset][0])*/
				divArray[i].getElementsByTagName('IMG')[0].setAttribute("src",albumdata[offset][0]);
				divArray[i].imageData = albumdata[offset];
				divArray[i].show();
				offset++;
			}
			$$('#thumb-nav div').each(Element.hide);
			if(page > 0) { $$('#thumb-nav .back')[0].show(); }
			if(offset < albumdata.length) { $$('#thumb-nav .next')[0].show(); }
		},
		populateMain : function(imageData){
			$$("#gallery-image .display")[0].hide();
			if (document.all) {
				Loader.cacheOrLoad(imageData[1],{onComplete: function(){								
						Effect.Appear($$("#gallery-image .display")[0],3);
						$$("#gallery-image .display")[0].setAttribute("src",imageData[1]);		
						$("caption").innerHTML = imageData[2];
				}});												
			}else {				
				setTimeout( function() {
					Loader.cacheOrLoad(imageData[1],{onComplete: function(){								
							Effect.Appear($$("#gallery-image .display")[0],3);
							$$("#gallery-image .display")[0].setAttribute("src",imageData[1]);		
							$("caption").innerHTML = imageData[2];
					}});				
				},100)
			}
			
			
		},
		loadPage : function(albumdata,page) {
			var offset = page * this.thumbLength;
			this.page = page;
			this.populateThumb(albumdata,page);
			this.populateMain(albumdata[offset]);
		}
		
		
		
	},
	Album : {
		current  : [],
		load : function(url) {
			new Ajax.Request(url,{method:"get",onSuccess:App.Album.load_cb});
		},
		load_cb : function(data) {
			App.Album.current = eval(data.responseText);
			App.Gallery.loadPage(App.Album.current,0);
			/*			alert(eval(data.responseText));*/
			
		}
		
	}
	
}


var Loader = {
  _assets: $H({}),
  _checkInterval: null,
  _options: {},
  _update: function() {
    var allLoaded = true;
    Loader._assets.each(function(a){
      if(!a[1].complete && a[1].image.complete) {
        a[1].complete = true;
        a[1].completed_at = new Date().getTime();;
        if(a[1].options.onComplete) a[1].options.onComplete(a[0]);
      }
      if(!a[1].complete && !a[1].image.complete) allLoaded = false;
    });
    if(allLoaded) {
      clearInterval(Loader._checkInterval);
      Loader._checkInterval = null;
      if(Loader._options && Loader._options.onComplete) Loader._options.onComplete();
      Loader._options = null;
    }
  },
  initialize: function() {
    var options = arguments[0] || {};
    Loader._options = options;
  },
  cacheOrLoad: function(url) {
    var options = arguments[1] || {};
    if(this.isLoaded(url)) {
      if(options.onComplete) options.onComplete();
    } else {
      this.load(url, options);
    }
  },
  load: function(url) {
    if(Loader._assets[url]) return;
    var options = arguments[1] || {};
    var a = {};
    a.image = new Image();
    a.image.src = url;
    a.complete = false;
    a.options  = options;
    a.loaded_at = new Date().getTime();
    Event.observe(a.image, 'error', function(){ Loader.error(url) });
    Loader._assets[url] = a;
    if(!Loader._checkInterval) Loader._checkInterval = setInterval(Loader._update,10);
  },
  error: function(url) {
    var asset = Loader._assets[url];
    asset.complete = true;
    if(asset.options.onComplete) asset.options.onComplete('/images/empty.gif');
  },
  stats: function(url) {
    return (Loader._assets[url]._complete ?
      (Loader._assets[url]._completed_at - Loader._assets[url]._loaded_at) : null);
  },
  isQueued: function(url) {
    return !!(Loader._assets[url]);
  },
  isLoaded: function(url) {
    return (Loader._assets[url] && Loader._assets[url].complete);
  },
  reset: function() {
    Loader._assets = $H({});
  }
};

var App = new Application();

