var currentChild = null;

function show_popup(cnt){
	var popup = document.getElementById('popup_my');
	var popup_content = document.getElementById('popup_content');
	var curr_content = document.getElementById(cnt);
	var new_content = curr_content.cloneNode(true);
	if (currentChild) popup_content.removeChild(currentChild);
	currentChild = new_content;
	popup_content.appendChild(new_content);
//	alert(document.body);
//	var top = document.body.scrollTop + 20;
//	popup.style.top = top+"px";
	popup.style.display = 'inline';
	new_content.style.display = 'inline';
	//document.body.scrollTop
}

function hide_popup(obj){
	var popup = document.getElementById('popup_my');
	var popup_content = document.getElementById('popup_content');
	popup.style.display = 'none';
	currentChild.style.display = 'none';
	popup_content.removeChild(currentChild);
	currentChild = null;
}

/*
if (window.attachEvent){
  	window.attachEvent('onresize',center);
 	window.attachEvent('onload',center);
 	window.attachEvent('onscroll',center);
}else if(window.addEventListener){
 	window.addEventListener('resize',center, false);
 	window.addEventListener('load',center, false);
 	window.addEventListener('scroll',center, false);
}
function center(){
  var i = document.getElementById('popup');
  i.style.top = parseInt(document.body.offsetHeight-i.offsetHeight)/2+'px';
  i.style.left = parseInt(document.body.offsetWidth-i.offsetWidth)/2+'px';
};
*/ 


//open pop-up
function showPopup(popup_type)
{
  //when IE - fade immediately
  if($.browser.msie)
  {
    $('#opaco').height($(document).height()).toggleClass('hidden').click(function(){ closePopup(); });
  }
  else
  //in all the rest browsers - fade slowly
  {
   $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7).click(function(){ closePopup(); });
  }

  $('#popup')
   .html($('#popup_' + popup_type).html())
   .alignCenter()
   .toggleClass('hidden');
   
  return false;
}

//open pop-up
function showPopupChrome(popup_type, path)
{
//	 alert(path);
	 
  $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7).click(function(){ closePopup(); });

  $('#popup')
   .html($('#popup_' + popup_type).html())
   .alignCenterChrome()
   .toggleClass('hidden');

  setTimeout(function() {
  	document.location.href = path; 
  }, 1800);
   
  return false;
}

//close pop-up box
function closePopup()
{
  $('#opaco').toggleClass('hidden').removeAttr('style').unbind('click');
  $('#popup').toggleClass('hidden');

  return false;
}

//additional properties for jQuery object
$(document).ready(function() {
  //align element in the middle of the screen
  $.fn.alignCenter = function() {
   //get margin left
   
   //var marginLeft = Math.max(40, parseInt($(window).width()/2 - $(this).width()/2)) + 'px';
   var marginLeft = Math.max(40, parseInt($(window).width() - $(this).width() - 35)) + 'px';
   //get margin top
   //var marginTop = Math.max(40, parseInt($(window).height()/2 - $(this).height()/2)) + 'px';
   
   //return updated element
   
   //return $(this).css({'margin-left':marginLeft, 'margin-top':marginTop});
   return $(this).css({'margin-left':marginLeft, 'margin-top':'0px'});
   
  };
  
  //align element in the middle of the screen
  $.fn.alignCenterChrome = function() {
  	
   //get margin left
   //var marginLeft = Math.max(40, parseInt($(window).width()/2 - $(this).width()/2)) + 'px';
   
   //get margin top
   var marginTop = Math.max(40, parseInt($(window).height() - $(this).height() - 35 )) + 'px';
   
   //return updated element
   return $(this).css({'margin-left':'320px', 'margin-top':marginTop});
   
  };
  
});

$.fn.togglePopup = function(){
   //detect whether popup is visible or not
   if($('#popup').hasClass('hidden'))
   {
    //hidden - then display
    //when IE - fade immediately
    if($.browser.msie)
    {
     $('#opaco').height($(document).height()).toggleClass('hidden');
    }
    else
    //in all the rest browsers - fade slowly
    {
     $('#opaco').height($(document).height()).toggleClass('hidden').fadeTo('slow', 0.7);
    }

    $('#popup')
     .html($(this).html())
     .alignCenter()
     .toggleClass('hidden');
   }
   else
   {
    //visible - then hide
    $('#opaco').toggleClass('hidden').removeAttr('style');
    $('#popup').toggleClass('hidden');
   }
  };