$(document).ready(function(){
	$(".sitemenu li span").click(function(){
		$(this).siblings().slideToggle();
	});
		
	if(initredeembox==true)
		tb_show("Redeem Security Token","php/redeem.php?height=50&width=350&modal=true",false);

		
	if(window.location.hash!=""){
		loadpage(window.location.hash.substr(1));
	}
	
	setupZoom();
});

function popupbrowseimages(){
	var popupwin = window.open("php/browseimages.php","popupwin","width=500,height=400,status=0,menubar=0,resizable=0") ;
	if (popupwin==null || typeof(popupwin)=="undefined"){
		//blocked
		alert("We tried to open the image cropper but couldn\'t due to a popup blocker. Please disable the popup-blocking software or \'whitelist\' this site if your software supports it");
	}
}

function validateauthcode(){
	$("#redeem_status").html("<img src='img/load-arrows.gif' alt='loading' /> Validating Code...");
	$.get("php/validate.php?authcode=" + document.getElementById("authcode").value,"",function(data){
		switch(data){
			case "invalid code":
				$("#redeem_status").html("<img src='img/exclamation.png' alt='error' /> That code is not valid");
				break
			case "code expired":
				$("#redeem_status").html("<img src='img/error.png' alt='warning' /> That code has expired. Please obtain a new code");
				break
			case "valid code":
				$("#redeem_status").html("<img src='img/accept.png' alt='OK' /> That code is valid");
				$("#redeem_main").html("Unlocking admin tools...<script type='text/javascript'>setTimeout(\"location.href='index.php'\",2500)</script>");
				break
			case  "code not yet valid":
				$("#redeem_status").html("<img src='img/error.png' alt='warning' /> That code is not yet valid");
				break
		}
	});
}


var swfu;
var swfu2;

function initswfupload() {
	// Max settings
	swfu = new SWFUpload({
		upload_script : "../php/imagemanagement.php?mode=fileupload",
		target : "SWFUploadContainer",
		flash_path : "js/SWFUpload.swf",
		allowed_filesize : 500,	// 500k
		allowed_filetypes : "*.jpg;*.jpeg;*.gif;*.png",
		allowed_filetypes_description : "Image Files",
		browse_link_innerhtml : "Add File",
		upload_link_innerhtml : "Upload Files",
		browse_link_class : "swfuploadbtn browsebtn",
		upload_link_class : "swfuploadbtn uploadbtn",
		flash_loaded_callback : 'swfu.flashLoaded',
		upload_file_queued_callback : "fileQueued",
		upload_file_start_callback : 'uploadFileStart',
		upload_progress_callback : 'uploadProgress',
		upload_file_complete_callback : 'uploadFileComplete',
		upload_file_cancel_callback : 'uploadFileCancelled',
		upload_queue_complete_callback : 'uploadQueueComplete',
		upload_error_callback : 'uploadError',
		upload_cancel_callback : 'uploadCancel',
		auto_upload : false			
	});
	$("#upload-init").fadeOut(function(){$("#uploadhide").fadeIn()});
}

function showcropper(imgsrc){
	var cropwin = window.open("php/cropimage.php?image=" + imgsrc,"cropwin","height=600,width=800,scrollbars=yes");
	if (cropwin==null || typeof(cropwin)=="undefined") alert("Please allow popups from this site.\n\nWe just tried to open the image cropper and found it was blocked.\n\nWe promise there won't be any popup ads :)");
}

function createtoken(isnew){
	var validfrom=$("#validfromdate").val();
	var validto=$("#validtodate").val();
	var accessrights=0;
	if(document.getElementById("access1").checked==true)accessrights+=1;
	if(document.getElementById("access2").checked==true)accessrights+=2;
	if(document.getElementById("access3").checked==true)accessrights+=4;
	if(document.getElementById("access4").checked==true)accessrights+=8;
	if(document.getElementById("access5").checked==true)accessrights+=16;
	if(document.getElementById("access6").checked==true)accessrights+=32;
	if(document.getElementById("access7").checked==true)accessrights+=64;
	var recipient=$("#recipient").val();
	var naccesscode="";
	var codeid="";
	if(isnew==false){
		naccesscode=$("#authcode").val();
		codeid=$("#codeid").val();
		isnew="false";
	}else{
		isnew="true";
	}
	var url="php/codemanagement.php?height=400&width=500&modal=true&mode=add2&validfrom=" + validfrom + "&validto=" + validto + "&accessrights=" + accessrights + "&recipient=" + recipient + "&amp;isnew=" + isnew + "&amp;naccesscode=" + naccesscode + "&amp;codeid=" + codeid;
	if(validfrom=="" || validto=="" || accessrights==0 || recipient==""){
		alert("Please fill in all fields before submitting this form.");
	}else{
		tb_show('Code Management',url,false)
	}
}

function loadpage(page){
	$("#structure-maincontent-padder").slideUp("slow",function(){
		$("#structure-maincontent-padder").html("<h1>Page Loading</h1><p>Please wait a few moments...");
		$("#structure-maincontent-padder").slideDown();
		$.get(page,function(data){
			$("#structure-maincontent-padder").slideUp("slow",function(){
				$("#structure-maincontent-padder").html(data);
				$("#structure-maincontent-padder").slideDown();
				window.location.hash=page;
			});
		});	
	});
}