$(document).ready(function() {
    $(document).pngFix();
	$(window).scroll(function () {
		var top = 155+$(window).scrollTop();
		$("#how_to_buy").css({ "top": top+"px"});
		$("#how_to_buy_btn").css({ "top": top+"px"});
	});
});

var timeoutID = null;
var current_index = 0;
var max_index = 2;
var play_state = true;
var playback_timeout = 5500;

function gotoPage(page_url, target) {
	if (page_url != "") {
		if(target != "" && target != null) {
			window.open(page_url,target);
		} else {
			window.location = page_url;
		}
	}
}

function swapImage(image, path) {
	var img = document.getElementById(image);
	if(img != null && path != null && path != "") {
		img.src = path;
	}
}

function swapSlideImage(slide_num, path) {
	var img = document.getElementById("slide_btn_" + slide_num);
	if(img != null && path != "") {
		if(slide_num != current_index) {
			img.src = path;
		}
	}
}

function showPlaybackOver() {
	if(play_state) {
		swapImage("slide_play_pause", "/img/home_slide_pause_over.gif");
	} else {
		swapImage("slide_play_pause", "/img/home_slide_play_over.gif");
	}
}

function showPlaybackOff() {
	if(play_state){
		swapImage("slide_play_pause", "/img/home_slide_pause.gif");
	} else {
		swapImage("slide_play_pause", "/img/home_slide_play.gif");
	}
}

function togglePlayback() {
	play_state = !play_state;
	if (timeoutID != null){
		clearInterval(timeoutID);
		timeoutID=null;
	}
	var obj = document.getElementById("slide_play_pause");
	if (obj != null){
		if (play_state){
			obj.src = "/img/home_slide_pause.gif";
		} else {
			obj.src = "/img/home_slide_play.gif";
		}
	}
	if (play_state){
		incrementImageAppear();
	}
}

function stopPlayback(){
	play_state = false;
	if (timeoutID != null){
		clearInterval(timeoutID);
		timeoutID=null;
	}
	var obj = document.getElementById("slide_play_pause");
	if (obj != null){
		obj.src = "/img/home_slide_play.gif";
	}
}

function startPlaybackTimer (){
	if (timeoutID != null){
		clearInterval(timeoutID);
		timeoutID=null;
	}
	timeoutID = setInterval ( "incrementImageAppear()", playback_timeout );
}

function incrementImage(){
	if (timeoutID != null){
		clearInterval(timeoutID);
		timeoutID=null;
	}
	current_index++;
	if (current_index > max_index){
		current_index = 0;
	}
	slide (current_index, false);
}

function incrementImageAppear(){
	var tmp_state = play_state;
	var tmp_index = current_index+1;
	if (tmp_index > max_index){
		tmp_index = 0;
	}
	showImage(tmp_index);
	play_state = tmp_state;
	if (play_state) startPlaybackTimer();
}

function showAndStopImage(index){
	showImage(index);
	stopPlayback();
}

function showImage(index){
	if (timeoutID != null){
		clearInterval(timeoutID);
		timeoutID=null;
	}
	if (index != current_index){
		var obj = document.getElementById("slide_btn_"+(current_index));
		if (obj != null){
			obj.src = "/img/home_slide_"+(current_index)+"_unselected.gif";
		}
		$('#slide_'+current_index).fadeOut(500);
		current_index = index;
		obj = document.getElementById("slide_btn_"+(current_index));
		if (obj != null){
			obj.src = "/img/home_slide_"+(current_index)+"_selected.gif";
		}
		$('#slide_'+current_index).fadeIn(500);
	}
	play_state = false;
}

function hideSlides(){
	for (i=0;i<=max_index;i++){
		if (i==0){
			$("#slide_"+i).show();
		} else {
			$("#slide_"+i).hide();
			var element = document.getElementById("slide_"+i);
			if (element != null){
				element.style.visibility = "visible";
			}
		}
	}
	startPlaybackTimer();
}
