$(document).ready(function() {

	if ($("#login_remember_input").length == 1)
	{
		//emulate graphical checkbox
		if ($("#login_remember_input").get(0).checked)
			$("#login_remember").addClass("selected");
		$("#login_remember_input").focus(function () {
			$("#login_remember").addClass("focused");
		});
		$("#login_remember_input").blur(function () {
			$("#login_remember").removeClass("focused");
		});
		$("#login_remember_input").change(function () {
			$("#login_remember").toggleClass("selected");
		});
		$("#login_remember").click(function () {
			$("#login_remember_input").get(0).checked = !$("#login_remember_input").get(0).checked;
			$("#login_remember_input").change();
			return false;
		});
	}
		
	if ($('#auth_login_input'))
		$('#auth_login_input').focus(function(){$(this).val('')})
	if ($('#auth_password_input'))
		$('#auth_password_input').focus(function(){$(this).val('')})

	// flashes
	if ($('#flash'))
		$("#flash").fadeTo("fast", 0.7).fadeTo("fast", 0.9).fadeTo("fast", 0.7).fadeTo("fast", 0.9).fadeTo("fast", 0.7)
//		$("#flash").fadeTo("slow", 0.9).animate({opacity: 0.9}, 5000).animate({opacity: 0}, 2000)

	// hide & show default text in searcher
	$('#search-query').each (function() {
		this.original_value = this.value
		this.onfocus = function() { if (this.value == this.original_value) this.value = '' }
		this.onblur = function() { if (!this.value) this.value = this.original_value }
		this.onkeyup = function (event) { if (event.keyCode == 27) this.value = '' }
	})

	// add confirms to buttons with 'confirm' class
	//$('.confirm').click (function(){
	//	confirm ('Czy na pewno?')
	//})
	
	$("#zatrabas_extra_fields_toggle").click(function () {
		$("#zatrabas_extra_fields").slideToggle();
		return false;
	});
})
	

/* STAR SNAPS */
function highlight_stars(id, div_id, note, do_highlight)
{
	for (var i = 1 ; i <= note ; i++)
	{
		if (do_highlight)
			$('#vote_'+id+'_'+div_id+'_'+i).addClass('ratehover');
		else
			$('#vote_'+id+'_'+div_id+'_'+i).removeClass('ratehover');
	}
}

/* Universal star-voting class */
function VoteStars (parent_id)
{
	this.parent = $('#'+parent_id);
	this.n = 5
	
	this.highlight = function (n)
	{
		if (n)
			for (i = 1; i <= n; i++)
				this.parent.find('div:eq('+(i-1)+')').addClass('over')
	}
	
	this.light = function (n)
	{
		for (i = 1; i <= n; i++)
			this.parent.find('div:eq('+(i-1)+')').addClass('on')
	}
	
	this.light_half = function (n)
	{
		this.parent.find('div:eq('+(n-1)+')').addClass('half')
	}

	this.darken = function (n)
	{
		for (i = n; i >= 1; i--)
			this.parent.find('div:eq('+(i-1)+')').removeClass('over')
	}

	this.your_vote = function (n)
	{
		if (n != null)
			this.parent.parent().find('#your_vote').html ('Twój głos: <strong>' + n + '</strong>')
	}

	this.render = function(login_url)
	{
		var html = ''
		for (i = 1; i <= this.n; i++)
			html += "<div class='star'/>"
		html += "<div id='in-progress'>"
		html += "	<img src='/images/in-progress-indicator.gif' width='16px' height='16px' style='border: none; display: none' alt='ładowanie…'/>"
		html += "</div>"
		this.parent.html(html)
		this.parent.parent().append ('<p id="your_vote"></p>')
		this.stars = this.parent.find('div')
		if (login_url)
		{
			this.stars.click(function(){
				document.location.href = login_url
			})
		}
	}

	this.avg = function(avg)
	{
		if (avg > 0)
			this.parent.parent().append ('<b>'+(Math.round(avg) == avg ? avg + '.0' : avg)+'</b>')

		if (avg != null)
		{
			if (avg > (4 + 5/9)) { this.light (5); return }
			if (avg > (4 + 1/9)) { this.light (4); this.light_half (5); return }
			if (avg > (3 + 6/9)) { this.light (4); return }
			if (avg > (3 + 2/9)) { this.light (3); this.light_half (4); return }
			if (avg > (2 + 7/9)) { this.light (3); return }
			if (avg > (2 + 3/9)) { this.light (2); this.light_half (3); return }
			if (avg > (1 + 8/9)) { this.light (2); return }
			if (avg > (1 + 4/9)) { this.light (1); this.light_half (2); return }
			if (avg > (1 + 0/9)) { this.light (1); return }
		}
	}

	this.allow_voting = function(url, ref_table, ref_id, security_token)
	{
		var t = this
		
		// Add numeric titles to stars 1…n (default is 'you must be authorized to vote')
		var i = 1
		t.stars.each(function(){
			$(this).attr('title', i)
			i += 1
		})

		// Stars highlighting when mouse over
		t.stars.mouseover(function(){
			t.highlight ($(this).attr('title'))
			$(this).mouseout(function(){
				t.darken ($(this).attr('title'))
			})
		})
		
		// Voting after clicking star
		t.stars.click(function(){
			var grade = $(this).attr('title')
			$.ajax({
				type: "post",
				url: url,
				data: { ref_table: ref_table, ref_id: ref_id, grade: grade, _csrf_token: security_token },
				beforeSend: function() { $('#in-progress img').show() },
				complete: function() { $('#in-progress img').hide() },
				success: function(html){
					if (html != 'fail')
					{
						for (i = 1; i <= t.n; i++)
						{
							if (i <= grade)
								t.parent.find('div[title='+i+']').addClass('on').removeClass('half')
							else
								t.parent.find('div[title='+i+']').removeClass('on half')
						}
					}
					else
						alert ('Nie udało się oddać głosu')
					t.your_vote(grade)
				}
			})
		})
	}
	
	this.disallow_voting = function(url)
	{
		$(this).parent().parent().parent().fadeOut('slow')
	}
}

function h (s)
{
    return s.toString().replace (/&/g, "&amp;").replace (/</g, "&lt;").replace (/>/g, "&gt;")
}

function auto_clear_input (jquery)
{
	jquery.focus (function (event) { if (this.value == this.defaultValue) this.value = '' })
	jquery.blur (function (event) { if (!this.value) this.value = this.defaultValue })
	jquery.keyup (function (event) { if (event.keyCode == 27) this.value = '' })
}

function get_cookie (name)
{
	var start = document.cookie.indexOf (name + "=")
	var len = start + name.length + 1
	if ((!start) && (name != document.cookie.substring (0, name.length)))
		return null
	if (start == -1)
		return null
	var end = document.cookie.indexOf (';', len)
	if (end == -1)
		end = document.cookie.length
	return unescape (document.cookie.substring (len, end))
}

function set_cookie (name, value, expires, path, domain, secure)
{
	var expires_date = new Date()
	expires_date.setTime (expires_date.getTime() + expires * 1000)
	document.cookie = name + '=' + escape (value) +
		(expires? ';expires=' + expires_date.toGMTString() : '') + //expires.toGMTString()
		(path? ';path=' + path : '') +
		(domain? ';domain=' + domain : '') +
		(secure? ';secure' : '')
}

/* Voting in news (like digg) */
function news_vote (news_id, url, vote_container, token)
{
	$.ajax({
		type: "post",
		url: url,
		data: { news_id: news_id, _csrf_token: token },
		success: function(html){
			var changer = vote_container.parent().find('.jslink')
			var new_changer = '';

			vote_container.parent().animate({opacity: 0.1}, 100, null, function(){vote_container.html(html)})
			vote_container.parent().toggleClass('undo')
			vote_container.parent().animate({opacity: 1.0}, 200)
		}
	})
}
