var ScriptLoader = {
	request: null, loaded: {}, load: function() { 
		for (var i = 0, len = arguments.length; i < len; i++) { 
			var filename = arguments[i]; 
			if (!this.loaded[filename]) { 
				if (!this.request) { 
					if (window.XMLHttpRequest) this.request = new XMLHttpRequest;
					else if (window.ActiveXObject) {
						try {
							this.request = new ActiveXObject('MSXML2.XMLHTTP'); 
						}
						catch (e) {
							this.request = new ActiveXObject('Microsoft.XMLHTTP');
						}
					}
				}
				if (this.request) {
					this.request.open('GET', filename, false);
					this.request.send(null);
					if (this.request.status == 200) {
						this.globalEval(this.request.responseText);
						this.loaded[filename] = true;
					}
				} 
			}
		}
	}, globalEval: function(code) {
		if (window.execScript) window.execScript(code, 'javascript');
		else window.eval(code);
	} 
}

function addReflect() {
	var images = [];
	$$('#content img.reflect').each(function(el, i){
		images[i] = el.src;
	});

	var loadedImages = [];

	new Asset.images(images, {
		onProgress: function(i) {
			loadedImages[i] = this;
		},
		onComplete: function() {
			loadedImages.each(function(image, i) {
				doReflect();
			});
		}
	});
}

function doReflect() {

	$$('#content img.reflect').each(function(el){
		Reflection.add(el ,{ height: 3/7, opacity: 2/7 });
	});
	
}

function loadSub(url) {
	
	new Ajax(url, {
		method: 'get',
		update: $('content_frame'),
		encoding: 'utf-8',
		onSuccess: function() {
			$('imageMenu').effect('height',{duration:500}).start(50);
			$('mainframe').effect('height',{duration:500}).start(50);
		},
		onComplete: function() {
			ScriptLoader.load('/templates/fiskmarkadurinn_2008/js/smoothbox.js');
			var box = $('content_frame');
			box.style.margin = '50px 0';
			var fxIn = box.effects({duration: 200, transition: Fx.Transitions.Quart.easeIn});
			fxIn.start({
			}).chain(function() {
				this.start.delay(0700, this, {'opacity' : 1});
			});

			$$("a.smoothbox").each(function(el){el.onclick=TB_bind});
			$$("a.smoothbox").each(function(el){el.onkeypress=TB_bind});
			
			addReflect().delay(200);
		}
	}).request();

}

window.addEvent('domready', function(){

	var myMenu = new ImageMenu(
  $$('#imageMenu a.menu'),
  {
    openWidth:675, 
    border:2, 
    onOpen:function(e,i){
			var box = $('content_frame');
			var fxOut = box.effects({duration: 10, transition: Fx.Transitions.Quart.easeOut});
			fxOut.start({
			}).chain(function() {
				this.start.delay(0100, this, {'opacity' : 0});
			}).chain(function() {
				$('imageMenu').effect('height',{duration:500}).start(50);
				$('mainframe').effect('height',{duration:500, onComplete:function(){
				new Ajax(e, {
					method: 'get',
					update: $('content_frame'),
					encoding: 'utf-8',
					onSuccess: function(){
					},
					onComplete: function(){
						ScriptLoader.load('/templates/fiskmarkadurinn_2008/js/smoothbox.js');
						var box = $('content_frame');
						box.style.margin = '50px 0';
						var fxIn = box.effects({duration: 200, transition: Fx.Transitions.Quart.easeIn});
						fxIn.start({
						}).chain(function() {
							this.start.delay(0020, this, {'opacity' : 1});
						});
						$$("a.smoothbox").each(function(el){el.onclick=TB_bind});
						$$("a.smoothbox").each(function(el){el.onkeypress=TB_bind});
						addReflect().delay(200);
					}
				}).request();
			}}).start(50)});
    },
    onClose:function(e,i){
      closeContent(e);
    }, 
    open: 2
  });
	
});