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

// assign elements to variables like this
img_slider.img = jQuery('#slide_menu li');

// when calling a function within an object, it should look like this
img_crtl.init();

});

// this is one object that we attach either variables or functions to
// object declarations

var img_slider = {};
var img_crtl = {};

// Controls
var fadeSpeed = 500 //transition time;
var slideShowInterval;
//inititialize
img_crtl.init = function() {

	img_crtl.setup();

	var config = {
		over:img_crtl.overImg,
		timeout: 500,
		out:img_crtl.outImg
	};
	
		img_crtl.hoverImg(config);

	// activate thumbnail
	jQuery('#slide_sm ul li:first-child').addClass("active_th");
	
	slideShowInterval = setInterval('img_crtl.runSlideShow()',showSpeed);
	img_crtl.th_click();
};








// FUNCTIONS



img_crtl.th_click = function() {


	jQuery('#slide_sm ul li').hoverIntent(directHover,directHoverOut);
	
	function directHoverOut() {
	
	
	
	}
	
	
	function directHover(){
		
		
		var newx  = jQuery(this).attr('class');

		newx = newx.substr(0,newx.length-3);


			clearInterval(slideShowInterval);
			
			swapImg(newx);
			swapTh(newx);
	
	
	};


}





img_crtl.setup = function() {

	jQuery('#slide_lg').children().each(function(){
	
		jQuery(this).hide().css('opacity','0');
	
	});
	
	// set the fist slide li image item to full opacity
	jQuery('#slide_lg').children().first().show().css('opacity','1');

	// run through each slide_sm li child and set b&w img opacity to .5
	// while hiding colored img
	jQuery('#slide_sm ul').children('li').each(function(){
	
		jQuery(this).children('img').css('opacity','.5');
	});

	//show first img 
	jQuery('#slide_sm ul').children().first().children('img').css('opacity','1');
	
	// set bg color of right col to selected
	jQuery('#slide_sm ul').children().first().css('border-color','#ffd307');
}

img_crtl.runSlideShow = function() {



if(jQuery('.active').attr('id') !== jQuery('#slide_lg a').last().attr('id'))
{
	var newx  = jQuery('.active').next('a').attr('id');
}
else
{
	var newx  = jQuery('#slide_lg a').first().attr('id');
}



swapImg(newx);
swapTh(newx);
}

// calls hoverIntent plugin for the left menu
img_crtl.hoverImg = function(config) {
	img_slider.img.hoverIntent(config);
}

// mouseover event
img_crtl.overImg = function() {

	jQuery(this).animate({opacity: .50}, 250);

	var li_class = jQuery(this).attr('class');
}

// mouseout event
img_crtl.outImg = function() {

	jQuery(this).animate({opacity: 1.0}, 250);
	
}

// function swaps main image
function swapImg(newx){

	var old = jQuery('.active').attr('id');
	old = old.replace(" active","");

	if(old != newx){
		jQuery('#'+old).removeClass('active');
		jQuery('#'+newx).addClass('active');

		jQuery('#'+newx).show().animate({opacity: 1.0}, fadeSpeed, function() {
			jQuery('#'+old).animate({opacity: 0}, fadeSpeed,function(){
			jQuery(this).hide();
			});
		});
	}
}

// function swaps thumbnail on the right
function swapTh(newx){

	var old = jQuery('.active_th').attr('class');
	old = old.replace(" active_th","");
	
	newx = newx.replace(" active","");

	if (!jQuery('.'+newx+'_th').hasClass('active_th')){
		jQuery('.'+old).removeClass('active_th');
		jQuery('.'+newx+'_th').addClass('active_th');
	
		//show colored image and hide b&w
		jQuery('.'+newx+'_th img').animate({opacity: 1.0}, fadeSpeed, function() {
		
		});
	
		//show b&w image and hide colored
		jQuery('.'+old+' img').animate({opacity: .5}, fadeSpeed, function() {
		});
	
		jQuery('.'+newx+'_th img').parent().stop().animate({borderTopColor:"#ffd307",borderRightColor:"#ffd307",borderBottomColor:"#ffd307",borderLeftColor:"#ffd307"},fadeSpeed);
		jQuery('.'+old).stop().animate({borderTopColor:"#ffffff",borderRightColor:"#ffffff",borderBottomColor:"#ffffff",borderLeftColor:"#ffffff"},fadeSpeed);
		
	}
}
