﻿/*
var isIE = /MSIE (5\.5|6)/.test(navigator.userAgent);
var isOpera = /Opera/.test(navigator.userAgent);

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) != 7;
Prototype.Browser.IE7 = Prototype.Browser.IE && !Prototype.Browser.IE6;
*/

var App = {
	Version: '1.0',
	
	load: function() {
		$$('div.mh').each(function(i) {
			var url = i.readAttribute('href');
			i.observe('click', function(){ window.location = url; });
		});
		
		if ($('acc')) {
			var a = new Accordion('acc');
			$$('.acc-toggle').each(function(i) { $(i.next(0)).setStyle({ height:'auto', display:'none' }); });
			//a.activate($$('#acc .acc-toggle')[0]);
		}
	},
	
	moveTo: function(element, container, options) {
		this.options = Object.extend({duration: 1}, options || {});
		Position.prepare();
		var container_x = Position.cumulativeOffset($(container))[0];
		var container_y	= Position.cumulativeOffset($(container))[1];
		var element_x = Position.cumulativeOffset($(element))[0];
		var element_y = Position.cumulativeOffset($(element))[1];
		var scrolling = new Effect.Scroll(container, {duration:this.options.duration, x:(element_x-container_x), y:(element_y-container_y)});
		return false;
	}
};

function iepngfix (bgimg) {
	return 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + bgimg + '", sizingMethod="scale")';
}

/*
//document.onresize ? document : window

Event.observe(window, "resize", function() {
	//alert(document.viewport.getWidth());
	//alert(document.viewport.getHeight());
});
kina={
	doc:{x:0,y:0},
	fix:function(){if(kina.bg.complete&&(db.offsetWidth!=kina.doc.x||db.offsetHeight!=kina.doc.y||kina.fix.src!=kina.bg.src)){var bg=kina.bg.getSize();kina.doc={x:db.offsetWidth,y:db.offsetHeight};kina.fix.src==kina.bg.src||$extend(kina.fix,{p:bg.x/bg.y,src:kina.bg.src});kina.bg.setStyles({width:(bg.y=kina.doc.x>(bg.x=Math.round(kina.doc.y*kina.fix.p)))?kina.doc.x:bg.x,height:bg.y?Math.round(kina.doc.x/kina.fix.p):kina.doc.y})}}
}
*/

var SlideContent = Class.create({
	initialize : function(tag, slide, page) {
		this.tag = $(tag);
		this.slide = $(slide);
		this.overState = 0;
		this.page = page;
		this.slideState = 0;
		
		var p = this.tag.readAttribute('href').split('=')[1];
		if (p != this.page)
		{
			//callbacks
			this.mouseover_callback = this.mouseOver.bindAsEventListener(this);
			this.mouseout_callback = this.mouseOut.bindAsEventListener(this);
			
			this.tag.observe('mouseover', this.mouseover_callback);
			this.tag.observe('mouseout', this.mouseout_callback);
		
			this.click_callback = this.click.bindAsEventListener(this);
			this.tag.observe('click', this.click_callback);
		}
		
		this.queueScope = this.tag.identify() + '-scope';
		this.queue = Effect.Queues.get(this.queueScope); //create a queue with a unique name
	},

	mouseOver : function() {
		this.queue.each(function(ev) { ev.cancel(); });
		
		new Effect.Morph(this.tag, {
			style:'width:140px;',
			queue:{ scope:this.queueScope, limit:1 },
			duration:1.0,
			transition:Effect.Transitions.spring,
			// using an effect callback to  make sure state is changes _after_ the last frame was renderd
			afterFinish : function() { this.overState = 1; }.bind(this)
		});
	},
	
	mouseOut : function() {
		this.queue.each(function(ev) { ev.cancel(); });
		
		new Effect.Morph(this.tag, {
			style:'width:130px;',
			queue:{ scope:this.queueScope, limit:1 },
			duration:0.3,
			transition:Effect.Transitions.EaseFrom,
			afterFinish : function() { this.overState = 0; }.bind(this)
		});
	},
	
	click : function() {
		window.location = this.tag.getAttribute('href');
	},
	
	slideIn : function() {
		new Effect.Parallel([
			new Effect.Move(this.tag, { sync:true, x:790, y:0, mode:'relative' }),
			new Effect.Move(this.slide, { sync:true, x:790, y:0, mode:'relative' })
			], { 
			duration:1.5,
			transition:Effect.Transitions.EaseFromTo,
			afterFinish : function() { this.slideState = 1; }.bind(this)
		});
	}
});

var Accordion = Class.create();
Accordion.prototype = {
    showAccordion : null,
    currentAccordion : null,
	duration : null,
	effects : [],
	animating : false,
	
	initialize: function(container, options) {
        if (!$(container)) {
            throw(container + " doesn't exist!");
            return false;
        }
        
		this.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'acc-toggle',
				toggleActive : 'acc-toggle-active',
				content : 'acc-content'
			},
			defaultSize : {
				height : null,
				width : null
			},
			direction : 'vertical',
			onEvent : 'click'
		}, options || {});
		
		this.duration = ((11 - this.options.resizeSpeed) * 0.15);

		var accordions = $$('#' + container + ' .' + this.options.classNames.toggle);
		
		accordions.each(function(accordion) {
            Event.observe(accordion, this.options.onEvent, this.activate.bind(this, accordion), false);
			if (this.options.onEvent == 'click') {
                accordion.onclick = function() {return false;};
			}
			
			if (this.options.direction == 'horizontal') {
                var options = $H({width: '0px'});
			} else {
                var options = $H({height: '0px'});
			}
			options.merge({display: 'none'});
			
			this.currentAccordion = $(accordion.next(0)).setStyle(options);
		}.bind(this));
	},
	
	activate : function(accordion) {
		if (this.animating)
			return false;
		
		this.effects = [];
	
		this.currentAccordion = $(accordion.next(0));
		
		this.currentAccordion.setStyle({ display:'block' });
		this.currentAccordion.previous(0).addClassName(this.options.classNames.toggleActive);

		if (this.options.direction == 'horizontal') {
			this.scaling = $H({
                scaleX: true,
                scaleY: false
            });
		} else {
			this.scaling = $H({
                scaleX: false,
                scaleY: true
			});
		}
		
		if (this.currentAccordion == this.showAccordion)
            this.deactivate();
		else
            this._handleAccordion();
	},
    
	deactivate : function() {
        var options = $H({
            duration: this.duration,
            scaleContent: false,
            transition: Effect.Transitions.EaseTo,
            queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			scaleMode: { 
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			},
            afterFinish: function() {
				this.showAccordion.setStyle({ height: 'auto', display: 'none' });
				this.showAccordion = null;
				this.animating = false;
			}.bind(this)
		});
        options = options.merge(this.scaling).toObject();
        
        this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
		new Effect.Scale(this.showAccordion, 0, options);
	},
    
	_handleAccordion : function() {
		var options = $H({
			sync: true,
			scaleFrom: 0,
			scaleContent: false,
			transition: Effect.Transitions.EaseTo,
			scaleMode: {
				originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
				originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
			}
		});
		options = options.merge(this.scaling).toObject();
		
		this.effects.push(
			new Effect.Scale(this.currentAccordion, 100, options)
		);

		if (this.showAccordion) {
			this.showAccordion.previous(0).removeClassName(this.options.classNames.toggleActive);
			
			options = $H({
				sync: true,
				scaleContent: false,
				transition: Effect.Transitions.EaseTo,
				scaleMode: { 
					originalHeight: this.options.defaultSize.height ? this.options.defaultSize.height : this.currentAccordion.scrollHeight,
					originalWidth: this.options.defaultSize.width ? this.options.defaultSize.width : this.currentAccordion.scrollWidth
				}
			});
			options = options.merge(this.scaling).toObject();
			
			this.effects.push(
				new Effect.Scale(this.showAccordion, 0, options)
			);
		}
		
        new Effect.Parallel(this.effects, {
			duration: this.duration, 
			queue: {
				position: 'end', 
				scope: 'accordionAnimation'
			},
			beforeStart: function() {
				this.animating = true;
			}.bind(this),
			afterFinish: function() {
				if (this.showAccordion) {
					this.showAccordion.setStyle({
						display: 'none'
					});				
				}
				$(this.currentAccordion).setStyle({
				  height: 'auto'
				});
				this.showAccordion = this.currentAccordion;
				this.animating = false;
			}.bind(this)
		});
	}
};
