/*
 * SimpleModal OSX Style Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 */
function setCookie(name, value, expires, path, domain, secure) {
        var curCookie = name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toUTCString() : "") +
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                ((secure) ? "; secure" : "")
        if ((name + "=" + escape(value)).length <= 4000)
                document.cookie = curCookie
        else
        	if (confirm("Cookie превышает 4KB и будет вырезан !"))
                        document.cookie = curCookie
}

function saveCoookies(redirect, end_of_session){
	if(end_of_session) {
		var date = false;
	}
	else{
		var date = new Date();
		date.setTime(date.getTime()+ 365 * 24 * 3600 * 1000);
	}
	setCookie('redirect', redirect, date, '/', '.gismeteo.'+ocx_go_from_domain)
}

function goToDomain(to, lang_use){
	var gotopath = document.location.pathname;
	if(ocx_go_to_def != undefined){
		gotopath = ocx_go_to_def;
	}
	if(lang_use != undefined && lang_use != '' && lang_use != false){
		gotopath = '/' + lang_use + gotopath;
	}
	document.location='http://www.gismeteo.'+to+gotopath;
}

function Recheck(e) {
	var checked = $("#osx-modal-content input#check").attr('checked');
	var h = $("#osx-container").height();
	if(checked)
	{
		$("#osx-container").animate( {height:h+140}, 'fast');
		$("#osx-modal-content .tip").show();
	}
	else{
		$("#osx-container").animate( {height:h-140}, 'fast');
		$("#osx-modal-content .tip").hide();
	}
}

jQuery(function ($) {
	var OSX = {
		container: null,
		init: function () {
			//$("input.osx, a.osx").click(function (e) {
			$('document').ready(function() {
				//e.preventDefault();	
				
				$("#osx-modal-content input#check").click(Recheck);
				
				$("button.this, a.this.simplemodal-close").click(function (e) {
					if($("#osx-modal-content input#check").attr('checked'))
					//не переходить на год
								saveCoookies(1);
					//не переходить до конца сессии
					else	saveCoookies(1, true);
				});
				$("button.follow").click(function (e) {
					if($("#osx-modal-content input#check").attr('checked')){
					//переходить на год
						saveCoookies(2);
					}
					else{
					//переходить до конца сессии
						saveCoookies(2, true);
					}
					//goToDomain(ocx_go_to_domain);
				});
				$("button.follow.to").click(function (e) {
					goToDomain(ocx_go_to_domain, ocx_go_to_lang);
				});
				$("button.follow.from").click(function (e) {
					goToDomain(ocx_go_to_domain, ocx_go_from_lang);
				});
				
				$("#osx-modal-content").modal({
					overlayId: 'osx-overlay',
					containerId: 'osx-container',
					closeHTML: null,
					minHeight: 80,
					opacity: 65, 
					position: ['25%',],
					overlayClose: false,
					onOpen: OSX.open,
					onClose: OSX.close
				});
			});
		},
		open: function (d) {
			var self = this;
			self.container = d.container[0];
				d.overlay.fadeIn('fast', function () {
				$("#osx-modal-content", self.container).show();
				var title = $("#osx-modal-title", self.container);
				title.show();
				d.container.slideDown('fast', function () {
					setTimeout(function () {
						var h = $("#osx-modal-data", self.container).height()
							+ title.height()
							+ 20 //padding;
						d.container.animate(
							{height: h}, 
							0,
							function () {
								$("div.close", self.container).show();
								$("#osx-modal-data", self.container).show();
								$("#osx-modal-content .tip").hide();
							}
						);
					}, 0);
				});
			})
		},
		close: function (d) {
			var self = this;
			d.container.animate(
				{top:"-" + (d.container.height() + 20)},
				200,
				function () {
					self.close();
				}
			);
		}
	};
	OSX.init();
});
