Array.prototype.in_array = function ( obj ) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if ( this[x] == obj ) return true;
	}
	return false;
};

String.prototype.addParam = function(param, val) {

	if (!this.getParam(param, false))
	{
		var tmp = this.trim('/'), hash = '';
		var iHash = (tmp.indexOf('?') != -1 ? tmp.indexOf('?') : (tmp.indexOf('#') != -1 ? tmp.indexOf('#') : false));
		if (iHash)
		{
			hash = tmp.substring(iHash);
			tmp = tmp.substring(0, iHash);
		}
		
		return tmp.concat('/'+ param +'/'+ val + hash);
	}

	return this;
};

String.prototype.getParam = function(param, defaultValue) {
    var v = this.split('/');
    if(param != '')
    {
        for(var i=0; i < v.length; i++)
        {
            if(v[i] == param) { return (v[i+1]); }
        }
    }
    return defaultValue;
};

if (typeof Flattr == 'undefined') {
	Flattr = {
		ajax: function(url, callback) {
				
			var token = this.getCookie("f_rt");
			if (!token) {
				return false;
			}
			
			var options = {};
			
			if (typeof url === "string") {
				options.url = url;
				
				if (callback) {
					options.success = callback;
				}
			}
			
			options.beforeSend = function(xhr) {
				xhr.setRequestHeader("X_REQUEST_TOKEN", token);
			};
	
			$.ajax(options);
		},
		
		getCookie: function(name) {
			var c_start, c_end;
			
			if (document.cookie.length>0) {

				c_start = document.cookie.indexOf(name + '=');
				if (c_start != -1) {
					c_start += 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 '';
		}
	};
}

function getSelected(formId, fieldName, flag)
{
	values = new Array();
	count = 0;

	$("form#" + formId + " INPUT[type='checkbox']").each(
		function()
		{
			if ( $(this).attr('checked') )
			{
				values.push($(this).val());
				count += 1;
			}
		}
	);
	if ( count > 0 )
	{
		return values;
	}
	return false;
}

function checkSelected(formId, fieldName, selectedValues, flag)
{
	values = selectedValues.split(',');
	if ( fieldName == '' )
	{
		$("form#" + formId + " INPUT[type='checkbox']").each(
			function() 
			{
				if ( values.in_array($(this).val()) )
				{
					$(this).attr('checked', flag);
				} 
			}
		);
	}
	else
	{
		$("form#" + formId + " INPUT[@name=" + fieldName + "][type='checkbox']").each(
			function() 
			{
				if ( values.toString().indexOf($(this).val()) !== -1 )
				{
					$(this).attr('checked', flag);
				} 
			}
		);
	}
}

function checkAll(formId, fieldName, flag)
{
	if ( fieldName == '' )
	{
		$("form#" + formId + " INPUT[type='checkbox']").attr('checked', flag);
	}
	else
	{
		$("form#" + formId + " INPUT[@name=" + fieldName + "][type='checkbox']").attr('checked', flag);
	}
}

$(function()
{
	$("#header .tabs li").hover(function() {
		$('ul:first', this).show();
	}, function() {
		$('ul:first', this).hide();
	});
});

/* Related to story */ 
function submitComment()
{
	$.ajax({
		url:  '/comment.php',
		type: 'post',
		data: $("#commentStory").serialize(),
		success: function(data) {
			$('#comments').append(data);
			$('#commentFrm').hide();
		},
		error: function(xhr, textStatus, errorThrown) {
			alert(xhr.responseText);
		}
	});
}
/* End story */

function setHover(obj, isHover)
{
	if (isHover)
		$(obj).addClass('hover');
	else
		$(obj).removeClass('hover');
}

/** TABS **/

function initTabs()
{
	var hash = window.location.hash;
	if (hash.length > 4 && hash.substring(1, 5) == 'tab_')
	{
		hash = hash.substring(1);
		var tabLink = $('a[name="'+ hash +'"]');
		if (tabLink.length > 0)
		{
			tab(hash, tabLink.attr('href'));
		}
	}
}

function tab(tabId, url)
{
	$('.tab_container').hide();
	
	if (!url)
	{
		$('#' + tabId).show();
		return;
	}

	var tab = $('#' + tabId);
	if (tab.length != 0)
	{
		if (tab.html() == '')
		{
			var token = $.cookie("f_rt");
			if (token) {
				
				$.ajax({
					url: url,
		    		beforeSend: function(xhr) {
		    			xhr.setRequestHeader("X_REQUEST_TOKEN", token);
		    		},
		    		success: function(response) {
		    			window.location.hash = tabId;

		    			tab.html(response);
		    			tab.show();
		    		},
					error: function(xhr, textStatus, errorThrown) {
						alert('An error has occured');
					}
				});
				
			}
		}
		else
		{
			tab.show();
		}
	}
}
