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 if (list_row) { 
		 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';
   }
		}
	}

}


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 Check_Search_Input( input )
{
	if (input == '') {
		alert( Get_Txt('FIELD_REQUIRED') );
		return false;
	}
	return true;
}

function Popup_Div( url, sender, width, height, title, center, title_class, title_image, close_image, print_url, print_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.backgroundColor = "#ffffff";
  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.border = "1px solid #CCCCCC";
  
  var titleSpan = document.createElement("div");
  if ( typeof title_class == 'string' && title_class != '' ) {
    titleSpan.setAttribute("class", title_class );
    titleSpan.className = title_class;
  } else {
    titleSpan.style.fontFamily = 'Arial, Helvetica, sans-serif';
    titleSpan.style.fontSize = '9px';
    titleSpan.style.fontWeight = 'bold';
    titleSpan.style.backgroundColor = "#ffffff";
    titleSpan.style.height = "12px";
    titleSpan.style.width = (pixelWidth - 5) +"px";
    titleSpan.style.verticalAlign = "middle";
    titleSpan.style.paddingLeft = "5px";
    titleSpan.style.paddingTop = "2px";
    titleSpan.style.color = "#000000";
  }
  titleSpan.style.margin = "0";
  titleSpan.style.position = "relative";

  if ( typeof title_image == 'string' && title_image != '' ) {
    titleSpan.style.backgroundImage = "url("+title_image+")";
  }
  titleText = document.createTextNode(title);
  titleSpan.appendChild(titleText);
  
  var closeSpan = document.createElement("span");
  closeSpan.style.position = "absolute";
  closeSpan.style.right = "3px";
  closeSpan.style.top = "2px";
  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 = "1px";
  closeImg.style.width = "21px";
  closeImg.style.marginLeft = "10px";
  closeImg.style.verticalAlign = "middle";
  closeImg.onclick = function(){
   document.body.removeChild(popupDiv);
  };

  if ( typeof print_url == 'string' && print_url != '' ) {
    printImage = new Image(); 
    printImage.src = print_image;
    
    var printImg = document.createElement("img");
    printImg.setAttribute("id", "print_button");
    printImg.setAttribute("src", print_image);
    printImg.setAttribute("alt", "");
    printImg.style.marginTop = "0px";
    printImg.style.verticalAlign = "middle";
    printImg.onclick = function(){
     window.open(print_url);
    };
  
    closeSpan.appendChild(printImg);
  }
  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" 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;

   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 eevnt handler IMPORTANT!!!
     $(document).unbind('click',monitorClick);
    }
   }
   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;
}
