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

# 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
05. jQuery - function: Ajax Send Book
06. jQuery - function: InnerFade
==============================================================================*/

/* 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: Ajax Send Book
  ============================================================================*/
  var book_form_values = new Array('', 'Zde napište váš vzkaz.');
  function book_form_focus(value) {
    for (i in book_form_values) if (book_form_values[i] == value) return true;
    return false;
  }
  jQuery('#book_form input').focus(function(){ if (book_form_focus(jQuery(this).val())) jQuery(this).val('') });
  jQuery('#book_form textarea').focus(function(){ if (book_form_focus(jQuery(this).val())) jQuery(this).val('') });
  jQuery('#book_form form').attr({action: window.location.href});
  jQuery('#book_form form').attr({method: 'post'});
  jQuery('#book_form form').submit(function() {

    var jmeno       = jQuery('#book_form input#jmeno').val();
    var email       = jQuery('#book_form input#email').val();
    var text        = jQuery('#book_form textarea#text').val();

    if (book_form_focus(jmeno) || book_form_focus(text)) alert('Položky označené hvězdičkou je nutné vyplnit.');
    else jQuery(this).submit();
    return false;
    
  });

  /* jQuery - function: Ajax Send Newsletter
  ============================================================================*/
  jQuery('form#newsletter').attr({action: window.location.href});
  jQuery('form#newsletter').attr({method: 'post'});
  jQuery('form#newsletter').submit(function() {
    var email = jQuery('#newsletter input#odber-akci').val();
    if (email=='' || email=='Sem zadejte svůj email') alert('Je nutné vyplnit váš e-mail.');
    else jQuery(this).submit();
    return false;
  });

  /* jQuery - function: InnerFade
  ============================================================================*/
  jQuery('.inner-slider').innerfade({
    speed: 500,
    timeout: 5000,
    type: 'sequence',
    containerheight: 	'301px',
    slide_timer_on: 'yes',
    slide_ui_parent: 'inner-slider',
    slide_nav_id:	 'controls'
  });

  /* 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);
    }
  }

  /* jQuery - function: lex
  ============================================================================*/
  jQuery(".toggle-container").hide();
  jQuery(".trigger").click(function(){
    jQuery(this).toggleClass("trigger-ac").next().slideToggle("slow");
  });

});
