(function($) {
    $.PeriodicalUpdater = function(options, callback){
        settings = jQuery.extend({
            url: '',
            method: 'get',
            sendData: '',
            minInterval: 1000,
            maxInterval: 10000,
            multiplier: 1.5,
            type: 'text'
        }, options);
        
        f = settings.method == 'post' ? $.post : $.get;

        var timerInterval = settings.minInterval;
        getdata();
        
        function getdata(){
            f(settings.url, settings.sendData, function(d){         	  
            	if(timerInterval < settings.maxInterval) timerInterval = timerInterval * settings.multiplier;
                if(timerInterval > settings.maxInterval) timerInterval = settings.maxInterval;  
                PeriodicalTimer = setTimeout(getdata, timerInterval);
                if(callback) callback(d);
            }, settings.type);
        }
    };  
})(jQuery);

function setCookie (name, value, expires, path, domain, secure){
	var cookie = name+"="+escape(value);
	
	if (!expires)  expires = 3600*24*30;
	var date = new Date();
	date.setTime (date.getTime() + expires);
	cookie += "; expires="+date.toGMTString();
	
	if(path) cookie += "; path="+escape(path);
	if(domain) cookie += "; domain="+escape(domain);  
	if(secure) cookie += "; secure";
	document.cookie = cookie;
}

function deleteCookie (name){
  var date = new Date();
  date.setTime (date.getTime() - 1);
  document.cookie = name += "=; expires="+date.toGMTString();
}

function getCookie (name){
  var cookie = document.cookie.match ( '(^|;) ?'+name+'=([^;]*)(;|$)' );
  if(cookie) return (unescape(cookie[2]));
  else return null;
}

function closeWindow() {
	//uncomment to open a new window and close this parent window without warning
	//var newwin=window.open("popUp.htm",'popup','');
	if(navigator.appName=="Microsoft Internet Explorer") {
	this.focus();self.opener = this;self.close(); }
	else { window.open('','_parent',''); window.close(); }
	} 

function listDistricts() {
	$.ajax({
		url: '/?site=registration',
		type: 'POST',
		data: 'action=listdistricts&country_id=' + document.registration.country_id.value,
		timeout: 2000,
		success: function(html){			
		$('#districts').html(html);
		listCities();
	}
	});
}

function listCities() {
	$.ajax({
		url: '/?site=registration',
		type: 'POST',
		data: 'action=listcities&district_id=' + document.registration.district_id.value,
		timeout: 2000,
		success: function(html){			
			$('#cities').html(html);
		}
	  });
}

function setOnlineStatus(status) {
	$.ajax({
		url: '/?site=users',
		type: 'POST',
		data: 'action=setonlinestatus&status='+status
	  });
}

function getUserName(user_id, element) {
	$.ajax({
		url: '/?site=registration',
		type: 'POST',
		data: 'action=get_user_name&user_id=' + user_id,
		success: function(html) {			
			element.style.display = '';
			element.innerHTML = html;
		}
	  });
}

function cancelInvitation(user_id) {
	$.ajax({
		url: '/?site=streaming',
		type: 'POST',
		data: 'action=cancelinvitation&user_id='+user_id
	});
}
function declineInvitation(user_id) {
	$.ajax({
		url: '/?site=streaming',
		type: 'POST',
		data: 'action=declineinvitation&user_id='+user_id
	  });
}

function reservationDateEndVisibility(recurring) {
	if (recurring == 'no') $('#reservationdateend').hide();
	else $('#reservationdateend').show();
}

function checker() {
	$.PeriodicalUpdater({
		url: '?site=reservation',
		method: 'post', 
		sendData: 'action=todaysReservations', 
		minInterval: 10000, 
		multiplier: 1},
		function (responseText){
			if(responseText.length > 0) document.getElementById('todays_reservations').innerHTML = responseText;
		}
	);
}

// Translations
function switchColumn(Column){
	var Width;
	var Text;
	if(document.getElementById(Column).style.width == '0px'){
		Width = 'auto';
		Text = '>|<';
	} else {
		Width = '0px';
		Text = '|<>|';
	}
	document.getElementById(Column).style.width = Width;
	document.getElementById(Column+'_sign').innerHTML = Text;
}

function ivSwitcher() {
	if (document.getElementById('iv_image').style.display != 'none') {
		document.getElementById('iv_image').style.display = 'none';
		document.getElementById('iv_video').style.display = '';
		document.getElementById('iv_holder').width = '320';
		document.getElementById('iv_holder').height = '240';
		document.getElementById('iv_button').innerHTML = '';
		document.getElementById('iv_button').id = '';
	} else {
		document.getElementById('iv_image').style.display = '';
		document.getElementById('iv_video').style.display = 'none';
		document.getElementById('iv_holder').width = '206';
		document.getElementById('iv_holder').height = '206';
	}
}