var $j = jQuery.noConflict();

var isIE6 = false;
function flags() {
	$j("body").removeClass("js-OFF").addClass("js-ON");
	if (jQuery.browser.msie && jQuery.browser.version < 7)
	{ $j("body").addClass("ie6"); isIE6 = true; }
}

function menuEffects() {
	$j("#topmenu a").removeAttr('title');
	$j("#topmenu > li").hover(function() {
		$j(this).find("ul:first").show(200);
	}, function() {
		$j(this).find("ul:first").hide();
	});
}

$j(function() {
	flags();
	menuEffects();

	if ($j("#shuffle .item").length > 0) {
		$j(".random", "#shuffle").click(function() { shuffleNext(); return false; });
		enableShuffle();
		autoShuffle();
	}
});

function testChameleon(color) { // you can remove this function
	$j(".bgCH").css("backgroundColor", color);
	$j(".CH").css("color", color);
	$j("#sidebar a").css("color", color);
	$j(".entry a").css("color", color);
	$j(".desc a").css("color", color);
	$j(".post-info a").css("color", color);
}



////////////////////////////////////
// twiroo shuffle
////////////////////////////////////

var dCardX = 36, dCardY = 12, dCardW = 0.8, shuffleSpd = 300; // visuals
var autoshuffle = 0;

function isEven(num) {
	return !(num % 2);
}

function cardPos(pos) {
	if (pos < 1 || pos > 11)
		return;
	var iseven = isEven(pos);
	var mul = (pos - (iseven ? 0 : 1)) / 2;
	this.mul = mul;
	this.W = defCard.W * Math.pow(dCardW, mul);
	this.Y = dCardY * mul * Math.pow(dCardW, mul) * 2;
	this.PB = defCard.Yp; // * Math.pow(dCardW, mul);
	this.X = (iseven) ?
		defCard.L - (dCardX * mul) :
		defCard.R + (dCardX * mul) - this.W;
	this.ZI = 1000 - pos;
}

function moveCardToPos(obj, pos, speed, type) {
	newPos = new cardPos(pos);
	obj.css({ 'z-index': newPos.ZI });
	obj.animate({ 'bottom': newPos.Y, 'width': newPos.W, 'left': newPos.X, 'paddingBottom': newPos.PB }, speed, type);
	obj.data("pos", pos);
}

defCard = new Object;
function calcDefaultCard() {
	var card = getCard(1);
	defCard.W = card.width();
	defCard.Yp = card.outerHeight() - card.height();
	defCard.L = card.position().left;
	defCard.R = defCard.W + defCard.L;
}

function getCard(pos) {
	return $j(".card:eq(" + (pos - 1) + ")", "#shuffle");
}

function getCardByData(pos) {
	var obj = null;
	$j(".card", "#shuffle").each(function() {
		if ($j(this).data("pos") == pos)
			obj = $j(this);
	});
	return obj;
}

function getDPosByData(pos) {
	var num = null;
	$j(".card", "#shuffle").each(function(i) {
		if ($j(this).data("pos") == pos)
			num = i + 1;
	});
	return num;
}

function getDescByCardData(pos) {
	return getCardByData(pos).siblings(".desc");
}

function shuffleToFrontFrom(pos) {
	var obj = getCardByData(pos);
	curPos = new cardPos(pos);
	var avrW = (defCard.W + curPos.W) / 2;
	var left = (!isEven(pos)) ? curPos.X + avrW / 2 - (avrW - curPos.W) : curPos.X - avrW / 2;
	obj.animate({ 'bottom': curPos.Y / (dCardW * 2), 'width': avrW, 'left': left, 'paddingBottom': curPos.PB / 2 }, shuffleSpd, 'easeInCirc', function() { obj.css({ 'z-index': 999 }); });
	obj.animate({ 'bottom': 0, 'width': defCard.W, 'left': defCard.L, 'paddingBottom': defCard.Yp }, shuffleSpd, 'easeOutCirc');
	return obj;
}

function shuffleFromFrontTo(pos) {
	var obj = getCardByData(1);
	toPos = new cardPos(pos);
	var avrW = (defCard.W + toPos.W) / 2;
	var left = (!isEven(pos)) ? defCard.L - (defCard.W / 2) : defCard.L + (defCard.W);
	obj.animate({ 'bottom': toPos.Y / (dCardW * 2), 'width': avrW, 'left': left, 'paddingBottom': toPos.PB / 2 }, shuffleSpd, 'easeInCirc', function() { obj.css({ 'z-index': 1000 - pos }); });
	obj.animate({ 'bottom': toPos.Y, 'width': toPos.W, 'left': toPos.X, 'paddingBottom': toPos.PB }, shuffleSpd, 'easeOutCirc');
	return obj;
}

function animateDesc(number) {
	var bye = getDescByCardData(1);
	bye.children().animate({ 'top': 0, 'opacity': 0 }, shuffleSpd, "easeOutCirc", function() { bye.children().css({ 'visibility': 'hidden' }); });
	var hi = getDescByCardData(number);
	hi.children().css({ 'top': 200, 'opacity': 0, 'visibility': 'visible' });
	hi.children("p").animate({ 'top': 145, 'opacity': 1 }, shuffleSpd, "easeInCirc");
	hi.children("h2").animate({ 'top': 80, 'opacity': 1 }, shuffleSpd, "easeInCirc");
}

function shuffle(number) {
	if (number == 1)
		return true;

	animateDesc(number);
	var bye = shuffleFromFrontTo(number);
	var hi = shuffleToFrontFrom(number);
	bye.data("pos", number);
	hi.data("pos", 1);

	clearShuffleTimer(); 

	return false;
}

function shuffleNext() {
	var l = $j(".card", "#shuffle").length;
	var r = Math.floor(Math.random() * (l - 1)) + 2;
	shuffle(r);
}

function enableShuffle() {
	calcDefaultCard();

	$j(".item.hidden .desc", "#shuffle").children().css({ 'visibility': 'hidden' });
	$j(".item.hidden", "#shuffle").removeClass("hidden");
	$j(".card", "#shuffle").each(function(i) {
		moveCardToPos($j(this), i + 1, 1000, 'easeOutElastic');
		$j(this).click(function() { return shuffle($j(this).data("pos")); });
	});
}

var t;
function autoShuffle() {
	if (!autoshuffle)
		return;
	t = setTimeout("autoShuffle(); shuffleNext();", autoshuffle);
}
function clearShuffleTimer() {
	if (!autoshuffle)
		return;
	clearTimeout(t);
	autoShuffle();
}











/*
* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
*
* Uses the built in easing capabilities added In jQuery 1.1
* to offer multiple easing options
*
* TERMS OF USE - jQuery Easing
* 
* Open source under the BSD License. 
* 
* Copyright © 2008 George McGinley Smith
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
* 
* Redistributions of source code must retain the above copyright notice, this list of 
* conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list 
* of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
* 
* Neither the name of the author nor the names of contributors may be used to endorse 
* or promote products derived from this software without specific prior written permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
*  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
* OF THE POSSIBILITY OF SUCH DAMAGE. 
*
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend(jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function(x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function(x, t, b, c, d) {
		return c * (t /= d) * t + b;
	},
	easeOutQuad: function(x, t, b, c, d) {
		return -c * (t /= d) * (t - 2) + b;
	},
	easeInOutQuad: function(x, t, b, c, d) {
		if ((t /= d / 2) < 1) return c / 2 * t * t + b;
		return -c / 2 * ((--t) * (t - 2) - 1) + b;
	},
	easeInCubic: function(x, t, b, c, d) {
		return c * (t /= d) * t * t + b;
	},
	easeOutCubic: function(x, t, b, c, d) {
		return c * ((t = t / d - 1) * t * t + 1) + b;
	},
	easeInOutCubic: function(x, t, b, c, d) {
		if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
		return c / 2 * ((t -= 2) * t * t + 2) + b;
	},
	easeInQuart: function(x, t, b, c, d) {
		return c * (t /= d) * t * t * t + b;
	},
	easeOutQuart: function(x, t, b, c, d) {
		return -c * ((t = t / d - 1) * t * t * t - 1) + b;
	},
	easeInOutQuart: function(x, t, b, c, d) {
		if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
		return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
	},
	easeInQuint: function(x, t, b, c, d) {
		return c * (t /= d) * t * t * t * t + b;
	},
	easeOutQuint: function(x, t, b, c, d) {
		return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
	},
	easeInOutQuint: function(x, t, b, c, d) {
		if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
		return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
	},
	easeInSine: function(x, t, b, c, d) {
		return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
	},
	easeOutSine: function(x, t, b, c, d) {
		return c * Math.sin(t / d * (Math.PI / 2)) + b;
	},
	easeInOutSine: function(x, t, b, c, d) {
		return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
	},
	easeInExpo: function(x, t, b, c, d) {
		return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
	},
	easeOutExpo: function(x, t, b, c, d) {
		return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
	},
	easeInOutExpo: function(x, t, b, c, d) {
		if (t == 0) return b;
		if (t == d) return b + c;
		if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
		return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function(x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
	},
	easeOutCirc: function(x, t, b, c, d) {
		return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
	},
	easeInOutCirc: function(x, t, b, c, d) {
		if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
		return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
	},
	easeInElastic: function(x, t, b, c, d) {
		var s = 1.70158; var p = 0; var a = c;
		if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3;
		if (a < Math.abs(c)) { a = c; var s = p / 4; }
		else var s = p / (2 * Math.PI) * Math.asin(c / a);
		return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
	},
	easeOutElastic: function(x, t, b, c, d) {
		var s = 1.70158; var p = 0; var a = c;
		if (t == 0) return b; if ((t /= d) == 1) return b + c; if (!p) p = d * .3;
		if (a < Math.abs(c)) { a = c; var s = p / 4; }
		else var s = p / (2 * Math.PI) * Math.asin(c / a);
		return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
	},
	easeInOutElastic: function(x, t, b, c, d) {
		var s = 1.70158; var p = 0; var a = c;
		if (t == 0) return b; if ((t /= d / 2) == 2) return b + c; if (!p) p = d * (.3 * 1.5);
		if (a < Math.abs(c)) { a = c; var s = p / 4; }
		else var s = p / (2 * Math.PI) * Math.asin(c / a);
		if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
		return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
	},
	easeInBack: function(x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c * (t /= d) * t * ((s + 1) * t - s) + b;
	},
	easeOutBack: function(x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
	},
	easeInOutBack: function(x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
		return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
	},
	easeInBounce: function(x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce(x, d - t, 0, c, d) + b;
	},
	easeOutBounce: function(x, 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;
		}
	},
	easeInOutBounce: function(x, t, b, c, d) {
		if (t < d / 2) return jQuery.easing.easeInBounce(x, t * 2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce(x, t * 2 - d, 0, c, d) * .5 + c * .5 + b;
	}
});

/*
*
* TERMS OF USE - EASING EQUATIONS
* 
* Open source under the BSD License. 
* 
* Copyright © 2001 Robert Penner
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
* 
* Redistributions of source code must retain the above copyright notice, this list of 
* conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list 
* of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
* 
* Neither the name of the author nor the names of contributors may be used to endorse 
* or promote products derived from this software without specific prior written permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
*  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
*  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
* OF THE POSSIBILITY OF SUCH DAMAGE. 
*
*/