$('document').ready(function () {
	$('a').focus(function () { $(this).blur(); });

	/***********************************
	 RIGHT COL
	 **********************************/
	/* Control Panel */
	$('#toggle_rightcol_controlpanel a').click(function () {
		if (getCookie('rightcol_controlpanel') == 1) {
			$('#content_rightcol_controlpanel').show();
			$(this).removeClass('down');
			$(this).addClass('up');
			setCookie('rightcol_controlpanel', 0);
		} else {
			$('#content_rightcol_controlpanel').hide();
			$(this).removeClass('up');
			$(this).addClass('down');
			setCookie('rightcol_controlpanel', 1);
		}
	});

	/* Currency */
	$('#toggle_rightcol_currency a').click(function () {
		if (getCookie('rightcol_currency') == 1) {
			$('#content_rightcol_currency').show();
			$(this).removeClass('down');
			$(this).addClass('up');
			setCookie('rightcol_currency', 0);
		} else {
			$('#content_rightcol_currency').hide();
			$(this).removeClass('up');
			$(this).addClass('down');
			setCookie('rightcol_currency', 1);
		}
	});

	/* Prorails */
	$('#toggle_rightcol_prorails a').click(function () {
		if (getCookie('rightcol_prorails') == 1) {
			$('#content_rightcol_prorails').show();
			$(this).removeClass('down');
			$(this).addClass('up');
			setCookie('rightcol_prorails', 0);
		} else {
			$('#content_rightcol_prorails').hide();
			$(this).removeClass('up');
			$(this).addClass('down');
			setCookie('rightcol_prorails', 1);
		}
	});

	/* Friendslist */
	$('#toggle_rightcol_friendlist a').click(function () {
		if (getCookie('rightcol_friendslist') == 1) {
			$('#content_rightcol_friendslist').show();
			$(this).removeClass('down');
			$(this).addClass('up');
			setCookie('rightcol_friendslist', 0);
		} else {
			$('#content_rightcol_friendslist').hide();
			$(this).removeClass('up');
			$(this).addClass('down');
			setCookie('rightcol_friendslist', 1);
		}
	});

	/* Ajax Request Friendship - Used on Searchpage */
	$('a.req_friendship').click(function() {
		var uid = $(this).attr('id');
	
		if (! uid) return false;

		$(this).hide();

		$.get('ajax.php', {module: 'friends/addfriend', userId: uid}, function (data) {
			if (data) {
				alert(data);
			}
		});
	});

	$('a.req_friendship_add').click(function() {
		var uid = $(this).attr('id').substr(4);
		var row = $(this).parent().parent();

		$('#add_'+ uid).hide();
		$('#del_'+ uid).hide();

		$.get('ajax.php', {module: 'friends/requestaction', action: 'approve', userId: uid}, function (data) {
			if ('OK' == data) {
				msg = 'You have authorized a friend to add you to his/hers contactlist.\n';
				msg += 'Do you want to add this person to your contactlist?';
				if (confirm(msg)) {
					$.get('ajax.php', {module: 'friends/requestaction', action: 'approve_me', userId: uid}, function (data) {
					});
				}
				alert('Friendship was accepted');
				row.empty();
			} else {
				alert('Request was not successful');
			}
		});
	});

	$('a.req_friendship_del').click(function() {
		var uid = $(this).attr('id').substr(4);
		var row = $(this).parent().parent();

		$('#add_'+ uid).hide();
		$('#del_'+ uid).hide();

		$.get('ajax.php', {module: 'friends/requestaction', action: 'deny', userId: uid}, function (data) {
			if ('OK' == data) {
				alert('Friendship was denied');
				row.empty();
			} else {
				alert('Request was not successful');
			}
		});
	});

	$('a.your_friendship_del').click(function() {
		var uid = $(this).attr('id').substr(4);
		var row = $(this).parent().parent();

		$('#rem_'+ uid).hide();

		$.get('ajax.php', {module: 'friends/requestaction', action: 'del', userId: uid}, function (data) {
			if ('OK' == data) {
				alert('Friendship request was deleted');
				row.empty();
			} else {
				alert('Request was not deleted');
			}
		});
	});

	$('a.add_block').click(function () {
		var realId = $(this).attr('id');
		var uid = realId.substr(6);
		var obj = $(this);

		$.get('ajax.php', {module: 'friends/blockaction', action: 'add', userId: uid}, function (data) {
			if ('OK' == data) {
				obj.hide();
				alert('User added to blocklist');
			} else {
				alert('User could not be added to blocklist')
			}
		});
	});

	$('a.del_block').click(function () {
		var uid = $(this).attr('id').substr(6);
		var obj = $(this);

		$.get('ajax.php', {module: 'friends/blockaction', action: 'del', userId: uid}, function (data) {
			if ('OK' == data) {
				obj.hide();
				alert('User deleted from blocklist');
			} else {
				alert('User could not be deleted from blocklist')
			}
		});
	});

	$('a.del_block_list').click(function () {
		var uid = $(this).attr('id').substr(6);
		var row = $('#row_'+ uid);
		var obj = $(this);

		$.get('ajax.php', {module: 'friends/blockaction', action: 'del', userId: uid}, function (data) {
			if ('OK' == data) {
				row.hide();
				alert('User deleted from blocklist');
			} else {
				alert('User could not be deleted from blocklist')
			}
		});
	});

	/* Ajax - Currency */
	$('#calcCurrencyForm').submit(function () {
		$('#sendCalcCurr').trigger("click");
		return false;
	});

	$('#sendCalcCurr').click(function () {
		var c_c = $('#calc_c').attr('value');
		var c_f = $('#calc_f').attr('value');
		var c_t = $('#calc_t').attr('value');

		$.get('ajax.php', {module: 'rightcol/calccurrency', calc_c: c_c, calc_f: c_f, calc_t: c_t}, function (data) {
			if (data) {
				$('#calcCurrResult').html('Result: <b>'+ data +'</b>')
			}
		});
	});


	/* Ajax - Picture */
	$('.thumbnail').click(function () {
		var info = new Array;
		info = $(this).attr('id').split('_');
		w = parseInt(info[1]) + 20;
		h = parseInt(info[2]) + 120;

		$('#imgholder').empty();
		$('#imgholder').show();
		$('#imgholder').css('width', w +'px');
		$('#imgholder').css('height', h +'px');
		$('#imgholder').addClass('imageloading');
		$('#imgholder').centerElement(-20);
		$.get('ajax.php', {module: "others/image", imageId: info[0] }, function (data) {

			$('#imgholder').removeClass('imageloading');
			$('#imgholder').html(data);
			$('#imgholder').find('a.closeImage').click(function() {
				$('#imgholder').hide();
				$('#imgholder').empty();
				$('#imgholder').css('width', 0);
				$('#imgholder').css('height', 0);
			});
			$('#imgholder').css('height', 'auto');
		});
	});

	$('#searchRbUser').submit(function () {
		$('#searchForm').toggleClass('hidden');
		$('#searchInfo').show();
	});
});

function setHome (myLink) {
	if ($.browser.msie) {
		myLink.style.behavior='url(#default#homepage)';
		myLink.setHomePage('http://www.railbirds.com');
	} else if ($.browser.mozilla) {
		alert('Drag this link onto your "Home button".');
	} else {
		alert('Sorry, your browser does not support this.');
	}
}


/* Friendlist toggle groups */
function showHideGroup (groupId) {
	var o = $('#group_'+ groupId);
	var c = 'friendlist_'+ groupId;

	if (! o) return;

	if (getCookie(c) == 1) {
		o.show(); setCookie(c, 0);
	} else {
		o.hide(); setCookie(c, 1);
	}
}

function showHideOnline (groupId) {
	var o = $('#group_'+ groupId +'_online');
	var c = 'friendlist_'+ groupId +'_online';

	if (! o) return;

	if (getCookie(c) == 1) {
      o.show(); setCookie(c, 0);
   } else {
      o.hide(); setCookie(c, 1);
   }
}

function showHideOffline (groupId) {
	var o = $('#group_'+ groupId +'_offline');
	var c = 'friendlist_'+ groupId +'_offline';

	if (! o) return;

	if (getCookie(c) == 1) {
      o.show(); setCookie(c, 0);
   } else {
      o.hide(); setCookie(c, 1);
   }
}

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 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 delCookie (key) {
	document.cookie = key + "=; expires=Sun, 31 Dec 2006 20:00:00 UTC; path=/";
}


function selectToUrl (selObj) {
	if (selObj && selObj.value) {
		document.location.href = selObj.value;
	}

	return false;
}

function textToUrl (url, objId) {
	var obj = $('#'+ objId);
	var url = url;

	if (obj) {
		val = obj.attr('value');
		if (val && val != 'undefined') {
			url += val + '/';
		}
	}

	if (url) {
		document.location.href = url;
	}

	return false;
}

// Select | Deselect all checkboxes within form
function selectAll (formId, checkboxclass, mode) {
	$('#'+ formId).find('.'+ checkboxclass).each(function(i) {
		this.checked = mode;
	});
}

// Set hidden input and submit form
function formSubmit (formId, hiddenId, hiddenValue) {
	$('#'+ hiddenId).attr('value', hiddenValue);
	$('#'+ formId).submit();
}


function chatpop (url) {
	var time = new Date();
	var name = Math.floor((Math.random() * Math.round(time.getTime())));
	var prop = 'toolbar=no,location=no,directories=no,status=yes,';
	prop += 'menubar=no,scrollbars=no,resizable=yes,copyhistory=no,';
	prop += 'width=260,height=600';

	var win = window.open(typeof(url) == 'string' ? url : url.href, name, prop);
	win.focus();
	return false;
}

function confirmLink(url, message) {
	if (confirm(message)) {
		document.location = url;
	}
}
