/**
 * vades.cms plugin
 * Copyright (c) 2010 Martin Vach (www.vades.cz,www.vades.net)
 * Plugin to manipulate with the tables and forms on vades.cms front-end
 */

/**
 * vcmsSubmitForm
 * Submit the form
 * @param $form The form selector
 * @param $error_mess Error message
 * @param $ok_mess Ok message
 * @param $reset reset form values
 * @param $divselector selector for message and loading 
 */
function vcmsSubmitForm($form,$error_mess,$ok_mess,$reset,$divselector){
  // Clear the OK message
  $('.DivMess').remove();
  $('#LoadingWrap').remove();
  // ajaxSubmit function
  $($form).ajaxSubmit({
  	beforeSubmit: function() { 
  	 // Show loading message
     $('<div id="LoadingWrap"><div id="Loading">&nbsp;</div></div>').insertBefore($divselector);  
      },
  	//target: '#Formput',
  	 success: function(html) { 
  	     //$html_output = html.replace(/(<.*?>)/ig,"");
         // Hide loading message
      	 $('#LoadingWrap').fadeOut(); 
         if(html == 1){
            $('<div class="DivMess OkMessage">' + $ok_mess +'</div>').insertBefore($divselector); 
            $('.DivMess').fadeOut(6000);
            if($reset == 1){$($form).get(0).reset();}
         }
         else alert($error_mess);
      },
  		error: function(html) { 
  	 	// Hide loading message
      $('#LoadingWrap').fadeOut(); 
       alert($error_mess); 
      }
  
  	});
}

/**
 * vcmsSimpleAjax
 * Proccess PHP script
 * @param $method Used method (GET, POST...)
 * @param $url The php script transaction
 */
function vcmsSimpleAjax($method,$url){
 //start the ajax
  $.ajax({
    //What method is used?
    type: $method,
    //this is the php file that processes the data
    url: $url,// + '&nocache='+new Date().getTime(),
    //Do not cache the page
		cache: false
		
  });
}

/*
 * jQuery tooltips
 * Version 1.1  (April 6, 2010)
 * @requires jQuery v1.4.2+
 * @author Karl Swedberg
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
function FlipTip(){
  
var $liveTip = $('<div id="livetip"></div>').hide().appendTo('body'),
    $win = $(window),
    showTip;

var tip = {
  title: '',
  offset: 12,
  delay: 300,
  position: function(event) {
    var positions = {x: event.pageX, y: event.pageY};
    var dimensions = {
      x: [
        $win.width(),
        $liveTip.outerWidth()
      ],
      y: [
        $win.scrollTop() + $win.height(),
        $liveTip.outerHeight()
      ]
    };

    for ( var axis in dimensions ) {

      if (dimensions[axis][0] < dimensions[axis][1] + positions[axis] + this.offset) {
        positions[axis] -= dimensions[axis][1] + this.offset;
      } else {
        positions[axis] += this.offset;
      }

    }

    $liveTip.css({
      top: positions.y,
      left: positions.x
    });
  }
};

$('body').delegate('.FlipTip', 'mouseover mouseout mousemove', function(event) {
  var link = this,
      $link = $(this);

  if (event.type == 'mouseover') {
    tip.title = link.title;
    link.title = '';

    showTip = setTimeout(function() {

      $link.data('tipActive', true);

      tip.position(event);

      $liveTip
      .html('<div>' + tip.title + '</div>')
      .fadeOut(0)
      .fadeIn(200);

    }, tip.delay);
  }

  if (event.type == 'mouseout') {
    link.title = tip.title || link.title;
    if ($link.data('tipActive')) {
      $link.removeData('tipActive');
      $liveTip.hide();
    } else {
      clearTimeout(showTip);
    }
  }

  if (event.type == 'mousemove' && $link.data('tipActive')) {
    tip.position(event);
  }
  
});

}
