/***************************************************************************/
/**
 * Cult Theme
 * @author Simon Jensen
 */
/***************************************************************************/

/**
 * Check if terms has been accepted
 */
function checkRegistration(form) {
	var terms_ok = $("#terms").is(":checked");
	if(!terms_ok) {
		$.facebox('<div class="error">Du skal godkende betingelserne før du kan blive medlem!</div>');
		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/Cult/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/Cult/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/Cult/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/Cult/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/Cult/_my_mail_counter.php', function(data) {
		$('#mailcounter').html(data);
	});
}

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

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

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

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

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

function updateNews() {
	$.get('/themes/Cult/_news.php?cacheFix='+Math.random(), function(data) {
		$('#news').fadeOut(500, function() {
			$('#news').fadeIn(500).html(data);
		});
	});
}

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

function updateEvents() {
	$.get('/themes/Cult/_events.php?cacheFix='+Math.random(), function(data) {
		$('#events').fadeOut(500, function() {
			$('#events').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 set interval for refresh
	updateShoutouts();
	var shoutOuts = setInterval(function() {
		updateShoutouts();
	}, 7000);
	
	// Load galleries and set interval for refresh
	updateGalleries();
	var galleries = setInterval(function() {
		updateGalleries();
	}, 7000);
	
	// Load news and set interval for refresh
	updateNews();
	var news = setInterval(function() {
		updateNews();
	}, 7000);
	
	// Load events and set interval for refresh
	updateEvents();
	var events = setInterval(function() {
		updateEvents();
	}, 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", {
		width: '465px',
		submit: 'Ok',
		select: true,
		onblur: 'submit'
	});
	
	// 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/Cult/gfx/box_growl_top.png) no-repeat center bottom; width: 320px; height: 15px;"></div>'
		+ '		<div style="background: url(/themes/Cult/gfx/box_growl_middle.png) repeat-y; color: #fff; padding: 0px 10px 5px 10px;">'
		+ '			%message%'
		+ '		</div>'
		+ '		<div style="background: url(/themes/Cult/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/Cult/_growl.php?cacheFix='+Math.random(), function(data) {
			if(data != "") {
				$.growl("growl", data);
			}
		});
	}, 8000);
	
	var webmaster_options = {
		type: 'post',
		dataType: 'json',
		resetForm: false,
		beforeSubmit: function() {
			$("#webmaster_submit").attr("disabled", "true"); 
			$("#webmaster_loading").show();
			$("#webmaster_message").html("").hide();
		},
		success: function(data) {
			$("#webmaster_submit").removeAttr("disabled"); 
			$("#webmaster_loading").hide();
			if(data.err == 0) {
				$("#webmaster_message").html(data.message).fadeIn();
			} else {
				var error_html = '';
				$.each(data.errors, function(i, val) {
					error_html += val.message+'\n';
				});
				alert(error_html);
			}
		}
	};
	$('#webmaster_form').ajaxForm(webmaster_options);
	
	var cult_options = {
		type: 'post',
		dataType: 'json',
		resetForm: false,
		beforeSubmit: function() {
			$("#cult_submit").attr("disabled", "true"); 
			$("#cult_loading").show();
			$("#cult_message").html("").hide();
		},
		success: function(data) {
			$("#cult_submit").removeAttr("disabled"); 
			$("#cult_loading").hide();
			if(data.err == 0) {
				$("#cult_message").html(data.message).fadeIn();
			} else {
				var error_html = '';
				$.each(data.errors, function(i, val) {
					error_html += val.message+'\n';
				});
				alert(error_html);
			}
		}
	};
	$('#cult_form').ajaxForm(cult_options);
	
	var sponsor_options = {
		type: 'post',
		dataType: 'json',
		resetForm: false,
		beforeSubmit: function() {
			$("#sponsor_submit").attr("disabled", "true"); 
			$("#sponsor_loading").show();
			$("#sponsor_message").html("").hide();
		},
		success: function(data) {
			$("#sponsor_submit").removeAttr("disabled"); 
			$("#sponsor_loading").hide();
			if(data.err == 0) {
				$("#sponsor_message").html(data.message).fadeIn();
			} else {
				var error_html = '';
				$.each(data.errors, function(i, val) {
					error_html += val.message+'\n';
				});
				alert(error_html);
			}
		}
	};
	$('#sponsor').ajaxForm(sponsor_options);
	
});

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