﻿(function(){

var checkHeader = function() {
	var m = $('head').getStyle('position') === 'fixed' ? $('head').getSize().y : 0;
	$('content').setStyle('margin-top', m );
}

var checkCats = function() {
	$$('#cats .c-accs','#cats .c-shoes','#cats .c-men','#cats .c-wmen').each( function(cat) {
		switch( cat.get('class') ) {
			case 'c-accs': var clss = 'taccs'; break;
			case 'c-shoes': var clss = 'tsho'; break;
			case 'c-men': var clss = 'tmen'; break;
			case 'c-wmen': var clss = 'twom'; break;
		}
		cat.addEvent('mouseenter', function() {
			$$('.'+clss).each( function(el) {
				el.addClass(clss+'hi');
			});
		});
		cat.addEvent('mouseleave', function() {
			$$('.'+clss).each( function(el) {
				el.removeClass(clss+'hi');
			});
		});
	});
	var path = window.location.pathname;
	if(path.test('^\/accessoires')) var clss = 'taccs';
	if(path.test('^\/men')) var clss = 'tmen';
	if(path.test('^\/women')) var clss = 'twom';
	if(path.test('^\/shoes')) var clss = 'tsho';
	$$('.'+clss).each( function(el) {
		el.addClass(clss+'hi');
	});
}

var checkTopMenu = function() {
	$$('#head .menu li').each( function( li ) {
		var a = li.getElements('a')[0];
		a.set('tween', {duration: 200});
		li.addEvent('mouseenter', function() {
			a.tween('margin-left', 20);
		});
		li.addEvent('mouseleave', function() {
			a.tween('margin-left', 0);
		});
		
		a.addEvent('click',function(e) {
			this.blur();
			$$('#head .menu li').each( function( _li ) {
				_li.removeEvents('mouseenter');
				_li.removeEvents('mouseleave');
			});
		});
	});
}

var BrandGal  = new Class({
	initialize : function(i){
		var $this = this;
		this.DIV = i;
		this.galWidth = $('gwin').getStyle('width').toInt();
		this.cWin = $$('#gwin > div')[0];
		this.cWidth = 0;
		$$('#gwin img').each( function(img) { $this.cWidth += img.get('width').toInt() + img.getStyle('margin-left').toInt(); });
		if( this.galWidth < this.cWidth ) {
			this.initArrows();
			this.initDrag();
		} else {
			$$('#gright','#gleft').setStyle('display','none');
		}
	}
	,initArrows: function () {
		var $this = this;
		this.cWin.set('tween', {duration: 200});
		$$('#gright','#gleft').addEvent('click', function(e) {
			e.preventDefault();
			this.blur();
			$this.slide(this.get('id'));
		});
	}
	,slide: function( way ) {
		if( way === 'gright' ) {
			var ziel = Math.max( -(this.cWidth-this.galWidth), this.cWin.getPosition().x - this.cWidth );
		}
		if( way === 'gleft' ) {
			var ziel = Math.min( 0, this.cWin.getPosition().x + this.cWidth );
		}
		this.cWin.tween('left', ziel);
	}
	,initDrag: function() {
		var $this = this;
		this.cWin.setStyle( 'cursor','ew-resize');
		this.drag = new Drag(this.cWin, {
			limit: {x: [-(this.cWidth-this.galWidth),0], y: [0,0]}
		});
		this.cWin.addEvent('mousedown', function() {
			$this.drag.attach();
		});
}
});
/* 
var XLGal  = new Class({
	initialize : function(i){
		var $this = this;
		this.DIV = i;
		this.srcs = $('xlstage').get('data-srcs').split('|');
		this.current = 0;
		this.loadingNr = -1;
		this.initActions();
	}
	,initActions: function () {
		var $this = this;
		$$('#xlright','#xlleft','#xlstage').addEvent('click', function(e) {
			e.preventDefault();
			this.blur();
			$this.changePic(this.get('id'));
		});
	}
	,changePic: function(way) {
		if( this.loadingNr !== -1 ) return false;
		var $this = this;
		if( way === 'xlleft' ) {
			this.loadingNr = this.current === 0 ? this.srcs.length - 1 : this.current - 1;
		} else {
			this.loadingNr = this.current + 1 === this.srcs.length ?  0 : this.current + 1;
		}
		this.nextIMG = Asset.image(this.srcs[this.loadingNr], {
			onLoad: function() { $this.showPic(); }
		});
	}
	,showPic: function() {
		this.current = this.loadingNr;
		this.loadingNr = -1;
		$('xlstage').empty();
		this.nextIMG.inject($('xlstage'));
	}
}); */
var XLGal  = new Class({
	initialize : function(i){
		var $this = this;
		this.DIV = i;
		this.slide = i.getElement('#xlgal-slide');
		var slw = 0;
		$$('#xlgal-slide img').each( function(el) {
			slw += parseInt(el.getStyle('width'));
			slw += parseInt(el.getStyle('margin-left'));
		});
		this.slide.setStyle('width', slw);
		this.slideMax = slw - this.DIV.getSize().x;
		this.initActions();
	}
	,initActions: function () {
		var $this = this;
		$$('#xlright','#xlleft').addEvent('click', function(e) {
			e.preventDefault();
			this.blur();
		});
		var intervalID = 0;
		$$('#xlright','#xlleft').addEvent('mouseenter', function(e) {
			e.preventDefault();
			this.blur();
			if(this.get('id') === 'xlleft') {
				intervalID = $this.movePics.periodical(30, $this, 6);
			} else {
				intervalID = $this.movePics.periodical(30, $this, -6);
			}
		});
		$$('#xlright','#xlleft').addEvent('mouseleave', function(e) {
			clearInterval(intervalID);
		});
	}
	,movePics: function(step) {
		var p = this.slide.getPosition(this.DIV);
		if( step > 0 ) {
			var newx = Math.min( 0, p.x+step) ;
		} else {
			var newx = Math.max( -this.slideMax, p.x+step) ;
		}
		this.slide.setPosition({ x: newx, y: p.y });
	}
});

window.addEvent('domready', function() {
	window.addEvent('resize', function(){ checkHeader(); });
	checkHeader();
	checkCats();
	checkTopMenu();
	if( $('brandgal') ) gal = new BrandGal($('xlgal'));
	if( $('xlgal') ) gal = new XLGal($('xlgal'));
});

})();
