jQuery(function ($) {
	
	var arrow_speed = 200;
	
	function fix_vertical_arrows() {
		var cont = $('#main #holder #content');
		if (cont.find('ul.vertical > li').length < 2) {
			$('.btn-top, .btn-bottom').hide();
		}
		
		var top = parseInt(cont.find('ul.vertical:eq(0)').css('top'));
		if (isNaN(top)) {
			top = 0;
		}
		var item = $('.vertical:eq(0) > li:eq(0)');
		var mt = parseInt(item.css('height'));
		if (isNaN(mt)) {
			mt = 0;
		}
		var mb = parseInt(item.css('margin-bottom'));
		if (isNaN(mb)) {
			mb = 0;
		}
		var item_height = mt+mb;
		var idx = Math.abs(Math.ceil(top / item_height));
		var total = cont.find('ul.vertical > li').length;		
		
		if (idx == 0) {
			$('.btn-top').fadeOut(arrow_speed);
			if (total < 2) {
				$('.btn-bottom').fadeOut(arrow_speed);
			}
		} else {
			$('.btn-top').fadeIn(arrow_speed);
			if (idx < total - 1) {
				$('.btn-bottom').fadeIn(arrow_speed);
			} else {
				$('.btn-bottom').fadeOut(arrow_speed);
			}
		}
		
	}
	
	fix_vertical_arrows();
	
	function fix_horizontal_arrows(ul) {
		var arrows = $('.btn-left, .btn-right');
		if (ul.find('li').length < 2) {
			arrows.fadeOut(arrow_speed);
		} else {
			var item = $('.horizontal:eq(0) > li:eq(0)');
			var item_width = parseInt(item.css('width')) + parseInt(item.css('margin-right'));
			var idx = Math.abs(Math.ceil(left / item_width));
			var total = ul.children('li').length;
			if (idx == 0) {
				$('.btn-right').fadeIn(arrow_speed);
			} else if(idx == total-1) {
				$('.btn-left').fadeIn(arrow_speed);
			} else {
				arrows.fadeIn(arrow_speed);
			}
		}
		fix_single_horizontal_arrows(ul);
	}
	
	function fix_single_horizontal_arrows(ul) {
		var left = parseInt(ul.css('left'));
		if (isNaN(left)) {
			left = 0;
		}
		var item = $('.horizontal:eq(0) > li:eq(0)');
		var ml = parseInt(item.css('width'));
		if (isNaN(ml)) {
			ml = 0;
		}
		var mr = parseInt(item.css('margin-right'));
		if (isNaN(mr)) {
			mr = 0;
		}
		
		var item_width = ml + mr;
		var idx = Math.abs(Math.ceil(left / item_width));
		var total = ul.children('li').length;
		if (idx == 0) {
			$('.btn-left').fadeOut(arrow_speed);
			if (total > 1) {
				$('.btn-right').fadeIn(arrow_speed);
			}
		} else if (idx < total - 1) {
			$('.btn-right, .btn-left').fadeIn(arrow_speed);
		} else {
			$('.btn-right').fadeOut(arrow_speed);
			if (total > 1) {
				$('.btn-left').fadeIn(arrow_speed);
			}
		}
	}
	
	fix_horizontal_arrows($('ul.horizontal:eq(0)'));
	fix_single_horizontal_arrows($('ul.horizontal:eq(0)'));
	
	/*********************
	 Content Slider START
	**********************/
	var width = 850;
	var height = 590;
	
	// blog page dimensions (not so elegant eh)
	if ($('#blog').length > 0) {
		width = 921;
		height = 475;
	}
	// blog page dimensions 
	
	var ver = $('.vertical');
	var hor = $('.horizontal');
	var horCount = ver.find('.horizontal').length;

	ver.height(horCount * height);

	for (var i=0; i < ver.length; i++) {
		for (var j=0; j < hor.length; j++) {
			hor.eq(j).width(hor.eq(j).find('> li').length * width);
		}
	}
	
	var verIndex = 0;
	var horIndex = Array();
	for (var i=0; i < horCount; i++) {
		horIndex[i] = 0;
	}

	var top = 0;
	var left = 0;
	
	$(document).keydown(function (e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		Calculate(code);
	});
	
	function Calculate (code) {
		var kvp = {};
		
		switch (code) {
			case 38: { // top
					if (verIndex > 0) {
						kvp['top'] = top+=height;
						verIndex--;
						left = -horIndex[verIndex]*width;
					}
				} break;
				
			case 40: { // down
					if (verIndex < horCount-1) {
						kvp['top'] = top-=height;
						verIndex++;
						left = -horIndex[verIndex]*width;
					}
				} break;
				
			case 37: { // left
					if (horIndex[verIndex] > 0) {
						kvp['left'] = left+=width;
						horIndex[verIndex]--;
					}
				} break;
				
			case 39: { // right
					if (horIndex[verIndex] < hor.eq(verIndex).find('> li').length-1) {
						kvp['left'] = left-=width;
						horIndex[verIndex]++;
					}
				} break;
		}

		Slide(kvp);
	}
	
	function Slide(kvp) {
		for (name in kvp) {
			var horiz_h = $('ul.horizontal:eq(0)').height() + parseInt($('ul.horizontal:eq(0)').parent().css('margin-bottom'));
			if (!horiz_h) {
				horiz_h = 590;
			}
			if (name == 'top') {
				ver.animate(kvp, 400, function () {
					var top = parseInt($('ul.vertical').css('top'));
					if (!top) {
						top = 0;
					}
					
					var idx = Math.abs(Math.ceil(top / horiz_h));
					fix_horizontal_arrows($('ul.horizontal:eq(' + idx + ')'));
					fix_vertical_arrows();
				});
				
				break;
				
			} else { // left
				hor.eq(verIndex).animate(kvp, 400, function () {
					var idx = Math.abs(Math.ceil(top / horiz_h));
					fix_single_horizontal_arrows($('ul.horizontal:eq(' + idx + ')'));
				});
				break;
			}
		}
	}
	
	$('.btn-top, .btn-bottom, .btn-left, .btn-right, .blog-top, .blog-bottom').click(function () {
		var code = -1;
		switch ($(this)[0].className) {
			case 'btn-top': case 'blog-top': code = 38; break;
			case 'btn-bottom': case 'blog-bottom': code = 40; break;
			case 'btn-left': code = 37; break;
			case 'btn-right': code = 39; break;
		}
		Calculate(code);
		return false;
	});
	/*********************
	 Content Slider END
	**********************/
	
	/*************************************************************************************************************************************************/
	
	/*********************
	 Home Slider
	**********************/
	var index = 2;
	var zindex = 1;
	$('.col').mouseover(function (e) { // may be the entire .col as well
		var idx = $('.col').index(this); // .col
		if (idx < index) { // left
			
			index = idx;
			sld(-984, 200, 300, 400);
			
		} else if (idx > index) {
			
			index = idx;
			sld(328, 400, 300, 200);
		}
		
		$(this).find('ul').show();
	});
	$('.col').mouseout(function (e) {
		$(this).find('ul').hide();
		return false;
	});
	
	function sld(start, a, b, c) {
		$('.col .image img').hide();
		for(var i = 0; i <= 2; i++) {
			var items = $('.col .image:eq(0)').find('.img-' + i);
			var rand = Math.floor(Math.random() * items.length);
			$('.col .image').find('.img-' + i + ':eq(' + rand + ')').show();
		}
		
		$('.image1 img').css({left: 0});
		$('.image2 img').css({left: -328});
		$('.image3 img').css({left: -656});
		
		$('.col').each(function (i) {
			$(this).find('img:visible').eq(index).css({zIndex: ++zindex, left: start});
		});
		
		$('.image1 img:visible').eq(index).stop().animate({left: 0}, a);
		$('.image2 img:visible').eq(index).stop().animate({left: -328}, b);
		$('.image3 img:visible').eq(index).stop().animate({left: -656}, c);
	}
	/*********************
	 Home Slider END
	**********************/
	
	/*************************************************************************************************************************************************/
	
	// nav dropdown
	$("#navigation li").hover(function() {
		$(this).css({ 'z-index' : 100 });
		$(this).find("> .dd").stop(true, true).fadeIn("fast");
		$(this).find("> a:eq(0)").addClass('hover');
	}, function() {
		$(this).css({ 'z-index' : 1 });
		$(this).find("> .dd").stop(true, true).fadeOut("fast");
		$(this).find("a:eq(0)").removeClass('hover');
	});
	$('#navigation li .dd ul li a').live('click', function () {
		if ($(this).parent().find('ul').length) {
			$(this).toggleClass('active');
			$(this).parent().find('ul').slideToggle(200);
			return false;
		}
	});
	
	$('#navigation > li:last').addClass('last');
	$('#navigation > li > ul').each(function() {
		var ul = $(this);
		ul.wrap('<div class="dd"><div class="dd-c"></div></div>');
		ul.parents('.dd-c:eq(0)').append('<div class="cl">&nbsp;</div>');
		ul.parents('.dd:eq(0)').append('<div class="dd-b">&nbsp;</div>').prepend('<div class="dd-t">&nbsp;</div>');
	});
	
	$('#nav .col ul.menu').each(function() {
		if (!$(this).find('li').length) {
			$(this).remove();
		}
	});
	
	// instructions
	$('.instructions').fadeIn(1000).delay(2000).fadeOut(1000);
});
