// bash.org.pl
// Copyright © 2009 Krzysztof Urbaniak <urban@fail.pl>


var lastid = 0;
var banners = '';

function losuj() {
	return 0.5 - Math.random()
}

function setActiveStyleSheet(styleName) {
		$('link[@rel*=style][title]').each(function(i)
		{
			this.disabled = true;
			if (this.getAttribute('title') == styleName) this.disabled = false;
		});
	$.cookie('st', styleName, {expires: 365, path: '/'});

}

$(document).ready(function() {
	var c = $.cookie('st');
	if (c) setActiveStyleSheet(c);

	$('#side_ads').html($('#side_ads2').html())
	$('#side_ads2').remove()
	$('#container').before($('#abovecontent').html())
	$('#abovecontent').remove()

	$('.q .points').each(function() {
		if(jQuery.cookie('v').match(':'+this.parentNode.parentNode.id.split('d')[1]+':')) {
			$(this.parentNode).children('.votes').css('visibility', 'hidden');
			$(this).css('visibility', 'visible');
		} else {
			$(this).css('visibility', 'hidden');
		}
		if($(this).hasClass('points2')) {
			$(this).css('visibility', 'visible');
		}
//		$(this.parentNode).children('.votes').click(function() {
//			return vote(this);
//		});
	});
	$('a.collapse').click(function(){
		$(this.parentNode.parentNode.parentNode).children('.quote').slideUp();
	})
	$('a.ajax').click(function(){
		$(this.parentNode.parentNode).children('.msg').html('&nbsp;');
		$(this.parentNode.parentNode).children('.msg').addClass('loading');
		obj=this
		$.ajax({
			type: 'GET',
			url: this.href.split("#")[0],
			timeout: 10000,
			processData: false,
			data: 'ajax=true',
			error: function(x, msg, y){
					$(obj.parentNode.parentNode).children('.msg').removeClass('loading');
					$(obj.parentNode.parentNode).children('.msg').text(msg + x.status);
				},
			success: function(msg){
					$(obj.parentNode.parentNode).children('.msg').removeClass('loading');
					var content = msg;
					$(obj.parentNode.parentNode).children('.msg').text(content);
				}
		})
		return false;
	});

	$('a.votes').click(function(){
		$(this.parentNode).children('.msg').html('&nbsp;');
		$(this.parentNode).children('.msg').addClass('loading');
		$(this.parentNode).children('.points').css('visibility', 'visible');
		$(this.parentNode).children('.votes').css('visibility', 'hidden');
		obj=this
		$.ajax({
			type: 'GET',
			url: obj.href.split("#")[0],
			timeout: 10000,
			processData: false,
			data: 'ajax=true',
			error: function(x, msg, y){
					$(obj.parentNode).children('.msg').removeClass('loading');
					$(obj.parentNode).children('.msg').text(msg + x.status);
				},
			success: function(msg){
					$(obj.parentNode).children('.msg').removeClass('loading');
					points = parseInt($(obj.parentNode).children('.points').text())
					if(obj.className.match('rox')) {
						$(obj.parentNode).children('.points').text(points + 1)
					} else {
						$(obj.parentNode).children('.points').text(points - 1)
					}
					$(obj.parentNode).children('.msg').text(msg);
				}
		})
		return false;
	});
	var cid = $.cookie('lastid') - 0;
	$('.q').each(function() {
		var id = this.id.split('d')[1] - 0;
		if(id > cid) {
			$(this).children('.bar').children('.msg').html('<b>Nowy!</b>');
		}
		if(id > lastid) {
			lastid = id;
		}
	});
	if(lastid > cid) {
		$.cookie('lastid', lastid, {expires: 356, path: '/'});
	}
	var links = new Array();
	var i = 0;
        $('div#links').children('.random').each(function(){
		links[i++] = $(this).html();
                $(this).remove();
        })

	links.sort(losuj)

	for(var a = 0; a < links.length; a++) {
		$('div#links').append('<p>' + links[a] + '</p>')
	}

	$('a.linkclick').click(function() {
		$.get(this.id);
		return true;
	})

	$.ajaxSetup ({
		cache: false
	});

	$('.q').each(function() {
		url = "http://bash.org.pl" + $(this).children('.bar').children('.qid').attr('href');
		title = $(this).children('.quote').text()
		fbshare = '<a class="fbshare" href="http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title) + '"></a>';
		$(this).children('.bar').children('.sux').after(fbshare);
	});

})
