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

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

/* 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: 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 Contact
  ============================================================================*/
  jQuery('#form form').attr({action: "./"});
  jQuery('#form form').attr({method: "post"});
  jQuery('#form input.submit').click(function() {

    var jmeno       = jQuery("#form input#name").val();
    var predmet     = jQuery("#form input#subject").val();
    var zprava      = jQuery("#form textarea#message").val();
    var email       = jQuery("#form input#e-mail").val();

    if ((jmeno=="")||(zprava=="")||(email=="")) { alert('Položky označené hvězdičkou je nutné vyplnit.'); }
    else
    {
      jQuery.ajax({
        type: "POST",
        url: "./",
        data: "jmeno=" + encodeURIComponent(jmeno) + "&predmet=" + encodeURIComponent(predmet) + "&zprava=" + encodeURIComponent(zprava) + "&email=" + email + "&phpmailer=",
        success: function(msg){
          if (msg.substr(0,1)=="1")
          { // uspesne odeslano, proto vymazeme kolonky
            jQuery("#form input#subject").val("");
            jQuery("#form textarea#message").val("");
          }
          jQuery("#mail-status").html( msg.substr(1) );
        }
      });
    }
    return false;
  });

});
