var previousWidth; var previousHeight;
function Resize_Tables () {

 if ($(window).width() != previousWidth || $(window).height() != previousHeight) {
  
  previousWidth = $(window).width(); 
  previousHeight = $(window).height();

  $("td.remaining-height").css('height','auto');
  $("td.remaining-height").each( function() {

    var siblings = $(this).parent().siblings();
    var siblings_height = 0;
    for (var i = 0; i < siblings.length; i++ ) {
     var max_height = $(siblings[i].cells[0]).css('max-height');
     if ( typeof max_height == 'undefined' || max_height == 'none') { 
      siblings_height += $(siblings[i]).height(); 
     } else { 
      siblings_height += parseInt(max_height); 
     }
    }
    var remaining_height = this.offsetParent.offsetParent.clientHeight - siblings_height-1;
    $(this).css('height',remaining_height + 'px');
  });
  
 }
}

// -------------- Shows the clicked row content of a list. Example : news list ---------------//

function List_Show_Hide( list_name, total_rows, current_id ) {
	
 var list_row;
	var i;
	
	for ( i = 1; i <= total_rows; i++ ) {		

		list_row = document.getElementById( list_name + "_" + i );

  if ( i == current_id ) {
			if ( list_row.style.display == '' ){
		  list_row.style.display='none';
    if ( document.getElementById( list_name + "_" + i + "_preview") ) {
     document.getElementById( list_name + "_" + i + "_preview").style.display = '';
    }
		  if ( document.getElementById( list_name + "_" + i + "_img") ) {
		   document.getElementById( list_name + "_" + i + "_img").src = document.getElementById( list_name + "_" + i + "_img").src.replace('on.jpg','off.jpg');
		  }
    if ( document.getElementById( list_name + "_" + i + "_div") ) {
     document.getElementById( list_name + "_" + i + "_div").className = 'dynamic-text-closed';
    }
			} else {
		  list_row.style.display='';
    if ( document.getElementById( list_name + "_" + i + "_preview") ) {
     document.getElementById( list_name + "_" + i + "_preview").style.display = 'none';
    }
    if ( document.getElementById( list_name + "_" + i + "_img") ) {
     document.getElementById( list_name + "_" + i + "_img").src = document.getElementById( list_name + "_" + i + "_img").src.replace('off.jpg','on.jpg');
    }
    if ( document.getElementById( list_name + "_" + i + "_div") ) {
     document.getElementById( list_name + "_" + i + "_div").className = 'dynamic-text-open';
    }
			}
		} else { 
		 list_row.style.display='none';
   if ( document.getElementById( list_name + "_" + i + "_preview") ) {
    document.getElementById( list_name + "_" + i + "_preview").style.display = '';
   }
   if ( document.getElementById( list_name + "_" + i + "_img") ) {
    document.getElementById( list_name + "_" + i + "_img").src = document.getElementById( list_name + "_" + i + "_img").src.replace('on.jpg','off.jpg');
   }
   if ( document.getElementById( list_name + "_" + i + "_div") ) {
    document.getElementById( list_name + "_" + i + "_div").className = 'dynamic-text-closed';
   }
		}
	}

}



// ------------------------- Opens/Closes Site Map Sections  ----------------------------- //
function Site_Map_Section_Opener( id ) {
	
	var site_map_button = document.getElementById( "site_map_" + id + "_button" );
	var site_map_arrow = document.getElementById( "site_map_" + id + "_arrow" );
	var site_map_content = document.getElementById( "site_map_" + id + "_content" );
 
	var	img_path = site_map_button.src.substring( 0, site_map_button.src.lastIndexOf("/") + 1  );
	var img_src = site_map_button.src.substring( site_map_button.src.lastIndexOf("/") + 1, site_map_button.src.length );
  
		if ( site_map_content.style.display == '' ){
		 
		 site_map_content.style.display = 'none';
			
			switch ( img_src ) {
				case 'minus_root.gif':
				 site_map_button.src = img_path + 'plus_root.gif';
				break;
				case 'minus_L.gif':
				 site_map_button.src = img_path + 'plus_L.gif';
				break;
				case 'minus_T_cross.gif':
				 site_map_button.src = img_path + 'plus_T_cross.gif';
				break;
			}
			
			site_map_arrow.src = img_path + 'container_arrow_right.gif';
						
		} else { 
		
		 site_map_content.style.display = '';
			
			switch ( img_src ) {
				case 'plus_root.gif':
				 site_map_button.src = img_path + 'minus_root.gif';
				break;
				case 'plus_L.gif':
				 site_map_button.src = img_path + 'minus_L.gif';
				break;
				case 'plus_T_cross.gif':
				 site_map_button.src = img_path + 'minus_T_cross.gif';
				break;
			}
			
			site_map_arrow.src = img_path + 'container_arrow_down.gif';
			
		}
	
}



function Validate_Access_Code( code, msg ){
	
		var params = { 'code': code, 'msg': msg };
		
		$.ajax({
				url: AJAX_PHP_ROOT + "validate_access_code.php",
				processData: true,      
				type: "GET",
				dataType: 'script',
				data: params
		});

}



function Popup_Div( url, sender, width, height, title, title_class, center, close_image ){
 if ( document.getElementById("popup_div") ) {
   return;
 }
   
 var scrollY = $(window).scrollTop();
 var innerWidth = $(window).width();
 var innerHeight = $(window).height();
 var pixelWidth = width;
 
 if ( width.toString().indexOf('%') > -1 ) {
   pixelWidth = innerWidth * (parseFloat(width) / 100);
 }
 
 if ( height.toString().indexOf('%') > -1 ) {
   height = innerHeight * (parseFloat(height) / 100);
 }
 
 var curleft = curtop = 0;
 var obj = sender;
 if ( !center ) {
   curleft += $(obj).offset().left;
   curtop += $(obj).offset().top;
   
   curtop += $(sender).height()+5;
   /*if ( scrollY && scrollY >= curtop ) {
    curtop = scrollY + 15;
    curleft += $(sender).width() +10;
   }*/
   
   if ( innerWidth <= (curleft + pixelWidth) ) {
    curleft = innerWidth - pixelWidth;
   }
 } else {
   curleft = (innerWidth/2) - (pixelWidth/2);
   curtop = ((innerHeight/2) - (height/2)) + scrollY;
 }
   
  if ( curleft < 10 ) {
    curleft = 10;
  }
  
  if ( curleft+width+4 > innerWidth ) {
     curleft = curleft - ((curleft+width+4) - innerWidth);
  }
  
  if ( curtop < 10 ) {
    curtop = 10;
  }
 
 var popupDiv = document.createElement("div");
 popupDiv.setAttribute("id", "popup_div");
 popupDiv.setAttribute("name", "popup_div");
 popupDiv.style.position = "absolute";
 popupDiv.style.width = pixelWidth + "px";
 popupDiv.style.height = height + "px";
 popupDiv.style.left = curleft + "px";
 popupDiv.style.top = curtop + "px";
 popupDiv.style.zIndex = 1;
 
 var titleSpan = document.createElement("div");
 titleSpan.setAttribute("class", title_class );
 titleSpan.className = title_class;
 titleSpan.style.margin = "0";
 titleSpan.style.position = "relative";

 titleText = document.createTextNode(title);
 titleSpan.appendChild(titleText);
 
 var closeSpan = document.createElement("span");
 closeSpan.style.position = "absolute";
 closeSpan.style.right = "20px";
 closeSpan.style.top = "0px";
 closeSpan.style.cursor = "pointer";
 closeSpan.style.border = "none";
 closeSpan.style.lineHeight = "14px";
 closeSpan.style.verticalAlign = "bottom";

 closeImage = new Image(); 
 closeImage.src = close_image;
 
 var closeImg = document.createElement("img");
 closeImg.setAttribute("src", close_image);
 //closeImg.setAttribute("alt", "X");
 closeImg.style.marginTop = "0px";
 closeImg.style.width = "21px";
 closeImg.style.marginLeft = "10px";
 closeImg.style.verticalAlign = "middle";
 closeImg.onclick = function(){
  document.body.removeChild(popupDiv);
  $(document).unbind('click',monitorClick);
 };

 closeSpan.appendChild(closeImg);
 titleSpan.appendChild(closeSpan);
 popupDiv.appendChild(titleSpan);
 
 // clone the title element so we can find its height
 titleSpanClone = titleSpan.cloneNode(true);
 titleSpanClone.style.visibility = 'hidden';
 document.body.appendChild(titleSpanClone);
 var titleHeight = titleSpanClone.offsetHeight;
 document.body.removeChild(titleSpanClone);
 
 var object_height = (height - titleHeight) + "px";
 
 if (typeof popupDiv.insertAdjacentHTML != 'undefined') {
   
   // ****** IFRAME is required in IE window.parent won't work on object! ******* //
   popupDiv.insertAdjacentHTML('beforeEnd', 
                               ['<iframe id="obj_results" frameborder="0" allowtransparency="true" src="' + url + '"', 
                                ' width="100%" height="' + object_height + '">', 
                                '<\/iframe>'  
                               ].join('\r\n'));
 } else {
  var frame_object = frame_object = document.createElement("object");
  frame_object.setAttribute("data", url);
  frame_object.setAttribute("type", "text/html");
  frame_object.style.width = "100%";
  frame_object.style.height = object_height;
  popupDiv.appendChild(frame_object);
 }
 
 document.body.appendChild(popupDiv);
 
 var monitorClick = function(e){
  var evt = (e)?e:event;
  
  var clicked_element = (evt.srcElement)?evt.srcElement:evt.target;

  try{
  var descendent_clicked = $(sender).has(clicked_element).length;
  if ( clicked_element != popupDiv && clicked_element != titleSpan && clicked_element != closeSpan && (!printImg || clicked_element != printImg) && clicked_element != sender && !descendent_clicked ){
   if ( document.getElementById("popup_div") ) {
    document.body.removeChild(document.getElementById("popup_div"));
    // remove event handler IMPORTANT!!!
    $(document).unbind('click',monitorClick);
   }
  }
  } catch(exception) {}
  return true;
};
 
 $(document).bind('click',monitorClick);
     
 if ( center ) {  
   $(window).resize(function(eventObject){
       var newWidth = $(window).width();
       var newHeight = $(window).height();
       var newPixelWidth = width;
       
       if ( width.toString().indexOf('%') > -1 ) {
         newPixelWidth = newWidth * (parseFloat(width) / 100);
         document.getElementById("popup_div").style.width = newPixelWidth + "px";
         titleSpan.style.width = (newPixelWidth - 5) +"px";
       }
     
       var new_left = (newWidth/2) - (newPixelWidth/2);
       if (new_left < 10) {
        new_left = 10;
       }
       var new_top = (newHeight/2) - (height/2);
       if (new_top < 10) {
        new_top = 10;
       }
       popupDiv.style.left = new_left + "px";
       popupDiv.style.top = new_top + $(window).scrollTop() + "px";
     
   });
   
   $(window).scroll(function ( ev ) { 
     var newHeight = $(window).height();
     var new_top = (newHeight/2) - (height/2);
     if (new_top < 10) {
      new_top = 10;
     }
     popupDiv.style.top = new_top + $(window).scrollTop() + "px";
   });
 }
 

     
 return false;
}



function Blowup_Image( src, event, img_width, img_height ){

		if ( document.getElementById("img_div") ) {
				$(document.getElementById("img_div")).remove();
				return;
		}
		
		src += "?" +(new Date()).getTime();
		
		var Xpos = event.pageX;
		var Ypos = event.pageY;
		
		var imgDiv = document.createElement("div");
		imgDiv.setAttribute("id", "img_div");
		imgDiv.setAttribute("name", "img_div");
		imgDiv.style.backgroundColor = "#ffffff";
		imgDiv.style.position = "absolute";
		imgDiv.style.display = "none";
		imgDiv.style.border = "1px solid #333333";
		imgDiv.style.padding = "0px";
		
		var blowupImg = document.createElement("img");
		blowupImg.setAttribute("src", src);
		blowupImg.setAttribute("alt", "");
		blowupImg.style.margin = "0px";
		blowupImg.style.verticalAlign = "top";
		
		imgDiv.appendChild(blowupImg);
		
		document.body.appendChild(imgDiv);
		
		var current_top = $(event.currentTarget).position().top;
		var current_left = $(event.currentTarget).position().left;
		var current_bottom = current_top + $(event.currentTarget).height();
		var current_right = current_left + $(event.currentTarget).width();
		
		$(blowupImg).load(function() {
				var pos = $(imgDiv).position();
				var top_pos = Ypos;
				var left_pos = Xpos;
				
				var top = top_pos - (img_height/2);
				var bottom = top + img_height +50;
				
				var left = left_pos+5;
				var right = left + img_width + 50;
				
				if (bottom >= $(window).height()+$(window).scrollTop()) {
					bottom = ($(window).height()+$(window).scrollTop()) - 10;
					top = bottom - img_height - 40;
				}
				
				if ( top <= $(window).scrollTop()+10 ) {
					top = $(window).scrollTop()+10;
					bottom = top + img_height + 40
				}
				
				if ( right >= $(window).width() ) {
					right = $(window).width()-10;
					left = right - img_width - 50;
				}
				
				if ( left <= 10 ) {
					left = 10;
					right = left + img_width + 50;
				}
				
				imgDiv.style.left = left + "px";
				imgDiv.style.top = top + "px";
				
				imgDiv.style.display = "";
				
				var mousemove = function(e){
																																														
								var top = (e.pageY - ($(blowupImg).height()/2));
								var bottom = (e.pageY + ($(blowupImg).height()/2)) +50;
								
								var left = e.pageX+5;
								var right = left + $(blowupImg).width() + 50;
								
								if (bottom >= $(window).height()+$(window).scrollTop()) {
									top -= bottom - ($(window).height()+$(window).scrollTop());
								}
								
								if ( top <= $(window).scrollTop()+10 ) {
									top = $(window).scrollTop()+10;
								}
								
								if ( right >= $(window).width() ) {
									right = $(window).width()-10;
									left = right - $(blowupImg).width() - 50;
								}
								
								if ( left <= 10 ) {
									left = 10;
									right = left + $(blowupImg).width() + 50;
								}
								
								imgDiv.style.left = left + "px";
								imgDiv.style.top = top + "px";
								
								if (e.pageY < current_top || e.pageY > current_bottom || e.pageX < current_left || e.pageX > current_right) {
									$(blowupImg).remove();
									$(imgDiv).remove();
									$(document).unbind( "mousemove", mousemove );
								}
								
								
				}
		
				$(document).mousemove( mousemove );
				
		});
}


function Get_Txt( textid ){
		var string = "";
		if ( aLang[textid] == null ){
			string = textid;
		} else {
			string = aLang[textid];
		}			
		
		return string;
}


function Check_Search_Input( input )
{
	if (input == '') {
		alert( Get_Txt('FIELD_REQUIRED') );
		return false;
	}
	return true;
}




// The following functions are for Google Maps

// Updates the map position from the address provided in the form
function updateMapLocation( elements ){

	var address = elements;

	var geocoder = new GClientGeocoder();

	geocoder.getLatLng(
			address,
			function(point) {
					if (!point) {
							//console.log(address + " not found");
					} else {
							// Center the map on the found point
							addMarker(point, 14);
							
							// Get the current center of the map, same as the found position, and fill out the
							// latitude and longitude form fields with this position
							var position = fld_gmap.getCenter();
							document.getElementById('latitude').value = position.lat();
							document.getElementById('longitude').value  = position.lng();

					}
			}
	);
}

// Adds a marker to the map, clearing the old one first
function addMarker(point, zoom)
{
	if (marker) {
		fld_gmap.removeOverlay(marker);
		marker = null;
	}

	marker = new GMarker(point, {draggable: false, bouncy: true});

	fld_gmap.addOverlay(marker);

	if (!zoom)
		zoom = fld_gmap.getZoom();

	fld_gmap.setCenter(point, zoom);
}

function absoluteMapLocation()
{
	var latitude = document.getElementById('latitude').value
	var longitude = document.getElementById('longitude').value
	
	var location = new GLatLng(latitude, longitude);
	fld_gmap.setCenter(location, 13);
}

function updateMapType() {
	switch (document.getElementById('map_type').value) {
		case 'Map':
			fld_gmap.setMapType(G_NORMAL_MAP);
			break;
		case 'Satellite':
			fld_gmap.setMapType(G_SATELLITE_MAP);
			break;
		case 'Hybrid':
			fld_gmap.setMapType(G_HYBRID_MAP);
			break;
	}
}

var marker;
