
var MyAccount = new Class({
    Implements: [Options, Events],
    options:{
		'pins': []
	},

	initialize: function(options){
		this.setOptions(options);
		// this.initPins(this.options.pins);
	},

	pins: [],

	initPins: function(pins){
		//$$(pins).each(function(pin){
		pins.each(function(pin){
			this.pins.include($(pin));
			togglePin(pin, false);
		}, this);
	},

	multiAct: function(sListType, sType, sTodo, aItemsId, sRedirectTo){

		var bDoReload = false;

		var qrs = MyAccountConfig.toQueryString() + '&act=' + sTodo +'&type=' + sType + '&aids=' + aItemsId;
		var req = new Request.JSON({
			method: 'post',
			url: MyAccountConfig.rpcUrl,
			encoding: MyAccountConfig.rpcEncoding,
			onComplete: function(objJson){

				if(objJson.result == 'OK') {
					if (objJson.aids && objJson.aids.length > 0) {
						objJson.aids.each(function(item, index){
							// var pin = $('pin_aid_' + item);
							var cbm = $('fld_cbm_' + item);
							if (sTodo=='add') {
								cbm.set('checked', false);
								/*
								if (sListType == 'ausstellerliste') {
									pin.addClass('pinned');
									togglePin(pin, false);
								}
								*/
							}
							if (sTodo=='remove') {
								cbm.set('checked', false);
								/*
								if (sListType == 'ausstellerliste') { // (bisher) in ausstellerliste NICHT vorgesehen
									pin.removeClass('pinned');
									togglePin(pin, false);
								}
								*/
								if (sListType == 'merkliste') { // in pers. merkliste kein toggle icon, stattdessen page reload
									bDoReload = true;
								}
							};

						});
					}

					if (bDoReload) {
						document.location.href = document.location.href;
					} else {
						if (sRedirectTo) {
							document.location.href = sRedirectTo + '&msh=1';
						// mkoverlay(MyAccountHint.textHint, MyAccountHint.textCloser, MyAccountHint.styleCloser);
						}
						else if (MyAccountConfig.rpcAccountMode == 'session') {
							if (MyAccountConfig.sessionHintShown != true) {
								mkoverlay(MyAccountHint.textHint, MyAccountHint.textCloser, MyAccountHint.styleCloser);
								MyAccountConfig.sessionHintShown = true;
							}

						}
					}
				}
			}
		});
		req.send(qrs);
	}

});


function togglePin(pin, bShowHint) {

	if (pin.hasClass('pinned')) {
		pin.setStyle('cursor', 'auto');
		pin.set('src', 'assets/img/icn_merken_heller.gif');
		pin.removeEvents('click');
	} else {
		pin.removeClass('pinned');
		pin.setStyle('cursor', 'pointer');
		pin.set('src', MyAccountConfig.icnPinSrc);
		pin.addEvent('click', function(e){
			e.stop();
			var qrs = MyAccountConfig.toQueryString() + '&act=add&type=aussteller&aids=' + pin.get('id');
			var req = new Request.JSON({
				method: 'post',
				url: MyAccountConfig.rpcUrl,
				encoding: MyAccountConfig.rpcEncoding,
				onComplete: function(objJson){
					if(objJson.result == 'OK') {
						pin.addClass('pinned');

						if (MyAccountConfig.rpcAccountMode == 'session' || bShowHint==true) {
							if (MyAccountConfig.sessionHintShown != true) {
								mkoverlay(MyAccountHint.textHint, MyAccountHint.textCloser, MyAccountHint.styleCloser);
								MyAccountConfig.sessionHintShown = true;
							}
						}

						togglePin(pin, bShowHint);
					}
				}
			});
			req.send(qrs);
		});
	}

}



function allAction(sListType, sType, sAct, sRedirectTo){
	var sel = [];
	// $$('#ausstellerliste input.cbm').each(function(item, index){
	$('ausstellerliste').getElements('input.cbm').each(function(item, index){
		sel.include(item.id);
	});
	if (sel.length > 0){
		oMyAccount.multiAct(sListType, sType, sAct, sel, sRedirectTo);
	}
	return false;
}

function multiAction(sListType, sType, sAct, sRedirectTo){
	var sel = [];
	// $$('#ausstellerliste input.cbm').each(function(item, index){
	$('ausstellerliste').getElements('input.cbm').each(function(item, index){
		if (item.checked) {
			sel.include(item.id);
		}
	});

	if (sAct=='export_all' || sAct=='export_selected'){
		if (sel.length > 0 || sAct == 'export_all'){
			var frm = $('frmSearch');
			if (frm){
				var frmAct = frm.get('action');
				frm.set('action', MyAccountConfig.exportUrl);
				frm.set('target', '_self');
				// wegen unseres Lieblings-Browsers per document.frmSearch...
				document.frmSearch.todo.value = sListType + '_' + sAct;
				document.frmSearch.submit();
				frm.set('action', frmAct);
				frm.set('target', '_self');
				document.frmSearch.todo.value = '';
			}
		} else {
			alert(MyAccountConfig.hintNoSelection);
		}
	} else if (sAct=='print_all' || sAct=='print_selected') {
		if (sel.length > 0 || sAct == 'print_all'){
			var frm = $('frmSearch');
			if (frm){
				var frmAct = frm.get('action');
				frm.set('action', frmAct + '&mode=print');
				frm.set('target', '_new');
				// wegen unseres Lieblings-Browsers per document.frmSearch...
				document.frmSearch.todo.value = sListType + '_' + sAct;
				document.frmSearch.submit();
				frm.set('action', frmAct);
				frm.set('target', '_self');
				document.frmSearch.todo.value = '';
			}
		} else {
			alert(MyAccountConfig.hintNoSelection);
		}

	}	else {
		if (sel.length > 0){
			oMyAccount.multiAct(sListType, sType, sAct, sel, sRedirectTo);
		} else {
			alert(MyAccountConfig.hintNoSelection);
		}

	}
	return false;
}

function act(sTodo){
	var frm = $('frmSearch');
	if (sTodo=='search') {
		frm.todo.value = frm.selids.value = '';
		frm.submit();
		return true;
	}

	return false;
}

function searchReset(elForm){
	if ($('fld_s_text')) $('fld_s_text').value = '';
	if ($('fld_s_name')) $('fld_s_name').value = '';
	if ($('fld_s_stand')) $('fld_s_stand').value = '';
	if ($('fld_s_zipcode')) $('fld_s_zipcode').value = '';
	if ($('fld_s_city')) $('fld_s_city').value = '';
	if ($('fld_s_nomsel')) $('fld_s_nomsel').value = '';

	if ($('fld_s_country')) $('fld_s_country').selectedIndex = 0;
	for (var n = 1; n<=4; n++) {
		if ($('fld_s_n'+n)) $('fld_s_n'+n).selectedIndex = 0;
	}

	var frm = $(elForm);
	if (frm) {
		frm.todo.value = 'searchreset';
		frm.selids.value = '';
		frm.submit();
	}

	return false;
}

function showNomenTree(sListType)
{
	var elContainer = $('nomentree_container');
	var elNomentree = $('nomentree');
	var frmPos = $('frmSearch').getPosition();
	var frmSize = $('frmSearch').getSize()
	var winSize = $(window).getSize();

	var qrs = MyAccountConfig.toQueryString() + '&act=show&type=nomenklatur&listtype=' + sListType;

	var req = new Request.HTML({
		method: 'post',
		url: MyAccountConfig.rpcUrl,
		encoding: MyAccountConfig.rpcEncoding,
		onComplete: function(respTree, respElements, respHTML, respJavaScript){

			elContainer.setStyle('position', 'absolute');
			//elContainer.setStyle('left', (frmPos.x - 15) + 'px');
			elContainer.setStyle('left', '200px');
			elContainer.setStyle('top', (frmPos.y -2) + 'px');
			//elContainer.setStyle('width', (frmSize.x + 30) + 'px');
			elContainer.setStyle('width', 585 + 'px');
			elContainer.setStyle('height', (winSize.y - frmPos.y - 30) + 'px');
			elContainer.setStyle('background-color', '#FFFFFF');
			elContainer.setStyle('border', '1px solid #E8E8E8');
			elContainer.setStyle('display', 'block');
			elContainer.setStyle('visibility', 'visible');

			elNomentree.set('html',respHTML);

		}
	});
	req.send(qrs);

	// return false;
}

function nomenSearch() {
	var sFor = $('fld_search_keyword').get('value');

	if (sFor.length) {
		var sSearch = sFor.toLowerCase();
		$$('div.nom').each(function(item, index){
			if (item.getParent().hasClass('highlight')) item.getParent().removeClass('highlight');
			if (item.get('text').test(sSearch, 'i')) {
				item.getParent().addClass('highlight');
			}

		});
	} else {
		return false;
	}

	return false;
}

function nomenClose() {
	var elContainer = $('nomentree_container');
	var elSearch = $('search_keyword');
	if (elSearch) elSearch.set('value', '');
	if (elContainer) {
		elContainer.setStyle('display', 'none');
		elContainer.setStyle('visibility', 'hidden');
	}
	return false;
}


// save display_options: ausstellerliste_anzeige/ausstellerdetail_anzeige..., element mit den options, element mi close-event
function saveOptions(sSaveKey, elOptsContainer, elCloser){

	// if (elOptsContainer) {
	//	elOptsContainer.send(MyAccountConfig.rpcUrl + '?' + MyAccountConfig.toQueryString() + '&act=add&type=sessiondaten&key=' + sSaveKey);
	//	dropdown(elCloser);
	//	$('frmSearch').submit();
	//	return true;
	// }

	$('frmSearch').submit();
}

function saveGroupState(sSaveKey, sEl, iState){

	var qrs = MyAccountConfig.toQueryString() + '&act=add&type=sessiondaten&key=' + sSaveKey + '&grp=' + sEl + '&state=' + iState;
	var req = new Request.JSON({
		method: 'post',
		url: MyAccountConfig.rpcUrl,
		encoding: MyAccountConfig.rpcEncoding
//		onComplete: function(objJson){
//			if(objJson.result == 'OK') {
//			}
//		}
	});
	req.send(qrs);

	return false;
}


function exportXls() {
	var frm = $('frmSearch');
	if (frm){
		var aSel = frm.getElements('')
		var frmAct = frm.get('action');
		frm.set('action', MyAccountConfig.exportUrl);
		frm.set('target', '_self');
		frm.submit();
		frm.set('action', frmAct);
		frm.set('target', '_self');
	}

	return false;
}


window.addEvent('domready', function(){
	if ($('ausstellerliste')) {
		//oMyAccount = new MyAccount({
		//	pins: $('ausstellerliste').getElements('img.addpin')
		//});
		oMyAccount = new MyAccount();
	}
});

