var carlgallery = Class.create({
	initialize: function(container){
		this.container = container;
		this.activeImage = -1;
		
		
	},
	_initSlideShow: function(imagearray){
	
		this.imageArray = imagearray;
		this.container.update("");
		this.nowLooping = false;
		this._preloadImage(0);
		
	},
	_preloadImage: function(imageNum){
		
		
		
		this.activeImage = imageNum;
		
		var preImage = new Element('img').setStyle({'top':'0px','left':'0px','position':'absolute'});
		this.container.insert({'bottom':preImage});
		var imgPreloader = new Image();
		preImage.hide();
		imgPreloader.onload = (function(){
            preImage.src = this.imageArray[this.activeImage];
             this._animateImage(preImage);
        }).bind(this);
		
		imgPreloader.src = this.imageArray[this.activeImage];
	},
	_animateImage: function(e){
		e.appear({ duration: 1.6, afterFinish:this._triggerNext.bind(this) });
	},
	_triggerNext:function(){
		if(this.nowLooping){
			this.container.down(0).remove();
		}
		
		this.activeImage++;
		
		if(this.activeImage>(this.imageArray.length-1))
		{
			this.activeImage = 0;
			this.nowLooping = true;
		}
		
		
		
		
		
		this._preloadImage(this.activeImage);
	}
});

var carlsapp = Class.create({
	initialize: function(){
		this.slider = null;
		this.kat = null;
		
		
		this._doBinds();
	
	
	this.content = $("content");
	
	this.content.hide();
	
	$("carlsteinslogo").hide();
	
	if($("omosstext"))
  		{
  			$("omosstext").hide();	
  		}
	
	this._hideLogos();
	
	$('globalheaderback').setStyle({'overflow':'hidden','width':'0px','left':'500px'});
	$('globalheaderback').hide();
	$('globalheader').hide();
	
	
	this.dit = new ditdot("main");
	var self = this;
	
		this.menu = $('globalnav').select('li');
		
		
		this.menu.each(function(a){
		
			Event.observe(a, 'mouseover', function() {a.addClassName("hover"); self.dit.movedot(a);});
			Event.observe(a, 'mouseout', function() {a.removeClassName("hover");});
		
			Event.observe(a, 'click', self.menuSelected.bindAsEventListener(self,a));
		
		});
		Event.observe($("globalnav"), 'mouseout', function() {self.dit.dothide();});
	
	},
	start: function(){
		
		
		
		$('globalheaderback').appear({duration:2.0});
		
		var self = this;
		new Effect.Morph('globalheaderback', {
  			style: {'width':'932px','left':'0px'}, // CSS Properties
  			duration: 2.0, // Core Effect properties
  			afterFinish: function(fx) {
				fx.element.setStyle({overflow:"visible"});
				self._introRun();
			}.bind(this)
			});
		

		
	
	},
	
	_introRun: function(){
			var self = this;
		$('globalheader').appear({
  			afterFinish: function() {
				
				self._logoRun();
			}.bind(this)
			});
		
		
		
	},
	_logoRun: function(){
		var self = this;
		
		$('carlsteinslogo').appear({
  			afterFinish: function() {
				
				self._runContent();
			}.bind(this)
			});
	
	},
	
	_runContent: function(){
	
		if($("katalog"))
		{
			if(this.kat==null)
			{
				this.kat = new carlstein();
			}
			else
			{
				this.kat.init();
			}
		
  						//Custom.init();
  						
  						
  		}
		var self = this;
		
		this._hideLogos();
		
		this.content.appear({
  			afterFinish: function(fx) {
			
				if($("omosstext"))
  				{
  					$("omosstext").appear();
  					
  					
  						
  				}
  				
  				if($("logolist"))
  					{
  						
  						self._runLogos();
  					}
			}.bind(this)

  		});
	},
	_hideLogos: function()
	{
		if($("logolist"))
		{
			$("logolist").select(".minilogo").invoke('hide');
		}
		
		
		
	},
	_runLogos: function(){
	
		var list = $("logolist").select(".minilogo");
		
		list.each(function(a,i){
			a.appear({delay:i});
		
		});
	
	},
	menuSelected: function(e,a){
		Event.stop(e);
		
		
		
		
		this.menu.each(function(aa){
			aa.removeClassName("selected");
		
		});
		a.addClassName("selected");
		
		this.subRequest(e,a)
	
	},
	subRequest: function(e,a){
		Event.stop(e);
		if($("intro"))
  		{
  			this.slider.stop()
  		}
		
		
		
		
		this.content.hide();
		var self = this;
		
		this.param = {fast:"fast"};
		var url = a.down('a').readAttribute('href');
		
		this.therequest = new Ajax.Request("index.php?page=" + url,{
			method: "get",
			parameters:this.param,
			onSuccess: function(transport){
            	self._processData(transport)
            	}
		});
	
	},
	_processData: function(DataRequest, ResponseHeader){
  		this.content.update(DataRequest.responseText);
  		
  		if($("omosstext"))
  		{
  			$("omosstext").hide();	
  		}
  		
  		/*if($("personal"))
  		{
  			$("personal").hide();	
  		}
  		*/
  		
  		
  		this._runContent();
  		//this.content.appear();
  		
  		this._doBinds();
  		
  		
  	},
  	_generateList: function(start,end,list,name){
  		var ii = 0;
		var num = end - start;
  		for (ii=0;ii<=(num);ii++)
  		{
  			var l = start + ii;
  			list.push('/global/images/bildspel2010/' + name + "/" + l + ".jpg");
  		}
  	
  	},
  	_doGallery: function(){
  		var list = $("varnyheter").select(".gallerylink");
  		
  		var cagallery = new carlgallery($("varnyheter"));
  		
  		var imagelist_barn = [];
  		this._generateList(15,30,imagelist_barn,'barn');
		var imagelist_herr = [];
		this._generateList(11,14,imagelist_herr,'herr');
		var imagelist_dam = [];	
		this._generateList(1,10,imagelist_dam,'dam');
		var imagelist = [imagelist_barn,imagelist_herr,imagelist_dam];
		
		list.each(function(a,i){
			
			
			
			
			
			a.observe('click',function(e){
				Event.stop(e);
				cagallery._initSlideShow(imagelist[i]);
			}.bind(this));
		
		});	
  	},
  	_doBinds: function(){
  		var self = this;
  		
  		if($("intro"))
  		{
  			if(this.slider == null)
  			{
  				this.slider = new slideshow("intro");
  			}
  			else
  			{
  				this.slider.initshow("intro");
  			}
  			
  		}
  		
  		
  		if($("varnyheter"))
  			this._doGallery();
  		
  		
  		if($("personal"))
  		{
  			
  			var personallist = $("personal").select('li');
  			personallist.each(function(a){
		
				Event.observe(a, 'click', self.subRequest.bindAsEventListener(self,a));
		
			});
  		
  		}
  		
  		
  		
  		
  		if($("ommenu"))
  		{
  			
  			var ommenulist = $("ommenu").select('li');
  			ommenulist.each(function(a){
		
				Event.observe(a, 'click', self.subRequest.bindAsEventListener(self,a));
		
			});
  		
  		}
  	
  	}

});


document.observe('dom:loaded', function() { 
	
	//Run the Carlstein Web App
	var thecarlsteinapp = new carlsapp();
	
	Event.observe(window, 'load', function(){
		thecarlsteinapp.start();
	
	});
	

});


	
