$(document).ready(function() {  
	var div_floor = $('#esk2 div');
	$('#esk2 div a').click(function(){
        var parent = $(this).parent().get(0);
		var index = $(div_floor).index(parent);
		index = div_floor.length - index - 1;
		var index_active = 1;
		var k = div_floor.length;
		for (i = 0; i <= div_floor.length; i++){
			k--;
			if($(div_floor[i]).is(".active_floor")) {
				index_active = k;
			}
		}
		escalator.motion(index, index_active);
		return false;
	});
	/*var nav_link = $(".navigation a");
	$(nav_link[2]).css('color', '#ED1C24');
	var data_link = '';
	$(nav_link[2]).mouseover(function(){
		if(data_link == ''){
			$.getJSON('/atrium/get_info/', function(data) {
				if (!data.error) {
					data_link = data.preview;
					$('#tooltip_atrium').html(data_link);
				}
			});
		}
	});
	$(nav_link[2]).tooltip({ 
		// place tooltip on the right edge 
		position: "top center", 
		// use the built-in fadeIn/fadeOut effect 
		effect: 'fade',
		// use this single tooltip element 
		tip: '#tooltip_atrium' 
	});*/
});


ghtml = {
    options: function(list, selected, first) {
        var code = '';
        if (first && first !== undefined) {
            code += '<option value="">'+first+'</option>';
        }
        $.each(list, function(i, n) {
            code += '<option value="'+i+'" ';
            if (selected && selected !== undefined && selected == i) {
                code += 'selected = "selected"';
            }
            code += '>'+n+'</option>';
        });
        return code;
    }
}

captcha = {
	refresh: function() {
		$('#captcha_img').attr('src', '/contacts/captcha/'+Number(new Date()));
	}
}

img_scroller = {
	data: new Array,
	interval: 5000,
	scroller: null,
	iteration: 1,
	init: function(data, interval) {
		this.scroller = $('#img_scroller');
		this.data = data;
		if (interval && interval !== undefined) this.interval = interval;
		
		if (this.data === undefined || this.data.length == 0) return false;
		this.scroller.append('<div style="background-image: url('+this.data[0]+');"></div>');
		
		if (this.data.length > 1)	this.start();
	},
	
	start: function() {
		if (img_scroller.iteration > img_scroller.data.length-1) {
			var src = img_scroller.data[0];
			img_scroller.iteration = 0;
		} else {
			var src = img_scroller.data[img_scroller.iteration];
		}
		img_scroller.scroller.append('<div style="background-image: url('+src+');z-index: 5;"></div>');
		
		setTimeout(
			function(){
				var imgs = img_scroller.scroller.find('div');
				var i0 = $(imgs[0]);
				var i1 = $(imgs[1]);
				i1.css({'opacity': 0}).animate({'opacity': 1}, 2000);
				//i0.animate({'marginTop': '-283px', 'opacity': 0}, 2000, function() {
				i0.animate({'opacity': 0}, 2000, function() {
					img_scroller.iteration++;
					i0.remove();
                    i1.css('z-index', 10);
					img_scroller.start();
				});
			},
			img_scroller.interval
		);
	}
}

jQuery.fn.get_pos = function() {
	var x = 0, y = 0;
	var el = this.get(0);
	while(el) {
		x += el.offsetLeft || 0;
		y += el.offsetTop || 0;
		el = el.offsetParent;
	}
	return {x:x, y:y};
}

escalator = {
	floor_ar: [
		{id : '#floor_numer0', left : '0', bottom : '0', z_index : '300'},
		{id : '#floor_numer1', left : '40px', bottom : '12px', z_index : '300'},
		{id : '#floor_numer2', left : '106px', bottom : '66px', z_index : '300'},
		{id : '#floor_numer3', left : '40px', bottom : '115px', z_index : '100'},
		{id : '#floor_numer4', left : '106px', bottom : '160px', z_index : '300'},
		{id : '#floor_numer5', left : '40px', bottom : '216px', z_index : '100'},
		{id : '#floor_numer6', left : '0', bottom : '0', z_index : '100'}
	],
	init: function(level) {
		var floor = escalator.floor_ar[level];
		$("#esk2 .active_floor").removeClass("active_floor");
		$(floor.id).addClass("active_floor");
		$('#man').css({"zIndex" : floor.z_index, 'left': floor.left, 'bottom': floor.bottom});
	},
	motion: function(i1, i2) {
		if (i1 == i2) 
			document.location.href = '/shop/map/'+i2+'/';
		if (i1 > i2){
			for (var i = i2; i <= i1; i++){
				escalator.move(i, i1);
			}
		} else {
			for (var i = i2; i >= i1; i--) {
				escalator.move(i, i1);
			}
		}
		return false;
	},
	move: function(i, res) {
        var floor = escalator.floor_ar[i];
		$('#man').animate({ 
				left : floor.left,
				bottom: floor.bottom
		}, 600, function(){
			$("#esk2 .active_floor").removeClass("active_floor");
			$(floor.id).addClass("active_floor");
			var nf = escalator.floor_ar[i+1];
			if (nf && nf !== undefined) {
				$('#man').css({"zIndex" : nf.z_index});
			}
			if(i == res) {
				document.location.href = '/shop/map/'+i+'/';
			}
		});
	}
}
