var regexp_valid_email = /^[0-9a-zA-Z_-]+(\.[0-9a-zA-Z_-]+)*@[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?(\.[0-9a-zA-Z]([0-9a-zA-Z-]*[0-9a-zA-Z])?)*$/;

var favourites_add = function() {}
var favourites_del = function() {}
var favourites_ajax;

var whowashere = function() {}
var whowashere_ajax;

$(function() {
	favourites_fn = function(action, url, title) {
		if (!action || action != "add" && action != "delete") return;
		if (favourites_ajax) favourites_ajax.abort();
		$(".favourites_add").hide();
		$(".favourites_del").hide();
		$(".favourites_wait").show();
		favourites_ajax = $.ajax({
			type: "post",
			url: "/favourites/" + action,
			data: {uri: url, title: title},
			async: true,
			dataType: "html",
			error: function() {
				$(".favourites_wait").hide();
				if (action == "add") {
					$(".favourites_add").show();
				} else {
					$(".favourites_del").show();
				}
			},
			success: function(data) {
				$(".favourites_wait").hide();
				if ((data == "OK") != (action == "add")) {
					$(".favourites_add").show();
				} else {
					$(".favourites_del").show();
				}
			}
		});
	}

	favourites_add = function(url, title) {
		favourites_fn("add", url, title);
	}

	favourites_del = function(url) {
		favourites_fn("delete", url, "");
	}

	whowashere = function(action, id) {
		if (typeof(action) != "string" || action != "addme" && action != "delme" || typeof(id) != "string" || !id.length) return;
		if (whowashere_ajax) whowashere_ajax.abort();
		$(".whowashere_addme").hide();
		$(".whowashere_delme").hide();
		$(".whowashere_wait").show();
		whowashere_ajax = $.ajax({
			type: "post",
			url: "/show/" + action + "/" + id,
			async: true,
			dataType: "html",
			error: function() {
				$(".whowashere_wait").hide();
				if (action == "addme") {
					$(".whowashere_addme").show();
				} else {
					$(".whowashere_delme").show();
				}
			},
			success: function(data) {
				$(".whowashere_wait").hide();
				if (data == "OK") {
					$('#whowashere_all').click();
				}
				if ((data == "OK") != (action == "addme")) {
					$(".whowashere_addme").show();
				} else {
					$(".whowashere_delme").show();
				}
			}
		});
	}

	$("#mycarousel").jcarousel({
		animation: 1000
	});
	$("#mycarousel2").jcarousel({
		animation: 1000,
		scroll: 1
	});

	$("#popup3 .close").click(function() {
		$("#popup3").hide();
		return false;
	});

	if (typeof ($.rating) != "undefined") {
		$(".auto-star").rating({
			callback: function(value, link){
			// "this" is the hidden form element holding the current value
			// "value" is the value selected
			// "element" points to the link element that received the click.
			var res = jQuery.ajax({
							type: "POST",
							url:  "/vote/",
							data: "name="+this.name+"&rating="+value,
							async: false
						}).responseText;
			if( res == "OK")
				$(".star_group_"+this.name).unbind("mouseover").unbind("mouseout").unbind("click").removeClass("star_live");
			}
		});
	}

	$(".open-window").click(function() {
		window.open($(this).attr("href"), "", "scrollbars=1, width=510, height=510, resizable=no");
		return false;
	});

	$(".open-player-window").click(function() {
		window.open($(this).attr("href"), "", "scrollbars=0, width=420, height=470, resizable=no");
		return false;
	});

	$(".open-player-na-window").click(function() {
		window.open($(this).attr("href"), "", "scrollbars=0, width=420, height=200, resizable=no");
		return false;
	});
});
