var ResizeFonts = Class.create({
	initialize: function(select, options) {
	
		this.select		= select;
		this.elements	= $A([]);
		this.options 	= {
			increment	: 1,
			offset		: 0
		};
		
		Object.extend(this.options, options || {});
		
		document.observe('dom:loaded', this.init.bind(this));
	},
	
	init: function() {		
		this.elements = $$(this.select);
		this.elements = this.elements.uniq();
		
		if(parseInt(GuiPack.readCookie('fontSizeOffset')) > 0) {
			this.options.offset = parseInt(GuiPack.readCookie('fontSizeOffset'));
		}
		
		if(this.options.offset > 15) {
			this.options.offset = 15;
		};
				
		if(this.options.offset > 0) {
			for(var i=0;i <= this.options.offset;i++) {
				this.up(false);
			};
		};
	},
	
	up: function(setCookie) {
		this.elements.each(function(e) {
			var size = parseInt(e.getStyle('fontSize'));
			
			if(!e.originalFontSize) {
				e.originalFontSize = size;
			};
			e.setStyle({fontSize: (size+this.options.increment) + 'px'});
		}, this);
		
		if(typeof setCookie == 'undefined') {
			this.options.offset++;			
			GuiPack.writeCookie('fontSizeOffset', this.options.offset, 365);
		};
	},
	
	down: function() {
		this.elements.each(function(e) {			
			if(e.originalFontSize) {				
				var size = parseInt(e.getStyle('fontSize'));
				if((size - this.options.increment) >= e.originalFontSize) {
					e.setStyle({fontSize: (size-this.options.increment) + 'px'});
				};
			};
		}, this);
		
		this.options.offset--;
		if(this.options.offset < 0) {
			this.options.offset = 0;	
		}
		GuiPack.writeCookie('fontSizeOffset', this.options.offset, 365);
	},
	
	reset: function() {
		this.elements.each(function(e) {			
			if(e.originalFontSize) {
				e.setStyle({fontSize: e.originalFontSize + 'px'});
			};			
		}, this);
		
		this.options.offset = 0;
		GuiPack.writeCookie('fontSizeOffset', this.options.offset, 365);
	}
});

var CustomSlider = Class.create({
	initialize: function(element, options) {
		
		this.element	= element;
		this.tid		= 0;
		
		this.options	= {
			speed			: 25,
			paddingRight	: 0,
			select			: 'img'
		};
		
		Object.extend(this.options, options || {});
		
		Event.observe(window, 'load', this.init.bind(this));
	},
	
	init: function() {
		var totalWidth	= 0;
		var width		= 0
		
		this.elements = $(this.element).select(this.options.select);
		this.elements.each(function(e) {
			
			if(typeof e.width != 'undefined') {
				width = e.width;
			} else {
				width = parseInt(e.getStyle('width'));
			}
		
			e.setStyle({
				position	: 'absolute',
				top			: 0,	
				left		: totalWidth + 'px'
			});
			
			totalWidth += width + this.options.paddingRight;
			
		}, this);
			
		this.rotate();
	},
		
	rotate: function() {
		clearTimeout(this.tid);
		
		if (!this.elements) return; 
				
		this.elements.each(function(e){
			e.setStyle({left: (parseInt(e.getStyle('left'),10)-1)+'px'});
		});
			
		
		var first 	= this.elements.first();
		var last	= this.elements.last();
		
		if(typeof first.width != 'undefined') {
			var firstWidth = first.width;
		} else {
			var firstWidth = parseInt(first.getStyle('width'));
		}
		
		if(typeof last.width != 'undefined') {
			var lastWidth = last.width;
		} else {
			var lastWidth = parseInt(last.getStyle('width'));
		}
					
		if((parseInt(first.getStyle('left'),10) + parseInt(firstWidth,10)) < 0) {
			this.elements.shift();
			
			first.setStyle({left: (parseInt(last.getStyle('left'),10) + parseInt(lastWidth, 10) + this.options.paddingRight) + 'px'});
			this.elements.push(first);
		} 
		
		this.tid = setTimeout(this.rotate.bind(this), this.options.speed);
	}
});

//homepage slideshow
function slideshow() {
	slide = slides[current];
	var pos		= 0;
	
	if(previous) {
		var pos = previous.getWidth() - slide.getWidth();
		var z	= previous.getStyle('zIndex') - 1;
	}	else {
		var pos = slide.getWidth() * -1;
		var z 	= 10;
	}		
	
	slide.setStyle({left: pos +'px', zIndex: z});
	slide.show();
	new Effect.Move(slide, {x:slide.getWidth(), y:0, duration:1.5, mode:'relative', afterFinish: 
		function() {
			//setTimeout(function(){
				if(previous) {
					var p = previous;
					previous = slide;
					current++;				
					if(current == slides.length) {
						current = 0;
					}
				
					//new Effect.Move(p, {x:(p.getWidth() * -1), y:0, duration:1, afterFinish: 
					new Effect.SlideUp(p, {duration:1.75, afterFinish: 
						function(){
							slide.setStyle({zIndex: 10});
							new Effect.Move(slide, {x:(p.getWidth() * -1), y:0, mode:'relative', duration:1.5, afterFinish: 
								slideshow
								//function() {
								//	setTimeout(slideshow, 2000);
								//}
							});
						}
					});
				} else {
					previous = slide;
					current++;				
					if(current == slides.length) {
						current = 0;
					}
				
					slideshow();
					//setTimeout(slideshow, 2000);
				}
			//}, 2000);
		}
	});
}

//shake image
function startShake(element) {
	element = $(element);
	
	element.pe = new PeriodicalExecuter(function(pe) {					
		var pos = element.positionedOffset();
		
		if(Math.random() > 0.5) {
			element.setStyle({top: ((pos.top + Math.random() * 2 < 10) ? (Math.random() * 2 * (-1)) : Math.random() * 2) + 'px'});
		} else {
			element.setStyle({left: ((pos.left + Math.random() * 2 < 10) ? (Math.random() * 2 * (-1)) : Math.random() * 2) + 'px'});
		}
	}, 0.15);
}

function stopShake(element) {
	element = $(element);
	
	if(element.pe) {
		element.pe.stop();
	}
}

function toggleSub(id) {	
	$$('#menu UL').each(function(e){
		if(e.visible()) {
			e.blindUp({duration:0.2});	
		} else if(e.id == 'sub_'+id) {
			e.blindDown({duration:0.2});
		}
	});
}
