/**
 * Copyright (c) 2010 Sylvain Gougouzian (sylvain@gougouzian.fr)
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 * GNU GPL (http://www.gnu.org/licenses/gpl.html) licensed.
 *
 * jQuery moodular version: 2.3
 *
 * Requires: jQuery 1.3.2+ 	// http://www.jquery.com
 * Compatible : Internet Explorer 6+, Firefox 1.5+, Safari 3+, Opera 9+, Chrome 0.9+
 */
jQuery(function($){
	$.fn.moodular = function(options){
		var el = null;
		var opts = $.extend({}, $.fn.moodular.defaults, options);
		var ctrls = $.extend({}, $.fn.moodular.controls);
		var effects = $.extend({}, $.fn.moodular.effects);
		this.each(function(){
			el = new $moodular(this, opts, ctrls, effects);
			$(window).bind('resize', function () { el._resize(); });
		});
		return opts.api ? el : null;
	};
	$.moodular = function(e, opts, ctrls, effects){
		this.e = $(e);
		if (opts.random) {
			var elems = this.e.children($('> ' + opts.item));
			elems.sort(function() { return (Math.round(Math.random())-0.5); });
			this.e.remove($('> ' + opts.item));
			for(var i=0; i < elems.length; i++)
				this.e.append(elems[i]);
		}
		if (opts.continuous) $(e).html($(e).html() + $(e).html());
		this.aItems = null;
		this.nbItems = 0;
		this.current = 0;
		this.locked = false;
		this.dep = 0;
		this.timerMoving = null;
		this.opts = opts;
		this.controls = ctrls;
		this.effects = effects;
		this.direction = ((opts.direction == 'left') || (opts.direction == 'top') ? 'next' : 'prev');
		this.pos = ((opts.direction == 'left') || (opts.direction == 'right') ? 'left' : 'top');
		this.dir = ((opts.direction == 'right') || (opts.direction == 'bottom') ? -1 : 1);
		this.vertical = (this.pos == 'left' ? false : true);
		this._init();
	};
	var $moodular = $.moodular;
	$moodular.fn = $moodular.prototype = {
		moodular: '2.3'
	};
	$moodular.fn.extend = $moodular.extend = $.extend;
	$moodular.fn.extend({
		_init: function(){
			var self = this;
			this._resize();
			this.e.wrap('<div></div>');
			this.e.parent().css({
				'position' : 'relative',
				'overflow' : 'hidden',
				'width'	: this.e.width(),
				'height' : this.e.height()
			});
			this.e.css({
				'position' : 'absolute'
			});
			s = 0;
			$('> ' + this.opts.item, this.e).each(function() {
				if (this.vertical) {
					s += parseInt($(this).outerHeight(true));
				}
				else {
					s += parseInt($(this).outerWidth(true));
				}
			});
			this.e.css(this.vertical ? 'height' : 'width', s + 'px');
			this.aItems = $('> ' + this.opts.item, this.e);
			this.nbItems = this.aItems.length;
			if (!this.opts.continuous) this.nbItems = this.nbItems * 2;
			var control = this.opts.controls.split(' ');
			var i;
			for (i = 0; i < control.length; i++) {
				if ($.isFunction(this.controls[control[i]]))
					this.controls[control[i]](this);
			}
			var effect = this.opts.effects.split(' ');
			for (i = 0; i < effect.length; i++) {
				if ($.isFunction(this.effects.init[effect[i]]))
					this.effects.init[effect[i]](this);
			}
			if (this.opts.startOn) {
				this.speed = this.opts.speed;
				this.opts.speed = 1;
				this.moveTo(this.opts.startOn);
			}
			if (this.opts.auto) {
				setTimeout(function(){
					self._animate('next');
				}, self.opts.dispTimeout);
			}
		},
		_animate: function(dir){
			dir = (dir == undefined ? this.direction : dir);
			if (!this.locked) {
				this.locked = true;
				clearTimeout(this.timerMoving);
				this.dep = this.dep == 0 ? this.opts.scroll : this.dep;
				if (this.dir == -1) {
					if (dir == "next") {
						dir = "prev";
					}
					else if (dir == "prev") {
						dir = "next";
					}
				}
				if (dir != 'next') {
					this.dep *= -1;
				}
				var cont = true;
				if (!this.opts.continuous) {
					if (dir == 'next') {
						if (this.current >= ((this.nbItems / 2) - 1)) {
							cont = false;
							this.locked = false;
						}
					}
					else {
						if (this.current <= 0) {
							cont = false;
							this.locked = false;
						}
					}
				}
				if (cont) {
					this._beforeMoving();
				}
			}
		},
		_beforeMoving: function(){
			var effect = this.opts.effects.split(' ');
			var i;
			for (i = 0; i < effect.length; i++) {
				if ($.isFunction(this.effects.before[effect[i]]))
					this.effects.before[effect[i]](this, (this.dep < 0 ? -1 : 1));
			}
			if (this.dep < 0 && this.opts.continuous) {
				var size = 0;
				for (i = 0; i < Math.abs(this.dep); i++) {
					var item = $('> ' + this.opts.item + ':last', this.e);
					size += parseInt(item.css(this.vertical ? 'height' : 'width'));
					$('> ' + this.opts.item + ':last', this.e).remove();
					this.e.prepend(item);
				}
				this.e.css(this.pos, -size);
			}
			this._move();
		},
		_move: function(){
			var self = this;
			if ($.isFunction(this.opts.move)) {
				this.opts.move(this, function(){
					self._afterMoving();
				});
			}
			else {
				var size = 0;
				var i;
				if (this.dep > 0) {
					for (i = 0; i < this.dep; i++) {
						if (this.vertical) {
							size += parseInt(this.aItems.eq(this._realpos(this.current) + i).outerHeight(true));
						}
						else {
							size += parseInt(this.aItems.eq(this._realpos(this.current) + i).outerWidth(true));
						}
					}
				}
				else {
					if (!this.opts.continuous && this.current <=0) {}
					else {
						for (i = 0; i < Math.abs(this.dep); i++) {
							if (this.vertical) {
								size += parseInt(this.aItems.eq(this._realpos(this.current) - i).outerHeight(true));
							}
							else {
								size += parseInt(this.aItems.eq(this._realpos(this.current) - i).outerWidth(true));
							}
						}
					}
				}
				if (this.e.css(this.pos) == 'auto') this.e.css(this.pos, 0);
				var dest = parseInt(this.e.css(this.pos)) + (this.dep > 0 ? -1 : 1) * size;
				if (!this.opts.continuous) {
					if (dest > 0) dest = 0;
					if (this.vertical) {
						if ((parseInt(this.e.height()) + dest) < parseInt(this.e.parent().height())) {
							dest = parseInt(this.e.parent().height()) - parseInt(this.e.height());
						}
					}
					else {
						if ((parseInt(this.e.width()) + dest) < parseInt(this.e.parent().width())) {
							dest = parseInt(this.e.parent().width()) - parseInt(this.e.width()) ;
						}
					}
				}
				if (this.vertical) {
					this.e.stop(true, true).animate({
						top: parseInt(dest) + 'px'
					}, this.opts.speed, this.opts.easing, function(){
						self._afterMoving();
					});
				}
				else {
					this.e.stop(true, true).animate({
						left: parseInt(dest) + 'px'
					}, this.opts.speed, this.opts.easing, function(){
						self._afterMoving();
					});
				}
			}
		},
		_afterMoving: function(){
			var i;
			if (this.dep > 0 && this.opts.continuous) {
				for (i = 0; i < this.dep; i++) {
					var item = $('> ' + this.opts.item + ':first', this.e);
					$('> ' + this.opts.item + ':first', this.e).remove();
					this.e.append(item);
				}
				this.e.css(this.pos, 0);
			}
			var self = this;
			this.current = this.current + this.dep;
			if (this.current == -1) {
				if (this.opts.continuous)
					this.current = this.nbItems - 1;
				else
					this.current = 0;
			}
			else {
				if (this.current == this.nbItems) {
					this.current = 0;
				}
				else {
					this.current = this._realpos(this.current);
				}
			}
			this.dep = 0;
			this.locked = false;
			var effect = this.opts.effects.split(' ');
			for (i = 0; i < effect.length; i++) {
				if ($.isFunction(this.effects.after[effect[i]]))
					this.effects.after[effect[i]](this);
			}
			for (i = 0; i < this.opts.callbacks.length; i++) {
				this.opts.callbacks[i](this);
			}
			var control = this.opts.controls.split(' ');
			for (i = 0; i < control.length; i++) {
				if ($.isFunction(this.controls.callback[control[i]]))
					this.controls.callback[control[i]](this);
			}
			if (this.opts.startOn) {
				this.opts.speed = this.speed;
			}
			if (this.opts.auto) {
				this.timerMoving = setTimeout(function(){
					self._animate('next');
				}, this.opts.dispTimeout);
			}
		},
		_realpos: function(i){
			if (i < 0) i = (this.nbItems / 2) - i;
			return (i < (this.nbItems / 2) ? i : (i - (this.nbItems / 2)));
		},
		_resize: function () {
			var s = $('> ' + this.opts.item, this.e).css(this.vertical ? 'height' : 'width');
			$('> ' + this.opts.item, this.e).each(function() {
				if (this.vertical) {
					$(this).height($(this).height());
				}
				else {
					$(this).width($(this).width());
				}
			});
		},
		reanimate: function() {
			if (!this.opts.auto) {
				this.locked = false;
				this.opts.auto = true;
				var self = this;
				this.timerMoving = setTimeout(function(){
					self._animate('next');
				}, this.opts.dispTimeout);
			}
		},
		start: function(){
			if (!this.opts.auto) {
				this.locked = false;
				this.opts.auto = true;
				this._animate('next');
			}
			return false;
		},
		stop: function(){
			clearTimeout(this.timerMoving);
			this.opts.auto = false;
			return false;
		},
		next: function(){
			this._animate('next');
			return false;
		},
		prev: function(){
			this._animate('prev');
			return false;
		},
		getCurrent: function(){
			return this._realpos(this.current);
		},
		moveTo: function(i){
			if (i > (this.nbItems / 2)) {
				i = (this.nbItems / 2) - 1;
			}
			this.dep = parseInt(i) - parseInt(this.current);
			if (this.dep != 0) {
				this._animate('next');
			}
			return false;
		}
	});
	$.fn.moodular.defaults = {
		item: 'li',
		controls: '',
		effects: '',
		easing: '',
		auto: true,
		continuous: true,
		speed: 2000,
		direction: 'left',
		scroll: 1,
		startOn: 0,
		dispTimeout: 1000,
		callbacks: [],
		random: false,
		api: false
	};
	$.fn.moodular.controls = {
		callback: {}
	};
	$.fn.moodular.effects = {
		init: {},
		before: {},
		after: {}
	};
});

/**
 * Copyright (c) 2010 Sylvain Gougouzian (sylvain@gougouzian.fr)
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 * GNU GPL (http://www.gnu.org/licenses/gpl.html) licensed.
 *
 * jQuery moodular controls by Sylvain Gougouzian http://sylvain.gougouzian.fr
 *
 * Requires: jQuery 1.3.2+ 	// http://www.jquery.com
 * 			jQuery moodular plugin
 *	 		jQuery Mouse Wheel Plugin v3.0.2 // Copyright (c) 2009 [Brandon Aaron] (http://brandonaaron.net)
 *			jQuery UI 1.7.2
 *
 * Compatible : Internet Explorer 6+, Firefox 1.5+, Safari 3+, Opera 9+, Chrome 0.9+
 */
jQuery(function($){
	$.extend($.fn.moodular.controls, {
		keys: function(moodular){
			$(document).keydown(function(event){
				if ((event.keyCode == 39) || (event.keyCode == 40)) {
					moodular._animate('next');
				}
				if ((event.keyCode == 37) || (event.keyCode == 38)) {
					moodular._animate('prev');
				}
			});
			return false;
		},
		click: function(moodular){
			for (var i = 0; i < moodular.nbItems; i++) {
				$('> ' + moodular.opts.item, moodular.e).css('cursor', 'pointer').attr('rel', moodular._realpos(i)).click(function(){
					moodular.locked = false;
					moodular.moveTo($(this).attr('rel'));
					return false;
				});
			}
		},
		stopOver: function (moodular) {
			moodular.e.parent().bind("mouseenter", function(){
				moodular.stop();
			}).bind("mouseleave", function () {
				if (!moodular.locked) { 
					moodular.reanimate();
				} else { 
					moodular.locked = false;
				}
			});
		},
		index: function(moodular){
			moodular.e.parent().parent().append('<ul class="moodular_itemList"></ul>');
			var h = "";
			for (var i = 0; i < (moodular.nbItems / 2); i++) {
				h += '<li class="moodular_itemList_li" rel="' + i + '">' + (i + 1) + '</li>';
			}
			$('.moodular_itemList', moodular.e.parent().parent()).html(h);
			$('li.moodular_itemList_li', moodular.e.parent().parent()).css('cursor', 'pointer').click(function(){
				if(!moodular.locked) {
					moodular.moveTo($(this).attr('rel'));
				} else { 
					moodular.locked = false;
				}
				return false;
			});
			$('.moodular_itemList_li:first').addClass('active');
		},
		tabs: function(moodular){
			if (moodular.opts.tabs) {
				$('a', moodular.opts.tabs).bind('click', function () {
					moodular.moveTo($('> ' + moodular.opts.item, moodular.e).index($($(this).attr('href'))));
				});
				if (window.location.hash.length) {
					moodular.moveTo($('> ' + moodular.opts.item, moodular.e).index($(window.location.hash)));
				}
			}
		},
		wheel: function(moodular){
			moodular.e.parent().parent().bind("mousewheel", function(event, delta){
				var dir = delta > 0 ? 'Up' : 'Down';
				if (dir == 'Up') {
					moodular.next();
				}
				else {
					moodular.prev();
				}
				return false;
			});
		},
		mouseover: function(moodular){
			moodular.opts.callbacks[moodular.opts.callbacks.length] = function(moodular){
				if (moodular.isMouseOver) {
					moodular._animate(mouseDirection(moodular));
				}
			};
			moodular.e.parent().parent().bind("mousemove", function(evt){
				moodular.mouseX = evt.pageX;
				moodular.mouseY = evt.pageY;
			});
			moodular.e.parent().parent().bind("mouseenter", function(evt){
				moodular.isMouseOver = true;
				moodular.mouseX = evt.pageX;
				moodular.mouseY = evt.pageY;
				moodular._animate(mouseDirection(moodular));
				return false;
			});
			moodular.e.parent().parent().bind("mouseleave", function(){
				moodular.isMouseOver = false;
				return false;
			});
		},
		touch: function(moodular){
			moodular.touchBPosX = null;
			moodular.touchBPosY = null;
			moodular.touchEPosX = null;
			moodular.touchEPosY = null;
			moodular.e.parent().bind('touchstart', function (event) {
				var e = event.originalEvent;
				moodular.touchBPosX = e.targetTouches[0].pageX;
				moodular.touchBPosY = e.targetTouches[0].pageY;
			}).bind('touchmove', function (event) {
				event.preventDefault();
				var e = event.originalEvent;
				moodular.touchEPosX = e.targetTouches[0].pageX;
				moodular.touchEPosY = e.targetTouches[0].pageY;
			}).bind('touchend', function(e) {
				if (moodular.vertical) {
					if (moodular.opts.direction == 'top') {
						if (moodular.touchEPosY < moodular.touchBPosY)
							moodular.next();
						else
							moodular.prev();
					}
					else {
						if (moodular.touchEPosY > moodular.touchBPosY)
							moodular.next();
						else
							moodular.prev();
					}
				}
				else {
					if (moodular.opts.direction == 'left') {
						if (moodular.touchEPosX < moodular.touchBPosX)
							moodular.next();
						else
							moodular.prev();
					}
					else {
						if (moodular.touchEPosX > moodular.touchBPosX)
							moodular.next();
						else
							moodular.prev();
					}
				}
				moodular.touchBPosX = null;
				moodular.touchBPosY = null;
				moodular.touchEPosX = null;
				moodular.touchEPosY = null;
				return false;
			});
		},
		slider: function (moodular) {
			moodular.e.parent().parent().append('<div id="' + moodular.id + '_slider"></div>');
			$("#" + moodular.id + "_slider", moodular.e.parent().parent()).slider({
				range: "max",
				min: 0,
				max: ((moodular.nbItems) / 2) - 1,
				value: 0,
				stop: function(event, ui) {
					moodular.moveTo(ui.value);
				}
			});
		}
	});

	$.extend($.fn.moodular.controls.callback, {
		index: function (moodular) {
			$('.moodular_itemList li.active').removeClass('active');
			$('.moodular_itemList li').eq(moodular._realpos(moodular.current)).addClass('active');
		},
		slider: function (moodular) {
			$("#" + moodular.id + "_slider", moodular.e.parent().parent()).slider('value', moodular.current);
		}
	});

	function mouseDirection(moodular){
		var offset = moodular.e.parent().parent().offset();
		if (moodular.vertical) {
			if ((moodular.mouseY >= parseInt(offset.top)) && (moodular.mouseY <= parseInt((parseInt(offset.top) + parseInt(moodular.e.parent().parent().height())) / 2))) {
				return 'prev';
			}
			else {
				return 'next';
			}
		}	
		else {
			if ((moodular.mouseX >= parseInt(offset.left)) && (moodular.mouseX <= parseInt((parseInt(offset.left) + parseInt(moodular.e.parent().parent().width())) / 2))) {
				return 'prev';
			}
			else {
				return 'next';
			}
		}
	}
});

/**
 * Copyright (c) 2009 Sylvain Gougouzian (sylvain@gougouzian.fr)
 * MIT (http://www.opensource.org/licenses/mit-license.php) licensed.
 * GNU GPL (http://www.gnu.org/licenses/gpl.html) licensed.
 *
 * jQuery moodular effects by Sylvain Gougouzian http://sylvain.gougouzian.fr
 *
 * Requires: jQuery 1.3.2+ 	// http://www.jquery.com
 *			jQuery corner plugin 2.01 // http://jquery.malsup.com/corner/
 *
 * Compatible : Internet Explorer 6+, Firefox 1.5+, Safari 3+, Opera 9+, Chrome 0.9+
 */

jQuery(function($) {
	$.extend($.fn.moodular.effects.init,{
		corner: function (moodular) {
			$.fn.corner.defaults.useNative = false;
			$('> ' + moodular.opts.item, moodular.e).corner("10px");
		},
		reflection: function (moodular) {
			moodular.e.parent().height(parseInt(moodular.e.parent().height()) * 1.33);
			$(moodular.opts.item + ' > img', moodular.e).load(function () {
				reflect(this);
				$(this).parent().height(parseInt($(this).parent().height()) * 1.33);
			});
		},
		fade: function (moodular) {
			$('> ' + moodular.opts.item, moodular.e).each(function(i){
				if (i > (moodular.nbItems / 2)) {
					$(this).remove();
				}
			});
			moodular.opts.scroll = 1;
			moodular.opts.move = function (c, b) {
				c.e.css({
					top: 0,
					left: 0
				});
				setTimeout(function () { 
					clearTimeout(c.timerMoving);
					$('.position_' + c.current, c.e).fadeOut(c.opts.speed); 
					$('.position_' + c._realpos(c.current + c.dep), c.e).fadeIn(c.opts.speed, function () { b(); }); 
				}, c.opts.dispTimeout);
			};
			$('> ' + moodular.opts.item, moodular.e).each(function (i){
				$(this).css('position', 'absolute').addClass('position_' + i);
			});
			$('> ' + moodular.opts.item, moodular.e).not(':first').hide();
		},
		zoom: function (moodular) {
			$('> ' + moodular.opts.item, moodular.e).each(function(i){
				if (i > (moodular.nbItems / 2)) {
					$(this).remove();
				}
			});
			moodular.opts.scroll = 1;
			moodular.opts.move = function (c, b) {
				c.e.css({
					top: 0,
					left: 0
				});
				setTimeout(function () { 
					clearTimeout(c.timerMoving);
					var $c = $('.position_' + c.current, c.e);
					$c.show();
					var w = $c.width();
					var wi = $('> img', $c).width();
					var h = $c.height();
					var hi = $('> img', $c).height();
					$c.hide();
					$c.fadeOut(c.opts.speed).animate({
						width: (2 * parseInt(w)) + 'px',
						height: (2 * parseInt(h)) + 'px',
						top: ( 0 - parseInt(h) / 2) + 'px',
						left: ( 0 - parseInt(w) / 2) + 'px'
					}, c.opts.speed, function () {
						$(this).css({
							top: 0,
							left: 0,
							width: w,
							height: h
						}).hide();
					}); 
					$('> img', $c).animate({
						width: (2 * parseInt(wi)) + 'px',
						height: (2 * parseInt(hi)) + 'px'
					}, c.opts.speed, function () {
						$(this).css({
							width: wi,
							height: hi
						});
					}); 
					$('.position_' + c._realpos(c.current + c.dep), c.e).fadeIn(c.opts.speed, function () { b(); }); 
				}, c.opts.dispTimeout);
			};
			$('> ' + moodular.opts.item, moodular.e).each(function (i){
				$(this).css('position', 'absolute').addClass('position_' + i);
			});
			$('> ' + moodular.opts.item, moodular.e).not(':first').hide();
		}
	});
	$.extend($.fn.moodular.effects.after,{
		fade: function (c) {
			c.e.css({
				top: 0,
				left: 0
			});
			$('> ' + c.opts.item, c.e).css({
				top: 0,
				left: 0
			}); 
		},
		zoom: function (c) {
			c.e.css({
				top: 0,
				left: 0
			});
			$('> ' + c.opts.item, c.e).css({
				top: 0,
				left: 0
			}); 
		}
	});
	$.extend($.fn.moodular.effects.before,{
		fade: function (c) {
			c.e.css({
				top: 0,
				left: 0
			});
			$('> ' + c.opts.item, c.e).css({
				top: 0,
				left: 0
			}); 
		},
		zoom: function (c) {
			c.e.css({
				top: 0,
				left: 0
			});
			$('> ' + c.opts.item, c.e).css({
				top: 0,
				left: 0
			}); 
		}
	});
	
	function reflect(img) {
		var $this = $(img);
		var w = parseInt($this.width());
		var h = parseInt($this.height());
		var r;
		if ($.browser.msie) {
			r = $("<img />").attr('src', $this.attr('src')).css({
				'width': w,
				'height': h,
				'margin-bottom': h - Math.floor(h * 0.33),
				'filter': "flipv progid:DXImageTransform.Microsoft.Alpha(opacity=50, style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy=33)"
			})[0];
		}
		else {
			r = $("<canvas />")[0];
			if (!r.getContext) { return; }
			var f = r.getContext("2d");
			try {
				$(r).attr({
					'width': w,
					'height': Math.floor(h * 0.33)
				});
				f.save();
				f.translate(0, h - 1);
				f.scale(1, -1);
				f.drawImage(img, 0, 0, w, h);
				f.restore();
				f.globalCompositeOperation="destination-out";
				var i = f.createLinearGradient(0, 0, 0, Math.floor(h * 0.33));
				i.addColorStop(0, "rgba(255, 255, 255, 0.5)");
				i.addColorStop(1, "rgba(255, 255, 255, 1.0)");
				f.fillStyle = i;
				f.rect(0, 0, w, Math.floor(h * 0.33));
				f.fill();
			}
			catch (e) {
				return;
			}
		}
		$(r).css('display', 'block');
		$this.parent().css({
			width: w,
			height: h + Math.floor(h * 0.33),
			overflow: "hidden"
		}).append($(r));
		return false;
	}
	function unreflect($this) {
		var html = $this.parent().html();
		$this.parent().parent().html(html);
		return false;
	}
});
