/***************************************************************************/
/**
 * Membersclub Theme
 * @author Simon Jensen
 */
/***************************************************************************/

/**
 * Check if terms has been accepted
 */
function checkRegistration(form) {
	var terms_ok = $("#terms").is(":checked");
	if(!terms_ok) {
		$.facebox("Du skal godkende betingelserne før du kan blive medlem!");
		return false;
	} else {
		return true;
	}
}

/***************************************************************************/

function changeLang(select, redir) {
	// Set cookie and redirect
	var selected_lang;
	for(var i=0; i<select.length; i++)
	{
		if ((select.options[i] != null) && (select.options[i].selected))
		{
			selected_lang = select.options[i].value;
		}
	}
	$.cookie('lang', selected_lang, { expires: 365, path: '/' });
	location.href = redir;
}

/***************************************************************************/

function makeFriend(friendid) {
	$("#friendlink").html('<img src="/themes/membersclub/gfx/loading2.gif" alt="Loading..." align="absmiddle" />');
	$.post("/actions/friend.php", {
		todo: "create",
		friendid: friendid
	},
	function(data) {
		$("#friendlink").fadeOut(300, function() {
			$("#friendlink").fadeIn(300).html(data);
		});
	});
}

function removeFriend(friendid) {
	$("#friendlink").html('<img src="/themes/membersclub/gfx/loading2.gif" alt="Loading..." align="absmiddle" />');
	$.post("/actions/friend.php", {
		todo: "delete",
		friendid: friendid
	},
	function(data) {
		$("#friendlink").fadeOut(300, function() {
			$("#friendlink").fadeIn(300).html(data);
		});
	});
}

function acceptFriend(friendid, requestid) {
	$("#request_msg_"+requestid).html('<img src="/themes/membersclub/gfx/loading2.gif" alt="Loading..." align="absmiddle" />');
	$.post("/actions/friend.php", {
		todo: "accept",
		friendid: friendid
	},
	function(data) {
		$("#request_msg_"+requestid).fadeOut(400, function() {
			$("#request_msg_"+requestid).fadeIn(400).html(data);
		});
	});
}

function rejectFriend(friendid, requestid) {
	$("#request_msg_"+requestid).html('<img src="/themes/membersclub/gfx/loading2.gif" alt="Loading..." align="absmiddle" />');
	$.post("/actions/friend.php", {
		todo: "reject",
		friendid: friendid
	},
	function(data) {
		$("#request_"+requestid).fadeOut(500);
	});
}

/***************************************************************************/

function acceptRelationship(requestid) {
	$.post("/actions/relationship.php", {
		todo: "accept",
		requestid: requestid
	},
	function(data) {
		$("#request_msg_"+requestid).fadeOut(400, function() {
			$("#request_msg_"+requestid).fadeIn(400).html(data);
		});
	});
}

function rejectRelationship(requestid) {
	$.post("/actions/relationship.php", {
		todo: "reject",
		requestid: requestid
	},
	function(data) {
		$("#request_"+requestid).fadeOut(500);
	});
}

/***************************************************************************/

function updateMailCounter() {
	$.get('/themes/membersclub/_my_mail_counter.php', function(data) {
		$('#mailcounter').html(data);
	});
}

/***************************************************************************/

function updateShoutouts() {
	$.get('/themes/membersclub/_shoutout.php?cacheFix='+Math.random(), function(data) {
		$('#shout').fadeOut(500, function() {
			$('#shout').fadeIn(500).html(data);
		});
	});
}

/***************************************************************************/

function updateEvents() {
	$.get('/themes/membersclub/_events.php?cacheFix='+Math.random(), function(data) {
		$('#upcomming_events').fadeOut(500, function() {
			$('#upcomming_events').fadeIn(500).html(data);
		});
	});
}

function updateGalleries() {
	$.get('/themes/membersclub/_galleries.php?cacheFix='+Math.random(), function(data) {
		$('#latest_galleries').fadeOut(500, function() {
			$('#latest_galleries').fadeIn(500).html(data);
		});
	});
}

/***************************************************************************/

function justDialog(url) {
	$.facebox(function() {
		$.get(url, function(data) {
			$.facebox(data);
		})
	});
}

function closeDialog() {
	jQuery(document).trigger('close.facebox');
}

function popUrl(url, w, h) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var winprops = "height="+h+",width="+w+",top="+wint+",left="+winl+",scrollbars=yes,resizable,maximizable";
	window.open(url,"",winprops);
}

/***************************************************************************/

/**
 * Things to do, when DOM is ready
 */
$(document).ready(function() {
	
	// Load shoutouts and refresh them each 6 seconds
	updateShoutouts();
	var shoutOuts = setInterval(function() {
		updateShoutouts();
	}, 7000);
	
	// Load events and set interval for refresh
	updateEvents();
	var events = setInterval(function() {
		updateEvents();
	}, 7000);
	
	// Load galleries and set interval for refresh
	updateGalleries();
	var galleries = setInterval(function() {
		updateGalleries();
	}, 7000);
	
	// Update mailcounter
	updateMailCounter();
	var mailCounter = setInterval(function() {
		updateMailCounter();
	}, 30000);
	
	// Create tabs
	$("#photos > ul").tabs();
	
	// Make status texts editable
	$("#status_msg").editable("/actions/message.php", {
		select: true,
		width: '520px',
		submit: 'Ok'
	});
	
	// Make sidebar slide
	$("#sidebar_left").scrollFollow();
	
	// Growl template
	$.growl.settings.displayTimeout = 7000;
	$.growl.settings.noticeTemplate = ''
		+ '<div class="%title%" style="margin-bottom: 5px;">'
		+ '		<div style="background: url(/themes/membersclub/gfx/box_growl_top.png) no-repeat center bottom; width: 320px; height: 15px;"></div>'
		+ '		<div style="background: url(/themes/membersclub/gfx/box_growl_middle.png) repeat-y; color: #fff; padding: 0px 10px 5px 10px;">'
		+ '			%message%'
		+ '		</div>'
		+ '		<div style="background: url(/themes/membersclub/gfx/box_growl_bottom.png) no-repeat center top; width: 320px; height: 15px;"></div>'
		+ '</div>';
	$.growl.settings.noticeCss = {
		position: 'absolute'
	};
	
	// IE 6 is not that well with fixed positioning
	if($.browser.msie && $.browser.version == "6.0") {
		$.growl.settings.dockCss = {
			position: 'absolute',
			top: '10px',
			right: '10px',
			width: '320px'
		};
	}
	
	var growl = setInterval(function() {
		$.get('/themes/membersclub/_growl.php?cacheFix='+Math.random(), function(data) {
			if(data != "") {
				$.growl("growl", data);
			}
		});
	}, 8000);
	
});

/***************************************************************************/