$(function() {
	if ($.address != undefined) {
		$.address.init(function(event) {
			$('#top-navigation ul li a').address();
			$('#product-navigation a').address();
			$('#flyout a').address();
			$('#sitemap a').address();
		}).change(function(event) {
			path = event.pathNames[0] == null ? document.location.pathname.replace('/','') : event.pathNames[0];
			$.ajax({
				type: "GET",
				url: "/" + path,
				data: "ajax=1",
				dataType: "json",
				success: function (response) {
					if (ajax) {
						document.title = response.title;
						$('#wrapper').html(response.content);
					}

					// reactivate ajax calls
					ajax = 1;

					if ($.browser.msie && parseInt($.browser.version) < 7) { // do nothing if IE 6
					} else {
						$('#left-fruit').fadeIn(800, function() {
							$('#hichew-product').show('slide', { direction: 'left', distance: '300'}, 500);
						});
					}

					$('div.scrollable').jScrollPane({showArrows:true, arrowSize:25, scrollbarWidth:25, scrollbarMargin:10});
				}
			});
			$.address.tracker($.address.value());
		});
	} else {
		$('div.scrollable').jScrollPane({showArrows:true, arrowSize:25, scrollbarWidth:25, scrollbarMargin:10});
	}
	
	$('#product-navigation a').hover(function() {
		if ($.browser.msie && parseInt($.browser.version) < 7) { // do nothing if IE 6
		} else {
			$('#flyout').removeClass().hide();
			$('#flyout').addClass($(this).attr('class')).show();
			$('#flyout a').attr('href', '/' + $(this).attr('class'));
		}
	});
	
	$(document).mousemove(function(e){
		leftx = (($(document).width() - $('#container').width()) / 2) + 640;
		rightx = leftx + 400;
		topy = 45; bottomy = 186;
		if (e.pageX < leftx || e.pageY < topy || e.pageX > rightx || e.pageY > bottomy) $('#flyout').hide();
	});

	if (!$.cookie('survey')) {
		$('#dialog').dialog({closeText: 'X', height: 100});
		$('#top-navigation ul li a').click(function() { $('#dialog').dialog('close'); });
		$('#product-navigation a').click(function() { $('#dialog').dialog('close'); });
		$('#flyout a').click(function() { $('#dialog').dialog('close'); });
		$('#sitemap a').click(function() { $('#dialog').dialog('close'); });
		$('.ui-dialog-titlebar-close').click( function() { $('#dialog').dialog('close'); $.cookie('survey', 'no', {expires: 7} ); });
		$('a#yes').click( function() { $('#dialog').dialog('close'); $.cookie('survey', 'no', {expires: 7} ); });	
		$('a#close').click( function() { $('#dialog').dialog('close'); $.cookie('survey', 'no', {expires: 7} ); return false; });
	} else {
		$('#dialog').hide();
	}
});
