$(document).ready(function(){

	// Featured Slideshow

	var items = jQuery('.featured .fimage img');

	var item_width =  850;

	var max_margin = items.length * item_width - item_width;

	var animstatus = false;

	var user_click = false;

	var animation_speed = 1000;

	var auto_speed = 7500;

	

	// Set the image link to the first item link

	$('.flinkarea').attr('href',$('.featured .fthumbs li:eq(0) a').attr('href'));

	

	// Thumbnail link actions

	jQuery('.featured .fthumbs a').each(function(i,link) {

		var index = i+1;

		

		// Click action

		jQuery(link).click(function(e) {

			e.preventDefault();

			user_click = true;

			

			// move it

			moveFeature(index);

		});

	});

	

	// Move it function

	function moveFeature(location) {

		if(animstatus == false) {

			animstatus = true;

			var feature = jQuery('.featured .fimage > div');

			var pos = parseInt(jQuery(feature).css('left'));

			

			// Check to see if a location is set

			if(location) {

				if(location=='next') {

					if(pos == -max_margin){

						left = 0;

					}

					else {

						left = pos-item_width;

					}

				}

				else if(location=='back') {

					if(pos == 0){

						left = -max_margin;

					}

					else {

						left = pos+item_width;

					}

				}							

				else {

					left = -(item_width*location)+item_width;

				}

				

				// Calculate the new index based off of the left value and item width

				var item = Math.abs(left/item_width);

				

				

				/*** Thumbnails ***/

				// Add active to li

				$('.fthumbs li').removeClass('active');

				$('.featured .fthumbs li:eq('+item+')').addClass('active');

				

				// Add fnum-active

				$('.fthumbs li .fnum').removeClass('fnum-active');

				$('.featured .fthumbs li:eq('+item+') a').children('.fnum').addClass('fnum-active');

				

				// Expand the newly clicked item

				$('.fthumbs li .fthumbs-bg').animate({width: 50}, 150);

				$('.featured .fthumbs li:eq('+item+') a').children('.fthumbs-bg').animate({width: '100%'}, 150);

				

				// Change the image link

				$('.flinkarea').attr('href',$('.featured .fthumbs li:eq('+item+') a').attr('href'));

				

				/*** Title and Link ***/

				// Fade the title and link and move it

				var info = $('.finfowrap > div');

				info.animate({opacity: 0},animation_speed/2,"swing",function() {

					info.css('left',(-1 * item*665));

					info.animate({opacity: 1},animation_speed/2,"swing");

				});

				

				// Animate image transtition

				feature.animate({left: left},animation_speed,"swing",function() {

					animstatus = false;

				});

			}

		}

	}

	

	// Automove/autostart

	function feature_automove() {

		if(user_click == true) {

			user_click = false;

		}

		else {

			moveFeature('next');

		}

	}

	var feature_auto = setInterval(feature_automove, auto_speed);

});