 $(function () {

	 $('.default').dropkick({
		 width: 240
	 });
	 $('.default-person-count').dropkick({
		 width: 38
	 });
	 
	 $('.phobs_fp_dest select').dropkick({
		 change: function(){
		 	setHotelList();
		 	$('#dk_container_hotel').remove();
		 	$('.phobs_fp_building select')
		 		.data('dropkick', {})
		 		.dropkick({
		 			width: 240
		 		});
	 	},
		width: 240
	 });
	 
	 $('.phobs_fp_building select').dropkick({
		 width: 240
	 });
	 
	$('.phobs_fp_nights select').dropkick({
		width: 93
	});
	
	$('#temp_date')
		.datepicker({
			onSelect: function(){
				var d = $(this).datepicker("getDate");
				$('select[name="check_in_day"]').val(d.getDate());
				$('select[name="check_in_month"]').val(d.getMonth()+1);
				$('select[name="check_in_year"]').val(d.getFullYear());
			}
		})
		.each(function(){
			try {
				$(this).datepicker('setDate', $.datepicker.parseDate('yy-mm-dd', $(this).val()));
			} catch (e) {
 			}
		})
	;
	 
	
	


	$('#box-weather .selector').click(function(){
		$('#box-weather .options')
			.css('left', $('#box-weather .selector').position().left - 5)
			.fadeToggle('fast');

		$('body').click(function(){
			$('#box-weather .options:visible').fadeOut('fast');
		});
		
		return false;
	});
	$('#box-weather .options a').click(function(){
		$('#box-weather .selector').html($(this).html());
		$('#box-weather .options').fadeToggle('fast');

		$('#box-weather ul').hide();
		$('#box-weather ul.'+$(this).attr('rel')).show();

		return false;
	});	
	
	
	// tabele v contentu
	$('#main .desc table').css('width', '100%');
	
	//mini galerija
	
	$('#main .main-inner a[href$="view=gallery"]').fancybox({
		'padding'			: 0,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'changeFade'        : 0,
		'overlayOpacity'	: 0.8,
		'overlayColor'		: '#000',
		'titlePosition'		: 'inside',
		'headerStr'			: '<?=LC_FOTOGALERIJA?>',
		'closeStr'			: '<?=LC_ZAPRI?>',
		'type'				: 'ajax',
		'ajax'				: {
			type	: "GET",
		    data	: 'mode=ajax&action=mini-gallery'
		},
		'custom' : {'nopadding': true}
	});

	
	 
	//vstopna vroce ta hip
	 
	$('#fp-specials .gallery li')
		.each(function(){
			var t = $(this);
			if (t.hasClass('selected')) {
				$($('#fp-specials .slide')[t.index()]).addClass('selected');
			} else {
				t.hide();
				t.css('visibility', 'visible');
			}
		});

	var slidesFpRunning = true;
	var sliderFpTimer;

	runFpSlide();
	
	function runFpSlide() {
		if ($('#fp-specials .gallery li').length == 1) {
			return;
		}
		clearTimeout(sliderFpTimer);
		sliderFpTimer = setTimeout(function(){
			if (slidesFpRunning) {
				var i = $('#fp-specials .gallery li.selected').index();
				var len = $('#fp-specials .gallery li').length;
				switchToFpSlide((i + 1) % len, 1000);
				runFpSlide();
			}
		}, 3500);
	}

	$('#fp-specials .slide').
		click(function(){
			slidesFpRunning = false;
			switchToFpSlide($(this).index(), 500);
			return false;
		});
	
	function switchToFpSlide(index, speed) {
		var currentSlide = $($('#fp-specials .gallery li.selected')[0]);
		var newSlide = $($('#fp-specials .gallery li')[index]);

		currentSlide.removeClass('selected');
		newSlide.addClass('selected');

		newSlide
			.hide()
			.css('z-index', parseInt(currentSlide.css('z-index')) + 1)
			.fadeIn(speed, function(){
			});


		$('#fp-specials .slide').removeClass('selected');
		$($('#fp-specials .slide')[newSlide.index()]).addClass('selected');
	}

	$('#fp-specials .arrow-left').click(function(){
		var len = $('#fp-specials .gallery li').length;
		var prev = ($($('#fp-specials .gallery li.selected')).index() + len - 1) % len;
		slidesFpRunning = false;
		switchToFpSlide(prev, 500);
		return false;		
	});

	$('#fp-specials .arrow-right').click(function(){
		var len = $('#fp-specials .gallery li').length;
		var next = ($($('#fp-specials .gallery li.selected')).index() + 1) % len;
		slidesFpRunning = false;
		switchToFpSlide(next, 500);
		return false;		
	});
	
	// priporoči prijatelju
	$('#sendtofriend form').submit(function() {
		
		var fld_email = ['sender', 'reciever'];
		var fld_text = ['comment'];
		var error = false;
		
		for (i = 0; i < fld_email.length; i++) {
			var obj = $('#'+fld_email[i]);
			if ($.trim(obj.val()) == '' || obj.val().indexOf('@') == -1) {
				error = true;
				obj.prev('label').addClass('error');
			}
		}
		
		for (i = 0; i < fld_text.length; i++) {
			var obj = $('#'+fld_text[i]);
			if ($.trim(obj.val()) == '') {
				error = true;
				obj.prev('label').addClass('error');
			}
		}
		
		if (!error) {
			$.post($(this).attr('action'), $(this).serialize(), function(data, textStatus) {
				
				var message_html = '<p class="response '+data.status.type+'">'+data.status.message+'</p>';
				
				if (data.status.type == 'error') {
					$('#sendtofriend form') .remove('.response')
											.prepend(message_html);
				} else {
					$('#sendtofriend form').hide();
					$('#sendtofriend').append(message_html);
				}
				
			}, 'json');
		}
		return false;
	});
	
	// priporoči prijatelju - odpiranje iz toolbarja
	
	function close_sendtofriend() {
		$(document).unbind('click', close_sendtofriend);
		if ($('#sendtofriend:visible').removeClass('toolbar').hide());
		$('#sendtofriend .response').remove();
		$('#sendtofriend form')[0].reset();
		$('#sendtofriend form').show();
	}
	
	$('#sendtofriend').click(function(event) {
		event.stopPropagation();
	});
	
	$('.tools .share').click(function (event){
		event.stopPropagation();
		event.preventDefault();
		if ($('#sendtofriend').is(':visible')) {
			close_sendtofriend();
		} else {
			$('#sendtofriend').addClass('toolbar').slideDown('normal');
			$(document).bind('click', close_sendtofriend);
			$('#sender').focus();
		}
	});
	
	$('.share a[href="#sendtofriend"]').fancybox({
		titleShow	: false,
		padding		: 0,
		margin		: 0,
		width		:571,
		height		:337,
		closeStr	: '<?=LC_ZAPRI?>',
		type		: 'inline',
		onStart	: function() {
			$('#sendtofriend').removeClass('toolbar').css('display', 'block');
			$('#fancybox-content').css('padding', '0');
		},	
		onClosed: function() {
			$('#fancybox-content').css('padding', '0 0 54px 0');
			close_sendtofriend();
		},
		onComplete: function() {
			$('#sender').focus();
		}
	});
	
	/* anchor hack */
	$('a[href^="#"]').each(function() {
		$(this).attr(
			'href', 
			window.location.href.replace(/#.*$/, '')+'#'+$(this).attr('href').replace(/^[^#]*#/, '')
		);
	});


	/* generic forms checkboxes */
	$('.generic-form .line.checkbox .right input').change(function() {
		if ($(this).is(':checked')) {
			$(this).closest('label').addClass('checked');
		} else {
			$(this).closest('label').removeClass('checked');
		}
	}).trigger('change');
	$('.generic-form .line.radio .right input, .inquiry .line.radio .right input, ').change(function() {
		if ($(this).is(':checked')) {
			$(this).closest('.right').find('label').removeClass('checked');
			$(this).closest('label').addClass('checked');
		} else {
			$(this).closest('label').removeClass('checked');
		}
	}).trigger('change');
	
	/* generic forms dates */
	$('.generic-form .line.date input, .inquiry .line.date input').each(function() {
		var minDate=null, maxDate=null;
		if ($(this).attr('data-min')) {
			minDate = $.datepicker.parseDate('yy-mm-dd', $(this).attr('data-min'));
		}
		if ($(this).attr('data-max')) {
			maxDate = $.datepicker.parseDate('yy-mm-dd', $(this).attr('data-max'));
		}

		$(this).datepicker({
			minDate:minDate,
			maxDate:maxDate
		});

		if ($(this).val()) {
			$(this).datepicker('setDate', $.datepicker.parseDate('yy-mm-dd', $(this).val()));
		}
	});

	/* inquery checkboxes */
	$('.inquiry .line.type .right input, .inquiry .submit label input').change(function() {
		if ($(this).is(':checked')) {
			$(this).closest('.right').find('label').removeClass('checked');
			$(this).closest('label').addClass('checked');
		} else {
			$(this).closest('label').removeClass('checked');
		}
	}).trigger('change');
	$('.inquiry #form_8').bind('change dk_change', function() {
		var val = $(this).val();
		$('.inquiry .children').removeClass('one two three');
		if (val==1) {
			$('.inquiry .children').addClass('one');
		} else if (val==2) {
			$('.inquiry .children').addClass('two');
		} else if (val==3) {
			$('.inquiry .children').addClass('three');
		}
	}).trigger('change');

	$('input[data-default]').focus(function() {
		if ($(this).hasClass('has-default')) {
			$(this).val('');
			$(this).removeClass('has-default');
		}
	}).blur(function() {
		if ($(this).val()=='') {
			$(this).val($(this).attr('data-default')).addClass('has-default');
		} else {
			$(this).removeClass('has-default');
		}
	}).trigger('blur');

	$('#footer #mini-map a').mousemove(function() {
		if ($(this).attr('title')) {
			$(this).attr('data-title', $(this).attr('title'));
			$(this).removeAttr('title');
		}

		if ($('#tooltip').size()==0) {
			$('#footer #mini-map').append('<div id="tooltip"><span></span></div>');
		}
		$('#tooltip span').html($(this).attr('data-title'));

		var pos = $(this).position();
		var width = $(this).width();

		$('#tooltip').stop().css('opacity', 1).hide().show().css({
			left:pos.left+width-43,
			top:pos.top-40
		});
	});

	$('#footer #mini-map a').mouseleave(function() {
		$('#tooltip').fadeOut();
	});
	
	var fontSizes = ['10px', '15px', '20px'];

	$('.print').click(function(){
		window.print();
	});
	
	$('.font-inc, .font-dec').click(function(){
		currentSize = readCookie('fontSize');
		if (currentSize == undefined) currentSize = fontSizes[0];
		ci = fontSizes.indexOf(currentSize);
		
		if ($(this).attr('class') == 'font-inc') {
			newSize = fontSizes[((ci + 1) % fontSizes.length)];
		} else {
			newSize = fontSizes[((ci - 1 + fontSizes.length) % fontSizes.length)];
		}
		
		createCookie('fontSize', newSize);
		
		$('#main').css('font-size', newSize);
	});
	
 });
 
 function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
 
