function processWarningDialogUser() {
	$('#age-verification-dialog').fadeOut();
	location.href = '/ex/login?do=login';	
}

function processWarningDialog18Ok() {

	$('#age-verification-dialog').jqmHide();

	$.ajax({
	url: "/ax/legalAgeOk",
	dataType: "json",
	async: true,
	success: function(data){
			if(data.code != 0) {
				ajaxError(data.code, "processWarningDialog18Ok");
			}
		}
	});
}

/**
  * Redirect to home with setting the prefs cookie to safe.
  */
function processWarningDialogFilter() {
	$('#age-verification-dialog').jqmHide();
	location.href = "/ex/home?do=home&prefs=true";
}

function showContentPrefsDialog() {
	// Create and show the modal dialog.
	$('#content-prefs-dialog').jqm(
		{
			modal:true, 
			overlay: 80,
			// Fade out on hide.
			// The onHide callback is dumb in that it interrupts the normal hide process,
			// hence we need to manually remove the overlay.
			onHide: function(hash) { hash.w.fadeOut(1000, function(){ hash.o.remove(); }); },
			// Fade in on show.
			// The onShow callback is dumb in that it interrupts the normal hide process,
			// hence we need to manually show the window.
			onShow: function(hash) { hash.w.show(); hash.o.fadeIn(1000); }
		}
	);
	$('#content-prefs-dialog').jqmShow();
	
	// Create the double slider in the content preferences panel.
	createHardnessDoubleSlider('select#hardness-slider-min', 'select#hardness-slider-max', updateImagesInScope);
	updateImagesInScope();
	
}

var updateImagesInScope = function() {
	
	// Serealize all the prefs into a single string just like the prefs cookie does.
	// Keep this code in sync!
	
	var prefs = "";

	var allOrientationBoxes = $('input[@name^=contentOrientation]');
	for (var i = 0; i < allOrientationBoxes.length; i++) {
		prefs += allOrientationBoxes[i].checked ? '1' : '0';
	}
	
	var minCore = $("#hardness-min").val();
	prefs += minCore < 10 ? '0' + minCore : minCore;
	var maxCore = $("#hardness-max").val();
	prefs += maxCore < 10 ? '0' + maxCore : maxCore;

	$.ajax({
	url: "/ax/imagesInScope",
	data: {prefs:prefs},
	dataType: "json",
	async: true,
	success: function(data){
			if(data.code == 0){
				var selected = data.response.selected;
				if (!selected) selected = 0;  // somehow, json sends 0 as null string;
				var total = data.response.total;
				var pctOfTotal = 100*selected/total;
				$("#imagesSelected").html(selected + ' (' + pctOfTotal.toFixed(2) + '%)');
			}
			else {
				ajaxError(data.code, "imagesInScope");
			}
		}
	});

}

/**
  * Process the preference form.
  */
function onPrefsButtonCancel() {
	$('#content-prefs-dialog').jqmHide();
	return false;
}
	
function onPrefsButtonSubmit() {
	$('#content-prefs-dialog').jqmHide();
	
	// Reinable all the orientation check boxes, just in case we're down to the last
	// one and it's disabled.  (Disabled form input elements don't get submitted to the server.)
	$('input[@name^=contentOrientation]').attr("disabled", false);
		
	// Manually submit the form for IE.
	$("#prefsForm").submit();	
}

/**
  * Check if only one checkbox in a set remains checked and if so, disable it
  * so the user cannot uncheck it. Note that disabled input element don't get submitted to 
  * the server, so we'll have to re-enable them at submit time.
  */
function disableLastBox(name) {
	var allCheckBoxes = $('input[@name^=' + name + ']');
	var numberChecked = 0;
	var lastChecked;

	for (var i = 0; i < allCheckBoxes.length; i++) {
		if (allCheckBoxes[i].checked) {
			numberChecked++;
			lastChecked = allCheckBoxes[i];
		}
	}
	if (numberChecked == 1) {
		lastChecked.setAttribute("disabled", true);
	}		
};

/********************************** SEARCH dropdown menu *******************************************/
var hideAction;
var showSearchMenu = function() {
	clearTimeout(hideAction);
	$('#search-dropdown-panel').show();

	// Keep visible if hovering over it.
	$('#search-dropdown-panel').bind(
		"mouseenter", 
		function(){
			clearTimeout(hideAction);
			$('#search-dropdown-panel').show();
		});
    $('#search-dropdown-panel').bind("mouseleave", hideSearchMenu);
}

function hideSearchMenu() {
	// delay the hiding of the dropdown panel so that if the cursor is moved over the panel it's still there.
	hideAction = setTimeout(function () {$('#search-dropdown-panel').hide();}, 200);
}
/********************************** BROWSE dropdown menu *******************************************/
var browsePanelHover = false;
var browseMenuHover = false;
var browseMenuInterval;
function showBrowseMenu() {
	$('#browse-dropdown-panel').show();
	browseMenuHover = true;
	// Keep visible if hovering over the menu item.
	$('#browseMenuItem').hover(
		function() {browseMenuHover = true;},
		function() {browseMenuHover = false;}
	);	
	// Keep visible if hovering over the menu panel.
	$('#browse-dropdown-panel').hover(
		function() {browsePanelHover = true;},
		function() {browsePanelHover = false;}
	);
	browseMenuInterval = setInterval(function() {hideBrowseMenu();}, 2000);
	
	// Highlight the main menu item while displaying the dropdown, unless it's the current tab.
	if (! $('#browseMenuItem').hasClass('activetab')) {
		$('#browseMenuItem a').css("background", "#4780b8 url(/images/hd_tab_hover.png) repeat-x bottom");
	}
}

function hideBrowseMenu() {
	if (browsePanelHover||browseMenuHover) return;
	clearInterval(browseMenuInterval);
	$('#browse-dropdown-panel').hide();
	// Un-highlight the main menu item while displaying the dropdown, unless it's the current tab
	if (! $('#browseMenuItem').hasClass('activetab')) {
		$('#browseMenuItem a').css("background", "#369 url(/images/hd_md_bg.png) repeat-x bottom");
	}
}

/****************************** MY UNSEEEN dropdown menu *******************************************/
var myPanelHover = false;
var myMenuHover = false;
var myMenuInterval;
function showMyMenu() {
	$('#my-dropdown-panel').show();
	myMenuHover = true;
	// Keep visible if hovering over the menu item.
	$('#myMenuItem').hover(
		function() {myMenuHover = true;},
		function() {myMenuHover = false;}
	);	
	// Keep visible if hovering over the menu panel.
	$('#my-dropdown-panel').hover(
		function() {myPanelHover = true;},
		function() {myPanelHover = false;}
	);
	myMenuInterval = setInterval(function() {hideMyMenu();}, 2000);

	// Highlight the main menu item while displaying the dropdown, , unless it's the current tab.
	if (! $('#myMenuItem').hasClass('activetab')) {
		$('#myMenuItem a').css("background", "#4780b8 url(../images/hd_tab_hover.png) repeat-x bottom");
	}
}

function hideMyMenu() {
	if (myPanelHover||myMenuHover) return;
	clearInterval(myMenuInterval);
	$('#my-dropdown-panel').hide();
	// Un-highlight the main menu item while displaying the dropdown, unless it's the current tab
	if (! $('#myMenuItem').hasClass('activetab')) {
		$('#myMenuItem a').css("background", "#369 url(../images/hd_md_bg.png) repeat-x bottom");
	}		
}

/********************************** Page load handler ****************************************/
$(document).ready(function(){

	// Display adult warning dialog, if required.
	if (displayAdultWarning) {
		$('#age-verification-dialog').jqm(
			{
				modal:true, 
				overlay: 80,
				// Fade out on hide.
				// The onHide callback is dumb in that it interrupts the normal hide process,
				// hence we need to manually remove the overlay.
				onHide: function(hash) { hash.w.fadeOut(1000, function(){ hash.o.remove(); }); },
				// Fade in on show.
				// The onShow callback is dumb in that it interrupts the normal hide process,
				// hence we need to manually show the window.
				onShow: function(hash) { hash.w.show(); hash.o.fadeIn(1000); }
			}
		);
		$('#age-verification-dialog').jqmShow();
	}

	$('#prefs-button-submit').click(onPrefsButtonSubmit);
	$('#prefs-button-cancel').click(onPrefsButtonCancel);
	

	// Create click events for the orientation check boxes.
	disableLastBox('contentOrientation');	
	$('input[@name^=contentOrientation]').click(
		function() {
			if ( ! $(this).is(":checked") ) {
				disableLastBox('contentOrientation');
			}
			else {
				$('input[@name^=contentOrientation]').attr("disabled", false);
			}
			
			updateImagesInScope();
		});
	
	// Hover events for the main menu dropdowns
	$('#browseMenuItem').hover(showBrowseMenu, hideBrowseMenu);
	$('#myMenuItem').hover(showMyMenu, hideMyMenu);
	$('#search-dropdown-button').hover(showSearchMenu, hideSearchMenu);
});
