var lifecycle_id=1;
var maxLifecycleID;
var lifecycleImage;
var lifecycleTitle;
var copyLifecycleText;
var more_url_lifecycle;
var download_url_lifecycle;
var isLifecycleAnimating;
var intervalID;
  
function cycle(){

  var newID = lifecycle_id;
  newID++;
  if(lifecycle_id < maxLifecycleID){
    $('#t'+newID).click();
  }else{
    $('#t1').click();
  }
  intervalID = setTimeout('cycle()', 8000);
  
}

function buildSection(lifecycleImage, lifecycleTitle, copyLifecycleText, more_url_lifecycle, download_url_lifecycle, xml_id){
	
	
	  // Build HTML string and return
	  output = '';
	  output += '<div class="slider-cnt" id="tab-nav-'+xml_id+'">';
	  output += '<ul><li><img src="'+lifecycleImage+'" alt=""/>';
	  output += '<div class="info"><h3>'+lifecycleTitle+'</h3>';
	  output += '<p>'+copyLifecycleText+'</p>';
	  output += '<p class="info-links">';
	
	  if(more_url_lifecycle.length > 0){output += '<a href="'+more_url_lifecycle+'" class="blue-btn"><span>More</span></a>';}
      if(download_url_lifecycle.length > 0){output += '<a href="'+download_url_lifecycle+'" target="_blank" class="blue-btn"><span>Download chart</span></a>';}

	  output += '</p><div class="cl">&nbsp;</div>';  
	  output += '</div></li></ul><div class="cl">&nbsp;</div></div>';
	  return output;
	
}

function handleLifecycleXML (xml){
	
	var xml_id = 1;
	var section_html = '';	
	
	$('section',xml).each(function() {
		
		lifecycleImage = $(this).find("pic").text();
	    lifecycleTitle = $(this).find("section_title").text();
	    copyLifecycleText = $(this).find("copy").text();
	    more_url_lifecycle = $(this).find("more_url").text();
	    download_url_lifecycle= $(this).find("download_url").text();
	
	    mydata = buildSection(lifecycleImage, lifecycleTitle, copyLifecycleText, more_url_lifecycle, download_url_lifecycle, xml_id);
	    section_html = section_html + mydata;
	    xml_id++;
	});
	
	section_html += '<p class="prev-btn"><a href="javascript: void(0);" class="notext">prev</a></p><p class="next-btn"><a href="javascript: void(0);" class="notext">next</a></p>';
	
	$("#tab-nav").append(section_html);
	
	$(".slider-cnt").hide();
	$("#tab-nav-1").show();
	maxLifecycleID = xml_id-1;
	// if (maxLifecycleID > 1){
	//   	//intervalID = setTimeout('cycle()', 5000);
	// }

	$('.slider-nav a').click(function() {
	    if (!isLifecycleAnimating){
  	
		var $this = $(this);
	    var rawID = $this.attr("id");
		var y = rawID.charAt(rawID.length-1);
		
		if(y != lifecycle_id){
		  isLifecycleAnimating = true;
	      $('.slider-nav a').removeClass('active');
		  $('.slider-nav a').parent().removeClass('ui-state-active');
	      $('a#t'+y).addClass('active');
	  	  $('a#t'+y).parent().addClass('ui-state-active');
	      $("#tab-nav-"+lifecycle_id).hide();
	   	  lifecycle_id=y;
	      $("#tab-nav-"+y).show(); 		
		  isLifecycleAnimating = false;
	      clearInterval(intervalID);  
		}
	    }
	  });
	
	$('.prev-btn a').click(function(){
		var newID = lifecycle_id;
		newID--;
		if(newID > 0){
		  $('#t'+newID).click();
		}else{
		  $('#t8').click();
		}

	});
	$('.next-btn a').click(function(){
		var newID = lifecycle_id;
		newID++;
		if(lifecycle_id < maxLifecycleID){
		  $('#t'+newID).click();
		}else{
		  $('#t1').click();
		}
	});
}

$(document).ready(function(){ 
  
  $.get("sites/all/themes/sme/xml/static_lifecycle.xml",{}, handleLifecycleXML);
});
