/*
// SuperModal, ver 2.0, 04.09.2009 (упрощенный для сайта Золотой Шар)
//
// Requires:
// jQuery 1.2.3 or higher (http://jquery.com/)
// jQuery Dimensions plugin (http://plugins.jquery.com/project/dimensions)
*/

// Initialization
$(document).ready(function(){   

  var html='<div id="superModalGround" style="display:none"></div>';
  html+='<div id="superModalOverlay" style="display:none;width:100px;height:100px">';
  html+='<div class="superModalHeader" style="height:1px;background:transparent;overflow:hidden;position:relative;left:0px;top:0px"></div>';
  html+='<div class="superModalContent" style="height:1px;background:transparent;overflow:hidden;position:relative;left:0px;top:0px"></div>';
  html+='<div class="superModalFooter" style="height:1px;background:transparent;overflow:hidden;position:relative;left:0px;top:0px"></div>';
  html+='</div>';
  $('body').append(html);
  $('a.superModal').superModal();
});




// Function called when the user clicks on a superModal link
function superModalShow (url) {
  var o = {
    width: 500,
    height: 400,
    heightheader: 0,
    heightfooter: 0,
    header: '',
    content: '',
    footer: '',
    valign: '',
    closeEsc: 1
  };
  url=url.replace(/^.*?#/,'');
  url=url.replace(/;+/g,';;');
  url=';'+url+';';
  url=url.replace(/;(width|height|heightheader|heightfooter|header|content|footer|valign|closeEsc)=(.*?);/g,
    function($0,$1,$2) {
      o[$1]=$2; return ';';
    }
  );
  $.each (['width','height','heightheader','heightfooter','closeEsc'], function(key,val) {
    o['val']=parseInt(o['val'],10);
  });
  $.each (['header','content','footer'], function(key,val) {
    if (o['val']!='' && $(o['val']).length==0) o['val']='';
  });

  $('#myMedia, div.myMedia').css({visibility: 'hidden'});
  if (ie6) {
    $('select').css({visibility: 'hidden'});
    $("body, html").css({height: "100%", width: "100%", overflow: "hidden"});
  };
  $('#superModalGround').addClass('superModalGround').css({
    display: 'block'
  });

  $('#superModalOverlay').addClass('superModalOverlay').css({
    display: 'block',
    width: o.width+'px',
    height: o.height+'px'}).
  css({
    marginTop: -parseInt($('#superModalOverlay').height()/2, 10) +'px',
    marginLeft: -parseInt($('#superModalOverlay').width()/2, 10) +'px'
  });

  if (o.header!='') {
    $(o.header).css({display:'block'});
    $('div.superModalHeader').css('height',(o.heightheader>0 ? o.heightheader : $(o.header).outerHeight())+'px').prepend($(o.header));
  };

  if (o.footer!='') {
    $(o.footer).css({display:'block'});
    $('div.superModalFooter').css('height',(o.heightfooter>0 ? o.heightfooter : $(o.footer).outerHeight())+'px').prepend($(o.footer));
  };

  var h = parseInt($('div.superModalOverlay').css('height'),10)
    - (o.header!='' ? $('div.superModalHeader').outerHeight() : 1)
    - (o.footer!='' ? $('div.superModalFooter').outerHeight() : 1)
    + 'px';

  if (o.content!='') {
    $(o.content).css({display:'block',maxHeight:h,overflow:'auto'});
    if ($(o.content).height()>parseInt(h,10)) $(o.content).css('height',h);
    $('div.superModalContent').css('height',h);
    if (o.valign=='middle' || o.valign=='bottom')
      $('div.superModalContent').prepend('<table border="0" cellpadding="0" cellspacing="0" style="width:100%"><tr><td id="superModalTD" style="width:100%;height:'+h+';vertical-align:'+o.valign+'"></td></tr></table>')
      .find('#superModalTD').prepend($(o.content));
    else $('div.superModalContent').prepend($(o.content));
  }
  else {
    $('div.superModalContent').css('height',h);
  };
  
  if (ie6) {
    var top=$('body').scrollTop();
    var w=$('body').width();
    $('#superModalGround').css({top: top+'px', width: w+'px'});
    $('#superModalOverlay').css({
      marginTop: (-parseInt(parseInt($('#superModalOverlay').css('height'))/2, 10)+top)+'px'
    });
  };

  if (o.closeEsc!=0)
    $(document).keydown(function(e){
      if (e.keyCode==27) {
        superModalClose();
        var el=$('#superModalOverlay a.superModalClose');
        if ($(el).length>0) $(el).eq(0).click();
        else superModalClose();
      };
    });

};




// Tune modal box sizes and close link behaviour after changing modal box content
function superModalTune (callbackClose) {
  if ($('#superModalOverlay').css('display')=='none') return;
  var el1=$('div.superModalHeader').children();
  var el2=$('div.superModalContent #superModalTD');
  if ($(el2).length==0) el2=$('div.superModalContent').children();
  else el2=$(el2).children();
  var el3=$('div.superModalFooter').children();
  var h1=$(el1).length==0 ? 1 : $(el1).outerHeight();
  var h2=$(el3).length==0 ? 1 : $(el3).outerHeight();
  $('div.superModalHeader').css('height',h1+'px');
  $('div.superModalFooter').css('height',h2+'px');
  var h = parseInt($('div.superModalOverlay').css('height'),10)-h1-h2+'px';
  if ($(el2).length>=0) {
    $('#superModalTD').css('height',h);
    $(el2).css('maxHeight',h);
    if ($(el2).height()>parseInt(h,10)) $(el2).css('height',h);
    $('div.superModalContent').css('height',h);
  };
  $('#superModalOverlay a.superModalClose').unbind ('click')
  .click (function() {
    this.blur();
    superModalClose();
    if (typeof callbackClose === 'function') callbackClose(el1,el2,el3);
    return false;
  });
};





// Close superModal box
function superModalClose () {
  $(document).unbind('keydown');
  $('#superModalOverlay').removeClass('superModalOverlay').hide();
  $('#superModalGround').removeClass('superModalGround').hide();
  $('div.superModalHeader').children().appendTo('body').hide();
  $('div.superModalHeader').css('height','1px');
  var el=$('div.superModalContent').children().appendTo('body').hide();
  if ($(el).find('#superModalTD').length>0) {
    $(el).find('#superModalTD').children().appendTo('body').hide();
    $(el).remove();
  };
  $('div.superModalContent').css('height','1px');
  $('div.superModalFooter').children().appendTo('body').hide();
  $('div.superModalFooter').css('height','1px');
  $('#myMedia, div.myMedia').css({visibility: 'visible'});
  if (ie6) {
    $('select').css({visibility: 'visible'});
    $("body,html").css({height: "auto", width: "auto", overflow: ""});
  };
};




function superModalCloseHandler (callbackClose) {
  this.blur();
  superModalClose();
  if (typeof callbackClose === 'function') callbackClose();
  return false;
};
        
        
// Preparing selected elements
if(jQuery) (function($){
  $.extend($.fn, {
    superModal: function(callbackOpen, callbackClose) {
      $(this).click(function(){
        this.blur();
        superModalShow (this.href);
        var el=$('div.superModalContent #superModalTD');
        if ($(el).length==0) el=$('div.superModalContent').children();
        else el=$(el).children();
        if (typeof callbackOpen === 'function') callbackOpen($('div.superModalHeader').children(),el,$('div.superModalFooter').children());
        $('#superModalOverlay a.superModalClose')
        .unbind ('click')
        .click (function() {
          this.blur();
          var el1=$('div.superModalHeader').children();
          var el2=$('div.superModalContent #superModalTD');
          if ($(el2).length==0) el2=$('div.superModalContent').children();
          else el2=$(el2).children();
          var el3=$('div.superModalFooter').children();
          superModalClose();
          if (typeof callbackClose === 'function') callbackClose(el1,el2,el3);
          return false;
        });
        return false;
      });
      return $(this);
    }
  });
}) (jQuery);

