/*function ajax_product_search(city_name){

  retVal = '';
  
  new Ajax.Request('index.php?id=search_project_ajax&sword='+city_name+'&type=95', {  
    onSuccess: function(transport) {	
	retVal = transport.responseText;
    }, asynchronous:false, evalScripts:true
  });  
  
  return retVal;
}*/

var seconds_to_popup_close=0;
var drawer; 

Event.observe(window, 'load',
      function() { 						
		document.ready_for_drawing=true;
		//alert(document.ready_for_drawing);
	}
    );


function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

//highlights the region in the image 
function highlight_region_func(x,y,x2,y2, el_id, link_alias){	
	
	width = x2-x;
	height = y2-y;

	image =$(el_id);
	image_wrapper = $(el_id+"_wrapper");

	overlay_div_id = x+'x'+y+'-'+width+'-'+height;
	overlay_div_id = "region_overlay_div";

	overlay_div = $(overlay_div_id);
	//if the overlay div does not exist create it
	if (overlay_div != null){			
		hide_region(x,y,width,height);
	}	
		overlay_div=document.createElement('div');
		overlay_div.className = 'overlay_popup_overview';
		overlay_div.id=overlay_div_id;
		overlay_div.name=overlay_div_id;
		overlay_div.style.position="absolute";
		overlay_div.style.left = findPosX(image)+x+'px';
		overlay_div.style.top = findPosY(image)+y+'px';
		overlay_div.style.width=width+'px';
		overlay_div.style.height=height+'px';
		overlay_div.style.cursor='pointer';				
		overlay_div.style.backgroundImage= "url(fileadmin/map/overlay_texture.gif)";		               
		image_wrapper.appendChild(overlay_div);
 		
		$(overlay_div_id).observe('mouseout', function(event){
			hide_region(x,y,width,height)
		});

		$(overlay_div_id).observe('click', function(event){
			popup_region_normal(link_alias)
		});



		 
}

//alias for our needs
function highlight_region(x,y,x2,y2, link_alias){
	highlight_region_func(x,y,x2,y2, 'overview_map', link_alias);
}


//hides the overlay div
function hide_region_func(x,y,width,height, el_id){		
	image = $(el_id);

	image_wrapper = $(el_id+"_wrapper");
	overlay_div_id = x+'x'+y+'-'+width+'-'+height;
	overlay_div_id = "region_overlay_div";
	overlay_div = $(overlay_div_id);

	if (overlay_div != null){		
		image_wrapper.removeChild(overlay_div)
	}
}

//alias for our needs
function hide_region(x,y,width,height){
	hide_region_func(x,y,width,height, 'overview_map');
}

//makes an ajax call to a page with a given id 
function ajax_call(element,alias){		
	new Ajax.Updater(element, 'index.php?id='+alias+'&type=95', {asynchronous:true, evalScripts:true});
}

function popup_region(alias){
	ajax_call('main_content',alias);
}

function popup_region_normal(alias){
	document.location = 'index.php?id='+alias;	
}


function show_location(x,y,sword){
	show_location_func(x,y,sword, 'regionselect_image')
}

//just another alias
function open_location(x,y,sword){
	show_location_func(x,y,sword, 'regionselect_image')
}

function show_location_func(x,y,sword, el_id){
		

	width=340;
	//height=120;

	
	image =$(el_id);
	image_wrapper = $(el_id+"_wrapper");

	overlay_div_id = 'popup-location';

	overlay_div = $(overlay_div_id);
	
	//if the overlay div does not exist create it
	if (overlay_div == null){				
		overlay_div=document.createElement('div');		
		overlay_div.id=overlay_div_id;
		overlay_div.name=overlay_div_id;
		overlay_div.style.position="absolute";
		image_wrapper.appendChild(overlay_div);	
		overlay_div.style.width=width+'px';
		//overlay_div.style.height=height+'px';				
		overlay_div.style.overflow='auto';		
		overlay_div.style.border='1px solid #15773a';				
		overlay_div.style.backgroundColor= "white";							
	}
		overlay_div.style.left = findPosX(image)+x+'px';
		overlay_div.style.top = findPosY(image)+y+'px';
		
		
		$(overlay_div).observe('mouseover', function(event){						
			set_popup_close(30);
		});
		$(overlay_div).observe('mouseout', function(event){						
			set_popup_close(2);
		});
		

		//setTimeout ( "hide_location("+x+","+y+")", 4000 );
			

	//make the call to the search page			
	overlay_div.innerHTML = "<span class=\"green smaller\">Bitte warten... Inhalte werden geladen...</span>";	
	ajax_call(overlay_div_id,'search_project_ajax&sword='+sword);		
	set_popup_close(7);
}

//hides the overlay div
function hide_location_func(el_id){		
	image = $(el_id);

	image_wrapper = $(el_id+"_wrapper");
	overlay_div_id ='popup-location';
	overlay_div = $(overlay_div_id);
	
	if (overlay_div != null){		
		image_wrapper.removeChild(overlay_div)		
	}
	//hide all drawings
	drawer.clear();
	
}

//alias for our needs
function hide_location(){	
	hide_location_func('regionselect_image');
}

function delay_popup_close(s){	
	seconds_to_popup_close+=s;
}

function set_popup_close(s){	
	seconds_to_popup_close=s;
}

function location_hide_watchdog(){	
	seconds_to_popup_close-=1;
	
	//alert(seconds_to_popup_close);
	if (seconds_to_popup_close<0) {
		seconds_to_popup_close=0;
		hide_location();
	}
	
	setTimeout(location_hide_watchdog,1000);
}


function enable_drawing(){
      drawer = new jsGraphics("regionselect_image_wrapper");
      drawer.setStroke(2);
      drawer.setColor("#15773a"); //dark green
}

function draw_circle(x,y,r){	
	drawer.clear();
	drawer.drawEllipse(findPosX($('regionselect_image'))+x-r-1,findPosY($('regionselect_image'))+y-r,r*2,r*2); //dark green
	drawer.paint();
}

//draw a circle around each map area
function highlight_map_areas(map_id){
	//initialize the drawer
		
	//if document has not loaded....
	if (findPosX($('regionselect_image'))<=0){
		setTimeout('highlight_map_areas(\''+map_id+'\')', 500);
		return;
	}
	

	//alert('testing before entering the first time '+document.ready_for_drawing);

	//if document has not loaded.... yet another test
	if (document.ready_for_drawing != true){
		//alert('not gud retrying '+document.ready_for_drawing);
		setTimeout('highlight_map_areas(\''+map_id+'\')', 500);
		return;
	}

	//alert('gud .. at'+document.ready_for_drawing);

	drawer2 = new jsGraphics("regionselect_image_wrapper");
	drawer2.setStroke(4);
        drawer2.setColor("#B50202");
	drawer2.clear();

	
	map = $(map_id);
	areas = map.areas;		
	for (i=0; i<areas.length; i++ ){
		area = areas[i];
		
		x=parseInt(area.coords.split(',')[0]);
		y=parseInt(area.coords.split(',')[1]);
		r=parseInt(area.coords.split(',')[2]);
		

		if (area.shape.toUpperCase() == "CIRCLE"){						
			drawer2.drawEllipse(findPosX($('regionselect_image'))+x-r+1,findPosY($('regionselect_image'))+y-r+2,r*2-6,r*2-6); //dark green	
		}
	}
	
	drawer2.paint();	
}