/* http://keith-wood.name/imageCube.html
   Image Cube for jQuery v1.2.1.
   Written by Keith Wood (kbwood{at}iinet.com.au) June 2008.
   Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 
   MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 
   Please attribute the author if you use it. */
(function($) {
	function ImageCube() {
		this._defaults = {
			direction : 'random',
			randomSelection : [ 'up', 'down', 'left', 'right' ],
			speed : 2000,
			easing : 'linear',
			repeat : true,
			pause : 2000,
			selection : 'forward',
			shading : true,
			opacity : 0.8,
			imagePath : '',
			full3D : true,
			segments : 20,
			reduction : 30,
			expansion : 10,
			lineHeight : [ 0.0, 1.25 ],
			letterSpacing : [ -0.4, 0.0 ],
			beforeRotate : null,
			afterRotate : null
		}
	}
	;
	var U = 0;
	var V = 1;
	var W = 2;
	var X = 3;
	var Y = 'imageCube';
	$
			.extend(
					ImageCube.prototype,
					{
						markerClassName : 'hasImageCube',
						setDefaults : function(a) {
							extendRemove(this._defaults, a || {})
						},
						_attachImageCube : function(b, c) {
							b = $(b);
							if (b.hasClass(this.markerClassName)) {
								return
							}
							var d = $.extend({
								_position : b.css('position')
							}, this._defaults, c || {});
							$.data(b[0], Y, d);
							b.addClass(this.markerClassName).css({
								position : 'relative'
							}).children().each(function() {
								var a = $(this);
								$.data(this, Y, {
									width : a.css('width'),
									height : a.css('height'),
									position : a.css('position'),
									lineHeight : a.css('lineHeight'),
									letterSpacing : a.css('letterSpacing')
								});
								a.css({
									width : b.css('width'),
									height : b.css('height'),
									position : 'absolute',
									lineHeight : d.lineHeight[1],
									letterSpacing : d.letterSpacing[1]
								})
							}).not(':first').hide();
							this._prepareRotation(b[0])
						},
						_prepareRotation : function(b) {
							b = $(b);
							b
									.children(
											'.imageCubeShading,.imageCubeFrom,.imageCubeTo')
									.remove();
							var c = $.data(b[0], Y);
							c.current = b.children(':visible')[0];
							var d = function(a) {
								return (!a.length ? a : a.filter(':eq('
										+ Math.floor(Math.random() * a.length)
										+ ')'))
							};
							c.next = (c.selection == 'random' ? d(b
									.children(':hidden'))
									: (c.selection == 'backward' ? $(c.current)
											.prev() : $(c.current).next()));
							c.next = (c.next.length ? c.next
									: (c.selection == 'random' ? c.current
											: (c.selection == 'backward' ? b
													.children(':last') : b
													.children(':first'))))[0];
							if (c.repeat && !c._timer) {
								c._timer = setTimeout(function() {
									$.imagecube._rotateImageCube(b[0])
								}, c.pause)
							}
							$.data(b[0], Y, c)
						},
						_rotateImageCube : function(a, b, c) {
							if (typeof b == 'function') {
								c = b;
								b = ''
							}
							a = $(a);
							this._stopImageCube(a[0], true);
							var d = $.data(a[0], Y);
							if (b != null) {
								b = (typeof b == 'number' ? a.children(':eq('
										+ b + ')') : $(b));
								if (a.children().filter(function() {
									return this === b[0]
								}).length > 0) {
									d.next = b
								}
							}
							var e = [ d.current, d.next ];
							if (d.beforeRotate) {
								d.beforeRotate.apply(a[0], e)
							}
							var f = {};
							f[Y] = 1.0;
							a.attr(Y, 0.0).animate(f, d.speed, d.easing,
									function() {
										if (d.afterRotate) {
											d.afterRotate.apply(a[0], e)
										}
										if (c) {
											c.apply(a[0])
										}
									})
						},
						_currentImageCube : function(a) {
							return ($(a).hasClass(this.markerClassName) ? $
									.data(a, Y).current : null)
						},
						_nextImageCube : function(a) {
							return ($(a).hasClass(this.markerClassName) ? $
									.data(a, Y).next : null)
						},
						_stopImageCube : function(a, b) {
							var c = $.data(a, Y);
							if (c._timer) {
								clearTimeout(c._timer);
								c._timer = null
							}
							if (!b) {
								c.repeat = false
							}
							$.data(a, Y, c)
						},
						_startImageCube : function(a) {
							this._changeImageCube(a, {
								repeat : true
							})
						},
						_changeImageCube : function(a, b, c) {
							if (typeof b == 'string') {
								var d = {};
								d[b] = c;
								b = d
							}
							var e = $.data(a, Y);
							extendRemove(e || {}, b || {});
							$.data(a, Y, e);
							this._prepareRotation(a)
						},
						_destroyImageCube : function(a) {
							a = $(a);
							if (!a.hasClass(this.markerClassName)) {
								return
							}
							this._stopImageCube(a[0]);
							var b = $.data(a[0], Y);
							a
									.stop()
									.css({
										position : b._position
									})
									.removeClass(this.markerClassName)
									.children(
											'.imageCubeShading,.imageCubeFrom,.imageCubeTo')
									.remove();
							a.children().each(function() {
								$(this).css($.data(this, Y)).show();
								$.removeData(this, Y)
							});
							$.removeData(a[0], Y)
						},
						_prepareAnimation : function(d) {
							var e = $.data(d, Y);
							var d = $(d);
							var f = {
								left : 0,
								top : 0
							};
							d.parents().each(function() {
								var a = $(this);
								if (a.css('position') == 'fixed') {
									f.left -= a.offset().left;
									f.top -= a.offset().top;
									return false
								}
							});
							var g = {
								width : d.width(),
								height : d.height()
							};
							var h = (e.direction != 'random' ? e.direction
									: e.randomSelection[Math.floor(Math
											.random()
											* e.randomSelection.length)]);
							h = Math.max(0, $.inArray(h, [ 'up', 'down',
									'left', 'right' ]));
							e._curDirection = h;
							var j = (h == U || h == V);
							var k = (h == W || h == X);
							var l = (h == U || h == W);
							var m = (l ? 0 : e.opacity);
							var n = $(e.current);
							var o = $(e.next);
							var q = [];
							var r = function(p) {
								var b = [ 0, 0, 0, 0 ];
								if (!$.browser.msie || p.css('border')) {
									for ( var i = 0; i < 4; i++) {
										b[i] = p
												.css('border'
														+ [ 'Left', 'Right',
																'Top', 'Bottom' ][i]
														+ 'Width');
										var a = ($.browser.msie ? 1 : 0);
										b[i] = parseFloat({
											thin : 1 + a,
											medium : 3 + a,
											thick : 5 + a
										}[b[i]] || b[i])
									}
								}
								return b
							};
							q[0] = r(n);
							q[1] = r(o);
							var s = [];
							s[0] = [ parseFloat(n.css('padding-left')),
									parseFloat(n.css('padding-right')),
									parseFloat(n.css('padding-top')),
									parseFloat(n.css('padding-bottom')) ];
							s[1] = [ parseFloat(o.css('padding-left')),
									parseFloat(o.css('padding-right')),
									parseFloat(o.css('padding-top')),
									parseFloat(o.css('padding-bottom')) ];
							var t = [];
							t[0] = [
									($.boxModel ? q[0][0] + q[0][1] + s[0][0]
											+ s[0][1] : 0),
									($.boxModel ? q[0][2] + q[0][3] + s[0][2]
											+ s[0][3] : 0) ];
							t[1] = [
									($.boxModel ? q[1][0] + q[1][1] + s[1][0]
											+ s[1][1] : 0),
									($.boxModel ? q[1][2] + q[1][3] + s[1][2]
											+ s[1][3] : 0) ];
							var u = [];
							u[0] = {
								elem : n[0],
								props : {
									left : {
										start : f.left,
										end : f.left + (h == X ? g.width : 0),
										units : 'px'
									},
									width : {
										start : g.width - t[0][0],
										end : (j ? g.width - t[0][0] : 0),
										units : 'px'
									},
									top : {
										start : f.top,
										end : f.top + (h == V ? g.height : 0),
										units : 'px'
									},
									height : {
										start : g.height - t[0][1],
										end : (j ? 0 : g.height - t[0][1]),
										units : 'px'
									},
									paddingLeft : {
										start : s[0][0],
										end : (k ? 0 : s[0][0]),
										units : 'px'
									},
									paddingRight : {
										start : s[0][1],
										end : (k ? 0 : s[0][1]),
										units : 'px'
									},
									paddingTop : {
										start : s[0][2],
										end : (j ? 0 : s[0][2]),
										units : 'px'
									},
									paddingBottom : {
										start : s[0][3],
										end : (j ? 0 : s[0][3]),
										units : 'px'
									},
									borderLeftWidth : {
										start : q[0][0],
										end : (k ? 0 : q[0][0]),
										units : 'px'
									},
									borderRightWidth : {
										start : q[0][1],
										end : (k ? 0 : q[0][1]),
										units : 'px'
									},
									borderTopWidth : {
										start : q[0][2],
										end : (j ? 0 : q[0][2]),
										units : 'px'
									},
									borderBottomWidth : {
										start : q[0][3],
										end : (j ? 0 : q[0][3]),
										units : 'px'
									},
									lineHeight : {
										start : e.lineHeight[1],
										end : (j ? e.lineHeight[0]
												: e.lineHeight[1]),
										units : 'em'
									},
									letterSpacing : {
										start : e.letterSpacing[1],
										end : (j ? e.letterSpacing[1]
												: e.letterSpacing[0]),
										units : 'em'
									}
								}
							};
							u[1] = {
								elem : o[0],
								props : {
									left : {
										start : f.left + (h == W ? g.width : 0),
										end : f.left,
										units : 'px'
									},
									width : {
										start : (j ? g.width - t[1][0] : 0),
										end : g.width - t[1][0],
										units : 'px'
									},
									top : {
										start : f.top + (h == U ? g.height : 0),
										end : f.top,
										units : 'px'
									},
									height : {
										start : (j ? ($.browser.msie ? 1 : 0)
												: g.height - t[1][1]),
										end : g.height - t[1][1],
										units : 'px'
									},
									paddingLeft : {
										start : (k ? 0 : s[1][0]),
										end : s[1][0],
										units : 'px'
									},
									paddingRight : {
										start : (k ? 0 : s[1][1]),
										end : s[1][1],
										units : 'px'
									},
									paddingTop : {
										start : (j ? 0 : s[1][2]),
										end : s[1][2],
										units : 'px'
									},
									paddingBottom : {
										start : (j ? 0 : s[1][3]),
										end : s[1][3],
										units : 'px'
									},
									borderLeftWidth : {
										start : (k ? 0 : q[1][0]),
										end : q[1][0],
										units : 'px'
									},
									borderRightWidth : {
										start : (k ? 0 : q[1][1]),
										end : q[1][1],
										units : 'px'
									},
									borderTopWidth : {
										start : (j ? 0 : q[1][2]),
										end : q[1][2],
										units : 'px'
									},
									borderBottomWidth : {
										start : (j ? 0 : q[1][3]),
										end : q[1][3],
										units : 'px'
									},
									lineHeight : {
										start : (j ? e.lineHeight[0]
												: e.lineHeight[1]),
										end : e.lineHeight[1],
										units : 'em'
									},
									letterSpacing : {
										start : (j ? e.letterSpacing[1]
												: e.letterSpacing[0]),
										end : e.letterSpacing[1],
										units : 'em'
									}
								}
							};
							if (e.shading) {
								var v = function(a, b, c) {
									return {
										left : {
											start : a.left.start,
											end : a.left.end,
											units : 'px'
										},
										width : {
											start : a.width.start,
											end : a.width.end,
											units : 'px'
										},
										top : {
											start : a.top.start,
											end : a.top.end,
											units : 'px'
										},
										height : {
											start : a.height.start,
											end : a.height.end,
											units : 'px'
										},
										paddingLeft : {
											start : a.paddingLeft.start
													+ a.borderLeftWidth.start,
											end : a.paddingLeft.end
													+ a.borderLeftWidth.end,
											units : 'px'
										},
										paddingRight : {
											start : a.paddingRight.start
													+ a.borderRightWidth.start,
											end : a.paddingRight.end
													+ a.borderRightWidth.end,
											units : 'px'
										},
										paddingTop : {
											start : a.paddingTop.start
													+ a.borderTopWidth.start,
											end : a.paddingTop.end
													+ a.borderTopWidth.end,
											units : 'px'
										},
										paddingBottom : {
											start : a.paddingBottom.start
													+ a.borderBottomWidth.start,
											end : a.paddingBottom.end
													+ a.borderBottomWidth.end,
											units : 'px'
										},
										opacity : {
											start : b,
											end : c,
											units : ''
										}
									}
								};
								u[2] = {
									elem : $(($.browser.msie ? '<img src="'
											+ e.imagePath
											+ 'imageCubeHigh.png"' : '<div')
											+ ' class="imageCubeShading" style="background-color: white; opacity: '
											+ m
											+ '; z-index: 10; position: absolute;"'
											+ ($.browser.msie ? '/>'
													: '></div>'))[0],
									props : v(u[l ? 0 : 1].props, m, e.opacity
											- m)
								};
								u[3] = {
									elem : $(($.browser.msie ? '<img src="'
											+ e.imagePath
											+ 'imageCubeShad.png"' : '<div')
											+ ' class="imageCubeShading" style="background-color: black; opacity: '
											+ (e.opacity - m)
											+ '; z-index: 10; position: absolute;"'
											+ ($.browser.msie ? '/>'
													: '></div>'))[0],
									props : v(u[l ? 1 : 0].props,
											e.opacity - m, m)
								}
							}
							if (e.full3D) {
								for ( var i = 0; i < e.segments; i++) {
									d.append(n.clone()
											.addClass('imageCubeFrom').css({
												display : 'block',
												position : 'absolute',
												overflow : 'hidden'
											}));
									if (e.shading) {
										d.append($(u[l ? 2 : 3].elem).clone())
									}
								}
								for ( var i = 0; i < e.segments; i++) {
									d.append(o.clone().addClass('imageCubeTo')
											.css({
												display : 'block',
												position : 'absolute',
												width : 0,
												overflow : 'hidden'
											}));
									if (e.shading) {
										d.append($(u[l ? 3 : 2].elem).clone())
									}
								}
								n.hide();
								o.css({
									width : g.width - t[1][0],
									height : g.height - t[1][1]
								})
							} else {
								var w = function(a) {
									return {
										left : a.left.start + 'px',
										width : a.width.start + 'px',
										top : a.top.start + 'px',
										height : a.height.start + 'px',
										lineHeight : a.lineHeight.start + 'em',
										padding : a.paddingTop.start + 'px '
												+ a.paddingRight.start + 'px '
												+ a.paddingBottom.start + 'px '
												+ a.paddingLeft.start + 'px',
										borderLeftWidth : a.borderLeftWidth.start
												+ 'px',
										borderRightWidth : a.borderRightWidth.start
												+ 'px',
										borderTopWidth : a.borderTopWidth.start
												+ 'px',
										borderBottomWidth : a.borderBottomWidth.start
												+ 'px',
										letterSpacing : a.letterSpacing.start
												+ 'em',
										overflow : 'hidden'
									}
								};
								n.css(w(u[0].props));
								o.css(w(u[1].props)).show();
								if (e.shading) {
									d.append(u[2].elem).append(u[3].elem)
								}
							}
							for ( var i = 0; i < u.length; i++) {
								for ( var x in u[i].props) {
									var y = u[i].props[x];
									y.diff = y.end - y.start
								}
							}
							return u
						},
						_drawFull3D : function(G, H, I) {
							var J = $.data(G, Y);
							if (!J.full3D) {
								return false
							}
							var G = $(G);
							var K = J._curDirection;
							var L = (K == U || K == V);
							var M = (K == U || K == W);
							var N = G.width();
							var O = G.height();
							if (N == 0 || O == 0) {
								return true
							}
							var P = (1 - H) * (L ? O : N);
							var Q = J.segments;
							var R = J.expansion
									* (1 - Math.abs(2 * P - (L ? O : N))
											/ (L ? O : N));
							var S = J.reduction
									- (J.reduction * P / (L ? O : N));
							var T = function(a, b, c, d, e, f, g, k, l, m, n, o) {
								var p = [ d - b, f - k ];
								var w = Math.max(p[0], p[1]);
								var q = [ l - c, g - e ];
								var h = Math.max(q[0], q[1]);
								var r = (L ? (p[0] - p[1]) / (Q - 1) / 2 : w
										/ Q);
								var s = (L ? h / Q : (q[0] - q[1]) / (Q - 1)
										/ 2);
								var t = n.paddingLeft[o] + n.paddingRight[o]
										+ n.borderLeftWidth[o]
										+ n.borderRightWidth[o];
								var u = n.paddingTop[o] + n.paddingBottom[o]
										+ n.borderTopWidth[o]
										+ n.borderBottomWidth[o];
								var v = Math.round(b);
								var x = Math.round(c);
								var y = v;
								var z = x;
								var i = 0;
								for ( var j = 0; j < G[0].childNodes.length; j++) {
									var A = G[0].childNodes[j];
									if (A.className != a) {
										continue
									}
									var B = Math.round(b + (i + 1) * r);
									var C = Math.round(c + (i + 1) * s);
									var D = p[0] - (L ? 2 * i * r : 0);
									var E = q[0] - (L ? 0 : 2 * i * s);
									A.style.left = (L ? y : b) + 'px';
									A.style.top = (L ? c : z) + 'px';
									A.style.width = Math.max(0, D - t) + 'px';
									A.style.height = Math.max(0, E - u) + 'px';
									A.style.letterSpacing = (L ? D
											/ w
											* (J.letterSpacing[1] - J.letterSpacing[0])
											+ J.letterSpacing[0]
											: H * n.letterSpacing.diff
													+ n.letterSpacing.start)
											+ n.letterSpacing.units;
									A.style.lineHeight = (!L ? E
											/ h
											* (J.lineHeight[1] - J.lineHeight[0])
											+ J.lineHeight[0]
											: H * n.lineHeight.diff
													+ n.lineHeight.start)
											+ n.lineHeight.units;
									A.style.clip = 'rect('
											+ (!L ? 'auto' : (z - x) + 'px')
											+ ','
											+ (L ? 'auto' : (B - v) + 'px')
											+ ','
											+ (!L ? 'auto' : (C - x) + 'px')
											+ ','
											+ (L ? 'auto' : (y - v) + 'px')
											+ ')';
									if (J.shading) {
										var F = A.nextSibling;
										F.style.left = y + 'px';
										F.style.top = z + 'px';
										F.style.width = (L ? p[0] - 2 * i * r
												: B - y)
												+ 'px';
										F.style.height = (L ? C - z : q[0] - 2
												* i * s)
												+ 'px';
										F.style.opacity = m;
										if ($.browser.msie) {
											F.style.filter = 'alpha(opacity='
													+ (m * 100) + ')'
										}
									}
									y = B;
									z = C;
									i++
								}
							};
							T('imageCubeFrom', [ S, -R, 0, N - P ][K], [ 0,
									O - P, S, -R ][K],
									[ N - S, N + R, P, N ][K], [ 0, O - P, -R,
											S ][K], [ N + R, N - S, P, N ][K],
									[ P, O, O + R, O - S ][K], [ -R, S, 0,
											N - P ][K],
									[ P, O, O - S, O + R ][K],
									(!J.shading ? 0 : (M ? H : 1 - H)
											* I[2].props.opacity.diff
											+ I[2].props.opacity.start),
									I[0].props, 'start');
							T(
									'imageCubeTo',
									[ -R, J.reduction - S, P, 0 ][K],
									[ P, 0, -R, J.reduction - S ][K],
									[ N + R, N - (J.reduction - S), N, N - P ][K],
									[ P, 0, J.reduction - S, -R ][K],
									[ N - (J.reduction - S), N + R, N, N - P ][K],
									[ O, O - P, O - (J.reduction - S), O + R ][K],
									[ J.reduction - S, -R, P, 0 ][K],
									[ O, O - P, O + R, O - (J.reduction - S) ][K],
									(!J.shading ? 0 : (M ? H : 1 - H)
											* I[3].props.opacity.diff
											+ I[3].props.opacity.start),
									I[1].props, 'end');
							return true
						}
					});
	function extendRemove(a, b) {
		$.extend(a, b);
		for ( var c in b) {
			if (b[c] == null) {
				a[c] = null
			}
		}
		return a
	}
	$.fn.imagecube = function(a) {
		var b = Array.prototype.slice.call(arguments, 1);
		if (a == 'current' || a == 'next') {
			return $.imagecube['_' + a + 'ImageCube'].apply($.imagecube,
					[ this[0] ].concat(b))
		}
		return this.each(function() {
			if (typeof a == 'string') {
				$.imagecube['_' + a + 'ImageCube'].apply($.imagecube, [ this ]
						.concat(b))
			} else {
				$.imagecube._attachImageCube(this, a)
			}
		})
	};
	$.fx.step[Y] = function(a) {
		if (a.state == 0 || !a.stepProps) {
			a.start = 0.0;
			a.end = 1.0;
			a.stepProps = $.imagecube._prepareAnimation(a.elem);
			var b = a.stepProps[0].elem;
			a.saveCSS = {
				borderLeftWidth : b.style.borderLeftWidth,
				borderRightWidth : b.style.borderRightWidth,
				borderTopWidth : b.style.borderTopWidth,
				borderBottomWidth : b.style.borderBottomWidth,
				padding : b.style.padding
			}
		}
		if (!$.imagecube._drawFull3D(a.elem, a.pos, a.stepProps)) {
			for ( var i = 0; i < a.stepProps.length; i++) {
				var c = a.stepProps[i];
				for ( var d in c.props) {
					var e = c.props[d];
					c.elem.style[d] = (a.pos * e.diff + e.start) + e.units;
					if ($.browser.msie && d == 'opacity') {
						c.elem.style.filter = 'alpha(opacity='
								+ ((a.pos * e.diff + e.start) * 100) + ')'
					}
				}
			}
		}
		if (a.state == 1) {
			$(a.stepProps[0].elem).hide().css(a.saveCSS);
			$(a.stepProps[1].elem).show();
			$.imagecube._prepareRotation(a.elem)
		}
	};
	$.imagecube = new ImageCube()
})(jQuery);
