/* Javascript functions for Specialty Produce Website */

var listManager;
var currentListID = 1;

function clearForm(){
//setting the value:
document.loginForm.username.value = document.loginForm.usernameTemp.value;
document.loginForm.usernameTemp.value = "";
document.loginForm.pw.value = document.loginForm.pwTemp.value;
document.loginForm.pwTemp.value = "";
document.loginForm.submit;
}

/* leftTop search area */
/* IE uses 'className' in the setAttribute and everyone else uses 'class' and they ignore each other so we call both... */
function updateLink(clickedLink, listID) {
	$$('#clickList p').each(function(el) {
		el.setAttribute("className", "linkList");
		el.setAttribute("class", "linkList");
	});
	if (listID > 0) {
		clickedLink.setAttribute("className", "currentList");
		clickedLink.setAttribute("class", "currentList");
		$('searchStr').setProperty('value', '');
	}
	if (listID != currentListID) {
		currentListID = listID;
		var scrollCookie = Cookie.write('SPCatScroll', 0, {
			duration : 0
		});
	}
	var catCookie = Cookie.write('SPCatalog', listID + ":"
			+ $('searchStr').getProperty('value'), {
		duration : 0
	});
	listManager.setOptions( {
		data : {
			'id' : listID,
			'searchStr' : $('searchStr').getProperty('value')
		}
	}).send();
}

// Call a produce page and POST pass the current location of the scroll
function setProdListScroll() {
	var scroll = $('leftBottom').getScroll();
	var scrollCookie = Cookie.write('SPCatScroll', scroll.y, {
		duration : 0
	});
}

// For tabbed content pages
var showTabContent = function() {
	$$('.hiddenTab').setStyle('display', 'none');
	var choice = this.get('id');
	$(choice + 'Content').setStyle('display', 'block');
	$$('.menuItem').removeClass('tab-selected');
	$(choice).addClass('tab-selected');
};

// Run when the page loads...
window.addEvent('domready', function() {

	if ($('nav'))
		$('nav').MooDropMenu( {
			onOpen : function(el) {
				el.fade('in');
			},
			onClose : function(el) {
				el.fade('out');
			},
			onInitialize : function(el) {
				el.fade('hide').set('tween', {
					duration : 250
				});
			}
		});

	if ($('searchStr')) {
		$('searchStr').addEvent('keydown', function(evt) {
			if (evt.key == 'enter')
				updateLink($('searchStr'), 0);
		});
	}

	listManager = new Request( {
		method : 'post',
		url : 'includes/getProdList.php',
		data : {
			'id' : 0,
			'searchStr' : ''
		},
		onRequest : function() {
			$('leftBottom').set('html',
					'<div id="leftList"><p><b>Processing...</b></p></div>');
		},
		onSuccess : function(responseText) {
			$('leftBottom').set('html', responseText);
		}
	});

	var listScroll = Cookie.read('SPCatScroll');
	if (listScroll == null)
		listScroll = 0;
	if ($('leftBottom'))
		$('leftBottom').scrollTo(0, listScroll);
});
