$(function () {
	$("body").append("<div class=\"tooltips\"></div>");
	
	if ($('.scroll_horizontal').length) {
		// custom easing called "custom"
		$.easing.custom = function (x, t, b, c, d) {
			var 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;
		}
		// main horizontal scroll 
		$(".scroll_horizontal").scrollable({ 
			easing: 'custom',
			// basic settings 
			vertical: false,
			size: 3,
			clickable: true,
			loop: true,
			speed: 1000,
			// class settings
			items: '.pages',
			item: '.page',
			// Navigation
			next: '.prev',
			prev: '.next'
		}).autoscroll(4000); 
	}
	
	stylized ();

    /* Chrome Frame */	
	CFInstall.check({
		node: "chromeframe"
	});
});

function stylized () {
	$("*[title]").tooltip({
		tip:'.tooltips',
		effect:'slide'
	});
	
    $(".gallery a[rel=gallery]").fancybox({
        'transitionIn'      : 'elastic',
        'transitionOut'     : 'elastic',
        'centerOnScroll'	: true,
        'titlePosition'     : 'over',
        'overlayOpacity'	: 0.75,
        'overlayColor'		: '#000',
        'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
            return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
        }
    });
	
    $("a.popup").fancybox ({
	    'transitionIn'      : 'elastic',
	    'transitionOut'     : 'elastic',
	    'autoDimensions'	: false,
	    'autoScale'			: true,
	    'centerOnScroll'	: true,
	    'overlayOpacity'	: 0.5,
	    'overlayColor'		: '#000',
	    'showNavArrows'		: false,
	    'width'				: 420,
	    'height'			: 'auto',
	    'padding'			: 20,
	    'onComplete'		: function () {
	        stylized ();
	    }
	});
	
    $("a.popup_large").fancybox ({
	    'transitionIn'      : 'elastic',
	    'transitionOut'     : 'elastic',
	    'autoDimensions'	: false,
	    'autoScale'			: true,
	    'centerOnScroll'	: true,
	    'overlayOpacity'	: 0.5,
	    'overlayColor'		: '#000',
	    'showNavArrows'		: false,
	    'width'				: 620,
	    'height'			: 'auto',
	    'padding'			: 20,
	    'onComplete'		: function () {
	        stylized ();
	    }
	});
	
	if ($('#comments').length) {
		$("#comments .detail:contains('(mario)')").each(function () { $(this).html($(this).html().replace(/\(mario\)/g, '<img alt="Mario" src="http://i28.tinypic.com/33eljwh.png" width="" height="16"/>')) }); $("#comments .detail:contains('(luigi)')").each(function () { $(this).html($(this).html().replace(/\(luigi\)/g, '<img alt="Mario" src="http://i36.tinypic.com/2w4a712.jpg" width="" height="16"/>')) }); $("#comments .detail:contains('(yoshi)')").each(function () { $(this).html($(this).html().replace(/\(yoshi\)/g, '<img alt="Mario" src="http://i34.tinypic.com/25sv7sj.jpg" width="" height="16"/>')) }); $("#comments .detail:contains('(headspin)')").each(function () { $(this).html($(this).html().replace(/\(headspin\)/g, '<img alt="Head Spin" src="http://statics.plurk.com/b0b0596acce9ffc1f2a27548aa642eaf.gif" width="" height="16"/>')) }); $("#comments .detail:contains('(rock)')").each(function () { $(this).html($(this).html().replace(/\(rock\)/g, '<img alt="Rock" src="http://statics.plurk.com/1c890273544559b17f090d09238fa763.gif" width="" height="16"/>')) });
	}
	
	/* Add common odd, even, first, last and hover */
	$("*").hover(function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	});
	
	$(".tab a:first-child").addClass("first");
	$(".tab a:last-child").addClass("last");
	
	$("input:text").addClass("text");
	$("input:password").addClass("password");
	$("input:reset").addClass("reset");
	$("input:submit").addClass("submit");
	$("input:button").addClass("button");
	$("input:radio").addClass("radio");
	
	$("ul > li:nth-child(odd), ol > li:nth-child(odd), .list > .comment:nth-child(odd)").addClass("odd");
	$("ul > li:nth-child(even), ol > li:nth-child(even), .list > .comment:nth-child(even)").addClass("even");
	$("ul > li:first-child, ol > li:first-child, .list > .comment:first-child").addClass("first");
	$("ul > li:last-child, ol > li:last-child, .list > .comment:last-child").addClass("last");
	
	$("[class^=blocks_] > div.block:first-child, [class^=blocks_] > div.block:first").addClass("first");
	$("[class^=blocks_] > div.block:last-child, [class^=blocks_] > div.block:last").addClass("last");
	
	$("[class^=tablelist] tr:first-child").addClass ("first");
	$("[class^=tablelist] tr:last-child").addClass ("last");
	$("[class^=tablelist] tr:nth-child(odd)").addClass ("even");
	$("[class^=tablelist] tr:nth-child(even)").addClass ("odd");
	
	$("[class^=tablelist] tr:first-child td:first-child").addClass ("first");
	$("[class^=tablelist] tr:first-child td:last-child").addClass ("last");
	$("[class^=tablelist] tr:first-child td:nth-child(odd)").addClass ("even");
	$("[class^=tablelist] tr:first-child td:nth-child(even)").addClass ("odd");
	
	$("[class^=tablelist] tr td:first-child").addClass ("first");
	$("[class^=tablelist] tr td:last-child").addClass ("last");
	$("[class^=tablelist] tr td:nth-child(odd)").addClass ("even");
	$("[class^=tablelist] tr td:nth-child(even)").addClass ("odd");
	
    $.fancybox.resize ();
}

var konami = "38,38,40,40,37,39,37,39,66,65".split(',');
var keyIndex = 0;
$(document).keydown(function(ev) { (konami[keyIndex] == ev.keyCode) ? keyIndex++:keyIndex = 0;
	if (keyIndex == konami.length) {
		if ($("#konamicode").length == 0) {
			$("body").prepend ('<div id="konamicode"><iframe title="YouTube video player" class="youtube-player" type="text/html" width="853" height="510" src="http://www.youtube.com/embed/tgbNymZ7vqY?rel=0&autoplay=1" frameborder="0"></iframe></div>');
		}
		$("#konamicode").fancybox({ 
			'width': 480, 
			'height': 385, 
			'overlayColor': '#000000', 
			'overlayOpacity': 0.75, 
       		'centerOnScroll': true,
			'onClosed': function () {
				$('.even').addClass ('rotate2');
				$('.odd').addClass ('rotate-2');
				$('#konamicode').remove (); 
			}
		});
		$("#konamicode").trigger('click');
		keyIndex = 0;
	}
});
