// ACTIONS

var delay = 5000;
var switch_delay = 500;
var ajax_script = "controller/ajax/actions.php";
var img_dir = "files/actions";
var actions_slider_elm = $("actions_slider");
var images = Array();
var img_width = 280;
var img_height = 170;
var nextImg = 1;

function slide_action() {
	if(images.length > 1) {
		var prevImg = nextImg - 1;
		
		if(prevImg < 0)
			prevImg = images.length - 1;
		
		//let previous image disappear
		var prevImgID = "action_img_" + prevImg;
		new Effect.Opacity(images[prevImg].id, { duration: 2.0, from: 1.0, to: 0.0 } );
		
		//let next image appear
		var nextImgID = "action_img_" + nextImg;
		new Effect.Opacity(images[nextImg].id, { duration: 2.0, from: 0.0, to: 1.0 } );
		
		nextImg++;
		if(nextImg >= images.length)
			nextImg = 0;
		
		setTimeout(slide_action, delay);
	}
}

//get images
new Ajax.Request(ajax_script, {  
	method: "post",  
	parameters: { data: "action=get_actions" }, 
	onSuccess: function(response) {
		
		var actions = response.responseXML.getElementsByTagName("action");
		if(actions.length > 0) {
			
			//get image properties for each image
			for(var i = 0; i < actions.length; i++) {
				var image = new Image();
				//image.width = img_width;
				//image.height = img_height;
				image.id = "action_img_" + i;
				image.src = img_dir + "/" + actions[i].getElementsByTagName("image_file_id")[0].firstChild.nodeValue + ".jpg";
				images[i] = image;
				
				//add image to DIV and make it invisible
				actions_slider_elm.appendChild(image);
				$(image.id).setOpacity(0.0);
			}
		}
		
		//let first image appear and start slide function
		new Effect.Opacity("action_img_0", { duration: 2.0, from: 0.0, to: 1.0 } );
		setTimeout(slide_action, delay);
	}, 
	onFailure: function(response) {
		alert("Ajax error: " + response.status + " -- " + response.statusText);
	} 
});


// DEALS

var deals_pause_delay = 3000; //ms
var deals_step = 10; //px
var deals_item_width = 280; //px
var deals_item_height =  155;//px

var deals_container_width = '';
var deals_scroll_content = '';
var deals_scroll_content_height = '';
var deals_timer = 0;
var deals_item_counter = 0;
var deals_total_items = 0;

function deals_start_scrolling() {
	deals_timer = setInterval(scroll_horizontal, 35);
}

function deals_pause_scrolling() {
	clearInterval(deals_timer);
	setTimeout(deals_start_scrolling, deals_pause_delay);
}

function scroll_horizontal() {
	
	if (parseInt(deals_scroll_content.style.left) < (deals_item_width * -1 * deals_item_counter + deals_step)) {
		deals_item_counter++;		
		if(deals_item_counter > deals_total_items) {
			//rewind
			deals_scroll_content.style.left = parseInt(deals_container_width) + "px";
			deals_item_counter = 0;
		} else {
			//pause
			deals_pause_scrolling();
		}
	} else {
		//scroll
		deals_scroll_content.style.left = (parseInt(deals_scroll_content.style.left) - deals_step) + "px";
	}
}

function initialize_deals_scroll() {
	deals_container_width = $("deals_scroll_container").offsetWidth;
	
	deals_scroll_content = $("deals_scroll_content");
	
	deals_scroll_content.style.top = 0;
	deals_scroll_content.style.left = parseInt(deals_container_width) + "px";
	
	deals_total_items = $$(".deal").length;
	
	//start scrolling
	deals_start_scrolling();
}

initialize_deals_scroll();


// POPULAR PRODUCTS

var popular_pause_delay = 3500; //ms
var popular_step = 10; //px
var popular_item_width = 280; //px
var popular_item_height =  155;//px

var popular_container_height = '';
var popular_scroll_content = '';
var popular_timer = 0;
var popular_item_counter = 0;
var popular_total_items = 0;

function popular_start_scrolling() {
	popular_timer = setInterval(scroll_vertical, 35);
}

function popular_pause_scrolling() {
	clearInterval(popular_timer);
	setTimeout(popular_start_scrolling, popular_pause_delay);
}

function scroll_vertical() {
	
	if (parseInt(popular_scroll_content.style.top) < (popular_item_height * -1 * popular_item_counter + popular_step)) {
		popular_item_counter++;		
		if(popular_item_counter > popular_total_items) {
			//rewind
			popular_scroll_content.style.top = parseInt(popular_container_height) + "px";
			popular_item_counter = 0;
		} else {
			//pause
			popular_pause_scrolling();
		}
	} else {
		//scroll
		popular_scroll_content.style.top = (parseInt(popular_scroll_content.style.top) - popular_step) + "px";
	}
}

function initialize_popular_scroll() {
	popular_container_height = $("popular_scroll_container").offsetHeight;
	
	popular_scroll_content = $("popular_scroll_content");
	popular_scroll_content.style.top = parseInt(popular_container_height) + "px";
	popular_scroll_content.style.left = 0;
	
	popular_total_items = $$(".popular_product").length;
	
	//start scrolling
	popular_start_scrolling();
}

//initialize_popular_scroll();
