var req;
var actElem;
var pos;
var uid;
var regionID;
var regionNames;

window.addEvent('domready', function() {
	if (!window.demo_path) window.demo_path = '';
	var demo_path = window.demo_path;
	// --
	var loc = String(window.location);
		
	loc = loc.substr(0,loc.lastIndexOf("/")+1);
	loc += 'index.php';
	
	$('myForm').action = loc+"?eID=ajax_search&action=refList";
	$('myForm').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();
		//Empty the log and show the spinning indicator.
		var log = $('log_res').empty().addClass('ajax-loading');
		//Set the options of the form's Request handler. 
		//("this" refers to the $('myForm') element).
		this.set('send', {
			onComplete: function(response) { 
				log.removeClass('ajax-loading');
				log.set('html', response);
				if($('maxImages') != null) {
					initGalleryHack();
					imageMax = parseInt($('maxImages').value);
					columnsOfThumbnails = (imageMax / 5)+1;
					css = "_ref"
					imagePerPage = 15;
					actElem = null;
					initGalleryScript();
					initInfoPanel();
					activatePicture('refpic_1');
					actImages = 1;
					pos = 1;
					/*
					if($('firstID') != null && $('firstID').value > 0) {
						getInfoForReference($('firstID').value, 'refpic_1',1);
					}*/
				}
			}
		});
		//Send the form.
		this.send();
	});
	
	//erste Anfrage ausführen
	$('submitButton').click();
	
	req = new Request.HTML({url:loc, 
		onSuccess: function(html) {
			$('tx-buso-ajax-info-result').set('text', '');
			$('tx-buso-ajax-info-result').adopt(html);
			
			initInfoPanel();
		},
		onRequest: function(){
			$('tx-buso-ajax-info-result').set('html','<p style="padding-top:205px;background:url(fileadmin/templates/extensions/buso/images/spinner.gif) no-repeat center;">&nbsp;</p>');
			//$('result').setProperty('class', 'request');
			
	    },
		onFailure: function() {
			$('result').set('text','Fehler :-( ...');
			$('tx-buso-ajax-info-result').setProperty('class', '');
		}
	});
});

function initInfoPanel() {
	if($('buso-reference-forward-img') != null) {
		$('buso-reference-forward-img').addEvent('click', function() {
					scrollPictures('forward');
					req.get({'eID': 'ajax_search','action': 'refInfo','uid' : uid, 'pos' : pos, 'pageAction' : 'forward'});
					req.send();		
		});
	}
	//TagToTip('toolTipInfo', BGCOLOR, '#E9EAEC',BORDERSTYLE ,'none',PADDING, '7')
	if($('buso-reference-back-img') != null) {
		$('buso-reference-back-img').addEvent('click', function() {
					scrollPictures('back');
					req.get({'eID': 'ajax_search','action': 'refInfo','uid' : uid, 'pos' : pos, 'pageAction' : 'back'});
					req.send();		
		});
	}
	
	if($('big_img_reference') != null) {
		$('big_img_reference').addEvent('mouseover', function() {
			TagToTip('toolTipInfo', BGCOLOR, '#E9EAEC',BORDERSTYLE ,'none',PADDING, '7');
		});
		
		$('big_img_reference').addEvent('mouseout', function() {
			UnTip();
		});
	}
}

function getInfoForReference(id, activeElement, picPos) {
	activatePicture(activeElement);
	actImages = picPos;
	req.get({'eID': 'ajax_search','action': 'refInfo','uid' : id, 'pos' : picPos});
	req.send();
}

function getNextInfoForReference(id, pageAction, nowPos) {
	scrollPictures(pageAction);
	req.get({'eID': 'ajax_search','action': 'refInfo','uid' : id, 'pos' : nowPos, 'pageAction' : pageAction});
	req.send();
}

function scrollPictures(act) {
	switch(act) {
   		case "back":
   			actImages--;
   			slideImages();
   		break;
   		case "forward":
   			actImages++;
   			slideImages();
   		break;
	}
}

function slideImages() {
	if(actImages < firstImageOnPage) {
			ajaxScroll = true;
			document.getElementById('DHTMLgoodies_leftArrow').onclick();
			ajaxScroll = false;
			setTimeout("slideImages()",slideSpeed+4);
	}
	if(lastImageOnPage < actImages) {
		ajaxScroll = true;
		document.getElementById('DHTMLgoodies_rightArrow').onclick();
		ajaxScroll = false;
		setTimeout("slideImages()",slideSpeed+4);
	}
}

function activatePicture(activeElement) {
	if(actElem != null) {
		$(actElem).setProperty('class', '');
	}

	actElem = activeElement;	
	$(activeElement).setProperty('class', 'actElementReference');
}

function showRegionForLand() {
	var selectedValue = 0;
	
	for (var land in regionID)
	{
	      if(land == document.getElementById("tx-buso-land-reference").value)
	      {
	         selectedValue = document.getElementById("tx-buso-land-reference").value;
	      	 break;
	      }
	}
 	
 	var selectedID = document.getElementById("tx-buso-region").value;
 	var sel = false;
 	
    for(var i=document.getElementsByName("tx-buso-region")[0].length;i > 0 ;i--)
    {
        document.getElementsByName("tx-buso-region")[0][i-1] = null;
    }
    	
    for(var i=0;i<regionNames[selectedValue].length;i++)
    {
    	if(selectedID == regionID[selectedValue][i]) {
    		sel = true;
    	}		        
    	else {
    		sel = false;
    	}
    	
      	var ValueOpt = new Option(regionNames[selectedValue][i], regionID[selectedValue][i], false, sel);
      	document.getElementsByName("tx-buso-region")[0].options[document.getElementsByName("tx-buso-region")[0].options.length] = ValueOpt;
    }
}

