/*
	background.js for mootools v1.1
	by Floor SA (http://www.floor.ch) - MIT-style license
*/

var Background = new Class({
	initialize: function(options){
		this.options = Object.extend({
			headerHeight: 195,
			checkIfLoaded: 1,
			bgContainer: $('bgimage'),
			btnToggle: $('imgbtn'),
			fallObj: $('roundbg'),
			pathToImg: '/data/category/background/',
			//pathToImg: 'index_files/',
			nAction: 'click'
		}, options || {});
		// Test if there is an image to preload then execute the script
		if(this.options.bgContainer.getAttribute('title')) this.doCheck();
	},
	
	doCheck: function(){
		var title = this.options.bgContainer.getAttribute('title');
		if(this.options.checkIfLoaded == 1){
			if(Cookie.get(title) == false){
				this.doPreload(title, true);
			}
			else{
				this.doPreload(title, false);
			}
		}
		else{
			this.doPreload(title, true);
		}
	},
	
	doPreload: function(title, fade){
		if(this.options.checkIfLoaded ==1){
			Cookie.set(title, "true", {duration: false});
		}
		// Preload the image		
		this.preload = new Image();
		this.preload.onload=function(){
			(function(){this.setBackground(fade)}.bind(this)).delay(10)
		}.bind(this);
		this.preload.src = this.options.pathToImg + title;
		return false;
	},
	setBackground: function(fade){
		// Get the background height and substract the header height;
		window.imgH = this.preload.height;
		window.imgH -= this.options.headerHeight;
		// Set opacity and background for bgContainer
		this.options.bgContainer.setStyles({'background' :'url(' + this.options.pathToImg + this.options.bgContainer.getAttribute('title') + ') no-repeat', 'opacity' : 0, 'height': '1000px', 'width':'910px', 'position': 'absolute', 'margin': '0px auto auto -455px', 'left':'50%'});
		// Fade-in
		if(fade==true){
			var x = this.fadeInBg.bind(this.options.bgContainer);
			x();
		} else{
			this.options.bgContainer.setStyle('opacity', 1);
		}
		// Create the link to show background
		this.createBtShowBg();
	},
	
	createBtShowBg: function (){
		var refObj = this.options.fallObj;
		// Test if an image has been loaded
		if(window.imgH >= 1){
			// Define the effect to apply
			fallFx = new Fx.Style(refObj, 'margin-top', {
				duration: 700, 
				transition: Fx.Transitions.quintOut
			});
			// btnToggle show or hide image
			this.options.btnToggle.addEvent(this.options.nAction, function(e){
				var o_height = refObj.getStyle('margin-top').toInt();
				if(o_height == 0 || isNaN(o_height)){
					fallFx.start('0px' ,window.imgH + "px");
				} else{
					fallFx.start(window.imgH + "px",'0px');
				}
			});
		}
	},
	
	// Fade function
	fadeInBg: function() {
		this.effect('opacity',{
			duration: 1000, 
			transition: Fx.Transitions.quartInOut
		}).start(0,1);
	}
});


// Execute the site.start
window.addEvent('domready', function() {
	var background = new Background();
	//$('category20').addClass('s');
	
	}
	
);
