var host = '';
//Load ambient graphic
function setAmbientCookie() {
	try {
		$j.cookie('ambientGraphic', null);
		$j.cookie('ambientGraphics',null);
	}
	catch(e){;}
	var cookie_status = ($j.cookie('ambient_Graphic') == "0")?1:0;
	$j.cookie('ambient_Graphic',cookie_status, {expires:365, path: "/", domain:
	".erepublik.com"})
}
function loadAmbient() {
	var cssDef = {
		'background-image': 'url('+host+'/images/bg.jpg)',
		'background-repeat': 'no-repeat',
		'background-position': 'center top'
	}
	$j("body").css(cssDef);
}

function togglerAmbient() {
	$j(".ambient_toggler").show();
}

function decideAmbientGraphics() {
	if($j.cookie('ambient_Graphic') == "0")	$j('body').css({'background-image': 'url('+host+'/images/bg.jpg)'});
	else $j('body').css({'background-image': 'none'});
	setAmbientCookie();
}

$j(document).ready(function () {
	$j(".ambient_toggler").toggle(
	function () {
		try{
			decideAmbientGraphics()
		}
		catch(e){;}
		$j(this).addClass('on');
	},
	function () {
		try {
			decideAmbientGraphics();
		}
		catch(e) {;}
		$j(this).removeClass('on');
	});
	try {
		$j(".preview > img").each(function(){processImage(this)});
	} catch(e) {;}
});

function processImage(obj) {
	var height = obj.height;
	var width = obj.width;
	if(height <= 400 && width <= 400) return;
	var ratio = height/width;
	if(ratio > 1) {
		height = 400;
		width = Math.ceil(height/ratio);
	}	else {
		width = 400;
		height = Math.ceil(width*ratio);
	}
	obj.height = (height);
	obj.width = (width);
	/*
	obj.css("width", width);
	obj.css("height", height);
	*/
}

window.onload = function () {
	
	try {
		if (ERPK_MAIN_HOST) {
			host = 'http://'+ERPK_MAIN_HOST;
		} 
	}
	catch(e) {;}
	if (screen.width > 1024) {
		loadAmbient();
		togglerAmbient();
		try {
			if($j.cookie('ambient_Graphic') == "0") { 
					$j('body').css({'background-image':	'none'});
					$j('.ambient_toggler').addClass('on');
				}
		}
		catch(e) {;}
	}
}

//Clock script
function live_clock(html_id, hours, minutes)
{
	minutes += 1;
	if(minutes > 59) {
		minutes = 0;
		hours += 1;
	}
	if(hours > 23) {
		hours = 0;
	}

	t_hours = hours;
	t_minutes = minutes;

	if(minutes < 10) {
		t_minutes = "0"+minutes;
	}
	if(hours < 10) {
		t_hours = "0"+hours;
	}

	time = t_hours+":"+t_minutes;

	obj_gametime = document.getElementById(html_id);
	obj_gametime.innerHTML = time;

	setTimeout("live_clock(\""+html_id+"\", "+hours+", "+minutes+")", 60000);
}

//Confirmation
document.write('<style type="text/css" media="screen"> .confirmationfade{background-color: #000;} div.confirmationbox {cursor:default;} div.confirmationbox * {cursor:default;} div.confirmationwarning #confirmation {background: #5ab3d0;} div.confirmation .confirmationclose{display: none;} </style>');

function jalert(message) {
	$j.prompt(message, {
		buttons: {
			ok: {
				"text": window.confirmation["ok"],
				"ret": true
			}
		}
	}, 1);
}

function jconfirm(message) {
	$j.prompt(message, {
		buttons: {
			yes: {
				"text": window.confirmation["yes"],
				"ret": true
			},

			no: {
				"text": window.confirmation["no"],
				"ret": false
			}
		}
	}, 2);
}

//Search form
(function ($) {
		$.fn.ERepublikSearchForm = function (options)
		{
			var settings = $.extend({}, $.fn.ERepublikSearchForm.defaults, options)

			return this.each(function () {
				var submit = $(this).find(settings.submitButton).attr('disabled', 'disabled');
				var field  = $(this).find(settings.searchField);

				field.click(function() {
					$(this).attr('value', '');
				});

				var toggleFieldEnabled = function () {
					var value = $(this).attr('value');

					if (value.length) {
						submit.removeAttr('disabled');
					} else {
						submit.attr('disabled', 'disabled');
					}
				}

				field.keydown(toggleFieldEnabled);
				field.keyup(toggleFieldEnabled);

			});
		}

		$.fn.ERepublikSearchForm.defaults = {
			searchField: '#search_field',
			submitButton: 'input.submit'
		}
})(jQuery);


