var showOptions = false;
var menuTimeout = 500;
var menuCloseTimer = 0;
var menuItem = 0;

function menuOpen() {
	menuStopTimer();
	menuClose();
	
	menuItem = jQuery(this).find('div').css('visibility', 'visible');
}

function menuClose() {
	if(menuItem) menuItem.css('visibility', 'hidden');
}

function menuTimer() {
	menuCloseTimer = window.setTimeout(menuClose, menuTimeout);
}

function menuStopTimer() {
	if(menuCloseTimer) {
		window.clearTimeout(menuCloseTimer);
		menuCloseTimer = null;
	}
}

function request(page, container) {
	document.getElementById(container).innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater(container, page, {asynchronous:true, evalScripts:true});
}

function setup_picker(control) {
	new Control.DatePicker(control, {icon: '/lib/js/datepicker/calendar.png'});
}

function setup_timepicker(control) {
	new Control.DatePicker(control, { icon: '/lib/js/datepicker/clock.png', datePicker: false, timePicker: true });
}

function thumbPreview(imageID) {
	if(imageID > 0) {
		var submit_url = '/gallery/ajax/thumbnail/'+imageID;
		var container = 'thumb_preview';
		document.getElementById(container).innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
		new Ajax.Updater(container, submit_url);
	};
}

function load_category_view(category,option,prev_category) {
	var params = Form.serialize($('option_norm'));
	
	document.getElementById('build_main').innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater('build_main', '/buildbike/ajax/category_view/'+category+'/'+option+'/'+prev_category, {asynchronous:true, parameters:params});
		
	if(showOptions == false) {
		showOptions = true;
		setTimeout(show_selected_options,3000);
	}
}

function load_option_view(category,option) {
	if(category == null || category < 1) { return false; }
	
	var params = Form.serialize($('option_norm'));
	
	document.getElementById('build_main').innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater('build_main', '/buildbike/ajax/option_view/'+category+'/'+option, {asynchronous:true, parameters:params});
	
	if(showOptions == false) {
		showOptions = true;
		setTimeout(show_selected_options,3000);
	}
}

function load_final() {
	var params = Form.serialize($('option_norm'));
	
	document.getElementById('build_main').innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater('build_main', '/buildbike/ajax/final', {asynchronous:true, parameters:params});

	if(showOptions == false) {
		showOptions = true;
		setTimeout(show_selected_options,3000);
	}
}

function email_build(email, name, org, dept, phone, ref) {
	if(email == "" || email == null) { return false; }
	document.getElementById('build_main').innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater('build_main', '/buildbike/ajax/email_build/'+escape(email)+'/'+escape( name)+'/'+escape(org)+'/'+escape(phone)+'/'+escape(dept)+'/'+escape(ref), {asynchronous:true, evalScripts:true});
}

function add_customer(user, pass, newPass) {
	if(user == "" || user == null) { return false; }
	if(pass == "" || pass == null) { return false; }
	if(newPass == "" || newPass == null) { return false; }
	if(pass != newPass) { return false; }
	
	var ampcharcode= '%26';
	var serializeOpts = "user="	+ escape(user) + unescape(ampcharcode) + "pass=" + escape(pass) + unescape(ampcharcode) + "new_pass=" + escape(newPass);
	var params = { method : 'post', parameters : serializeOpts };
	
	document.getElementById('build_main').innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater('build_main', '/buildbike/ajax/add_customer_login', {asynchronous:true, parameters:serializeOpts});
}

function add_customer_details(aName, cName, org, dept, email, phone) {
	document.getElementById('build_main').innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater('build_main', '/buildbike/ajax/add_customer_details/'+escape(aName)+'/'+escape(cName)+'/'+escape(org)+'/'+escape(dept)+'/'+escape(email)+'/'+escape(phone), {asynchronous:true, evalScripts:true});
}

function email_auth(email, name, org, phone, initials, birthmonth) {
	if(email == "" || email == null) { return false; }
	if(initials == "" || initials == null) { return false; }
	if(birthmonth == "" || birthmonth == null) { return false; }
	
	document.getElementById('build_main').innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater('build_main', '/buildbike/ajax/email_auth/'+escape(email)+'/'+escape( name)+'/'+escape(org)+'/'+escape(phone)+'/'+escape(initials)+'/'+escape(birthmonth), {asynchronous:true, evalScripts:true});
}

function show_email_form() {
	request('/buildbike/ajax/email_form/', 'build_main');
}

function show_auth_form() {
	request('/buildbike/ajax/auth_form/', 'build_main');
}

function show_add_customer_form() {
	request('/buildbike/ajax/sign_up/', 'build_main');
}

function set_continue(id, sub, sid, sact, grp) {
	document.getElementById('build_navigation_buttons').innerHTML = '<img src="/img/ajax_loader.gif"> <B style="font-face: Verdana; font-size: 12px;">Loading...</B>';
	new Ajax.Updater('build_navigation_buttons', '/build_a_bike/set_continue/'+id+'/'+sub+'/'+sid+'/'+sact+'/'+grp, {asynchronous:true, evalScripts:true});	
}

function show_selected_options() {
	request('/buildbike/ajax/selected_options/', 'build_detail_list');
	showOptions = false;
}

function show_step_icons(category) {
	request('/buildbike/ajax/step_option_icons/'+escape(category),'build_option_icons');
}

function show_status_bar(catId) {
	category = convert_category_to_sec(catId);
	request('/buildbike/ajax/status_bar/'+escape(category),'statusBarDiv');
}

function resetBuild() {
	new Ajax.Updater('emptyDiv', '/buildbike/ajax/reset_build/', {asynchronous:true, evalScripts:true});
	load_category_view('1');
}

function convert_category_to_sec(catId) {
	switch(catId) {
		case "1":
			return "electrical";
		case "2": 
			return "bike_body";
		case "3":
			return "mechanical";
		case "4":
			return "accessories";
		case "5":
			return "wheels";
		case "6":
			return "graphics";
		case "7":
			return "final";
		default:
			return "default";
	}
}
