// ------- begin :: Functionality to expend/collapse divs ---
    var heights = new Object();
    var objects = new Object();
    var intervals = new Object();
    var timeout = 5; // ms
    var step = 6;
    
    function expendDiv(this_obj,div_id) {
        var div_obj = getObjectById(div_id);
        if (!heights[div_id]) heights[div_id] = parseInt(div_obj.offsetHeight);
        shrinkHeightTo(div_id, parseInt(div_obj.scrollHeight));
        objects[div_id+'expend'] = this_obj.parentNode;
        objects[div_id+'expend'].style.visibility = 'hidden';
        return false;
    }
    
    function collapseDiv(div_id) {
        shrinkHeightTo(div_id, heights[div_id]);
        return false;
    }
    
    function shrinkHeightTo(obj_id, final_height) {
        intervals[obj_id] = setInterval('shrink(\''+obj_id+'\','+final_height+')', timeout);
        return;
    }
    
    function shrink(obj_id, final_height) {
        var obj = getObjectById(obj_id);
        var obj_height = parseInt(obj.offsetHeight);
        var direction = final_height>obj_height ? 1 : -1;
        var obj_next_height = obj_height + direction*step;
//        alert(obj_next_height +' | '+ direction + ' | ' + final_height);
        if ((obj_next_height>=final_height && direction>0) || (obj_next_height<=final_height && direction<0)) {
            obj_next_height = final_height;
            if (direction<0) {
                objects[obj_id+'expend'].style.visibility = 'visible';
            }
            clearInterval(intervals[obj_id]);
        }
        obj.style.height = (obj_next_height) + 'px';
    }
    
    function getObjectById(obj_id) {
        if (!objects[obj_id]) 
            objects[obj_id] = document.getElementById(obj_id);
        return objects[obj_id];
    }
// ------- end :: Functionality to expend/collapse divs ---

// --- begin :: functionality of popups ---
    function showPopupInfo(related_id,popup_text) {
        var popup = getObjectById('popup');
        var related = getObjectById(related_id);
        popup.innerHTML = popup_text;
        popup.style.top = (findPosY(related) - popup.offsetHeight - 3)  + 'px';
        related_x = findPosX(related);
        popup.style.left = (related_x - (popup.offsetWidth-related.offsetWidth)/2) + 'px';
        popup.style.visibility = 'visible';
        return;
    }
    
    function hidePopupInfo() {
        var popup = getObjectById('popup');
        popup.style.visibility = 'hidden';
        return;
    }
// --- end :: functionality of popups ---

// --- begin :: subscription ---

var text_default = null;
var class_default = null;

function on_focus(object) 
{
    if (text_default==null) 
    {
        text_default  = object.value;
        class_default = object.className;
    }
    
    if (text_default==object.value)
    {
        object.value = '';
        object.className = class_default + ' iType';
    }
    
    return;
}

function on_blur(object) 
{
    if (trim(object.value)=='') 
    {
        object.value     = text_default;
        object.className = class_default;
    }
        
    return;
}

function on_submit(form) 
{
    return (text_default!=null && form.email.value!=text_default);
}

// --- end :: subscription ---

  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;
  }
  
function trim(string)
{
    return string.replace(/(^\s+)|(\s+$)/g, "");
}

function switchShip(object)
{
  if (object.checked)
    $("#shipping_address").slideUp();
  else
    $("#shipping_address").slideDown();
  return true;
}

function changeCountry(object, language)
{
  $("#region").html('Loading regions...');
  $.post('/ajax/', { country_id: object.value,  language:  language }, onAjaxSuccess);
  return;
}

function onAjaxSuccess(data)
{
  $("#region").html(data);
}
