/*==============================================================================
 JavaScript Document // EDVENS MEDIA s.r.o. // revize 0.8

# CONTENT OF JS
-------------------------
01. jQuery: Initialize library
02. jQuery - function: FancyBox
03. jQuery - function: Animated Scrolling for Same-Page Links
04. jQuery - function: Ajax Send Form
==============================================================================*/

/* Initialize jQuery Library
==============================================================================*/
jQuery.noConflict();
jQuery(document).bind('jQuery', function(event, data) {

  // ---------------------------------------------------------------------------
  // Variable name 'data' is RESERVED ARRAY !!! for interact with internal 
  // JavaScripts within XHTML templates
  // for (i in data) { alert ('debug: ' + i + ' = ' + data[i]); }
  // ---------------------------------------------------------------------------

  /* jQuery - function: FancyBox (http://fancy.klade.lv/howto)
  ============================================================================*/
  jQuery('a.fancy').fancybox({
    'overlayOpacity' : 0.7,
    'zoomSpeedIn'    : 1000,
    'zoomSpeedOut'   : 1000,
    'overlayShow'    : true
  });
  jQuery('.fancy a').fancybox({
    'overlayOpacity' : 0.7,
    'zoomSpeedIn'    : 1000,
    'zoomSpeedOut'   : 1000,
    'overlayShow'    : true
  });
  jQuery('.iframe').fancybox({
    'overlayOpacity' : 0.7,
    'width'     : 600,
    'height'    : 400,
    'scrolling' : 'no',
    'type'      : 'iframe'
  });

  /* jQuery - function: Animated Scrolling for Same-Page Links
  ============================================================================*/
  function filter(string) {
    return string
      .replace(/^\//,'')
      .replace(/(index|default)\.[a-zA-Z]{3,4}$/,'')
      .replace(/\/$/,'')
  }
  jQuery('a[href*=#]:not([href*=#object])').each(function() {
    if (filter(location.pathname) == filter(this.pathname)
    && location.hostname == this.hostname
  	&& this.hash.replace(/#/,'') ) {
      var $targetId = jQuery(this.hash), $targetAnchor = jQuery('[name=' + this.hash.slice(1) +']');
      var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
      if ($target) {
        var targetOffset = $target.offset().top;
        jQuery(this).click(function() {
          jQuery('html, body').animate({scrollTop: targetOffset}, 400);
  	      return false;
  	    });
      }
    }
  });

  /* jQuery - function: Ajax Send Form
  ============================================================================*/
  jQuery('#form form').attr({action: './'});
  jQuery('#form form').attr({method: 'post'});
  jQuery('#form input.input-submit').click(function() {

    var jmeno       = jQuery('#form input#jmeno').val();
    var email       = jQuery('#form input#email').val();
    var kod         = jQuery('#form input#kod').val();
    var foto        = jQuery('#form input#foto').val();
    var souhlas     = jQuery('#form input#souhlas').is(':checked');

    if ((jmeno=='')||(email=='')||(kod=='')||(foto=='')||(!souhlas)) {
      jQuery('body').append('<a class="validMessage iframe" href=""></a>');
      jQuery('a.validMessage').fancybox({
        'href' : site + 'index_message.php?' + "jmeno=" + encodeURIComponent(jmeno) + "&email=" + email + "&kod=" + kod + "&foto=" + foto + "&souhlas=" + souhlas + "&form-empty",
        'overlayOpacity' : 0.7,
        'height' : 200,
        'width'  : 500
      }).trigger('click');
      return false;
    }

    else {
      //jQuery('a.uploadingFoto').attr({"display:''"});
      return false;
    }

  });

  /* jQuery - function: Show Message
  ============================================================================*/
  for (param in data) {
    if (param == 'message-ok') {
      jQuery(function() {
        jQuery('body').append('<a class="validMessageC iframe" href=""></a>');
        jQuery('a.validMessageC').fancybox({
          'href' : site + 'index_message.php?' + "title=" + encodeURIComponent(data[param]['title']) + "&text=" + encodeURIComponent(data[param]['text']) + "&form-sent-ok",
          'overlayOpacity' : 0.7,
          'height' : 100,
          'width'  : 500
        }).trigger('click');
      });
      jQuery("#form input#jmeno").val(data[param]['jmeno']);
      jQuery("#form input#email").val(data[param]['email']);
      jQuery("#form input#souhlas").attr('checked', true);
    }
  }

});
