var rotis = '#main h1 a, #thumbs h2 span, div.content h2, #footer p';
var rotis_h = '#main li a';

Cufon.replace(rotis, { fontFamily: 'Rotis' });
Cufon.replace(rotis_h, { fontFamily: 'Rotis', hover : true });

$(function(){
	$('body').addClass('js');
	$('#thumbs').preloadImg().thumbs();
	$('#gallery img').gallery();
	$('#thumbs h2 span').togglethumbs();
	$('#main li a').getcontent();
});

(function($) {

//	PRELOADING GALLERY IMAGES BASED ON IMAGES IN THUMBNAILS

	$.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);
	}

//	SHOW AND HIDE THUMBNAILS STRIP

	$.fn.togglethumbs = function(){
		if (this.length == 0){
			return $(this);
		}
		var $th = $('#thumbs');
		var ht = $th.outerHeight();
		var showthumbs = function(){
			$th.animate({
				marginTop : 0
			}, 400);
			$(this).addClass('on');
		}
		var hidethumbs = function(){
			$th.animate({
				marginTop : -ht
			}, 400);
			$(this).removeClass('on');
		}
		$(this).toggle(showthumbs, hidethumbs).parent().disClick();
		return $(this);
	}

//	SETUP AND RESIZE GALLERY IMAGES TO FILL ENTIRE SCREEN

	$.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);
	}

//	THUMBNAILS FUNCTIONALITY AND SCROLLER HANDLER

	$.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);

//	THUMBNAILS INTIAL SETUP

		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);
			}
		}

//	HANDLER FOR CLICK FUNCTIONALITY ON THUMBNAILS

		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);
		}

//	HANDLER FOR CLICK FUNCTIONALITY ON SCROLL BUTTONS

		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);
	}

//	HANDLER FOR CLICK FUNCTIONALITY ON MAIN LINKS AND RETRIEVE CONTENT VIA AJAX

	$.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 (data.length == 0){
							return;
						}
						$('body').append($(data));
						Cufon.refresh();
						var $obj = $('#' + dat.name).addscroll().drag().mousedown(actcon).actions();
						if ($('div.content').length == 1){
							$obj.css({
								top : $(window).height() * 0.25,
								left : $(window).width() * 0.40
							}).show();
						}
						else {
							var num = $('div.content').length - 2;
							var postop = $('div.content').eq(num).cssInt('top') - 60;
							var posleft = $('div.content').eq(num).cssInt('left') + 60;
							if (postop < 0){
								postop = $(window).height() * 0.25;
							}
							if (posleft + 450 > $(window).width()){
								posleft = $(window).width() * 0.4;
							}
							$obj.css({
								top : Math.round(postop),
								left : Math.round(posleft)
							}).show();
						}
					}
				});
			}
			else {
				$('#' + dat.name).addClass('active').show();
			}
		};

		$obj.click(clfn);

		var actcon = function(){
			$('div.content').removeClass('active');
			$(this).addClass('active');
		}

		var reset = function(){
			var postop;
			var posleft;
			var ct = $('div.content').length;
			for (i=0; i<ct; i++){
				if (i==0){
					postop = $(window).height() * 0.25;
					posleft = $(window).width() * 0.40;
				}
				$('div.content').eq(i).css({
					top : Math.round(postop),
					left : Math.round(posleft)
				});
				postop -= 60;
				posleft += 60;
				if (postop < 0){
					postop = $(window).height() * 0.25;
				}
				if (posleft + 450 > $(window).width()){
					posleft = $(window).width() * 0.40;
				}
			}
		}

		$(window).resize(reset);

		return $(this);
	}

//	HANDLER FOR CLICK FUNCTIONALITY ON BUTTONS INSIDE POPUP BOXES

	$.fn.actions = function(){
		if (this.length == 0){
			return $(this);
		}

		var $obj = $(this).children('ul.action').find('a').disClick();

		var clfn = function(){
			if ($(this).hasClass('close')){
				var $con = $(this).parents('div.content').hide().removeClass('collapsed')
							.find('a.exp').toggleClass('coll').toggleClass('exp').attr('title', 'collapse').text('collapse').end();
				var num = $('div.content').index($con);
				if (num != 0){
					num--;
					$('div.content').eq(num).addClass('active');
				}
				else {
					$('div.content').addClass('active');
				}
			}
			else if ($(this).hasClass('coll')){
				$('div.content').removeClass('active');
				$(this).parents('div.content').addClass('collapsed').addClass('active').end()
				.toggleClass('coll').toggleClass('exp').attr('title', 'expand').text('expand');
			}
			else {
				$('div.content').removeClass('active');
				$(this).parents('div.content').removeClass('collapsed').addClass('active').end()
				.toggleClass('coll').toggleClass('exp').attr('title', 'collapse').text('collapse');
			}
		}

		$obj.click(clfn);

		return $(this);
	}

//	HANDLER FOR DRAG FUNCTIONALITY FOR POPUP BOXES

	$.fn.drag = function(){
		if (this.length == 0){
			return $(this);
		}

		var disabledrag = function(){
			$(this).parent().draggable('disable');
		}
		var enabledrag = function(){
			$(this).parent().draggable('enable');
		}

		$(this).draggable({
			containment : 'window'
		}).children().not('h2').hover(disabledrag, enabledrag);

		return $(this);
	}

//	HANDLER FOR ADDING SCROLLBAR TO POPUP BOXES WHEN CONTENT OVERFLOW IS PRESENT

	$.fn.addscroll = function(){
		if (this.length == 0){
			return $(this);
		}

		var name = $(this).find('div.int').children().wrapAll('<div class="scroll"></div>').end().parent().attr('id');

		var setup = function(){

			var $int = $('#' + name).find('div.int');
			var scroll = { ht : $int.find('div.scroll').height() };

			if (scroll.ht < 280){
				return $(this);
			}

			$int.before($('<div class="scrollbar"><a class="handle" href="#" title=""></a></div>'));

			var step = 0;
			$int.parent().find('div.scrollbar a').disClick().draggable({
				axis : 'y',
				containment : 'parent',
				drag : function(e, ui){
					$int.attr('scrollTop', scroll.ht*ui.position.top/280);
					step = Math.round(ui.position.top*100/180);
				}
			});

			$int.mousewheel(function(event, delta){
				step -= delta*4;
				if (step < 0){
					step = 0;
				}
				else if (step > 100){
					step = 100;
				}
				$int.attr('scrollTop', (scroll.ht-280)*step/100).siblings('div.scrollbar').children().css('top', 180*step/100);
			});
		}

		$(this).find('div.scroll').ready(function(){
			setTimeout(setup, 0);
		});

		var reset = function(){
			var $cont = $('div.content');
			var ct = $('div.content').length;
			for (i=0; i<ct; i++){
				$obj = $cont.eq(i);
				if ($obj.find('div.scrollbar').length == 0){
					continue;
				}
				var scrollht = $obj.find('div.scroll').height();
				var top = $obj.find('div.scrollbar a').cssInt('top');
				$.log(top);
				$obj.find('div.int').attr('scrollTop', scrollht * top / 280);
			}
		}

		$(window).resize(reset);

		return $(this);
	}

//	HANDLER FOR DISABLING CLICKS ON LINKS

	$.fn.disClick = function(){
		if (this.length == 0 || this.attr('id') == 'error_404'){
			return $(this);
		}
		var clfn = function(){ return false; }
		$(this).click(clfn);
		return $(this);
	}

//	HELPER RETURNS INTEGER VALUES FOR STYLING PARAMETERS

	$.fn.cssInt = function(par){
		dat = $(this).css(par);
		return parseInt(dat);
	}

//	LOGGING FUNCTIONS FOR TROUBLESHOOTING

	$.fn.log = function(){
		if (typeof window.console !== 'undefined'){
			console.log($(this));
		}
		return $(this);
	}

	$.log = function(inp){
		if (typeof window.console !== 'undefined'){
			console.log(inp);
		}
	}
})(jQuery);

