/*!
 * Virtual keyboard script
 * http://www.keyboard.su/
 *
 * Copyright 2010, Makarychev Yurii
 */
$(document).ready(function() {
	width = (screen.width) ? screen.width:'';
	height = (screen.height) ? screen.height:'';
	// check for windows off standard dpi screen res
	if (typeof(screen.deviceXDPI) == 'number') {
		width *= screen.deviceXDPI/screen.logicalXDPI;
		height *= screen.deviceYDPI/screen.logicalYDPI;
	} 
	$('#yourinfo').append('<li><b>' + l.resolution + '</b> ' + width + 'x' + height + 'px</li>')
	$('#yourinfo').append('<li>' + l.jsenabled + '</li>');
	
	if (null == Get_Cookie('cookie_test')) {
		expires = 10;
		Set_Cookie('cookie_test', 'it_worked' , expires, '', '', '');
	}
	cookie_append = (Get_Cookie('cookie_test')) ? l.cookiesenabled : l.cookiesdisabled;
	$('#yourinfo').append('<li>' + cookie_append + '</li>');
	
	/*
	 * Установка cookie
	 */
	function Set_Cookie(name, value, expires, path, domain, secure) {
		var today = new Date();
		today.setTime( today.getTime() );
		if (expires) {
			expires = expires * 1000;
		}
		var expires_date = new Date(today.getTime() + (expires));
	
		document.cookie = name + "=" +escape( value ) +
						  ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
						  ((path) ? ";path=" + path : "") +
						  ((domain) ? ";domain=" + domain : "") +
						  ((secure) ? ";secure" : "");
	}
	
	/*
	 * Чтение cookie
	 */
	function Get_Cookie(check_name) {
		var a_all_cookies = document.cookie.split(';');
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false;

		for ( i = 0; i < a_all_cookies.length; i++ ) {
			a_temp_cookie = a_all_cookies[i].split('=');
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

			if (cookie_name == check_name) {
				b_cookie_found = true;
				if (a_temp_cookie.length > 1) cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if (!b_cookie_found) return null;
	}
	
	/*
	 * Обновление captcha
	 */
	$('#refreshCaptcha').click(function(){
	    rand_ints = Math.random() * 10000000000000000000;
		$('#captcha').removeClass('inputError');
        $('#captcha').val('');
        $('#captcha').focus();
	    $('#captcha_img').attr('src', 'app/library/Captcha.php?zone=' + $(this).attr('rel') + '&rnd=' + rand_ints);
	});
	
	
	/*
	 * Ссылки сделать стартовой и добавить в избранное
	 */
	sethomepage();
	$('#fav_link').click(function() { bookmarksite(sitetitle, url); });
	$('#fav_link').addClass('red');

	// Sets links
	function bookmarksite(title, url) {
		if ($.browser.firefox) // firefox
			window.sidebar.addPanel(title, url, "");
		else if(window.opera && window.print){ // opera
			var elem = document.createElement('a');
			elem.setAttribute('href',url);
			elem.setAttribute('title',title);
			elem.setAttribute('rel','sidebar');
			elem.click();
		} 
		else if($.browser.msie)// ie
			window.external.AddFavorite(url, title);
	}
	
	function sethomepage() {
		home_link = '<a onClick="this.style.behavior=\'url(#default#homepage)\'; this.setHomePage(\'' + url + '\');" href="javascript://">' + l['startpage'] + '</a>';
		if ($.browser.msie) {
			$('#top-vertical-menu').append('<li>' + home_link + '</li>');
		}
	}
	
	/*
	 * e-mail-hider: отрисовка формочки с выбором шрифтов и т.п.
	 */
	$('input[name=encodetype]').click(function(){
		if ('4' == $(this).val()) {
			$('#fontOptions').slideDown('slow');
		} else {
			$('#fontOptions').slideUp('slow');
		}
		
	});
});
