header_location = 1;

sky_animation_duration = 2000;
product_animation_duration = 700;

var carousel = null;
var certificates = null;
var products = null;
var fancy_sliding = null;

var active_element = 0;

function setup_slider_width(first) {

	if (first == "first")
	{
		w = $(window).width();
		// $('.header_item').css('width', w);
	}
	w = $(window).width();

	// $('.header_item').eq(active_element).css('width', w);
	
	pom = parseInt((w - 980) / 2);
	$('.prev').css('left', pom);
	
	pom += 980;
	$('.next').css('left', pom);
	
	// $('#products').trigger( 'goto', [ 3 ] );

	// console.log($('#products').serialScroll);

};

var resizeTimer;
	
$(function(){

	fancy_sliding = new Fancy_sliding();
	fancy_sliding.initialize();
	
	carousel = new Carousel();
	carousel.initialize();
	
	certificates = new Certificates();
	certificates.initialize();
	
	products = new Products();
	products.initialize();
	
	box = new Box();
	box.initialize();
	
	setup_slider_width('first');
	
	$(window).resize(function() {
		// console.log($(window).width());
		// clearTimeout(resizeTimer);
		// resizeTimer = setTimeout(function(){ setup_slider_width() }, 20);
		setup_slider_width();
	});
});


function Carousel(element_name)
{
	selected = 0;
	
	if (!element_name) element_name = "scrollable";
	var element = $('#' + element_name);
	
	this.initialize = function()
	{
		$(element).serialScroll({
			items:'div.carousel_item', //selector to the items ( relative to the matched elements, '#sections' in this case )
			axis:'y',
			cycle: true,
			stop: true, 
			lock: false,
			duration: 1000,
			interval: 6000,
			constant: false,
			force: true,
			onBefore:function( e, elem, $pane ){
				id = parseInt($(elem).attr('id').substring(14));
				$('.carousel_handle').removeClass('selected');
				$('#carousel_handle_' + id).addClass('selected');
			},
			onAfter:function( elem ){
			}
			

		});
		
		$(element).hover(function(){
			$(this).trigger('stop');
		},function(){
			$(this).trigger('start');
		});
	}
	
	this.go_to = function(location)
	{
		$('.carousel_handle').removeClass('selected');
		$('#carousel_handle_' + location).addClass('selected');
		$(element).trigger( 'goto', [location] );
		
		$(element).trigger('stop');
	}
}
function Certificates()
{
	this.initialize = function()
	{
		$('#certificates').serialScroll({
			items:'div.certificate',
			axis:'y',
			cycle: true,
			stop: true, 
			duration: 700,
			interval: 6000,
			force: true,
			constant: false
		});
		
		$('#certificates').hover(function(){
			$(this).trigger('stop');
		},function(){
			$(this).trigger('start');
		});
	}
}
function Products()
{
	this.initialize = function()
	{
		$('#products').serialScroll({
			items:'div.header_item',
			axis:'x',
			cycle: false,
			lock: false,
			stop: false, 
			duration: product_animation_duration,
			easing: "easeOutQuad",
			onBefore: function( e, elem, $pane, $items, pos )
			{
				active_element = pos;
			}
		});
		
		// $('#products').serialScroll.setEasing = function(i){
			// easing = i;
		// }
	
	}
	this.setEasing = function(newEasing)
	{
		$('#products').serialScroll.setEasing(newEasing);
	}
}
function Box()
{
	this.initialize = function()
	{
		$('#scrollable_box').serialScroll({
			items:'div.box_item',
			axis:'x',
			cycle: true,
			lock: false,
			stop: false, 
			duration: 600,
			interval: 8000,
			force: true
		});
	
	}
}
function Fancy_sliding()
{

	this.step_sky = 140;
	this.step_city = 80;
	
	this.slideshow_duration = 3000;
	
	this.storedTimeoutID = null;
	
	this.initialize = function()
	{
		this.storedTimeoutID = setTimeout("fancy_sliding.move('left', false);", this.slideshow_duration);
	}
	this.move = function(direction, stop)
	{
		if (stop) 
		{
			if (this.storedTimeoutID) clearTimeout(this.storedTimeoutID);
			// products.setEasing('easeInBounce');
		}
		
		if (direction == "left")
		{
			if (header_location == header_tab_number) return;
			header_location++;
			prefix = "-=";
			$('#products').trigger('next');
		}
		if (direction == "right")
		{
			if (header_location == 1) return;
			header_location--;
			prefix = "+=";
			$('#products').trigger('prev');
		}
		
		p_sky = prefix + this.step_sky;
		p_city = prefix + this.step_city;
		
		effect = "easeOutCubic";
		duration = sky_animation_duration; 
		
		//animiraj pomicanje neba i grada
		$('.sky').stop(true, false).animate({ left: p_sky }, duration, effect);
		$('.city').stop(true, false).animate({ left: p_city }, duration, effect);
		
		if (!stop)
		{
			if (direction == "right") 
			{
				if (header_location == 1) this.storedTimeoutID = setTimeout("fancy_sliding.move('left', false);", this.slideshow_duration);
				else this.storedTimeoutID = setTimeout("fancy_sliding.move('right', false);", this.slideshow_duration);
			}
			else 
			{
				if (header_location == header_tab_number) this.storedTimeoutID = setTimeout("fancy_sliding.move('right', false);", this.slideshow_duration);
				else this.storedTimeoutID = setTimeout("fancy_sliding.move('left', false);", this.slideshow_duration);
			}
		}
	} 
	
}
