// JavaScript Document
// Floor SA: 18.01.2007 by sb
// JS-Functions for the Pamisa-searchmenu, uses prototype.js

// ENV Vars
var searchstr = new Array(3);

// --------------------------------------------------------------------------
// executes the search
function doSearch(querystring,searchstring,cid,searchid) {
	var url = '/floor/cs?'+querystring+getSearchString(searchstring,searchid)+'&action=getitemlist&ID='+cid+'&randomnumber='+Math.random();;

	//showHideLoading('show',$('cataloglist'),$('updating'));
	//$('navipage').innerHTML = '';
	
	var myAjax = new Request.HTML( {
		url: url, 
		method: 'get', 
		onComplete: function(t) {
			
		},
		update: $('cataloglist'),
		// Handle other errors
		onFailure: function(t) {
			//showErrorMessage('TestError ' + t.status + ' -- ' + t.statusText);
		}
	});
	
	myAjax.send();
	return false;
}

// --------------------------------------------------------------------------
// shows the search results
function showSearchResult(t) {
	//showHideLoading('hide','',$('updating'));
	$('cataloglist').innerHTML = t.responseText;
	//$('navipage').innerHTML = $('newnavipage').innerHTML;
	return false;
}

// --------------------------------------------------------------------------
// constructs the searchstring
function getSearchString(str,searchid) {
	if (searchstr[searchid] == str)
		searchstr[searchid] = '';
	else
		searchstr[searchid] = str;
	
	var newstr = '';
	for (var i = 0; i<searchstr.length; i++)	{
		if (searchstr[i])
			newstr = newstr+'&'+searchstr[i];
	}
	return newstr;
}

// --------------------------------------------------------------------------
// gets the content for the winetype-, millesime- and price-searchbox

function getSearchbox(querystring,cid,action,layout,type) {
	var url = '/floor/cs?'+querystring+'&action='+action+'&ID='+cid+'&layout='+layout+'&randomnumber='+Math.random();;
	//console.log(type);
	showHideLoading('show',$(type+'searchbox'),$('updating'+type));
	
	//console.log('test',$(type+'searchbox').getElement('ul'));
	$(type+'searchbox').getElement('ul').addClass('loading');
	//$(type+'searchbox').getElement('ul').setStyle('backgrounColor','#000');
	var myAjax = new Request.HTML( {
		url: url, 
		method: 'get',
		update : $(type+'searchbox').getElement('ul'),
		onComplete: function(t) {
			//console.log('content loaded',type);
			//showHideLoading('hide','',$(type+'searchbox'));
			//console.log('type before updating scroll: '+type,fscrollbars);
			$(type+'searchbox').getElement('ul').removeClass('loading');
			fscrollbars[type].update();
			//var scrollBox1 = new Scrollbar({container:$(type+'searchbox')});
			//setSearchboxResult(t,type);
			//scrollToSelectedLi();
		},
		// Handle other errors
		onFailure: function(t) {
			//showErrorMessage('TESTError ' + t.status + ' -- ' + t.statusText);
		}
	});
	
	myAjax.send();
	
	return false;
}

// --------------------------------------------------------------------------
// shows the content for the winetype-, millesime- and price-searchbox
function setSearchboxResult(t,type)	{
	$(type+'searchbox').innerHTML = t.responseText;
	showHideLoading('hide','',$('updating'+type));
}

// --------------------------------------------------------------------------
// shows the content for the winetype-, millesime- and price-searchbox
function getCellarsbox(querystring,cid,action,layout,type,pathkey,pathkeylength)	{
	//if ($('cellsearchbox').innerHTML == '')
		getSearchbox(querystring+'&pathkey='+pathkey.substr(0,8)+'0___0%',cid,action,layout,type);
}

// --------------------------------------------------------------------------
// shows the content for the winetype-, millesime- and price-searchbox
function getRegionsbox(querystring,cid,action,layout,type,pathkey,pathkeylength)	{
	//if ($('regionsearchbox') && $('regionsearchbox').innerHTML == '')
		getSearchbox(querystring,cid,action,layout,type);
}

/// Coordinates function ********************************

function getElementLeft(obj) {
	
	if (obj.offsetParent)
		return (obj.offsetLeft + getElementLeft(obj.offsetParent));
		
	else
		return (obj.offsetLeft);
}

function getElementTop(obj) {
	
	if (obj.offsetParent)
		return (obj.offsetTop + getElementTop(obj.offsetParent));
		
	else
		return (obj.offsetTop);
}

// --------------------------------------------------------------------------
// showHide loading image
function showHideLoading(action,el,loading,position)	{
	if (loading) {
	if (action == 'show')	{
		if (position == 'right')	{
			loading.style.left = (getElementLeft(el)+el.offsetWidth+10) + "px";
			loading.style.top = (getElementTop(el)+2) + "px";
			loading.style.width = '17' + "px";
			loading.style.height =  '17' + "px";
		}
		else	{
			loading.style.width = (el.offsetWidth) + "px";
			loading.style.left = (getElementLeft(el)) + "px";
			loading.style.top = (getElementTop(el)) + "px";
			loading.style.height =  (el.offsetHeight) + "px";
		}
		loading.style.visibility =  'visible';
	}
	else	{
		loading.style.visibility =  'hidden';
	}
	}
}

// -----------------------------------------------------------------
// function that highlights an li andremoves all other highlights
function highlight(el,elhighlight,noapply){
	var oldClass = elhighlight.className;
	var litab=$(el).getElementsByTagName('a');
	for(i=0;i<litab.length;i++) {
		litab[i].className="";
	}
	
	if (oldClass != 's' && noapply != '1')
		elhighlight.className="s";
}

// -----------------------------------------------------------------
// general error-handling function
function showErrorMessage(msg)	{
	alert('There has been a problem loading the desired information.\nPlease try again!\n\n'+msg);
}

// -----------------------------------------------------------------


function scrollToSelectedLi(){
	var boxes  = $A($('finder').getElementsByTagName('ul')); //$('finder').element(); getChildren().getElements('ul');
	boxes.each(function(ul) {
		var lis = $A(ul.getElementsByTagName('li'));
		lis.each(function(li) {
			if ($(li).className=='s'){
				var dim = Position.positionedOffset(li);
				top = dim[1]-84;
				var father = li.up(1);
				father.scrollTop = top;
			}
		}); //end each li
	}); //end each ul
}
