jQuery(document).ready(function($) {

	$('.menu-item').hover(
		function () {
			var nav_position = $(this).offset().left;
			if($(this).find('ul:first').is(':hidden')) {
				$(this).find('ul:first').css('left', (nav_position-4));
				$(this).find('ul:first').fadeIn(200);
			}
			$('a:first', this).css("background", "#D4E446");
		},
		function () {
			$('a:first', this).css("background", "#B7B9A1");
			if($('.sub-menu').is(':visible')) {
				$(this).find('ul:first').fadeOut(200);
			}
			
		}
	);

	$('.header-logo').hover(
		function () {
			$('div:first', this).animate({'opacity': '1'}, 200 );
		},
		function () {
			$('div:first', this).animate({'opacity': '.1'}, 100 );
	});


	$(".footer-job-multi").hover(
		function () {
			$('img:first', this).animate({
			'margin-left': '-161px'
			}, 200 );
		},
		function () {
			$('img:first', this).animate({
			'margin-left': '0px'
			}, 100 );
	});

	if($('.scroll').length > 0) {
		$('.scroll').scrollable({circular: true}).autoscroll({ autoplay: true, interval: 5000 });
/*
		window.api = root.data("scrollable");
		$(".scroll").hover(function () {
			api.play();
		},
		function () {
			api.play();
		});
		$('.scroll').click(function() {
			api.next('200');
		});
*/
	};

	if($('#gallery').length > 0) {

		$('#stop').hide();
		slideSwitch($('#thumbs img:first'));

		var number_imgs = $('#thumbs img').length;
		var number_imgs = Math.ceil((1000/number_imgs))-12;
		$('#thumbs img').width(number_imgs);
		$('#thumbs img').height(number_imgs);
		
		$('#thumbs img').click(function() {
			slideSwitch(this);
		});

		$('#image_wrap').click(function() {
			viewLarger($('#thumbs img.active'));
		});

		$('#play').click(function() {

				$('#play').hide();
				$('#stop').show();
				var startShow = setInterval(function() {

					var change_me = $('#thumbs img.active').next();
					if(change_me.length == 0) {
						var change_me = $('#thumbs img').first();
					};
					slideSwitch(change_me);
				}, 5000);

				$('#stop').click(function() {
					$('#stop').hide();
					$('#play').show();
					clearInterval(startShow);
				});
				$('#next').click(function() {
					$('#stop').hide();
					$('#play').show();
					clearInterval(startShow);
				});
				$('#previous').click(function() {
					$('#stop').hide();
					$('#play').show();
					clearInterval(startShow);
				});

		});

		$('#next').click(function() {

			var change_me = $('#thumbs img.active').next();
			if(change_me.length == 0) {
				var change_me = $('#thumbs img').first();
			};
			slideSwitch(change_me);

		});

		$('#previous').click(function() {

			var change_me = $('#thumbs img.active').prev();
			if(change_me.length) {
			} else {
				var change_me = $('#thumbs img').last();
			};
			slideSwitch(change_me);
		});

		$('a#view-large').click(function() {
			viewLarger( $('#thumbs img.active'));
		});


	};

	if($('.content-portfolio').length > 0) {
		$(".content-portfolio").click(function() {
			window.location = $(this).attr('rel');
		});
	};

	if($('#view_larger').length > 0) {
		$('#view_larger img').click(function() {
			viewLarger($(this));
		});
	};

	$('a#zoom').fancybox();

	if($('.content-text').length > 0) {
		$('.content-imgs').height(($('.content-text').height()-10));
	
	};
	
	if($('.portfolio-title').length > 0) {
		$('.portfolio-title a').height(($('.portfolio-title').height()-2));
	
	};

});

function viewLarger(getAlt) {
	get_url = getAlt.attr('alt');
	$.fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'href'			: get_url
	});
};


function slideSwitch(change_me) {
	$("#image_wrap").find("img").fadeOut();

	var new_img = $(change_me).attr('rel');
	var image = new Image();
	image.onload = function() {
		$('#image_wrap').find('img').attr('src', [new_img]);
		$('#image_wrap').find('img').fadeIn();
	};
	image.src = [new_img];

	$("#thumbs img").removeClass("active");
	$(change_me).addClass("active");	
};

