/**
 * jQuery utility
 * Copyright (c) 2010 Martin Vach (www.vades.net)
 * 
 */

/**
 * 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);  
      },
  		success: function(html) { 
         // Hide loading message
      	 $('#LoadingWrap').fadeOut(); 
         if(html == 1){
            $('<div class="DivMess OkMessage">' + $ok_mess +'</div>').insertBefore($divselector); 
            $('.DivMess').fadeOut(6000);
						// Reset form values after submit
            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
		
  });
}
/**
 * vcmsTabs
 * Display tab content
 */
function vcmsTabs(){
  //Default Action
  $(".tab_content").hide(); //Hide all content
  $("ul.tabs li:first").addClass("active").show(); //Activate first tab
  $(".tab_content:first").show(); //Show first tab content

  //On Click Event
  $("ul.tabs li").click(function() {
	  $("ul.tabs li").removeClass("active"); //Remove any "active" class
	  $(this).addClass("active"); //Add "active" class to selected tab
	  $(".tab_content").hide(); //Hide all tab content
	  var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
	  $(activeTab).show(); //Fade in the active content
	  return false;
  });
}

/**
 * SLIDESHOW - Switch images 
 */
function slideSwitch() {

  $('#Slideshow img').css({'cursor':'pointer'});
  $('#Slideshow img').click(function(){
		$link = $(this).attr('rel');
		window.location=$link;
  });
  var $active = $('#Slideshow img.active');
  if ( $active.length == 0 ) $active = $('#Slideshow img:last');
  // use this to pull the images in the order they appear in the markup
  var $next =  $active.next().length ? $active.next(): $('#Slideshow img:first');
  // uncomment the 3 lines below to pull the images in random order
  // var $sibs  = $active.siblings();
  // var rndNum = Math.floor(Math.random() * $sibs.length );
  // var $next  = $( $sibs[ rndNum ] );
  $active.addClass('last-active');
  $next.css({opacity: 0.0})
      .addClass('active')
      .animate({opacity: 1.0}, 1000, function() {
          $active.removeClass('active last-active');
      });
}

