$(document).ready(function(){
	//load app data
	var appData = $.getAppData();
	var baseUrl = appData.baseUrl;
	var lan = appData.lan;
	var dict = appData.dict;

	//cache images
	$.cacheImage(appData.preloadImages);

	//favorite add
	if($('a#addToCalendar').length) {
		$('a#addToCalendar').click(function(event){
			event.preventDefault();
			parts = $('a#addToCalendar').attr('href').split('=');

			$.get(baseUrl+lan+'/ajax.php', {type: 'favoriteAdd', id: parts[1]}, function(data){

				if($('div#favMarker').length) {
					if($('div#favorites').length) {
						$('div#favorites').replaceWith(data);
					} else {
						$('div#favMarker').after(data);
					}
				}

				alert(dict['favorite_add']);
			})
		});
	}

	//empty favorites
	if($('a#emptyFavorites').length) {
		$('a#emptyFavorites').click(function(event){
			event.preventDefault();
			parts = $('a#emptyFavorites').attr('href').split('=');

			$.get(baseUrl+lan+'/ajax.php', {type: 'favoritesEmpty'}, function(data){

				if($('div#favorites').length) {
					$('div#favorites').remove();
					$('a.removeFavorite').remove();
				}

				alert(dict['favorites_deleted']);
			})
		});
	}

	//remove favorite
	if($('a.removeFavorite').length) {
		$('a.removeFavorite').click(function(event){
			event.preventDefault();
			event.stopPropagation();
			var relLink = $(this);
			parts = relLink.attr('href').split('=');

			$.get(baseUrl+lan+'/ajax.php', {type: 'favoriteRemove', id: parts[1]}, function(data){
				relLink.remove();

				if($('div#favorites').length) {
					$('div#favorites').replaceWith(data);
				}
			})
		})
	}

	//katalog
	if($('table.list').length) {
		$('table.list td').click(function(){
			window.location.href = $(this).parents('tr').find('a').attr('href');
		});
	}

	//mouse over na hp
	/*
	if($('#catalogueCommingSoon').length) {
		$('#catalogueCommingSoon').bind('mouseenter', function(){
			$('#catalogueCommingSoon').css('background-image', 'url("'+baseUrl+'img/cat_comming_o.jpg")');
		});

		$('#catalogueCommingSoon').bind('mouseleave', function(){
			$('#catalogueCommingSoon').css('background-image', 'url("'+baseUrl+'img/cat_comming.jpg")');
		});
	}
	*/

	//mouse over v pravem sloupci
	//tmpCat
	if($('div.rightCol a.tmpCat').length && $('div#tmpCatOver').length) {
		$('div.rightCol a.tmpCat').bind('mouseenter', function(){
			$('div#tmpCatOver').css('display', 'block');
		});

		$('div#tmpCatOver').bind('mouseleave', function(){
			$('div#tmpCatOver').css('display', 'none');
		});
	}

	if($('div#calendar').length) {
		$('body').append('<div id="toolTip">&nbsp;</div>');
		var toolTip = $('div#toolTip');
	}

	function activateLinks() {
		//update kalendare
		if($('div#calendarControll h3 a').length && $('div.#calendar').length) {
			$('div#calendarControll h3 a').each(function(){
				$(this).click(function(event){
					event.preventDefault();
					var gOff = $(this).attr('href').split('=');

					$('div#calendar').animate({opacity: 0}, 'normal', 'linear', function(){
						gOff = gOff[1];
						$.getJSON(baseUrl+lan+'/ajax.php', {type: 'calendar', caloff: gOff}, function(data){
							$('div#calendarControll').replaceWith(data.text);
							$('div#calendar').replaceWith(data.calendar);
							$('div#calendar').css('opacity', 0);
							$('div#calendar').animate({opacity: 1}, 'normal', 'linear');
							activateLinks();
						});
					});
				});
			});
		}

		//popisky kalendare
		if($('div#calendar').length) {
			var hrefs = $('div#calendar a[title]');

			hrefs.each(function(){
				this.hoverTitle = this.title;
				this.title = '';

			});

			toolTip.bind('mouseenter', function(event){
				toolTip.show();
			});

			toolTip.bind('mouseleave', function(event){
				toolTip.hide();
			});

			hrefs.bind('mouseenter', function(event){
				var events = event.target.hoverTitle.toString().split('||');
				var ttHtml = '';
				$(events).each(function(i){
					var parts = events[i].toString().split('|');
					if(parts.length == 2) {
						ttHtml += '<a href="'+parts[1]+'">'+parts[0]+'</a>'
					}
				});

				toolTip.html(ttHtml);
				toolTip.show();

				pos = $(event.target).position();
				toolTip.css({
					top: pos.top - 1,
					left: pos.left - toolTip.outerWidth()
				});
			});

			hrefs.bind('mouseleave', function(event){
				toolTip.hide();
			});
		}
	}

	activateLinks();
});

$(window).load(function () {
	//init shadowbox
	Shadowbox.init({
		initialWidth: 600,
		initialHeight: 450
	});

	//posuvy v detailu
	if($('div#smallImages').length) {
		var cont = $('div#smallImages');
		var scrl = $('div#smallImages div');
		var imgs = $('div#smallImages img');
		var step = 4;
		var tWidth = 0;
		var scrollZoneWidth = 50;
		var mover = null;
		var moving = '';
		var lastLink = false;

		imgs.each(function(){
			tWidth += $(this).outerWidth(true);
		}).click(function(event){
			event.preventDefault();
			event.stopPropagation();
			lastLink = $(event.target).parent().get(0);

			$('div#offerImages img.mainImg').attr('src', event.target.src.replace('galsm', 'galbig'));
			$('div#offerImages img.mainImg').unbind('click');
			$('div#offerImages img.mainImg').bind('click', function(){
				Shadowbox.open(lastLink);
			})
		});

		$('div#offerImages img.mainImg').bind('click', function(){
			Shadowbox.open($('div#smallImages div a').get(0));
		})

		scrl.css({
			position: 'relative',
			left: 0
		});

		var maxLeft = cont.innerWidth() - tWidth;

		function moveLeft() {
			var cLeft = parseInt(scrl.css('left'));

			if((cLeft - step) > maxLeft) {
				scrl.css('left', parseInt(cLeft - step))
			} else {
				moveStop();
			}
		}

		function moveRight() {
			var cLeft = parseInt(scrl.css('left'));

			if((cLeft + step) < 0) {
				scrl.css('left', parseInt(cLeft + step))
			} else {
				moveStop();
			}
		}

		function moveStop() {
			window.clearInterval(mover);
		}

		cont.mousemove(function(event){
			posi = cont.offset();
			x = event.pageX;
			relLeft = x - posi.left;
			cWidth = cont.width();

			if(relLeft > 0) {
				if(relLeft < scrollZoneWidth) {
					if(moving != 'right') {
						moving = 'right';
						moveStop();
						mover = window.setInterval(moveRight, 30);
					}
				} else if(relLeft > cWidth - scrollZoneWidth) {
					if(moving != 'left') {
						moving = 'left';
						moveStop();
						mover = window.setInterval(moveLeft, 30);
					}
				} else {
					moving = 'no';
					moveStop();
				}
			}
		});

		cont.bind('mouseleave', function(){
			moving = 'no';
			moveStop();
		});
	}
});
