Cufon.replace('#main h1 a, #thumbs h2 span, div#cwrapper h2, #footer p, #c2a, .smoke', { fontFamily: 'Yanone Kaffeesatz Light' });
Cufon.replace('#main li a, a#goff', { fontFamily: 'Yanone Kaffeesatz Light', hover : true });

$(function(){
	$('body').addClass('js');
	
	$("html").bind("ajaxStart", function(){  
		$(this).addClass('busy');  
	}).bind("ajaxStop", function(){  
		$(this).removeClass('busy');  
	});
	
	var cdivleft = $('#content').outerWidth();
	$('div#content').css({left: -cdivleft});			
	
	$('#thumbs').preloadImg().thumbs();
	$('#gallery img').gallery();
	$('a.galeria').bind('click', function(){
		var $th = $('#thumbs');
		var $main = $('#main');
		var $content = $('div#content');
		var ht = $th.outerHeight();
		var mw = $main.outerWidth();
		var cw = $content.outerWidth();

		if(! $(this).hasClass('on'))
		{
			$content.animate({
				left : -mw - cw
			}, 400, function() {
				$main.animate({
					left : -mw
				}, 400, function() {
					$th.animate({
						marginTop : 0
					}, 400);
				});
			});			
			$('a.galeria').addClass('on');
		}
		else
		{
			$th.animate({
				marginTop : -ht
			}, 400, function() {
				$main.animate({
					left : 0
				}, 400, function() {
						if($content.html().length > 0)
						{
							$content.animate({
								left : mw + 1
							}, 400);
						}
				});
			});
			$('a.galeria').removeClass('on');
		}
		return false;
	});

	$('#main li a.getcontent').getcontent();
});

(function($) {
	$.fn.preloadImg = function(){
		if (this.length == 0){
			return $(this);
		};
		var img = {
			width : 1024,
			height : 683
		};
		var $newimg;
		var $img = $(this).find('img');
		for (i=0; i<$img.length; i++){
			img.src = $img.eq(i).attr('src').replace('thumb_', '');
			$newimg = $('<img src="' + img.src + '" width="' + img.width + '" height="' + img.height + '" alt="" />');
			$('#gallery').append($newimg);
		};
		return $(this);
	};
	$.fn.gallery = function(){
		if (this.length == 0){
			return $(this);
		};

		var $img = $(this);
		var img = {
			wd : $img.attr('width'),
			ht : $img.attr('height')
		};

		var setup = function(){
			$('body').addClass('rs');
			var win = {
				wd : $(window).width(),
				ht : $(window).height()
			};
			var doc = {
				wd : $('body').width(),
				ht : $('body').height()
			};
			var bbg = { wd : doc.wd };

			if (win.ht > doc.ht){
				bbg.ht = win.ht;
			}
			else {
				bbg.ht = doc.ht;
			};

			var ratio = {
				wd : bbg.wd/img.wd,
				ht : bbg.ht/img.ht
			};

			if (ratio.ht > 1 || ratio.wd > 1){
				if (ratio.wd > ratio.ht){
					$img.css({
						width : bbg.wd,
						height : 'auto',
						marginLeft : -Math.round(bbg.wd/2),
						marginTop : -Math.round(img.ht*ratio.wd/2)
					});
				}
				else {
					$img.css({
						width : 'auto',
						height : bbg.ht,
						marginTop : -Math.round(bbg.ht/2),
						marginLeft : -Math.round(img.wd*ratio.ht/2)
					});
				}
			}
			else {
				$img.css({
					marginTop : -Math.round(img.ht/2),
					marginLeft : -Math.round(img.wd/2)
				});
			};
			$img.parent().css({
				width : bbg.wd,
				height : bbg.ht
			});
			$('body').removeClass('rs');
		}

		setup();

		$(window).resize(setup);

		$('#gallery img').hide().eq(0).load(function(){
			$('#gallery').addClass('nobg').children('img').first().fadeIn(400);
		});

		return $(this);
	};
	$.fn.thumbs = function(){
		if (this.length == 0){
			return $(this);
		};

		var $obj = $(this);
		var $img = $obj.children('a');

		var img = { width: $img.width() + 24 };
		img.totalwidth = img.width * $img.length;

		var $clone;
		for (i=0; i < 3; i++){
			$clone = $img.clone().addClass('dup').hide();
			$obj.append($clone);
		};

		$obj.children('a').wrapAll('<div id="display"></div>').wrapAll('<div id="slider"></div>').end().prepend('<a class="button" href="#" title=""></a><a class="button right" href="#" title=""></a>');

		var display = { busy : false };
		var slider = { width: img.totalwidth*4 };

		var $display = $('#display');
		var $dup = $obj.find('a.dup');
		var $but = $obj.find('a.button');
		var $slider = $('#slider').width(slider.width);
		var setup = function(){
			display.count = 0;
			$obj.width($('body').width() - 48);
			display.width = $obj.width() - 72;
			$display.width(display.width);
			if (display.width < img.totalwidth - 24){
				$dup.show().parents('#thumbs').removeClass('scrolloff').find('#slider').css('marginLeft', 2*(-img.totalwidth)-12);
			}
			else {
				$dup.hide().parents('#thumbs').addClass('scrolloff').find('#slider').css('marginLeft', -12);
			}
		};
		var imgclfn = function(){
			var href = $(this).children().addClass('act').end().attr('href');
			var $links = $(this).siblings().children().removeClass('act').end();
			for (i=0; i<$links.length; i++){
				$l = $links.eq(i);
				if ($l.attr('href') == href){
					$l.children().addClass('act');
				}
			};
			var num = $('#thumbs').find('img').parent().index(this) % $img.length;
			if ($('#gallery img').eq(num).css('display') == 'block'){
				return;
			};
			$('#gallery img').fadeOut(400).eq(num).fadeIn(400);
		};
		var left = { count : 0 };
		var right = { count : 0 };

		var btclfn = function(){
			if (display.busy){
				return;
			};
			display.busy = true;
			mleft = $slider.cssInt('marginLeft');
			if (! $(this).hasClass('right')){
				if ((mleft+12)%img.width == 0){
					right.count = 0;
					right.reset = mleft + display.width % img.width + 24;
					$slider.animate({
						marginLeft : right.reset
					}, 600);
				}
				else {
					if (right.count == $img.length){
						right.count = 0;
						mleft = right.reset;
						$slider.css('marginLeft', right.reset);
					}
					right.count++;
					mleft += img.width
					$slider.animate({
						marginLeft : mleft
					}, 600);
				}
			}
			else {
				if ((mleft+12)%img.width != 0){
					left.count = 0;
					left.reset = mleft - display.width % img.width - 24;
					$slider.animate({
						marginLeft : left.reset
					}, 600);
				}
				else {
					if (typeof left.reset == 'undefined'){
						left.reset = mleft;
					}
					if (left.count == $img.length){
						left.count = 0;
						mleft = left.reset;
						$slider.css('marginLeft', left.reset);
					}
					left.count++;
					mleft -= img.width
					$slider.animate({
						marginLeft : mleft
					}, 600);
				}
			};
			setTimeout(function(){ display.busy = false; }, 600);
		};

		setup();
		$(window).resize(setup);
		$slider.find('a').disClick().click(imgclfn);
		$but.disClick().click(btclfn);
		return $(this);
	};
	$.fn.getcontent = function(){
		if (this.length == 0){
			return $(this);
		};

		var $obj = $(this).disClick();

		var clfn = function(){
			$('div.content').removeClass('active');
			var dat = { name : $(this).attr('rel') };
			var rurl = $(this).attr('href');
			if ($('#' + dat.name).length == 0){
				$.ajax({
					  url: rurl,
					  cache: false,
					  dataType: 'html',
					  success: function(data) {
						if($('#content').cssInt('left') > 0)
						{
							$('#content').animate({left: ($('#main').outerWidth() + $('#content').outerWidth() + 1) * -1}, 400, function(){
								$(this).html(data).animate({left: $('#main').outerWidth() + 1}, 400);
								Cufon.refresh();
							});
						}
						else
						{
							$('#content').html(data).animate({left: $('#main').outerWidth() + 1}, 400);
							Cufon.refresh();
						}
						_gaq.push(['_trackEvent', 'Menu click', rurl]);
					  }
				});			
			}
			else {
				$('#' + dat.name).addClass('active').show();
			}
		};

		$obj.click(clfn);
		return $(this);
	};
	$.fn.disClick = function(){
		if (this.length == 0 || this.attr('id') == 'error_404'){
			return $(this);
		};
		var clfn = function(){ 
			return false; 
		};
		$(this).click(clfn);
		return $(this);
	};
	$.fn.cssInt = function(par){
		dat = $(this).css(par);
		return parseInt(dat);
	};
})(jQuery);

