/**
 * Jolicloud Tour Slider.
 * Based on the jQuery Coda Slider: <http://jqueryfordesigners.com/demo/coda-slider.html>
 */
 
var tour_actual_item = 1;
var tour_total_items = 0;
 
function tour_replace_item(i) {
    var title = $('#tour .page:eq('+i+') h3').html();
    var description = $('#tour .page:eq('+i+') p').html();
    $('#tour .post_title h3').html(title);
    $('#tour .entry p').html(description);
	$("#tour .post_title h3").css({
      'text-align': 'left'
	});
    if(i == 0)
        $("#tour .action .previous").addClass("disabled")
    else
        $("#tour .action .previous").removeClass("disabled")
    if(i == tour_total_items - 1)
        $("#tour .action .next").addClass("disabled")
    else
        $("#tour .action .next").removeClass("disabled")
        
}
function tour_previous() {
    if(tour_actual_item <= 0) {
        tour_actual_item = tour_total_items-1;
		tour_update_item;};
    tour_actual_item--;
    tour_update_item();
}
function tour_next() {
    if(tour_actual_item >= tour_total_items - 1) {
        tour_actual_item = 0;
		tour_update_item;};
    tour_actual_item++;
    tour_update_item();
}
function tour_update_item() {
    var okay = false;
    $('#tour .subhead h3,#tour .entry p').fadeOut(250,function() {
        if(okay) return;
        okay = true;
        tour_replace_item(tour_actual_item);
        $('#tour .subhead h3,#tour .entry p').fadeIn(250);
    });
    
    $("#tour_sliding_inner").animate({
        'marginLeft': "-" + (tour_actual_item * 760) + "px"
    },{duration:500, queue:false});
}

$(document).ready(function() {

  var items = $('#tour .page');

  tour_total_items = items.length;

  // Float the panels left as we're going horizontal
  items.css({
    'float': 'left',
    'position': 'relative' // IE fix to ensure overflow is hidden
  });
  
  // Height of description
  //$('#tour .entry p').css({
  //    'height': '38px'
  //})

  // Hide h2 and p in the tour_items
  $('#tour .page h3,#tour .page p').css({
  //$('#tour .page p').css({
     'display': 'none' 
  });

  // Add the "previous" and "next" buttons dynamically
  $("#tour .action").html(
    '<a class="previous">Previous</a><a class="next">Next</a>'
  );
  
  // Add actions to buttons
  $("#tour .action .previous").click(function() {
      tour_previous(); 
  });
  $("#tour .action .next").click(function() {
      tour_next(); 
  });
  
  // Set overflow hidden
  $("#tour_sliding").css({
     'overflow': 'hidden',
     'width': '760px' 
  });
  
  // Enlarge tour_sliding_inner
  $("#tour_sliding_inner").css({
      'width': (tour_total_items * 760) + "px"
  });
  
  // Floating of all images
  $('#tour .page img').css({
      'float': 'left' 
  });

  tour_replace_item(tour_actual_item);
});
