$(document).ready(function(){
	
	jobsSlider();
	gallery();
	
	$('#faqs h3').css('cursor', 'pointer').click(
		function(){
			$(this).next().toggle('blind', 250);
		}
	);
	
	//store original submenu widths
	$('ul.menu > li ul').each(function(i){
		$(this).data('origWidth', $(this).innerWidth());
		$(this).data('origPaddingLeft', $(this).css('padding-left'));
		$(this).data('origPaddingRight', $(this).css('padding-right'));
	});
	
	$('ul.menu > li').hover(
		function(){
			submenu = $(this).find('ul');
			$(submenu).stop(true).css('z-index', '200').animate( { width: $(submenu).data('origWidth'), paddingLeft: $(submenu).data('origPaddingLeft'), paddingRight: $(submenu).data('origPaddingRight') }, 500);

		}, 
		function(){
			$(this).find('ul').stop(true).css('z-index', '199').animate( { width: "hide", paddingLeft: "hide", paddingRight: "15px" }, 500);
		}
	);

});

function gallery(){
	$('#index #gallery li img').load(function(){
		getImgDimension(this);
		$(this).mouseover(
			function(){
				$('#gallery li').each(function(){ gallery_mouseout(this); });
				gallery_mousein(this);
			}
		);
	});
	$('#index #gallery li').mouseout(
		function(){
			gallery_mouseout(this);
		}
	);
}

function gallery_mousein(img){
	var padding = 25;
	li = $(img).closest('li');
	p = $(img).closest('li').find('p');
	$(li).css({ 'height' : '100%', 'width' : '100%', 'position' : 'absolute', 'top' : 0, 'left' : 0, 'z-index' : 9999});
	$(img).stop(true).clearQueue().delay(100).queue(
		function(){
			//if(!$(img).data('nativeDimensions').width || !$(img).data('nativeDimensions').height) getImgDimension(this);
			$(this).animate(
				{ 
					width: $(img).data('nativeDimensions').width,
					height: $(img).data('nativeDimensions').height
				}, 
				350
			);
			$(this).dequeue();
		}
	);
	$(p).stop(true).clearQueue().delay(100).queue(
		function(){
			$(this).css('padding', padding + 'px').animate(
				{ 	//figure out the height and width automatically pls :)
					width: ($(img).data('nativeDimensions').width - (padding*2)),
					height: (($(li).attr('class') == 'gallery5') ? '160' : ($(img).closest('ul').innerHeight() - $(img).data('nativeDimensions').height)) - (padding*2)
				}, 
				350
			);
			$(this).dequeue();
		}
	);
}

function gallery_mouseout(li){
	img = $(li).find('img');
	p = $(li).find('p');
	if($(img).data("origDimensions")/*  && $(img).data("nativeDimensions") && (($(img).width() > $(img).data("origDimensions").width) || ($(p).height > 0)) */){
		$(img).stop(true).clearQueue().animate(
			{
				width: $(img).data('origDimensions').width, 
				height: $(img).data('origDimensions').height 
			}, 
			200,
			function(){
				$(li).css({ 'height' : '', 'width' : '', 'position' : '', 'top' : '', 'left' : '', 'z-index' : 0});
			}
		);
		$(p).stop(true).clearQueue().animate(
			{
				height: "hide", 
				width: "hide", 
				padding: "hide", 
				margin: "hide"
			}, 
			200
		);
	}
}

function getImgDimension(img){
	$(img).data("origDimensions", {height : $(img).height(), width : $(img).width()}); //set original dimensions
	//alert($(img).data("origDimensions").width + ' x ' + $(img).data("origDimensions").height);
	
	$(img).css({ width: 'auto', height: 'auto' }); //unset resizing
	$(img).data("nativeDimensions", {height : $(img).height(), width : $(img).width()}); //get native dimensions
	$(img).css({ width: $(img).data('origDimensions').width, height: $(img).data('origDimensions').height }); //reset resizing
}

function jobsSlider(){
	list = $('div.jobs ul');
	$(list).css(
		{
			'position': 'relative',
			'overflow': 'hidden',
			'height': '35px',
			'width': '150px'
		}
	);
	
	setInvisibleItems(list);
	slideListItem(list);
	
}

function slideListItem(list){
	setTimeout(function(){
		$(list).find('li:eq(0)').animate(
			{
			    left: '-=150'
			},
			1000,
			function(){
				sendToBack(this);
			}
		).closest('ul').find('li:eq(1)').animate(
			{
				left: '-=150'
			}, 
			1000, 
			function(){
				slideListItem(list);
			}
		);
	}, 5000);
}

function setInvisibleItems(list){
	//stack all list items on the right out of sight (I'm a poet and I didn't know it)
	$(list).find('li').css(
		{
			'position' : 'absolute',
			'top' : '0',
			'left' : '150px',
			'height': '35px',
			'width': '150px'
		}
	);
	$(list).find('li:first').css('left', '0'); //put the first in place
}

function sendToBack(item){
	$(item).css('left', '150px').appendTo($(item).closest('ul'));
}



