// deziddon.com/mki0.93
(function(){
	function MK(ls){
		this.els = [];
		for(var i = 0;i < ls.length;i++){
			var l = ls[i];
			if(typeof l == 'string'){
				l = $i(l);
			}
			this.els.push(l);
		}
		return this;
	}

	MK.prototype = {
		// Add additional elements
		push:function(a){
			for(var j=0;j<a.length;j++){
				this.els.push(a[j]);
			}
			return this;
		},
		
		// Apply to all elements  e.g. *.all(somefunction);
		all:function(f){
			for (var i = 0,xn = this.els.length;i < xn;++i){
				f.call(this, this.els[i]);
			}
			return this;
		},

		// Attach events e.g. *.on('mouseover', somefunction);
		on:function(t, f){
			var lsn = function(el){
				if(window.addEventListener){
					el.addEventListener(t, f, false);
				} else if (window.attachEvent){
					el.attachEvent('on'+t, function(){
							f.call(el, window.event);
						}
					);
				}
			};
			this.all(function(el){
				lsn(el);
			});
			return this;
		},

		// Apply a specific style to elements e.g. *.style('opacity',90);
		style:function(p, v){
			this.all(function(el){
				var u = v;
				if(p == 'opacity'){
					el.style['filter'] = 'alpha(opacity='+ u +')';		
					u = u/100;
				}
				el.style[p] = u;
			});
			return this;
		},

		// Apply many styles to elements e.g. *.css({opacity:90,border:'1px #000 solid'});
		css:function(o){
			var that = this;
			this.all(function(el){
				for(var p in o){
					that.style(p, o[p]);
				}
			});
			return this;
		},

		// Tween between states e.g. *.anim(ease.bounce, {top:10, left:10}, {top:100, left:121}, 500);
		// Params: easing function, start state (object literal), end state (object literal), time to complete (in ms), callback function (optional)
		anim:function(f, b, e, t, c){
			var that = this;
			for(var p in b){			
				if(p != 'opacity' || p != 'zIndex'){
					e[p] = (e[p] - b[p]);
				}
			} 
			var nd = new Date().getTime() + t;
			var _a = window.setInterval(function(){
				var k = (t - (nd - new Date().getTime()));
				if(t <= k){
					window.clearInterval(_a);
					k = t;
				}
				that.all(function(el){
					for(var p in b){
						if(p == 'scrollLeft' || p == 'scrollTop'){
							el[p] = Math.floor(f(k, b[p], e[p], t));
						} else {
							var prt = (p == 'zIndex' || p == 'opacity') ? '' : 'px';
							that.style(p, Math.floor(f(k, b[p], e[p], t)) + prt);
						}
					}
				});
				if(t == k && c){
					c();
				}
			},10);
			return this;
		}
	};
	window.$ = function(){
		return new MK(arguments);
	};

	// Grab an element
	$i = function(o){
		if(typeof o == 'string'){
			return document.getElementById(o);
		}
		return o;
	};

	// Grab specified elements from specific id
	$e = function(i, e){
		var d = e ? $i(i) : document;
		return d.getElementsByTagName(e);
	};

	// GetElementsByClassName
	$c = function(c,e){
		var d = e ? $i(e) : document;
		if(!document.getElementsByClassName){
		var r = [];
			c = new RegExp('\\b'+c+'\\b');
			d = d.getElementsByTagName('*');
			for(var i = 0;i < d.length;i++){
				if(c.test(d[i].className)){
					r.push(d[i]);
				}
			}
			return r;
		}
		return d.getElementsByClassName(c);
	};

	// XMLHTTP Call
	// PARAMS url, postdata, success function (optional, receives response text + xml obj), failure function (optional, receives response text + xml obj)
	xhr = function(u, p, s, f){
		var X = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
		if(p == ''){
			X.open('GET', u, true);
		} else {
			X.open('POST', u, true);
			X.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		}
		X.onreadystatechange = function(){
			if(X.readyState == 4){
				var r = X.responseText;
				if(X.status == 200){
					if(s){s(r, X);}
				} else {
					if(f){f(r, X);}
				}
			}
		};
		X.send(p);
	};

	// Load additional javascript 
	load = function(s,c){
		var f = document.createElement('script');
		f.type = 'text/javascript';
		f.onloadDone = false;
		if(c) {
            f.onload = function(){
	     		f.onloadDone = true;            
            	c();
            };
	     	f.onreadystatechange = function() {
	     		if (('loaded' === f.readyState || 'complete' === f.readyState) && !f.onloadDone) {
		     		f.onloadDone = true;
	     			c();
	     		}
	     	};
        }
		f.src = s;
		$e(document, 'head')[0].appendChild(f);
	};

	// Get X/Y position of an element
	offSet = function(o){
		var l = 0;
		var t = 0;
		if(o.offsetParent){
			do{l += o.offsetLeft; t += o.offsetTop;} while(o = o.offsetParent);
			return [l, t];
		}
	};
	
	preventDefault = function(e){
		if (!e) e = window.event;
		e.cancelBubble = true;
		e.returnValue = false;
		if(e.preventDefault){
			e.preventDefault();
		}
	};
	
	if(!Array.indexOf){
		Array.prototype.indexOf = function(o,i){
			for(var j = this.length, i = i < 0 ? i + j < 0 ? 0 : i + j : i || 0; i < j && this[i] !== o; i++);
			return j <= i ? -1 : i
		}
	}

	ease = {
		cubic : function(t, b, c, d){
			if((t /= d / 2)<1){
				return (c / 2 * t * t * t + b) * 1;
			}
			return (c / 2 * ((t -= 2) * t * t + 2) + b) * 1;
		},
		
		bounce : function(t, b, c, d){
			if((t /= d)<(1 / 2.75)){
				return c * (7.5625 * t * t) + b;
			} else if(t <(2 / 2.75)){
				return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b;
			} else if(t < (2.5 / 2.75)){
				return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
			}else{
				return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
			}
		}
	};
	
	MKAvail = true;
})();

$(window).on('load', function(){
	$().push($e('nav', 'li')).on('mouseover', function(){
		if(this.className == 'cur'){return;}
		$(this).css({backgroundImage:'url(/media/img/nbgi.png)'});
	}).on('mouseout', function(){
		if(this.className == 'cur'){return;}
		$(this).css({backgroundImage:'none'});	
	});
	
	var nm = document.createElement('img');
	nm.src = '/media/img/b'+ (Math.round(Math.random() * 5) + 1) +'.png';
	nm.setAttribute('alt', '*');
	$i('ldg').appendChild(nm);
	
	$().push($c('pLoad')).css({visibility: 'visible'});
	$('previewBox').css({opacity: 85});

	if($i('content')){
		var valFrm = $e('content', 'form')[0];
		if(valFrm){
			$(valFrm).on('submit', function(e){
				var rsp = false;
				$().push($c('mandatory', 'content')).all(function(el){
					if(el.value.length == 0 && !rsp){
						rsp = true;
						alert('Please enter your '+ el.getAttribute('name'));
						el.focus();
						preventDefault(e);
					}
				});
			});
		}
	}
	
    var zIdx = 10;
    var cIdx = 0;
    var lks = $e('spnnControl', 'a');
    var SPN = $().push(lks);
    SPN.on('mouseover', function(){
        var that = this;
        if(that.getAttribute('working') == '1' || that.className == 'cur'){
            return false;
        }
        SPN.all(function(el){el.className = ' ';});
        that.setAttribute('working', '1');
        that.className = 'cur';

        zIdx++;
        var sIdx = 0;
        var thisId = this.getAttribute('rel');
        for(var i = 0; i < lks.length; i++){
            if(lks[i].getAttribute('rel') == thisId){
                sIdx = i;
                break;
            }
        }
        var os = offSet($i('sldr'))[0];
        if(sIdx > cIdx){
            var lft = os + 850;
        } else {
            var lft = os + -850;
        }
		$(thisId).anim(ease.cubic, {left: lft, zIndex: zIdx, opacity: 0}, {left: os, zIndex: zIdx, opacity: 100}, 800, function(){
             that.setAttribute('working', 0);
        });
        cIdx = sIdx;
    });

    var cvr = function(){
   		if(!$i('SLDlft')){
	    	var lft = document.createElement('div');
	    	lft.id = 'SLDlft';
	    	var rgt = document.createElement('div');
	    	rgt.id = 'SLDrgt';
			var bdy = document.getElementsByTagName('body')[0];
			bdy.appendChild(lft);
			bdy.appendChild(rgt);
	    }
    	$('SLDlft').css({position:'absolute',top:'0px',left:'0px',width:offSet($i('cont'))[0]+'px',height:'330px',backgroundImage:'url(/media/img/bg.png)',zIndex:99999});
		$('SLDrgt').css({position:'absolute',top:'0px',right:'0px',width:($i('SLDlft').offsetWidth-1)+'px',height:'330px',backgroundImage:'url(/media/img/bg.png)',zIndex:99999});
		
		$('previewBox').css({position: 'relative', zIndex: 9999999, top: offSet($i('previewBox'))[1] +'px', left: offSet($i('previewBox'))[0] +'px'});
		$('previewBox').css({position: 'absolute'});
    };
    cvr();
    
    $(window).on('resize', function(){
    	cvr();
    });
    
	// STATS
	(function(){
		load('http://www.google-analytics.com/ga.js', function(){
			try {
				var pageTracker = _gat._getTracker('UA-9703551-1');
				pageTracker._trackPageview();
			} catch(err) {}
		});
	})();    
});