/* $Id: poker.js 330 2008-05-21 09:28:02Z mackan $ */

// Globals
var tabIsLoading = false;
var hasPwForm = false;

  

$('document').ready(function () {
	$('#pollform .poll_vote').click(function () {
		var answer = $(this).val();
		$.get('/pse-ajax/', {module: 'poll/vote', vote: answer}, function (data) {
         if (data) {
				$('#polldiv').html(data);
			}
      });
	});

	$('#pollform .show_result').click(function() {
		$.get('/pse-ajax/', {module: 'poll/poll', view: 'yes'}, function (data) {
			if (data) {
				$('#polldiv').html(data);
			}
		});
	});

	$('#video-embed').click(function() {
		$(this).select();
	});

	// Tabs
	$('#right-tab-tabs a').click(function () {
		if (tabIsLoading) return;

		tabIsLoading = true;

		var tid = $(this).attr('id');
		var val = $(this).html();
	
		$('#right-tab-tabs a').each(function () {
			$(this).removeClass('tab-on');
			$(this).addClass('tab');
		});

		$(this).removeClass('tab');
		$(this).addClass('tab-on');

		$('#right-tab-html').html('<b>Laddar...</b><br/>');

		$.get('/pse-ajax/', {module: 'right/blocks/'+ tid}, function (data) {
			$('#right-tab-html').html(data);
			tabIsLoading = false;
		});

		return false;
	});

	$('div#comment-page a').click(function () {
		var next = parseInt($(this).text());
		var arti = $(this).attr('id');
		arti = arti.substring(0, arti.indexOf('_'));

		
		$('div#comment-list').html('<img src="static/images/loading-small.gif" alt="Laddar"/><br/>');

		$.get('/pse-ajax/', {module: 'articles/comment-list', articleID: arti, page: next}, function (data) {
			$('div#comment-list').html(data);
		});

		return false;
	});


	$('#event-list a.event-view').click(function () {
		var idstr = $(this).attr('id');
		var idval = idstr.substring(11);
		var open = $('#event-list tr#event-body-'+ idval).css('display') != 'none';

		$('#event-list tr.event-body').hide();

		if (open) {
			$('#event-list tr#event-body-'+ idval).hide();
		} else {
			$('#event-list tr#event-body-'+ idval).show();
		}
	});
});

function getEditorPreview (instanceName) {
	return popup('/articles/editorpreview.php', 'editorpreview', 520, 600, 'no', 'no', 'yes');
}


function delCookie (key) {
	document.cookie = key + "=; expires=Sun, 31 Dec 2001 01:00:00 UTC; path=/";
}

function setCookie (key, value) {
	document.cookie = key + "=" + value + "; expires=Mon, 31 Dec 2012 20:00:00 UTC; path=/";
}

function getCookie (key) {
	var cookie = document.cookie.split(';');
	var name = key + "=";

	for (var i = 0; i < cookie.length; ++i) {
		var c = cookie[i];

		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}

		if (c.indexOf(name) == 0) {
			return c.substring(name.length, c.length);
		}
	}

	return null;
}

function myAlert (str) {
	var tmp = document.createElement("textarea");
	tmp.innerHTML = str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
	alert(tmp.value);
}

function popup (url, name, width, height, status, menu, resizable, scrollbars) {
	var prop = 'toolbar=no,location=no,directories=no,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? resizable : 'yes') +',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'scrollbars='+ (scrollbars ? scrollbars : 'yes') +',';
	prop += 'width='+ (width ? width : 500) +',';
	prop += 'height='+ (height ? height : 400);

	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);
	win.focus();

	return false;
}

function freshpopup (url, width, height, status, menu, resizable) {
	var time = new Date();
	var name = Math.floor((Math.random() * Math.round(time.getTime())));
	var prop = 'toolbar=no,location=no,directories=no,scrollbars=yes,copyhistory=no,';
	prop += 'status='+ (status ? 'yes' : 'no') +',';
	prop += 'resizable='+ (resizable ? 'yes' : 'no') + ',';
	prop += 'menu='+ (menu ? 'yes' : 'no') +',';
	prop += 'width='+ (width ? width : 500) +',';
	prop += 'height='+ (height ? height : 400);

	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);
	win.focus();

	return false;
}

function selectToUrl (o) {
	if (o && o.value) {
		document.location.href = o.value;
	}

	return false;
}






