var rotator_items = new Array();
var current_rotator_item;
var rotator_timer;
var ONCLICK_FOO_STR = "event.returnValue = false; event.preventDefault() ; return false;";


$(document).ready( function(){
	
	/*
	rotator_timer = $.timer(5000, function(timer){
		
	//	incrementRotator();
	
	});
	*/
	
	//preload images
	$(".rotator_image_thumb img").each(function(){
		jQuery("<img />").attr("src", $(this).attr("data-fullimage"));
	
		
	})
	
	$(".rotator_main_image .rotator_block").animate({ opacity: 1.0 }, 1 ); 
	
	//$(".rotator_image_thumb ul li:first").addClass('active');
	makeRotatorItemActive($(".rotator_image_thumb ul li:first"));
	
	$(".rotator_image_thumb ul li").each(function(){
		
		rotator_items.push($(this));
	})
	
	
	$(".rotator_image_thumb ul li").click(function(){
		
	//	rotator_timer.stop();
		makeRotatorItemActive($(this));
		
		
	}).hover(function(){
		
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	
	

	$(".rotator_image_thumb a").click(function(event){
		makeRotatorItemActive($(this).parent().parent());
		event.returnValue = false; 
		event.preventDefault() ;
		return false;
		
	//	rotator_timer.stop();
	})

	
	

	resizeMainImageArea();
	
	
});


function resizeMainImageArea(){
	
	var rh_diff = $(".rotator_image_thumb").height() - $(".rotator_main_image").height();
	var px_change = jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 ? -1 : 1;
	
	if(rh_diff > 0){
		$(".rotator_main_image").height($(".rotator_main_image").height()+rh_diff - px_change);		
	}else{
		
		$(".rotator_main_image .rotator_img_wrapper").height($(".rotator_main_image .rotator_img_wrapper").height() + rh_diff);
	
		
		
		$(".rotator_main_image").height($(".rotator_main_image").height() + rh_diff - px_change);
		
		
		
		
	}
	
}

function isEmpty(str){
	return (str == undefined || str==="" || str==null);
}

function incrementRotator(){
	
	var inc = 0;
	for(var i=0; i < rotator_items.length; i++){
		if(rotator_items[i] == current_rotator_item){
			inc = i;
		}
	}
	
	inc = (inc+1)%rotator_items.length;
	
	makeRotatorItemActive(rotator_items[inc]);
	
}

function makeRotatorItemActive(item){
	
	current_rotator_item = item;
	
	var imgAlt = item.find('img').attr("alt");
	var imgFullSrc = !isEmpty(item.find('img').attr("data-fullimage")) ? item.find('img').attr("data-fullimage") : item.find('img').attr("src");
	var href = item.find("a").attr("href");
	
	
	
	var imgDesc = item.find('.rotator_block').html();
//	var imgDescHeight = $(".rotator_main_image").find('.rotator_block').height();
	
	
	
	if (item.hasClass("active")){ //should use 'is' but want to maintain compat with jquery 1.2
		return false;
	}else{
		
		$(".rotator_image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
	//	$(".rotator_image_thumb").find("a").attr('onclick', ONCLICK_FOO_STR); 
	//	$(".rotator_image_thumb").find("a").click(function(){ return false;}); // Remove click-to-url behavior for nonactive items
			
		
		$(".rotator_main_image div").click(function(){
			window.open(href, "_self");
			//redundant, but want to make sure it works across all browsers
		});
		
		$("#main_rotator_block").hide().empty().html(imgDesc).fadeIn(200);
	
		
		$(".rotator_main_image img").hide().attr({ src: imgFullSrc , alt: imgAlt}).fadeIn(100); 
			
		$("#rotator_main_image_link").attr("href", $(item).find("a").attr("href") );
		
		//deal with non active items
	
		
		item.addClass('active');
		
		/*
		
		item.find("a").attr("onclick", ""); //remove code that cancels href follow through
		
		
		item.find("a").click(function(){
			do_trace("1");
			window.open(href, "_self");
		
		});*/
		
	}
	

	
	

	
	//adjust heights
	

	
	return false;

}

