﻿var path = '/';

/*
 * showOverlay() - Zobrazi informacni zpravu v nove vrstve
 * @param title    Titulek vrstvy
 * @param text     Text zpravy (muze obsahovat HTML tagy)
 * @param width    Sirka okna se zpravou (nepovinne)
 **/
function showOverlay(title, text, width, buttons) {
  if(!$('#overlay').length) {
    // generate HTML code
    var html = '<div id="overlay">'+
               '<div class="lightbox">'+
                  '<div class="top"><b></b></div>'+
                  '<div class="inside">'+
                    '<h2></h2>'+
                    '<div class="msg">'+
                      '<p></p>'+
                    '</div>'+
                    '<a class="close xclose" href=""></a>'+
                    '<p class="buttons"></p>'+
                    '<div class="clear1px"></div>'+
                  '</div>'+
                  '<div class="bottom"><b></b></div>'+
                '</div>'+
                '<div class="lightbox_overlay"></div>'+
                '</div>';

    $('body').append(html);

    $('#overlay').click(function() {
      hideOverlay();
    });

    $('#overlay .lightbox').click(function(event) {
      event.stopPropagation();
      return;
    });
  }

  // remove all old buttons
  $('#overlay .buttons a').remove();

  if(buttons) {
    // custom buttons
    for(button in buttons) {
      var cssClass = '';
      if(buttons[button].close) {
        cssClass = ' xclose';
      }

      if(!buttons[button].link)
        buttons[button].link = '';

      $('#overlay .buttons').append(' &nbsp; <a class="button button-overlay '+cssClass+'" href="'+buttons[button].link+'"><span><span>'+buttons[button].text+'</span></span></a>');
    }
  } else {
    // default button
    $('#overlay .buttons').append('<a class="button button-overlay xclose"><span><span>OK</span></span></a>');
  }

  // add actions to close buttons
  $('#overlay a.xclose').click(function(event) {
    hideOverlay();
    event.stopPropagation();
    return false;
  });

  // reset css
  $('#overlay .inside').css('height', 'auto');
  $('#overlay .lightbox').css('margin-top', 0);

  // show title & text
  $('#overlay .inside h2').text(title);
  $('#overlay .inside .msg').html(text);

  // show overlay
  $('#overlay').show();

  // calculate position of the box
  if(!$('#overlay .lightbox').data('defaultWidth')) {
    $('#overlay .lightbox').data('defaultWidth', $('#overlay .lightbox').width());
  }

  if(width) {
    // user defined width
    $('#overlay .lightbox').css('width', width);
    $('#overlay .lightbox').css('margin-left', -Math.round(width/2));

  } else {
    // default width
    $('#overlay .lightbox').css('width', '');
    $('#overlay .lightbox').css('margin-left', -Math.round($('#overlay .lightbox').data('defaultWidth')/2));
  }

  height = getWindowSize().height - 150;
  if($('#overlay .lightbox').height() > height) {
    $('#overlay .inside').css('height', height);
    $('#overlay .inside .msg').css('height', height - 120);
    $('#overlay .lightbox').css('margin-top', -Math.round(height/3));
  }

  return false;
}

function showOverlayContent(title, url, width, height, buttons) {
  if(!buttons)
    buttons = new Array({
                  'text': 'OK',
                  'href': '',
                  'close': true
              })
  // zobrazeni overlay (zatim prazdne)
  showOverlay(
    title,
    loader(),
    width,
    buttons
  );

  // nacteni dat
  $.get(url, null, function(data) {
    $('#overlay .inside .msg').html(data);
  });

  if(height == 'max') {
    height = getWindowSize().height - 150;
  }

  $('#overlay .inside').css('height', height);
  $('#overlay .lightbox').css('margin-top', -Math.round(height/3));
  $('#overlay .inside .msg').css('height', height-120);

  return false;
}

function hideOverlay() {
  $('#overlay').hide();
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  return {'width': myWidth, 'height': myHeight};
}

function select_classes(){
  var value = $('#school_id').val();

  $.ajax({                    
    url: path + 'muj-snacker/' + 'classes/'+value,
    type: 'GET',
    dataType: 'text',
    timeout: 10000,
    error: function(){
    },
    success: function(text){
    $('#class_id').html(text)
    }
   });
 }
function select_schools(){
  var value = $('#town_id').val();
  var first = 0;

  if($('#town_id').hasClass('active-intactive'))
   var first = 1;
     
  if($('#town_id').hasClass('only_active_active')){
   var first = -1;  
  }
   
     
  $.ajax({                    
    url: path + 'poptavky/' + 'towns/'+value+'/'+first,
    type: 'GET',
    dataType: 'html',
    timeout: 10000,
    error: function(){
    },
    success: function(text){
    if($('#town_id').hasClass('only_active')){
     var value = $('#school_id').val();
     $('#school_id').html(text);
     $('#school_id').val(value); 
     select_classes();
   
     } 
     else{
     var value = $('#inactive_school_id').val();
     $('#inactive_school_id').html(text);
     $('#inactive_school_id').val(value);    
     }
    }
   }); 
 }
 
function close_warning(){
  $.ajax({                    
    url: path + 'muj-snacker/' + 'leaving',
    type: 'GET',
    dataType: 'text',
    timeout: 10000,
    error: function(){
    },
    success: function(text){
     text = text.replace(/^\s*/, "").replace(/\s*$/, "");
     if(text!='')
      showOverlay('', text, 530, '260');
     else 
      window.location = $('#user-logout-js').attr('href');
    }
   });
  return false; 
 }  
 
$(window).load(function() {          
      $("select#town_id").change(function(){
       select_schools();
      });     
      
    $("a.fancybox").fancybox({
      'zoomSpeedIn': 300,
      'zoomSpeedOut': 300,
      'overlayShow' :true
    });

    $("a#fancybox-errors").fancybox({ 'hideOnContentClick': true}).trigger('click');; 
            
});

/* Prihlaseni */
$(document).ready(function() {
  $("#login h4 a.login-link").click(function() {
    $('#login .inside').slideToggle();

    return false;
  });
  
  $("#pay_vs").focus(function() {
    $('#box-help-pay-vs').fadeIn('normal');
    return false;
  });
  
  $("#pay_vs").blur(function() {
    $('#box-help-pay-vs').fadeOut('fast');
    return false;
  });  
  
  $("#pay_account").focus(function() {
    $('#box-help-pay-account').fadeIn('normal');
    return false;
  });
  
  $("#pay_account").blur(function() {
    $('#box-help-pay-account').fadeOut('fast');
    return false;
  });   
  
  $("#username").focus(function() {
    $('#box-help-username').fadeIn('normal');
    return false;
  });
  
  $("#username").blur(function() {
    $('#box-help-username').fadeOut('fast');
    return false;
  });   
  
  $("#password").focus(function() {
    $('#box-help-password').fadeIn('normal');
    return false;
  });
  
  $("#password").blur(function() {
    $('#box-help-password').fadeOut('fast');
    return false;
  });   
  
  $("#password_again").focus(function() {
    $('#box-help-password').fadeIn('normal');
    return false;
  });
  
  $("#password_again").blur(function() {
    $('#box-help-password').fadeOut('fast');
    return false;
  });         
      
 /* chybove hlasky */ 
/*  $(".graphic-form .formLeft input").focus(function() {
      $('.error-message').slideDown('normal');
    return false;
  });
  
  $(".graphic-form .formLeft input").blur(function() {
      $('.error-message').slideUp('normal');
    return false;
  });
  */  
});