

var slideIndex = 2;

$(document).ready(function() {
	$('.slide').click(function(e) {
		e.preventDefault();
		$this = $(this);
		$('.slide').each(function(index) {
			$(this).removeClass('selected');
		});
		$this.addClass('selected');
		slideId = string_subtract($this.attr('id'), "slide");
		
		$('.slidecontent').hide();
		$('#slide'+slideId+'-content').fadeIn(2000);
		slideIndex = slideId;
		incrementIndex();
	});
	
	var timerId = window.setInterval(function() {
		$('#slide'+slideIndex).click();
	}, 8000);
	
	$(window).focus(function() {
		clearInterval(timerId);
        timerId = window.setInterval(function() {
			$('#slide'+slideIndex).click();
		}, 8000);
    });

    $(window).blur(function() {
        clearInterval(timerId);
    });

	/*$(window).focus(function() {
		console.log('in focus');
    });

    $(window).blur(function() {
		console.log("out of focus");
    });*/
	
	
	/*$('#hottest-article').click(function(e) {
		e.preventDefault();
		clearInterval(timerId);
	});*/
	
	var issuePosition = 1;
	
	$('#arrow-interactive-right').click(function(e) {
		e.preventDefault();

		issuePosition++;

		$.ajax({
			url: getBaseURL()+'home/getLatestIssues',
			type: 'POST',
			dataType: 'json',
			data: {position: issuePosition},
			complete: function(xhr, textStatus) {
			  //called when complete
			},
			success: function(data, textStatus, xhr) {
				$('#slides-holder-overflow').empty();
				$('#slides-holder-overflow').append(data.outputString);
				issuePosition = data.position;
			},
			error: function(xhr, textStatus, errorThrown) {
			  //called when there is an error
			}
		});

	});
	
	$('#arrow-interactive-left').click(function(e) {
		e.preventDefault();

		issuePosition--;

		$.ajax({
			url: getBaseURL()+'home/getLatestIssues',
			type: 'POST',
			dataType: 'json',
			data: {position: issuePosition},
			complete: function(xhr, textStatus) {
			  //called when complete
			},
			success: function(data, textStatus, xhr) {
				$('#slides-holder-overflow').empty();
				$('#slides-holder-overflow').append(data.outputString);
				issuePosition = data.position;
			},
			error: function(xhr, textStatus, errorThrown) {
			  //called when there is an error
			}
		});
	});
	
});

function incrementIndex()
{
	slideIndex++;
	if(slideIndex % 6 === 0) slideIndex = 1;
}
