var _ = {
    menu: {
        current: 0,
        timer: null,
		active: 0,
        show: function(id) {
            if (!id) return;
            var num = id.split('_');
            if (!num || !num[1]) return;
            num = num[1];
			if (num != this.active) $('#menu_href_'+ num).addClass('header_menu_block_href_active');
            if (this.current != num) this.hide_total(this.current);
            if (this.timer) clearTimeout(this.timer);
            $('#submenu_'+ num).show();
            this.current = num;
        },
        hide: function() {
            this.timer = setTimeout('_.menu.hide_total(\''+ this.current +'\')', 300);
        },
        hide_total: function(num) {
			if (num != this.active) $('#menu_href_'+ num).removeClass('header_menu_block_href_active');
            $('#submenu_'+ num).hide();
        }
    },
	contacts: {
		submit: function() {
			$('#contacts_form').submit();
		},
		validate: function() {
			var all = ['name', 'phone', 'email', 'message'], so = all.length, i = 0, o, error = false;
			for (; i < so ; i++) {
				o = $('#'+ all[i]);
				if (o.val() === '') {
					$('#'+ all[i]).addClass('input_err');
					error = true;
				} else $('#'+ all[i]).removeClass('input_err');
			}
			
			if (!/^[^\@]+\@[a-z0-9\-\_\.]+\.[a-z]{2,6}$/i.test($('#email').val())) {
				$('#email').addClass('input_err');
				error = true;
			} else $('#email').removeClass('input_err');

			return error;
		}
	},
	gallery: {
		images: [],
		current: 0,
		shift: 0,
		show: function (o) {
			if (!o || !o.src) return;
			var num = o.id.split('_')[1];
			if (!num) return;
			this.shift = num;
			var src = o.src.replace('&resizefit=240x240', '').replace('&watermark=2', '&resizefit=640x427&watermark=1');
			$('#gallery_image').attr('src', src);
		},
		next: function() {
			var current = _.gallery.current, total = _.gallery.images.length, shift = parseInt(this.shift);
			current++;
			var selected = current + shift;
			if (selected >= total) selected = shift - (total - current);
			$('#gallery_image').attr('src', _.gallery.images[selected].replace('&resizefit=240x240', '').replace('&watermark=2', '&resizefit=640x427&watermark=1'));
			this.left();
		},
		left: function(back) {
			var current = _.gallery.current, total = _.gallery.images.length, nums = [], i = 0;
			
			current++;
			if (current >= total) current = 0;
			
			for (; i < 5; i++) {
				next = i + current;
				if (next > total - 1) next = next - total;
				if (next < 0) next = 0;
				nums[i] = _.gallery.images[next];
			}

			_.gallery.current = current;
			if (total > 5) _.gallery.set_images(nums);
		},
		right: function() {
			var current = _.gallery.current, total = _.gallery.images.length, nums = [], i = 0;
			if (total < 6) return;

			current--;
			if (current <= 0) current = total - 1;
			
			for (; i < 5; i++) {
				next = i + current;
				if (next > total - 1) next = next - total;
				if (next < 0) next = 0;
				nums[i] = _.gallery.images[next];
			}

			_.gallery.current = current;
			_.gallery.set_images(nums);
		},
		set_images: function(nums) {
			for (var i = 0; i < 5; i++) {
				$('#image_'+ i).attr('src', nums[i]);
			}
		}
	},
	map: {
		projects: {},
		ids: {},
		current: 0,
		show_project: function(num) {
			if (!num) return;
			var so = this.count, i = 0, o;
			for (; i < so; i++) {
				o = this.projects[this.ids[i]];
				if (o.id == num) break;
			}
			if (!o) return;
			this.current = num;
			$('#project_image').attr('src', o['image']);
			$('#project_title').html(o['title']);
			$('#project_address').html(o['address']);
			$('#project_text').html(o['text']);
			$('#project_href').attr('href', o['href']);
		},
		next: function() {
			var current = this.current, so = this.count, i = 0, o, next = false;
			for (i in this.objects) {
				o = this.objects[i];
			}
		}
		/* this.show_project(o.id); */
	},
	plans: {
		hulls: {},
		current: 1, /* Hull */
		floor: 0, /* Floor */
		showflats: null,
		curr_hull: null,
		house_id: null,
		flat_plan_clicked: false,
		house_plan_image: null,
		show_hull: function(hull) {
			this.hide_all();
			if (!hull || !this.hulls[hull]) return;
			$('.all_coords').hide();
			$('#plan').show();
			$('#plan_over').show();

			$('.bottom_hrefs_js').removeClass('plans_house_top_table_td_href_active');
			$('#plan_top_href_'+ this.current).removeClass('plans_house_top_table_td_href_active');
			$('#floors_'+ this.current).hide();
			$('#floors_'+ hull).show();
			$('#plan_top_href_'+ hull).addClass('plans_house_top_table_td_href_active');
			$('#plan').attr('src', this.house_plan_image);

			this.current = hull;
			this.floor = 0;
		},
		/*show_floors: function(floor) {  // TODO REMOVE if no errors
			this.hide_all();
			if (floor === false) return;

			var hull = this.current;
			if (!this.hulls[hull] || !this.hulls[hull][floor]) return;
			
			
			$('.all_coords').hide();
			$('#plan').hide();
			$('#coords_'+ floor).show();
			$('.map').maphilight({alwaysOn: true});
			
			$('.bottom_hrefs_js').removeClass('plans_house_top_table_td_href_active');
			$('#plan').attr('src', this.hulls[hull][floor]);
			$('.plan_top_href_bottom_'+ floor).addClass('plans_house_top_table_td_href_active');

			this.floor = floor;
		},*/
		show_plan: function() {
			this.hide_all();
			var hull = this.current;
			$('#hull_'+ hull).show();
			if (this.house_id === '6') this.flat_plan_clicked = true;
			$('#plan_top_href_1').click(function() { 
				if (_.plans.flat_plan_clicked) _.plans.show_plan(); 
			});
			$('#plan_top_href_2').click(function() { 
				if (_.plans.flat_plan_clicked) _.plans.show_plan(); 
			});
		},
		hide_all: function() {
			var hull = this.current, floor = this.floor;
			$('#plan_over').hide();
			$('#hull_'+ hull).hide();
			$('#floor_'+ floor).hide();	
		}
	},
	first_banners: {
		banners: {},
		current: 0,
		total: null,
		next: function() {
			var el, next = this.current + 1, i = 0;
			if (next == this.total) next = 0;
			for (el in this.banners) {
				if (i == next) this.set(this.banners[el]);
				i++;
			}
			this.current = next;
		},
		set: function(data) {
			if (!data) return;
			$('#top_banner_src').attr('src', data['file']);
			$('#top_banner_text').html(data['text']);
			$('.top_banner_href_js').attr('href', data['link']);
		}
	},
	show_overlay: function(num, floor) {
		if (!floor) floor = num;
		$('.over_lay').hide();	
		//if (_.plans.current === 2) {
		//	test = num + 4;
		//	$('#overlay_'+ test).show();
		//} else {
			$('#overlay_'+ num).show();
		//}
		$('.bottom_hrefs_js').removeClass('plans_house_top_table_td_href_active');
		$('.plan_top_href_bottom_'+ floor).addClass('plans_house_top_table_td_href_active');
	},
	hide_overlays: function() {
		$('.over_lay').hide();
		$('.bottom_hrefs_js').removeClass('plans_house_top_table_td_href_active');
	}
}

$(function() {
    $('.header_menu_block').hover(
    function() {
        _.menu.show(this.id);
    }, function() {
       _.menu.hide();
    });
	
	$('.gallery_bottom_images_inner_block_img').click(
	function() {
		_.gallery.show(this);
	});
	
	$('#gallery_image').click(
	function() {
		_.gallery.next();
	});
	if (_.plans.showflats === 'all') _.plans.show_plan();
});
