$(window).load(function() {
	/**
	 * Podmiana przycisku w ostatnio wykonanej akcji
	 */
	if(document.location.hash){
		var hash = ((document.location.hash).slice(1));

		$('#' + hash).find('.button-doit').removeClass('button-doit').addClass('button-doit-again');
	}

	/**
	 * Tooltip dla prezentów w akcjach
	 */
	$('.action-gifts img').each(function(){
		large = $('#item-tooltip');
		$(this).hover(function(){
			large.find('strong').text($(this).attr('alt'));
			large.show();

			clearTimeout();
		},function(){
			setTimeout("$('#item-tooltip').hide()", 100);
		});

		$(this).mousemove(function(e){
			large.css({
				'visibility': 'visible',
				'left': (e.pageX+5) + 'px',
				'top': (e.pageY-105) + 'px'
			});

			clearTimeout();
		});

		setTimeout("$('#item-tooltip').hide()", 100);
	});

	/**
	 * Tooltip dla info o koktailu osobowości
	 */
	$('.button-info-coctail').each(function(){
		large = $('#item-tooltip');
		$(this).hover(function(){
			large.html($(this).text());
			large.show();

			clearTimeout();
		},function(){
			setTimeout("$('#item-tooltip').hide()", 100);
		});

		$(this).mousemove(function(e){
			large.css({
				'visibility': 'visible',
				'left': (e.pageX+5) + 'px',
				'top': (e.pageY-105) + 'px'
			});

			clearTimeout();
		});

		setTimeout("$('#item-tooltip').hide()", 100);
	});

	/**
	 * Ukrywa wiadomość "flash" po 10s
	 */
	setInterval('$("#flash-messages").slideUp("slow")', 10000);

	/**
	 * Czyści wartość inputa
	 */
	$('input.clear-value').bind('focus', function(){
		var name = $(this).attr('name');
		var data = $(this).data(name);
		if ( data == undefined ) {
			$(this).data(name, $(this).val());
			$(this).val('');
		}
	});

	/**
	 * Przywraca wartość inputa jeśli nie było zmian
	 */
	$('input.clear-value').bind('blur', function(){
		var name = $(this).attr('name');
		if ( $(this).val() == '' ) {
			$(this).val($(this).data(name));
			$(this).removeData(name);
		}
	});

	/**
	 * Listowanie tylko konkretnych typów akcji + zapis do ciacha
	 */
	$('input.skill-checkbox').change(function(){
		$('li.'+$(this).attr('id')).slideToggle('slow');

		var divs = $('input.skill-checkbox:checked').toArray();
		var a = [];
		for (var i = 0; i < divs.length; i++) {
			a.push($(divs[i]).attr('id'));
		}

		setCookie( 'selectedSkills', a.join('|'), 14 );
	});

	/**
	 * Kupowanie określonej ilości przedmiotów
	 */
//	$('.overlay-wrapper-desc .button-buyit[rel]').live('click', function(){
//		var iHowManyOld = $(this).attr('rel');
//		var iHowMany = $('.overlay-wrapper-desc option:selected').val();
//		var sLink = $(this).attr('href');
//
//		iHowManyOld = '/'+iHowManyOld+'/';
//
//		window.location.replace(sLink.replace(iHowManyOld, '/'+iHowMany+'/'));
//
//		return false;
//	});

	/**
	 * Overlay dla zakupów
	 */
	$('a.required-items-missing[rel],a.item-pay[rel]').overlay({
		expose: {loadSpeed: 200, 'color':null, 'maskId': 'overlay-bg', 'opacity': 1},
		abolute: true,
		top: '30%',

		onBeforeLoad: function() {
			var wrap = this.getContent().find("div.wrap");

			wrap.load(this.getTrigger().attr("href"));
		}
	});

	/**
	 * Overlay dla wybierania przyjaciół
	 */
	$('.action-group a.button[rel]').overlay({
		expose: {loadSpeed: 200, 'color':null, 'maskId': 'overlay-bg', 'opacity': 1},
		abolute: true,
		top: '30%',

		onBeforeLoad: function() {
			var wrap = this.getContent().find("div.wrap");

			wrap.load(this.getTrigger().attr("href"));
		}
	});

	/**
	 * Wysyłanie zmiany sortowania w overlay'u wybierania przyjaciół
	 */
	$('#form-sorting').change(function(){
		$(this).submit();
	});

	if ( bUpdateEnergy ) {
		oData = new Date();
		oData.setSeconds( iTime );

		$('#hiddenClock').countdown({until: '+' + iTime + 's', layout: 'Następna za: <span>{mn}:{snn}</span>', compact: true, onExpiry: updateCountdownField});
		$('#hiddenClockWrapper').show();
	}
});

function updateCountdownField() {
	var iNewEnergy = parseInt($('#actualUserEnergy').html()) + iEnergyPerSecond;

	if( iNewEnergy >= iMaxEnergy ) {
		window.location.href = window.location.href;
	} else {
		$('#actualUserEnergy').html( iNewEnergy );
		$('#hiddenClock').countdown('change', 'until', '+' + iUpdateInSeconds + 's');
	}
}

/**
 * Ustawia ciastko na expiredays dni (gdy null to do zabicia przegladarki)
 */
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
	';path=/;host=.teenstar.pl;';
}

/**
 * Pobiera cookie, zwraca pusty ciag jezeli nie znalazl
 * @reutrn string
 */
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}