
//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;
var popupRegionHandling;

window.addEvent('domready', function(){	
	
	var createPopup = function(response){

        //alert("Ajax-Call succeeded!\n"+response);
        
    	//alert("response.length: "+response.length);
    	//var responseArray = Array.from(response);
    	 
        // erzeuge popup
		popupRegionHandling = new Element('div', {
			'id': 'overlay',
			'class': 'overlay',
			'style': 'display:none;'
		});
			
		//alert(popupRegionHandling);
		    
        // fülle popup mit Daten
        var contentContainer = "";
        var containerName = "";
        var containerUid = "";
            
		//Array.each(responseArray,function(row,i){
		response.each(function(row,i){
					
			//alert("in each");
			
			containerName = "";
			containerName = row.title;
			containerUid = row.uid;
			
			//alert(containerName);
			
			var regionName = "";
			var regionImage = "";
			var regionLanguage = "";
			var contentRegions = "";
			var regionUid = 0;
			var rowDataArray = row.data;
			
			// create region div
			Array.each(rowDataArray,function(dataRow,j) {            				
				
				regionName = dataRow.title;
				regionImage = dataRow.image;
				regionLanguage = dataRow.preferred_language;
				regionUid = dataRow.uid;
				
				contentRegions += "<div id=\"regionName\"><span><a href=\"index.php?id=2&L="+regionLanguage+"&R="+regionUid+"\">"+regionName+"</a></span></div>";
				
			});
			//complete content container
			contentContainer += "<div class=\"regioncontainer\"><strong>"+containerName+"</strong>"+contentRegions+"</div>";
		});
		
		contentContainer = "<div class=\"regioncontainercontainer\">"+contentContainer+"</div>";
		var contentHeadline = '<div class="headline"><h3 class="header">Please select</h3></div>';
		var boxClose = '<div class=\"boxclosecontainer\"><a class="boxclose" id="boxclose">close</a></div>';
		
		//surround with div for styling
		contentContainer = "<div id=\"box\" class=\"box\" >" + contentHeadline + contentContainer + boxClose + "</div>";
		
		popupRegionHandling.set('html', contentContainer );
				
		// inject into body element
		popupRegionHandling.inject($(document.body),'top');
        
        // define FX functions
		var popupShow = new Fx.Tween('box', {
    		duration: 150,
    		//transition: 'bounce:out',
    		transition: Fx.Transitions.Sine.easeIn,
    		property: 'top'
		});

		var backgroundOut = new Fx.Morph(popupRegionHandling, {
			duration: 250,
			transition: Fx.Transitions.Sine.easeOut,
			onComplete: function(){
				popupShow.start(-200,160);	
			}
		});
		
		var popupHide = new Fx.Tween('box', {
    		duration: 250,
    		transition: Fx.Transitions.Sine.easeIn,
    		property: 'top',
    		onComplete: function(){
    			backgroundOut.start({
					'display': ['block','none'], 
					'visibility': ['visible','hidden']
				})
			} 
		});
	
		var backgroundIn = new Fx.Morph(popupRegionHandling, {
			duration: 200,
			transition: Fx.Transitions.Sine.easeIn,
			onComplete: function(){
				popupShow.start(-200,160);	
			}
		});
		backgroundIn.start({
			'display': ['none','block'], 
			'visibility': ['hidden','visible']			
		}) 

		$('boxclose').addEvent('click',function(){
		    popupHide.start(160,-200);
		});
	};
	
	$('rlm_currentRegion').addEvent('click', function(event){

	    //prevent the page from changing
	    event.stop();
	    
	    //make the ajax call
	    var req = new Request.JSON({
			method: 'get',
			url: 'index.php',
			data: { 'eID' : '0312ee946a34dffcd701614b288382fe' },
   			onComplete: function(response){   				
   				createPopup(response);
   			},		
    		onFailure: function(){
        		alert("Sorry, hat nicht funktioniert: "+error);
    		}			
	    }).send();

        //Damit das Formular nicht doch noch abgeschickt wird
        return false;
    });
    
    // fire click event on first site visit
    if( dcwrlmfirstvisit==true ){
		var evObjElement = document.createEvent("MouseEvents");
		evObjElement.initEvent('click', true, true);
    	$('rlm_currentRegion').dispatchEvent(evObjElement);
	}
});

