/*
// SuperModal, ver 3.0, 25.07.2011
//
// Requires:
// jQuery 1.6.2 or higher (http://jquery.com/)
*/



var superModalOpt;


// Настройки по умолчанию
function superModalDef () {
  superModalOpt = {
    width: 500,
    height: 0,
    header: '',
    content: '',
    footer: '',
    valign: '',
    closeEsc: 1,
    closeClick: 0,
    duration: 100,
    forcedCenter: ( $('html.fix_ipad, html.fix_galaxy').length ? 0 : 1 )
  };
};


// Initialization
$(document).ready(function(){   
  $('html, body').css({height:'100%',margin:'0px'});
  $('a.superModalOpen').superModal();
});


// Function called when the user clicks on a superModal link
function superModalShow (url) {
  $(':focus').not('html, body').blur();
  superModalDef();
  url=url.replace(/^.*?#/,'');
  url=url.replace(/;+/g,';;');
  url=';'+url+';';
  url.replace(/;(width|height|header|content|footer|valign|closeEsc|closeClick|groundColor|groundOpacity|winColor|winBorder|winPadding|duration|forcedCenter)=(.*?);/g,
    function($0,$1,$2) {
      superModalOpt[$1]=$2; return '';
    }
  );
  superModalOpt.closeEsc=parseInt(superModalOpt.closeEsc);
  superModalOpt.closeClick=parseInt(superModalOpt.closeClick);
  superModalOpt.duration=parseInt(superModalOpt.duration);
  superModalOpt.forcedCenter=parseInt(superModalOpt.forcedCenter);
  var o=superModalOpt;

  var html=
  '<div id="superModalGround" style="display:none; z-index:9998; position:absolute; top:0px; left:0px; width:100%"></div>'
  +'<div id="superModalOverlay" style="display:none; position:absolute; overflow: hidden; z-index:9999; top: 50%; left: 50%">'
  +'<div id="superModalHeader" style="background:transparent;overflow:hidden;position:relative;left:0px;top:0px"></div>'
  +'<div id="superModalContent" style="background:transparent;overflow:hidden;position:relative;left:0px;top:0px"></div>'
  +'<div id="superModalFooter" style="background:transparent;overflow:hidden;position:relative;left:0px;top:0px"></div>'
  +'</div>';

  $(navigator.userAgent.indexOf('MSIE')!=-1 ? 'html' : 'body').css({overflow:'hidden'});
  $('body')
    .css({marginRight: ($(window).height()<$(document).height() ? '21' : '0')+'px'})
    .append(html);
  $('iframe, object, .fix_ie6 select').css({visibility: 'hidden'});
  var css={
    width: $(document).width()+'px',
    height: $(document).height()+'px'
  };
  if (o.groundColor) css.backgroundColor=o.groundColor;
  var opacity=$('#superModalGround').css('opacity');

  $('#superModalGround').css(css).fadeTo(o.duration,opacity,function(){

    css={ width: o.width+'px' };
    if (o.winColor) css.backgroundColor=o.winColor;
    if (o.winBorder) css.border=o.winBorder;
    if (o.winPadding) css.padding=o.winPadding;
    $('#superModalOverlay').css(css);

    if (o.header!='') $('#superModalHeader').prepend($(o.header).show()).show();
    else $('#superModalHeader').hide();

    if (o.footer!='') $('#superModalFooter').prepend($(o.footer).show()).show();
    else $('#superModalFooter').hide();

    if (o.height>0) {
      var h = o.height
        - (o.header!='' ? $('#superModalHeader').outerHeight() : 0)
        - (o.footer!='' ? $('#superModalFooter').outerHeight() : 0)
        + 'px';
      if (o.content!='') {
        $(o.content).css({display:'block',maxHeight:h,height:'auto',overflow:'auto'});
        if (o.valign=='middle' || o.valign=='bottom')
          $('#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 $('#superModalContent').prepend($(o.content));
        if ($(o.content).height()>parseInt(h,10)) $(o.content).css('height',h);
      }
      else $('#superModalContent').html('<div></div>');
      $('#superModalContent').css('height',h).show();
    }
    else {
      if (o.content!='') $(o.content).prependTo($('#superModalContent').show()).show();
      else $('#superModalContent').hide();
    };

    $('#superModalOverlay').fadeIn(o.duration);

    $('#superModalOverlay').css({
      marginTop: (-parseInt($('#superModalOverlay').outerHeight()/2, 10)+$(window).scrollTop()) +'px',
      marginLeft: (-parseInt($('#superModalOverlay').outerWidth()/2, 10)+$(window).scrollLeft()) +'px'
    });

    if (o.closeEsc)
      $(document).bind('keydown.superModal',function(e){
        if (e.keyCode==27) {
          e.preventDefault();
          var el=$('#superModalOverlay .superModalClose');
          if ($(el).length>0) $(el).eq(0).click();
          else superModalClose();
        };
      });

    if (o.closeClick)
      $(document).bind('click.superModal',function(){
        // superModalClose();
        var el=$('#superModalOverlay .superModalClose');
        if ($(el).length>0) $(el).eq(0).click();
        else superModalClose();
      });

    $('#superModalOverlay .superModalClose').prop('href','javascript:void(0)').unbind ('click.superModal')
    .bind('click.superModal',function() {
      this.blur();
      superModalClose();
      return false;
    });

  });

  $(window)
  .bind('resize.superModal',function(){
    $('#superModalGround').css({
      width:$(document).width()+'px',
      height:$(document).height()+'px'
    });
    $('#superModalOverlay').css({
      marginTop: (-parseInt($('#superModalOverlay').outerHeight()/2, 10)+$(window).scrollTop()) +'px',
      marginLeft: (-parseInt($('#superModalOverlay').outerWidth()/2, 10)+$(window).scrollLeft()) +'px'
    });
  })
  .bind('scroll.superModal',function(){
    if (superModalOpt.forcedCenter) $('#superModalOverlay').css({
      marginTop: (-parseInt($('#superModalOverlay').outerHeight()/2, 10)+$(window).scrollTop()) +'px',
      marginLeft: (-parseInt($('#superModalOverlay').outerWidth()/2, 10)+$(window).scrollLeft()) +'px'
    });
  });

};




// Tune modal box sizes and close link behaviour after changing modal box content
function superModalTune (callbackClose) {
  if ($('#superModalGround').length==0) return false;
  var o=superModalOpt;
  $('#superModalGround').hide();
  $('#superModalGround').css({
    height: $(document).height()+'px',
    width:$(document).width()+'px'
  }).show();
  $('#superModalOverlay').css('width',o.width+'px');
  var el1=$('#superModalHeader').children();
  var el2=$('#superModalTD');
  if ($(el2).length==0) el2=$('#superModalContent');
  el2=$(el2).children();
  var el3=$('#superModalFooter').children();
  if ($('#superModalHeader').css('display')!='none')
    $('#superModalHeader').css('height',$(el1).outerHeight()+'px');
  if ($('#superModalFooter').css('display')!='none')
    $('#superModalFooter').css('height',$(el3).outerHeight()+'px');
  var h = o.height
    - (o.header!='' ? $('#superModalHeader').outerHeight() : 0)
    - (o.footer!='' ? $('#superModalFooter').outerHeight() : 0)
    + 'px';
  if ($('#superModalContent').css('display')!='none') {
    $('#superModalTD').css({ height:h, verticalAlign:o.valign });
    if (o.height>0) {
      $(el2).css({display:'block',maxHeight:h,height:'auto',overflow:'auto'});
      if ($(el2).height()>parseInt(h,10)) $(el2).css('height',h);
      $('#superModalContent').css('height',h);
    }
    else {
      $(el2).css({display:'block',height:'auto',overflow:'auto'});
      $('#superModalContent').css('height','auto');
    };
  };
//$('body').css({overflow:'auto', marginRight:'0px'});
  $('#superModalOverlay').css({
    marginTop: (-parseInt($('#superModalOverlay').height()/2, 10)+$(window).scrollTop()) +'px',
    marginLeft: (-parseInt($('#superModalOverlay').width()/2, 10)+$(window).scrollLeft()) +'px'
  });
  $('#superModalOverlay .superModalClose').prop('href','javascript:void(0)').unbind ('click.superModal')
  .bind('click.superModal',function() {
    this.blur();
    superModalClose();
    if (typeof callbackClose === 'function') callbackClose(el1,el2,el3);
    return false;
  });
};





// Close superModal box
function superModalClose (quickly) {
  if ($('#superModalGround').length==0) return false;
  var o=superModalOpt;
  $(document).add(window).unbind('.superModal');
  $('#superModalOverlay, #superModalGround').fadeOut(quickly ? 0 : o.duration, function() {
    $('#superModalHeader, #superModalTD, #superModalFooter'+(o.content!='' ? ', #superModalContent' : '')).children().hide().appendTo('body');
    $('#superModalTD').parent().parent().add('#superModalOverlay, #superModalGround').remove();
    $(navigator.userAgent.indexOf('MSIE')!=-1 ? 'html' : 'body').css({overflow:'auto'});
    $('body').css({marginRight:'0px'});
  });
  $('iframe, object, .fix_ie6 select').css({visibility: 'visible'});
  return false;
};




// Preparing selected elements
if(jQuery) (function($){ $.extend($.fn, {
  superModal: function(callbackOpen, callbackClose) {
    $(this).click(function(){
      this.blur();
      superModalShow (this.href);
      var el=$('#superModalTD');
      if ($(el).length==0) el=$('#superModalContent');
      el=$(el).children();
      if (typeof callbackOpen === 'function') callbackOpen($('#superModalHeader').children(),el,$('#superModalFooter').children());
      $('.superModalClose')
      .unbind ('.superModal')
      .bind ('click.superModal',function() {
        this.blur();
        var el1=$('#superModalHeader').children();
        var el2=$('#superModalContent #superModalTD');
        if ($(el2).length==0) el2=$('#superModalContent');
        el2=$(el2).children();
        var el3=$('#superModalFooter').children();
        superModalClose();
        if (typeof callbackClose === 'function') callbackClose(el1,el2,el3);
        return false;
      });
      return false;
    });
    return $(this);
  }
}); }) (jQuery);

