
window.BASE_HREF = window.BASE_HREF || '../';
var presbeton = window.presbeton = window.presbeton || {};

$(document).ready(function()
{
	$('.jshide').hide();
	$.fn.kfBox && $('.lightbox').kfBox();
	$('.col-product-box-a').each(function(i){
		$(this).find('.lightbox2').kfBox();
	});
	$('a.ext, a.external').click(function(){return !window.open($(this).attr("href"))});
	
	$('table tr:nth-child(odd)').addClass('even');

	$('#q').inputDefaultText({ text: 'Hledaný výraz'});
	
	if($('.photogallery').size())
	{
		var photogallery = new window.presbeton.PhotoGalleryController();
		photogallery.init();
	}
	
	if($('#map').size())
	{
		var map = new presbeton.MapController('#map-svg', mapOptions);
		map.init();
	}
	
	$('a.panzoom').bind('click', function(){
		var dialog = new presbeton.PanZoomDialog({ imgUrl: $(this).attr('href') });
		dialog.init();
		dialog.open();
		return false;
	});
	
	if($('.intro-frames').size())
	{
		var intro = new presbeton.IntroboxController({ timeout: 4000, speed: 500 });
		intro.init();
	}
	
	/* Thumbnails Scroller */
	$('.photogallery2 .thumbnails').wrapInner('<div class="hscroll-box"></div>');
	var $prevnext2 = $('<div class="prevnext"><a href="#" class="prev">předchozí</a> <a href="#" class="next">další</a></div>');
    $('.photogallery2 .thumbnails .hscroll-box').before($prevnext2);
    this.thumbnailsScroller = new kff.widgets.Scroller('.photogallery2 .thumbnails .hscroll-box', { 
		scrollWidth: 580, 
		carouselWidth: 565, 
		prev: '.photogallery2 .thumbnails .prev', 
		next: '.photogallery2 .thumbnails .next' 
	});
	this.thumbnailsScroller.init();
	
	$('.col-contact-a, .contact-map').each(function(){
		var $address = $(this).find('.address');
		var $map = $(this).find('.googlemap');
		if($address.size() && $map.size())
		{
			var gmap = new presbeton.GoogleMap({ element: $map });
			gmap.init();
			gmap.showAddress($address.text());
		}
	});
	
	if($('.product-detail').size() && presbeton.ProductController) new presbeton.ProductController().init();
	
	$('.btn-related-files').bind('click', function(){ $('#related-files-wrap').slideToggle(200); return false; });
	$('#related-files-wrap').slideToggle(0);
	
	$('.legend .ico-arrow-down').bind('click', function(){ $('.legend .inner').slideToggle(200); return false; });
	$('.legend .inner').slideToggle(0);
	
	if($.browser.msie)
	{
		var $css3 = $('css3-container');
		var $css3Prev = $css3.prev();
		$(window).bind("beforeprint", function(){
			$css3.remove();
		});
		$(window).bind("afterprint", function(){
			$css3Prev.after($css3);
		});	
	}
	
	$('#contactForm-name').inputDefaultText({ text: 'Karel Novák'});
	$('#contactForm-email').inputDefaultText({ text: 'karel@novak.cz'});
	$('#contactForm-city').inputDefaultText({ text: 'Město, kde se nacházíte'});
	$('#contactForm-message').inputDefaultText({ text: 'Sem napište zprávu'});
	$('#address').inputDefaultText({ text: 'Vložte Vaši adresu'});
	
		
});

$(window).bind('load', function()
{
	fontSizeListener('#footer', equalizeHeights);
});


$.fn.inputDefaultText = function(options)
{
	options = $.extend({
		text: 'Hledany vyraz'
	}, options);
	
	return this.each(function()
	{
		var $this = $(this);
		if($this.val() == options.text || $this.val() == '') $this.val(options.text);
		$this
			.bind('focus', function(){ if($(this).val() == options.text) $(this).val(''); })
			.bind('blur', function(){ if($(this).val() == '') $(this).val(options.text); });
		
		$this.closest('form').bind('submit', function(){
			if($this.val() == options.text) $this.val('');
		});
	});
};
$.fn.kfEqualizeColumns = function(options)
{
	options = $.extend({
		column: '>li'
	}, options);

	return this.each(function(i)
	{
		var $columns = $(options.column, this);
		var maxHeight = 0;
		$columns
			.height('auto')
			.each(function(){
				var h = $(this).height();
				if(h > maxHeight) maxHeight = h;
			})
			.height(maxHeight);
	});
};

function fontSizeListener($el, callback)
{
	$el = $($el);
	var h = 0;
	var interval = setInterval(function()
	{
		if($el.height() != h){
			h = parseInt($el.height());
			callback();
		}
	}, 200);
};

function equalizeHeights()
{
	$('.sortiment2 ul').kfEqualizeColumns({ column: 'h2' });
	$('.triple-boxes').kfEqualizeColumns({ column: '.triple-box' });
	$('.sublinks').kfEqualizeColumns({ column: '.sublinks-col' });
	$('.product-box').kfEqualizeColumns({ column: '.col>h4' });
};	


presbeton.GoogleMap = function(options)
{
	this.options = $.extend({
		element: null,
		zoom: 8
	}, options);
	
	this.$element = null;
};

presbeton.GoogleMap.prototype.init = function()
{
	this.$element = $(this.options.element);
	this.geocoder = new google.maps.Geocoder();
	this.googlemap = new google.maps.Map(this.$element.get(0), {
		zoom: this.options.zoom,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
};

presbeton.GoogleMap.prototype.zoomTo = function(address)
{
	this.geocoder.geocode(
		{
			address: address
		}, 
		$.proxy(function(results, status)
		{
			if(status == google.maps.GeocoderStatus.OK) 
			{
				this.googlemap.setCenter(results[0].geometry.location);
			}
		}, this));
};

presbeton.GoogleMap.prototype.showAddress = function(address)
{
	this.geocoder.geocode(
		{
			address: address
		}, 
		$.proxy(function(results, status)
		{
			if(status == google.maps.GeocoderStatus.OK) 
			{
				if(results[0])
				{
					this.googlemap.setCenter(results[0].geometry.location);
					this.googlemap.setZoom(15);
					new google.maps.Marker({
						map: this.googlemap, 
						position: results[0].geometry.location
					});
					
					//console.log(results[0].formatted_address);
					
					var url = "http://maps.google.com/?ll=" + results[0].geometry.location + "&z=" + this.googlemap.getZoom() + "&t=" + this.googlemap.getMapTypeId() + "&q=" + results[0].formatted_address;
					this.$element.after('<p><a href="' + url + '" target="_blank" class="small">Zvětšit mapu</a></p>');
				}				

			}
		}, this));
};

