var newsitems;
var curritem=0;
var iPause=0;

$(document).ready(function(){
	/* News Ticker */
	var tickerSelector = "ul#ticker li";
	newsitems = $(tickerSelector).hide().hover(
		function(){
			$(this).addClass("hovered");
			iPause=1;
		},
		function(){
			$(this).removeClass("hovered");
			iPause=0;
		}
	).filter(":eq(0)").show().add(tickerSelector).size();
	
	setInterval(ticknews,4000);
	
	/* Homepage Image Cycle */
	var delayTime = -500;
	var delayTimeIncrement = 300;
	
	$('.imageSlider').each(function() {
		var maxWidth = 10;
		delayTime += delayTimeIncrement;
		
		$(this).children().each(function() {
			if ($(this).attr('width') > maxWidth) {
				maxWidth = $(this).attr('width');
			}
		});
		
		$(this).css('width', maxWidth + 'px');
		
		$(this).cycle({
			fx:      'fade',
			speed:   1000,
			timeout: 5000,
			delay:   delayTime
		});
		
		$(this).show();
	});
	
	$("area.popup").bind('click', function(){
		//window.open($(this).attr('href'), 'salesMap', 'width=650;height=650,menu=no,scrollbars=yes,resizable=yes,location=no,status=yes');
		//return false;
	});
	
	$(".productSelectInput input:checkbox, .productSelectInput input:radio").bind('click', function(){
		updateProductSelector();
	});
	
	updateProductSelector();
});

function updateProductSelector()
{
	var classes = [];
	var showItems = [];
	var hideItems = [];
	var selectedLocation = $(".locationOptions .productSelectInput input:radio:checked").val();
	var applicationItems = [];
	$(".applicationOptions .productSelectInput :checked").each(function(){ applicationItems.push($(this).attr('id')); });
	
	$("#secondaryContent .productList li").each(function(){
		var id = $(this).attr('id');
		
		if ($(this).attr('class')) {
			var itemClass   = $(this).attr('class');
			var classes = itemClass.split(/ /);
			
			var addItem = true;
			
			for (i=0; i<applicationItems.length; i++) {
				if ($(this).hasClass(applicationItems[i])) {
					if ($(this).hasClass(selectedLocation)) {
					}
					else {
						var addItem = false;
						continue;
					}
				}
				else {
					var addItem = false;
					continue;
				}
			}
		}
		
		if (addItem) {
			showItems.push(id);
		}
		else {
			hideItems.push(id);
		}
	});
	
	for(i=0; i<showItems.length; i++){
		id = showItems[i];
		$("li#"+id+" a").fadeTo("fast", 1);
	}
	
	for(i=0; i<hideItems.length; i++){
		id = hideItems[i];
		$("li#"+id+" a").fadeTo("fast", 0.33);
	}
}

function ticknews() {
	if (iPause==0){
		$("#ticker li:eq("+curritem+")").fadeOut("slow",function(){$(this).hide();});
		curritem = ++curritem%newsitems;
		$("#ticker li:eq("+curritem+")").fadeIn("slow");
	}
}
