$(document).ready(
	function() 
	{

	    $('form input[type="text"]').addClass("idleField");
	    $('form input[type="text"]').focus(function() {
			$(this).removeClass("idleField").addClass("focusField");
	        if (this.value == this.defaultValue){
	        	this.value = '';
	    	}
	        if(this.value != this.defaultValue){
		    	this.select();
	        }
	    });
	    $('form input[type="text"]').blur(function() {
	    	
	        if ($.trim(this.value) == ''){
	        	this.value = (this.defaultValue ? this.defaultValue : '');
	        	$(this).removeClass("focusField").addClass("idleField");
	    	}
	    });
	    
	    $('a').click(function(){
	    	var hash = $(this).attr('hash');
	    	if(hash.substring(0, 9) == 'base64://'){
		    	hash = hash.replace('base64://', '');
		    	hash = Base64.decode(hash);
		    	window.location = hash;
		    	return false;
	    	}
	    	return true;
	    });


		var dreamstheme = $.cookie('dreamstheme');
		if(dreamstheme == 'hidden'){
			$('a.hider').toggle(
				function()
				{
					$(this).html('скрыть [-]');
					$(this).parent().parent().children('.cols').show('fast');
					$.cookie('dreamstheme','visible',{ expires: 365, path: '/'});
				},
				function()
				{
					$(this).html('показать [+]');
					$(this).parent().parent().children('.cols').hide('fast');
					$.cookie('dreamstheme','hidden',{ expires: 365, path: '/'});

				}
			);
		}else{
			$('a.hider').toggle(
				function()
				{
					$(this).html('показать [+]');
					$(this).parent().parent().children('.cols').hide('fast');
					$.cookie('dreamstheme','hidden',{ expires: 365, path: '/'});
				},
				function()
				{
					$(this).html('скрыть [-]');
					$(this).parent().parent().children('.cols').show('fast');
					$.cookie('dreamstheme','visible',{ expires: 365, path: '/'});
				}
			);
		}
		
		// Скрыть/показать форму добавления комментариев
		
		$('.comments a.add').toggle(
			function()
			{
				$(this).html('- Убрать комментирование');
				$(this).parent().parent().children('form').slideToggle('fast');
			},
			function()
			{
				$(this).html('+ Добавить комментарий');
				$(this).parent().parent().children('form').slideToggle('fast');
			}
		);

		$(".tooltip").tipTip({maxWidth: '230px', defaultPosition:'bottom', edgeOffset: 60, delay:100});
		$("a.annotate").hover(function(){
			
			offsetTop = $(this).children('span').height()+18+18+9;
			//offsetLeft = -$(this).width()/2-;
			offsetLeft = $(this).children('span').width()/2+6-$(this).width()/2;
			
			$(this).children('span').css({ top: -offsetTop, left: -offsetLeft });

			$(this).children('span').show();
						
		},function(){
			
			$(this).children('span').hide();
			
		});

		$('#printpage').hover(function(){
			$(this).children('span').show();
			
		},function(){
			$(this).children('span').hide();
		
		});
		var day,month,year,hour;
		var moondays = new Array();
		moondays['1'] = '<p align=center>Ваш сон приснился в <b>1-ый лунный день</b>.<br /><a href="/dreams/moondays/1.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['2'] = '<p align=center>Вам прис<b>нился сон во 2-ый лунный день</b>.<br /><a href="/dreams/moondays/2.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['3'] = '<p align=center>Вам приснился сон в 3-ый лунный день.<br /><a href="/dreams/moondays/3.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['4'] = '<p align=center>Вам приснился сон в <b>4-ый лунный день</b>.<br /><a href="/dreams/moondays/4.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['5'] = '<p align=center>Вам приснился сон в <b>5-ый лунный день</b>.<br /><a href="/dreams/moondays/5.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['6'] = '<p align=center>Вам приснился сон в <b>6-ый лунный день</b>.<br /><a href="/dreams/moondays/6.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['7'] = '<p align=center>Вам приснился сон в <b>7-ый лунный день</b>.<br /><a href="/dreams/moondays/7.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['8'] = '<p align=center>Вам приснился сон в <b>8-ый лунный день</b>.<br /><a href="/dreams/moondays/8.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['9'] = '<p align=center>Вам приснился сон в <b>9-ый лунный день</b>.<br /><a href="/dreams/moondays/9.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['10'] = '<p align=center>Вам приснился сон в <b>10-ый лунный день</b>.<br /><a href="/dreams/moondays/10.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['11'] = '<p align=center>Вам приснился сон в <b>11-ый лунный день</b>.<br /><a href="/dreams/moondays/11.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['12'] = '<p align=center>Вам приснился сон в <b>12-ый лунный день</b>.<br /><a href="/dreams/moondays/12.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['13'] = '<p align=center>Вам приснился сон в <b>13-ый лунный день</b>.<br /><a href="/dreams/moondays/13.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['14'] = '<p align=center>Вам приснился сон в <b>14-ый лунный день</b>.<br /><a href="/dreams/moondays/14.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['15'] = '<p align=center>Вам приснился сон в <b>15-ый лунный день</b>.<br /><a href="/dreams/moondays/15.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['16'] = '<p align=center>Вам приснился сон в <b>16-ый лунный день</b>.<br /><a href="/dreams/moondays/16.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['17'] = '<p align=center>Вам приснился сон в <b>17-ый лунный день</b>.<br /><a href="/dreams/moondays/17.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['18'] = '<p align=center>Вам приснился сон в <b>18-ый лунный день</b>.<br /><a href="/dreams/moondays/18.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['19'] = '<p align=center>Вам приснился сон в <b>19-ый лунный день</b>.<br /><a href="/dreams/moondays/19.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['20'] = '<p align=center>Вам приснился сон в <b>20-ый лунный день</b>.<br /><a href="/dreams/moondays/20.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['21'] = '<p align=center>Вам приснился сон в <b>21-ый лунный день</b>.<br /><a href="/dreams/moondays/21.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['22'] = '<p align=center>Вам приснился сон в <b>22-ый лунный день</b>.<br /><a href="/dreams/moondays/22.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['23'] = '<p align=center>Вам приснился сон в <b>23-ый лунный день</b>.<br /><a href="/dreams/moondays/23.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['24'] = '<p align=center>Вам приснился сон в <b>24-ый лунный день</b>.<br /><a href="/dreams/moondays/24.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['25'] = '<p align=center>Вам приснился сон в <b>25-ый лунный день</b>.<br /><a href="/dreams/moondays/25.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['26'] = '<p align=center>Вам приснился сон в <b>26-ый лунный день</b>.<br /><a href="/dreams/moondays/26.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['27'] = '<p align=center>Вам приснился сон в <b>27-ый лунный день</b>.<br /><a href="/dreams/moondays/27.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['28'] = '<p align=center>Вам приснился сон в <b>28-ый лунный день</b>.<br /><a href="/dreams/moondays/28.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['29'] = '<p align=center>Вам приснился сон в <b>29-ый лунный день</b>.<br /><a href="/dreams/moondays/29.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['30'] = '<p align=center>Вам приснился сон в <b>30-ый лунный день</b>.<br /><a href="/dreams/moondays/30.html">Прочитать про толкование снов в этот день.</a></p>';
		moondays['31'] = '<h4>Это Новолуние.</h4>Все сны — к радости.';
		

		$("#datepicker").datepicker({
			maxDate: "0",
			dateFormat : "dd-mm-yy",
			monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
			dayNamesMin: 	 ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
			onSelect:
			function(){
	            day = $("#datepicker").datepicker('getDate').getDate();
	            month = $("#datepicker").datepicker('getDate').getMonth() + 1;
	            year = $("#datepicker").datepicker('getDate').getFullYear();
				if(day && month && year && hour){
					$('#moonday').html('<p align=center>Подождите идет опеределение лунного дня…<br /><img src="/media/img/loading.gif" /></p>');
					setTimeout(fmoondays , 2000)
				}
            }
		});
		$('#timepicker').timepickr({convention:24, trigger : 'focus', minutes:false});
		$('#timepicker').change(function(){
			hour = $('#timepicker').val().substring(0,2);
			if(day && month && year && hour){
				$('#moonday').html('<p align=center>Подождите идет опеределение лунного дня…<br /><img src="/media/img/loading.gif" /></p>');
				setTimeout(fmoondays , 2000)

			}
		});
		
		function fmoondays(){
			$('#moonday').html( moondays[MoonDay(year + '', month, day, hour)] );
		}
	}
);

var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
