﻿window.addEvent('domready', function() {
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {$('top').setStyle('-webkit-text-size-adjust','none');	}
	var animation = (typeof doAnimate == 'undefined') ? 0: doAnimate;
	var rotation = (typeof doRotate == 'undefined') ? 0: doRotate;
		var myAnimation = function (eleObj){var myEffectA = new Fx.Morph(eleObj.ele, {duration: eleObj.tt, onComplete: function(){if(animate.length-1 == this.order) {$$('.gridbox').set('opacity',1);if(rotation) {rotateBox(this.ele);}}}.bind(eleObj)});
	    	myEffectA.start({'position':'absolute','top':[eleObj.oy,eleObj.dy],'left':[eleObj.ox,eleObj.dx],'display' : 'block'});}
		var getTransformProperty = function (element) {
		    var properties = ['transform','WebkitTransform','msTransform','MozTransform','OTransform'], p;		  
		    while (p = properties.shift()) {if (typeof element.style[p] != 'undefined') {return p;}}	return false;}		
		    var rotateBox  = function (e){ var property = getTransformProperty(e), deg = {d : 0};
			if(property) {
				var rotateFunction = 	function () {e.style[property] = 'rotate(' + (this.d++ % 360) + 'deg)';}
    		   	var timer = rotateFunction.periodical(25, deg);	
	    	}}
		var animate = $$('div.animate');
		if(animate && animate.length) {
			Array.each(animate, function(e, ind){
				var	eleObj = {ele : e, order: ind},	tempObj = {},oxy, dxy;			
				var temp = e.getProperty('class').split(' ');				
				Array.each(temp, function(item){
						if(item.contains('oxy')) {	tempObj.oxy = item.replace('oxy_','');	}
						if(item.contains('dxy')) {	tempObj.dxy = item.replace('dxy_','');}
						if(item.contains('tt_')) {tempObj.tt = item.replace('tt_','');}
				});
				oxy = new String(tempObj.oxy).split('_');
				dxy = new String(tempObj.dxy).split('_');
				eleObj.ox = (oxy[0])?oxy[0].toInt():-50;
				eleObj.oy = (oxy[1])?oxy[1].toInt():-50;
				eleObj.dx = (dxy[0])?dxy[0].toInt():-50;
				eleObj.dy = (dxy[1])?dxy[1].toInt():-50;		
				eleObj.tt = (tempObj.tt)?tempObj.tt.toInt():1000;
				var d = Math.floor(Math.random()*3);
				if(animation) {		
					$$('.gridbox').set('opacity', 0);
					$$('.animate').setStyle('display', 'none');
					if(1) {myAnimation.delay(eleObj.order*1000,this,[eleObj]);} 
					else {myAnimation.delay(d*eleObj.order*500,this,[eleObj]);	}
				} else {	eleObj.ele.setStyles({'position' : 'absolute','top':eleObj.dy,'left' : eleObj.dx,'display' : 'block'});}
			});
		}		
		var mySmoothScroll = new Fx.SmoothScroll({duration:1500, transition: 'pow:out'});
		var subPage = $$('ul.level_2 li.active span.active');		
		if(subPage.length) {
			var toplink = $('topLink'), myElement = $('top'), myOption = {duration: 2000,transition: Fx.Transitions.easeOut,onStart: function(){ 
											toplink.addEvent( 'click', function(e){ myFx.cancel(); });
											myElement.addEvent( 'mousewheel', function(e){ myFx.cancel();});
										}};		
			var myFx = new Fx.Scroll(myElement, myOption);
			myFx.toElement("footer");
		}

});

Fx.TransMorph = new Class({Extends: Fx.CSS}).implement(Fx.Morph.prototype);
Fx.TransMorph.implement({	
	step: function(){
		var time = $time();
		if (time < this.time + this.options.duration){
			var delta = {};
			var d = (time - this.time) / this.options.duration;
			var t = this.transition(d);
			for (p in this.from){
				var trans = this.transitions[p];
				delta[p] = (trans) ? trans(d) : t;
			}
			this.set(this.compute(this.from, this.to, delta));
		} else {
			this.set(this.compute(this.from, this.to, 1));
			this.complete();
		}
	},	
	compute: function(from, to, delta){
		var now = {};
		var isObj = ($type(delta) == 'object');
		for (var p in from) now[p] = this.parent(from[p], to[p], isObj ? delta[p] : delta);
		return now;	},	
	start: function(properties, transitions){
		if (!this.check(properties, transitions)) return this;
		if (typeof properties == 'string') properties = this.search(properties);
		var from = {}, to = {};
		for (var p in properties){
			var parsed = this.prepare(this.element, p, properties[p]);
			from[p] = parsed.from;
			to[p] = parsed.to;
		}
		transitions = transitions || {};
		$each(transitions, function(trans, key){
			transitions[key] = this.getTransition(trans);
		}, this);
		this.transitions = transitions;
		return this.parent(from, to);
	},
	getTransition: function(transition){
		var trans = transition || this.options.transition || Fx.Transitions.Sine.easeInOut;
		if (typeof trans == 'string'){
			var data = trans.split(':');
			trans = Fx.Transitions;
			trans = trans[data[0]] || trans[data[0].capitalize()];
			if (data[1]) trans = trans['ease' + data[1].capitalize() + (data[2] ? data[2].capitalize() : '')];
		}
		return trans;
	}	
});

Element.Properties.transmorph = {
	set: function(options){
		var transmorph = this.retrieve('transmorph');
		if (transmorph) transmorph.cancel();
		return this.eliminate('transmorph').store('transmorph:options', $extend({link: 'cancel'}, options));
	},
	get: function(options){
		if (options || !this.retrieve('transmorph')){
			if (options || !this.retrieve('transmorph:options')) this.set('transmorph', options);
			this.store('transmorph', new Fx.TransMorph(this, this.retrieve('transmorph:options')));
		}
		return this.retrieve('transmorph');
	}
};

Element.implement({
	transmorph: function(props, trans){
		this.get('transmorph').start(props, trans);
		return this;
	}
});
