var module_container='';
var res='';
var header='';
var closebutton='';
var mapelement='';
var directionselement ='';
var directions_container ='';

function showDirections(blockid){
	var blockstring = agent.call('../ajax/Content.ajax.php','AJ_GetBlockDetails','',blockid);
	if(blockstring.substring(0,1) == '{'){
	
		var block = JSON.parse(blockstring);
		var container = document.getElementById('map_and_directions');
		//container.style.backgroundImage = "url('../images/contact_overlay.png')";
		container.style.backgroundPosition = "0px 0px";
		
		container.style.display = "block";
		if(document.documentElement){
			container.style.width = parseInt(document.documentElement.clientWidth,10)+'px';
			temp_height=parseInt(document.documentElement.clientHeight,10);			 
			container.style.height = temp_height+'px';
		}
		// Creating outer-most div
		var res = document.createElement('div');
		res.style.position = "absolute";
		res.style.width = "761px";
		res.style.background = "#ffffff";
		res.style.border = "solid 8px #294867";
		res.style.zIndex = "500000";
		
		// Creating header div
		var header = document.createElement('div');
		header.className = 'CMContact_heading';
		header.innerHTML = 'Map and Directions';
		
		// Creating the close button
		var closebutton = document.createElement('div');
		closebutton.className = 'CMContact_close';
		var elem_id = "'map_and_directions'";
		closebutton.innerHTML = '<a href="javascript:closeDirections('+elem_id+');"><img src="/images/contact_close.gif" style="cursor: pointer;" /></a>';
		
		// Creating div to contain map
		var mapelement = document.createElement('div');
		mapelement.className = 'CMContact_map';
		if(block.content.length<1){
		    mapelement.innerHTML = document.getElementById('temp_image').innerHTML;
		}else{
		    mapelement.innerHTML = block.content;
		}


		
		// Creating Div to contain the directions
		var directions_container =document.createElement('div');
		directions_container.id ="slidepanel";
		//directions_container.style.height = 1+'px';
		directions_container.style.overflow = "hidden";
		
		// Creating panel for the directions
		var directionselement = document.createElement('div');
		directionselement.className = 'CMContact_directions';
		directionselement.innerHTML = block.directions.replace(/\n/g, '<br />');
		
		//res.style.top = (Math.ceil(document.body.clientHeight/2)-300)+'px';
		res.style.top = 200+'px';
		res.style.left = (Math.ceil(document.body.clientWidth/2)-381)+'px';
		
		directions_container.appendChild(directionselement); // inserting the directions div to the panel
		
		// Creating the actual divs and appending them to res
		res.appendChild(header);
		res.appendChild(closebutton);
		res.appendChild(mapelement);
		res.appendChild(directions_container);
		
		var content_area = document.getElementById("map_content");
		content_area.style.display="block";
		res.style.display="block";
		content_area.appendChild(res);
		// The slide starts here
		//module_container = document.getElementById('slidepanel');
		//module_container.true_height = directionselement.offsetHeight;
		
		//if(directions_container.style.height=='1px'){
			// Pausing the programm for 3 seconds then calling startslide function
		//	setTimeout(startslide,3000);
		//}
	}
	else{
		return false;
	}
			
}

function startslide(){
	directions_container.timerId=setInterval(slidedown,30);
}

function slidedown(){
	// Updating height of the panel with the currently set height of the panel
	module_container.full_height=directionselement.offsetHeight;
	
	if (parseInt(module_container.style.height)>=module_container.true_height){
		// Stopping the slide 
		module_container.full_height=module_container.true_height; 
	    var cleared =clearInterval(directions_container.timerId);
		
		return;
		
	}
	else {
	
		if(module_container.style.height=='1px') {
			// Sets the first slide to not slide pixels less than 40px
			height=module_container.true_height; height = Math.min(40,height/2); height = Math.ceil(height);
		}
		else {
			// Calculating the next height to slide down the div
			var remainder = module_container.true_height - parseInt(module_container.style.height);
			height = Math.min(20,remainder/2); height = Math.ceil(height);
		}
		// Applying the new height
		module_container.style.height=parseInt(module_container.style.height)+height+'px';
	}	
}

function closeDirections(id){
	// This funtion closes the map and directions div
	var mod_container = document.getElementById(id);
	if(mod_container.style.display=="block"){
		var mc=document.getElementById("map_content");
		mc.style.display="none";
		mc.innerHTML ="&nbsp;";
		mod_container.innerHTML = '&nbsp';
		mod_container.style.display="none";
	}
	else{
		mod_container.innerHTML = '&nbsp';
		mod_container.style.display ="none";
	}
}
