$(document).ready(function(){

	// modal 
	$(".modaledit").click(
		function (){
			$("#modalshell").empty();
			$("body").prepend("<div id=\"modalshell\" class=\"jqmWindow\">loading&hellip;</div>");
			$("#modalshell").jqm({ajax: $(this).attr("href"), onLoad: initAllBindings});
			$("#modalshell").jqmShow();
			return false;
		}
	);
	
	// open and close toggle content
	$(".togglebtn").click(function(){
		var myid = $(this).attr('id');
		var nextSibID = "#"+$(this).next().attr('id');
		$(nextSibID).slideToggle("normal", function(){
			toggleBlockIcon(myid);
		});
		return false;
	});
	
	// open/close main nav photo search block
	$("#tb-photosearch").click(function(){
		$("#tc-photosearch").slideToggle("normal");
		return false;
	});
	
	// open/close main nav login block
	$("#tb-login").click(function(){
		$("#tc-login").slideToggle("normal");
		return false;
	});
	
	// open and close news list months
	$(".news-list-month").click(function(){
		var nextSibID = $(this).next().attr('id');
		var iconID = $(this).children('small').attr('id');
		$("#"+nextSibID).slideToggle("normal", function(){
			toggleNewsIcon(iconID,nextSibID);
		});
		return false;
	});
	
	initAllBindings();
	
});

function initAllBindings()
{
	$('#modalshell').jqmAddClose('.modalclose');
	
		// submit inside modal
	$("form#navlogin").submit(function() {
		
		var form = $(this);
		if (form.hasClass("modalform")){
			// the form is modal - submit via ajax
			$.ajax({
				type: "POST",
				url: form.attr("action"),
				data: form.serialize(),
				cache: false,
				success: function(html){
					$("#modalshell").html(html);
					initAllBindings();
					$('#modalshell').jqmAddClose('.modalclose');
				}
			});
		} else {
			// the form is not modal - submit normally
			form.submit();
		}
		return false;
	});
}

// events calendar popup links
function calendarInfo(date){
	var object = "#daybtn-"+date;
	var offset = $(object).offset();
	$(".date-info").remove();
	$("body").prepend("<div class=\"date-info\" style=\"display:none;left:"+(offset.left+4)+"px;top:"+(offset.top+4)+"px;\"><span style=\"color:#4c4c4c;font-size:10px;\">loading events for "+date+"...</span></div>");
	$(".date-info").show("normal");
	$.ajax({
		url: "/includes/ajax_calendarinfo.php?date="+date,
		cache: true,
		success: function(html){
			$(".date-info").html(html);
		}
	});
};

// toggle collapsable content block icons (+/-)
function toggleBlockIcon(button){
	if (document.getElementById(button).innerHTML == "+"){
		document.getElementById(button).innerHTML = "-";
	} else {
		document.getElementById(button).innerHTML = "+";
	}
}

// toggle news list arrows
function toggleNewsIcon(icon,container){
	if (document.getElementById(container).style.display == "none"){
		document.getElementById(icon).innerHTML = "&#9658;";
	} else {
		document.getElementById(icon).innerHTML = "&#9660;";
	}
}

// toggle login form pages
function newJoinForm(){
	$("#tc-login-existing").slideToggle("normal");
	$("#tc-login-new").slideToggle("normal");
	return false;
}

// change month view of a table list
function updatemonth(selectId,pre){
	var Sel_Ind = document.getElementById(selectId).selectedIndex;
	var issue = document.getElementById(selectId).options[Sel_Ind].value;
	if (issue != "null"){
		document.location.href = pre+"/"+issue;
	}
}

function deleteUserComment(cid,type){
	if (confirm("Are you sure you want to delete your comment?")){
		document.forms[type+'commentdelete'].comment_id.value = cid;
		document.forms[type+'commentdelete'].submit();
	}
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 10;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}
