jQuery(function( $ ){

	$.scrollTo.defaults.axis = 'xy'; 			

});

jQuery.extend( jQuery.easing,
{

  // t: current time, b: begInnIng value, c: change In value, d: duration

  easeInOutSine: function (x, t, b, c, d) {
    return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  },

	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	}

});

$(document).ready(function() {

  var dur = 2000;
  var efekt = "easeInOutSine";

  $('.menu a').click(function() {return false});

  $('a.prvni_odkaz').click(function() {
    $('#obsah').animate({ left: 0, top: 0 }, {duration: dur, easing: efekt});
  });


  $('a.druhy_odkaz').click(function() {
    $('#obsah').animate({ left: -3000, top:0 }, {duration: dur, easing: efekt});
  });


  $('a.treti_odkaz').click(function() {
    $('#obsah').animate({ left: 0, top: -2100 }, {duration: dur, easing: efekt});
  });


  $('a.ctvrty_odkaz').click(function() {
    $('#obsah').animate({ left: -3000, top:-2100 }, {duration: dur, easing: efekt});
  });


  $('#obsah').draggable(
    {
      zIndex: 	1000,
      cursor: 'move'
    }
  );
  
  


  // reference:nacteni prvni reference
  $('.menu ul li a:first').each(function() {

    var ref = new Reference (this);
    if (ref.prefix.length === 0 || ref.pocet === 0)  return false;

    nastavReferenci (ref);
  
  });
  
  // reference:nacteni referenci
  $('.menu ul li a').click(function() {

    var ref = new Reference (this);
    if (ref.prefix.length === 0 || ref.pocet === 0)  return false;

    nastavReferenci (ref);

  });


  
  
  // chovani fomulare
  $('#k_email, #k_predmet').focus(function() {
    var def = new Array();
    def['k_email'] = 'VÁŠ EMAIL';
    def['k_predmet'] = 'PŘEDMĚT';
    if (this.value == def[this.id])  this.value = '';
  });

  $('#k_email, #k_predmet').blur(function() {
    var def = new Array();
    def['k_email'] = 'VÁŠ EMAIL';
    def['k_predmet'] = 'PŘEDMĚT';
    if (this.value == '')  this.value = def[this.id];
  });

  
  // formular pro kontakty
  $('#kontakt_form').submit(function() {
    var e = $.trim( $('#k_email').val() );
    var p = $.trim( $('#k_predmet').val() );
    var t = $.trim( $('#k_text').val() );
    var s = $('#k_stav');
    
    nastavOK (s);

    if (e == '' || e == 'VÁŠ EMAIL') {
      s.text( 'Email není vyplněn.' );
      nastavChybu (s);
      return false;
    }
    else if (p == '' || p == 'PŘEDMĚT') {
      s.text( 'Předmět není vyplněn.' );
      nastavChybu (s);
      return false;
    }
    else if (t == '') {
      s.text( 'Text zprávy není vyplněn!' );
      nastavChybu (s);
      return false;
    }

    s.text( 'Odesílám zprávu...' );
    nastavOK (s);

    $.ajax({
      type: "POST",
      url: "index.php?ajax-kontakt",
      dataType: "html",
      data: "email=" + e + "&predmet=" + p + "&text=" + t,
      success: function(msg){
        var c = parseInt( msg.substr(0,1) );
        if (c == 1)  nastavChybu (s);
        else {
          nastavOK (s);
          $('#k_predmet').val( '' );
          $('#k_text').val( '' );
        }
        s.text( msg.substr(1) );
      },
      error: function(){
        nastavChybu (s);
        s.text( 'Při zpracování požadavku došlo k chybě.' );
      }
    });

    return false;
  });
  

});


  function nastavChybu (el) {
    if (el.hasClass('ok')) {
      el.removeClass('ok');
    }
    el.addClass('chyba');
  }
  
  
  function nastavOK (el) {
    if (el.hasClass('chyba')) {
      el.removeClass('chyba');
    }
    el.addClass('ok');
  }


  
  // reference
  function Reference (odkaz) {

    this.dir      = 'di/img/ref/';  
    this.prefix   = '';
    this.pocet    = 0;
    this.popis    = '';
    this.aktualni = 1;

    if (odkaz.length === 0)  return false;

    var arg = odkaz.rel.split(',');
    this.prefix = arg[0];
    this.pocet = parseInt( arg[1] );
    this.popis = unescape( arg[2] );
    if (arg.length > 3) {
      for (var ar = 3; ar < arg.length; ar++) {
        this.popis += ',' + unescape ( arg[ar] );
      }
    }

  }

  
  // ochrana proti mnohonasobnemu kliknuti (vazne to neni hezky kluci!)
  ref_konflikt = false;
  ref_timeout = null;

  function nastavReferenci (ref) {
    if (ref_konflikt)  return false;
    ref_konflikt = true;
    window.clearTimeout(ref_timeout);

    var doba = 1500;
    var src = ref.dir + ref.prefix + '_' + ref.aktualni + '.jpg';
    // spatne cachovani v IE
    if ($.browser.msie)  src += '?time=' + (new Date).getTime();

    
    // pracuje az po nacteni noveho obrazku
    var preloader = new Image();
    preloader.src = src;
    preloader.onload = function() {

      // nastavi pozadi jako novy obrazek
      $('#obsah_ref').css ( 'background-image', 'url('+this.src+')' );
      // necha zmizet stary a vykresli novy misto nej
      $('#obsah_ref > img').fadeOut(doba, obrazkuj);
      ref_timeout = window.setTimeout("ref_konflikt=false", doba);

    };

    // vykresli novy obrazek
    obrazkuj = function () {

      var ob = '<img src="' + src + '" alt="' + ref.prefix + '" width="600" height="450" />';
      ob += '<br /><div id="popis">' + ref.popis + '</div>';
      $('#obsah_ref').html ( ob );

    }


    // odkazy na jednotlive obrazky
    if (ref.pocet == 1) {
      // smazat menu
      $('#ref_menu').html('');
    }
    else {
      // nastavit menu
      var zvyr = ' class="aktualni"';
      var span = $('#ref_menu');
      span.html ( '' );
      for (i=1; i<=ref.pocet; i++) {
        var vyrazny = (i == ref.aktualni) ? zvyr : '';
        var oddel   = (i == ref.pocet) ? '' : ' | ';
        span.html( span.html() + '<a href="javascript:;" rel="' + i + '"' + vyrazny + '>' + i + '</a>' + oddel );
      }
      $('#ref_menu a').click(function() {
      
        ref.aktualni = parseInt(this.rel);
        nastavReferenci(ref);
      
      });
    }

  } 

