jQuery(function () {
	initEvents();
});

var star_on = base_url + '/images/star_big.gif';
var star_off = base_url + '/images/star_big_off.gif';

function initEvents()
{
	$('#a-loginbutton').click(function () {
		$('#loginform').submit();

		return false;
	});

	$('#a-searchbutton').click(function () {
		$('#searchform').submit();

		return false;
	});


	$('#a-site').click(function () {
		$('#a-profile').removeClass('checked');
		$(this).addClass('checked');
		$('#searchfilter').val('site');

		return false;
	});
	$('#a-profile').click(function () {
		$('#a-site').removeClass('checked');
		$(this).addClass('checked');
		$('#searchfilter').val('profile');

		return false;
	});

	$('.vote img').mouseover( function () {
		rating = 0;
		for (a = 0; a < 5; a++) {
			if ($('.vote img').get(a) == this) {
				rating = a;
			}
		}

		for (a = 0; a < 5; a++) {
			if (a <= rating) {
				$($('.vote img').get(a)).attr('src', star_on);
			} else {
				$($('.vote img').get(a)).attr('src', star_off);
			}
		}

	});

	$('.vote').click( function () {
		post_id = $('#current_entry').val();

		if (typeof post_id == 'undefined') {
			return false;
		}

		rating = 1;

		temp = $('.vote');
		for (a = 0; a < 5; a++) {
			if (temp.get(a) == this) {
				rating = a;
			}
		}

		vote(post_id, rating);

		return false;
	});

	$('#taf').click(function () {

		var width = 250;
		var height = 250;
		var leftPos = ($(window).width() - width) / 2;
		var topPos = ($(window).height() - height) / 2;

		window.open (messagent_url + escape(window.location), 'window', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,width=' + width + ',height=' + 650 + ',top='+ topPos + ',left=' + leftPos);

		return false;
	});

	$('#loginform input[@type=password]').keypress ( function (key) {
		if (key.keyCode == 13) {
			$('#loginform').get(0).submit();
		}
	});

	$('#login-form input[@type=password]').keypress ( function (key) {
		if (key.keyCode == 13) {
			$('#login-form').get(0).submit();
		}
	});

	// Top 10 salt and sweet water onclicks
	$('#top10 .zoet-btn').click( function () {
		$('#fishwater').val('zoet');
		$('#fish').get(0).submit();
	});

	$('#top10 .zout-btn').click( function () {
		$('#fishwater').val('zout');
		$('#fish').get(0).submit();
	});

	// replace the main breadcrumb
	// with BBPress' breadcrumb
	$('#bbpress-iframe').load( function () {
		var container = $(this).contents().find(".bbcrumb");
		var topic_element = $(this).contents().find(".topictitle");

		// add target="#iframe" to all links in the breadcrumb
		$('a', container).each( function() {
			$(this).attr('target', 'bbpress-iframe');
		});

		// get the html from the bbcrumb
		var html = $(container).html();
		var home = '<a href="' + site_url +  '">Home</a> &gt; ';
		var topic = $(topic_element).html();

		// if the topic title is set we need to add it
		if (topic != null) {
			html = html + ' &gt; ' + topic;
		}

		// set the home breadcrumb to bbpress' breadcrumb
		if (html != null) {
			$('#content .breadcrumb').html(home + html);
		} else {
			$('#content .breadcrumb').html(home + 'Forum');
		}
	});

}

function vote(post_id, rating)
{
    var vote_url = base_url + '/ajax/vote.php';
	$.get(vote_url,
		{
			rating: rating,
			post_id: post_id,
			author_id: current_user
		},
		function (data) {
			// we silently vote and do not handle it, this will also avoid that the user thinks the vote is hanging
		}
	);

	// we always show that the result was correct
	alert('Bedankt voor je stem!');

	return false;
}
