$(document).ready(function() {

	jQuery("langselect UL li:last-child").addClass("last");


	$('#menu01 li a, #menu01 li ul').hover(
		function() {
			$(this)
				.parent()
					.children('a')
					.addClass('active')
				.parent()
					.children('ul')
					.show();
		},
		function() {
			$(this)
				.parent()
					.children('a')
					.removeClass('active')
				.parent()
					.children('ul')
					.hide();
		}
	);
	// Top menu spacer
	$('#menu01 > LI:eq(2)').addClass('spacer');


	$('UL.newslist LI').masonry({
		columnWidth: 252
	});


	$('.temp_visible').bind('click', function(){
		$('.temp_visible').toggleClass('active');
		$('.temp_hidden').slideToggle();
		return false;
	});


	$(".ngg-galleryoverview a, .content a:has(img)").colorbox();
	
	/*
	$("#front-cafes LI a").bind('mouseover', function(){
		$('.block01_backgrounder').css('background', 'url("'+$(this).attr('data-img')+'") no-repeat scroll 50% 0 transparent');
	});
	*/
	$('#map_myyrivahe').click(function(event) {
		event.preventDefault();
		zoomToMap('59.4347572, 24.7447186', 15);
	});
	$('#map_vene').click(function(event) {
		event.preventDefault();
		zoomToMap('59.4367828, 24.746397', 14);
	});
	$('#map_viru').click(function(event) {
		event.preventDefault();
		zoomToMap('59.4372406, 24.7537651', 14);
	});
	$('#map_hobujaama').click(function(event) {
		event.preventDefault();
		zoomToMap('59.4384079, 24.7572327', 14);
	});
	$('#map_viimsi').click(function(event) {
		event.preventDefault();
		zoomToMap('59.497139, 24.8285141', 12);
	});
	$('#map_perh_aatrium').click(function(event) {
		event.preventDefault();
		zoomToMap('59.39814, 24.6977787', 13);
	});
	$('#map_parnu_mnt').click(function(event) {
		event.preventDefault();
		zoomToMap('59.4306412, 24.745491', 13);
	});
});

function zoomToMap(coordinates, zoom) {
	document.getElementsByName('gm-map-1')[0].contentWindow.GeoMashup.map.setZoom(zoom);
	document.getElementsByName('gm-map-1')[0].contentWindow.GeoMashup.locations[coordinates].marker.click.fire();
}

function goToByScroll(id){
	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}
/*

jQuery(window).load(function(){
	try {
		var pagination_w = 0;
		var pagination_y = 0;
		var pagination_w = jQuery('.block01_c').width();
		var pagination_y = jQuery('#front-cafes').width();
		pagination_margin = parseInt((pagination_w - pagination_y) / 2 - 10 );
		jQuery('#front-cafes').css({"margin-left": pagination_margin });
	} catch (e) { }


	try {
		var offset_x = 0;
		var offset_y = 0;
		var offset_x = jQuery('.menu-products').width();
		var offset_y = jQuery('.menu-products H2').width();
		offset_margin = parseInt((offset_x - offset_y) / 2 - 60 );
		jQuery('.menu-products H2').css({"margin-left": offset_margin });
	} catch (e) { }
});
*/
$.fn.cycle.defaults.timeout = 6000;
$(function() {
	if ($('#front-cafes').length >= 1)
	{
		$('.block01_backgrounder').html('');
		$('#front-cafes LI').each(function(k, v){
			$(v).bind('mouseover', function(){
				 $(v).trigger('click');
			});
			if ($(v).data('img')) {				
				$('<div class="element"><img src="' + $(v).data('img') + '" alt="" /></div>').appendTo('.block01_backgrounder');
			} else {
				$('<div class="element"><img src="' + bloginfo_stylesheet_directory + '/img/tmp01.jpg" alt="" /></div>').appendTo('.block01_backgrounder');
			}
		});

		$('#front-cafes LI a').bind('click', function(){
			window.location.href = $(this).attr('href');
		});

		$('.block01_backgrounder').cycle({
			fx:     'fade',
			speed:   600,
			timeout: 6000,
			pause:   1,
			cleartype:  true,
			cleartypeNoBg:  true,
			pager:   '#front-cafes',
			pagerAnchorBuilder: function(idx, slide){
				return '#front-cafes li:eq(' + idx + ')';
			}
		});
	}
});

var mapCloseTimer = null;
var mapCloseTimerInt = 300;
var bannerPopupCarousel = {
	roller: null,
	options: {
		speed: 5000,
		enabled: true,
		currentItem: 0,
		itemsCount: 0
	},
	init: function(){
		this.options.itemsCount = jQuery("#banner-pops ul li").length;
		this.roller = setInterval(this.rotate, this.options.speed);
		// Show first item
		bannerPopupCarousel.show(bannerPopupCarousel.options.currentItem);
	},
	rotate: function(){
		if (bannerPopupCarousel.options.enabled != true) {
			return;
		}
		bannerPopupCarousel.options.currentItem++;
		if (bannerPopupCarousel.options.currentItem >= bannerPopupCarousel.options.itemsCount)
		{
			bannerPopupCarousel.options.currentItem = 0;
		}
		bannerPopupCarousel.show(bannerPopupCarousel.options.currentItem);
	},
	show: function(item){
		jQuery('#map-pop').css({"top": "-999px", "left": "-999px", "display": "none"});
		showBannerPopup(jQuery("#banner-pops ul li").eq(item));
	}
};


function showBannerPopup(item){
	try {
		var offset_top = jQuery(item).offset().top;
		var offset_left = jQuery(item).offset().left;
		var width = jQuery(item).width();
		var map_pop_w = jQuery('#map-pop').width();
		var map_pop_h = jQuery('#map-pop').height();

		jQuery('#map-pop').css({"top": offset_top - map_pop_h, "left": offset_left - map_pop_w + ( width / 2 + 15 ), "display": "block"});
		if (!setBannerPopData(jQuery(item).attr('class'))) {
			// jQuery('#map-pop').css({"top": "-999px", "left": "-999px", "display": "none"});
		}
	} catch (e) {}
};

jQuery(function(){
	if (jQuery("#banner-pops ul li").length) {
		bannerPopupCarousel.init();
	}

	jQuery("#banner-pops ul li").hover(
		function(){
			bannerPopupCarousel.options.enabled = false;
			showBannerPopup(jQuery(this));
			clearTimeout(mapCloseTimer);
		},
		function(){
			mapCloseTimer = setTimeout(function(){
				// jQuery('#map-pop').css({"top": "-999px", "left": "-999px", "display": "none"});
				bannerPopupCarousel.options.enabled = true;
			}, mapCloseTimerInt);
		}
	);
	jQuery('#map-pop').hover(
		function () {
			bannerPopupCarousel.options.enabled = false;
			clearTimeout(mapCloseTimer);
		},
		function () {
			mapCloseTimer = setTimeout(function(){
				// jQuery('#map-pop').css({"top": "-999px", "left": "-999px", "display": "none"});
				bannerPopupCarousel.options.enabled = true;
			}, mapCloseTimerInt);
		}
	);
});

function setBannerPopData(city){
	// Clear old stuff
	jQuery('#map-pop div.b').html('');
	// Return if data not found
	if (typeof agencyPopData[city] == 'undefined'){
		return false;
	}
	// Draw contacts for selected city
	jQuery('#map-pop div.b').html('<span class="title">' + agencyPopData[city].title + '</span>');
	// jQuery('<span>').text(agencyPopData[city].title2).appendTo('#map-pop div.b');
	/*
	var contacts = jQuery('<ul>');
		jQuery.each(agencyPopData[city].data, function(i, v){
			var clink = jQuery('<li>').text(v.address);
				jQuery('<br />').prependTo(clink);
				jQuery('<a>', {
					href: v.link,
					text: v.title,
					text: v.title2

				}).prependTo(clink);
				clink.appendTo(contacts);
		});
		contacts.appendTo('#map-pop div.b');
	*/
	return true;
}
