$(document).ready(function()
{	
	jQuery(function(){
		jQuery('ul.sf-menu').superfish();
	});
	
	
	var action = getUrlVars()['action'];
	
	switch(action)
	{
	    case 'modifyEvents':
    	    loadEvents();
    	    return;
    	    
	    case 'modifyPrograms':
	    	loadPrograms();
	    	return;
	    	
	    case 'modifyNews':
	    	loadNews();
	    	return;
	    	
	    case 'newsArchive':
	    	changeNewsYear();
	    	return;
	    	
	    case 'contact':
	    	loadMap();
	    	return; 
	    	
	    case 'imageManager':
	    	loadImagePage(0);
	    	return;
	    	
	    case 'linkManager':
	    	loadLinks();
	    	return;
	}
	
	loadHomeWidgets();
    
});

function loadHomeWidgets()
{	
	if ($(".slideshow").length) 
	{
		 $('.slideshow').cycle({
				fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			});
	}
}

function loadMap()
{
	try
	{
	    var map = new google.maps.Map2(document.getElementById("map"));
	    map.setCenter(new google.maps.LatLng(-37.784719, 144.96564), 15);
		map.addControl(new GLargeMapControl());
	    var point = new GLatLng(-37.784719, 144.96564);
	    map.addOverlay(new GMarker(point)); 
		
	}
	catch(err)
	{// Not on the contact page so we need to catch this.
	}
}

//function showNofification(msg)
//{
//	$.jGrowl(msg);
//}


function changeNewsYear()
{	
	var year;
	
	$("#newsListContent").html('<img src="web/images/loader.gif" />');
	
	$("#year option:selected").each(function () {
	    year = $(this).val();
	});
	
	
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=changeNewsYear&year='+year,
        success: function(data) {
            $("#newsListContent").html(data);
        }
    });
}

function loadLinks()
{
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=listLinks',
        success: function(data) {
            $("#data").html(data);
            $("#loader").html('');
        }
    }); 
}

function loadEvents()
{
    var year;
	
    $("#loader").html('<img src="web/images/loader.gif" />');
	
	$("#year option:selected").each(function () {
	    year = $(this).val();
	});
	
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=listEvents&year='+year,
        success: function(data) {
            $("#data").html(data);
            $("#loader").html('');
        }
    }); 
}

function loadPrograms()
{  
	var id;
	
    $("#loader").html('<img src="web/images/loader.gif" />');
	
	$("#category option:selected").each(function () {
	    id = $(this).val();
	});
	
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=listPrograms&id='+id,
        success: function(data) {
            $("#data").html(data);
            $("#loader").html('');
        }
    });
    
  
}

function loadNews()
{
    var year;
	
    $("#loader").html('<img src="web/images/loader.gif" />');
	
	$("#year option:selected").each(function () {
	    year = $(this).val();
	});
	
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=listNews&year='+year,
        success: function(data) {
            $("#data").html(data);
            $("#loader").html('');
        }
    }); 
}

function loadCategories()
{  
	
    $("#loader").html('<img src="web/images/loader.gif" />');
	
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=listCategories',
        success: function(data) 
        {
		    alert(data);
        }
    }); 
}

function changeFull(id)
{
	$("#loader").html('<img src="web/images/loader.gif" />');
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=setFull&id='+id,
        success: function(data) 
        {
		    $("#loader").html('');
        }
    });
	
//	$.jGrowl("Full status updated.");
}

function changeEnrol(id)
{
	$("#loader").html('<img src="web/images/loader.gif" />');
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=setEnrol&id='+id,
        success: function(data) 
        {
		    $("#loader").html('');
        }
    });
	
//	$.jGrowl("Full status updated.");
}

function changeSlideShow(id)
{
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=changeSlideShow&id='+id,
        success: function(data) 
        {
//		$("#loader").html('');
        }
    }); 
}

function changeDisplay(id)
{
	$("#loader").html('<img src="web/images/loader.gif" />');
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=setDisplay&id='+id,
        success: function(data) 
        {
		$("#loader").html('');
        }
    }); 
	
//	$.jGrowl("Display status updated.");
}

function confirmRemove(name, id)
{
	
   var r = confirm("***NOTE!: Are you sure you want to remove: "+name);
   if(r == true)
   {
	   $("#loader").html('<img src="web/images/loader.gif" />');
		$.ajax({
	        url: 'web/ajax.php',
	        data: 'action=removeProgram&id='+id,
	        success: function(data) 
	        {
			$("#loader").html('');
			$("#data").html('');
			loadPrograms();
	        }
	    });

//		$.jGrowl(name+" removed.");
  
   }

	 
}

function confirmRemoveCategory(name, id)
{
   var r = confirm("***NOTE!: Are you sure you want to remove the category:\n "+name+"\n \n\n****All programs will be removed also.");
   if(r == true)
   {
	   $("#loader").html('<img src="web/images/loader.gif" />');
		$.ajax({
	        url: 'web/ajax.php',
	        data: 'action=removeCategory&id='+id,
	        success: function(data) 
	        {
			    $("#loader").html('');
			    $("#listTable").html(data);
	        }
	    });
//		$.jGrowl(name+" removed.");
   }    
}

function confirmRemoveNews(name, id)
{
   var r = confirm("***NOTE!: Are you sure you want to remove:\n "+name+"\n");
   if(r == true)
   {
	   $("#loader").html('<img src="web/images/loader.gif" />');
		$.ajax({
	        url: 'web/ajax.php',
	        data: 'action=removeNews&id='+id,
	        success: function(data) 
	        {
			    $("#loader").html('');
			    $("#data").html('');
			    loadNews();
	        }
	    });
//		$.jGrowl(name+" removed.");
		
   }    
}

function confirmRemoveEvent(name, id)
{
   var r = confirm("***NOTE!: Are you sure you want to remove:\n "+name+"\n");
   if(r == true)
   {
	   $("#loader").html('<img src="web/images/loader.gif" />');
		$.ajax({
	        url: 'web/ajax.php',
	        data: 'action=removeEvent&id='+id,
	        success: function(data) 
	        {
			    $("#loader").html('');
			    $("#data").html('');
			    loadEvents();
	        }
	    });
//		$.jGrowl(name+" removed.");	
   }    
}

function confirmRemoveLink(id, name)
{
   var r = confirm("***NOTE!: Are you sure you want to remove:\n "+name+"\n");
   if(r == true)
   {
	   $("#loader").html('<img src="web/images/loader.gif" />');
		$.ajax({
	        url: 'web/ajax.php',
	        data: 'action=removeLink&id='+id,
	        success: function(data) 
	        {
			    $("#loader").html('');
			    //$("#data").html('');
			    loadLinks();
			    
	        }
	    });
//		$.jGrowl(name+" removed.");	
   }    
}

function limitText(limitField, limitNum) 
{
    if (limitField.value.length > limitNum) 
    {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function clearNotice()
{
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=clearNotice',
        success: function(data) 
        {
		    $("#noticeBoard").html('');
        }
    });
	$("#noticeBoard").html('');
}

function updateNotice()
{	
	$.post('web/ajax.php', { action: "updateNotice", notice: $("#noticeBoard").val() } );
}

function addImage(id, name, type)
{
	var html = '<div id=\"I'+id+type+'\">';//
	html += '<img src=\"web/images/userimages/thumbs/'+name+'\"/> <a class=\"softLink\" onclick=\"removeAttachment(\'I'+id+type+'\')\">Remove</a>';
	html += '</div>';
	document.getElementById('attachments').innerHTML += html;	
	
	var htmlTwo = '<input id=\"dataI'+id+type+'\" type=\"hidden\" name=\"image'+id+'\" value=\"'+id+'\" />';
	document.getElementById('itemForm').innerHTML += htmlTwo;
}

function removeAttachment(id)
{	
	try
	{
		$('#data'+id).remove();
		$('#'+id).remove();
	}
	catch(err)
	{}
	
	var htmlTwo = '<input id=\"data'+id+'\" type=\"hidden\" name=\"removeAttachment'+id+'\" value=\"'+id+'\" />';
	document.getElementById('itemForm').innerHTML += htmlTwo;
}


function loadImagePage(page)
{  
    $("#loader").html('<img src="web/images/loader.gif" />');
	
	$.ajax({
        url: 'web/ajax.php',
        data: 'action=loadImagePage&page='+page,
        success: function(data) {
            $("#data").html(data);
            $("#loader").html('');
        }
    });
}

function deleteImage(id, name, page)
{
	var r = confirm("***NOTE!: Are you sure you want to remove this image");

	 if(r == true)
	 {
	     $.ajax({
         url: 'web/ajax.php',
         data: 'action=deleteImage&id='+id+'&name='+name,
         success: function(data) {
	    	 loadImagePage(page);
         }
         });
	}
}

function setOther()
{
	var selected = $("#hear option:selected").text();
	if(selected.localeCompare('Other') == 0)
	{
		document.getElementById('other').innerHTML += '<br/><span class="tiny">Please specify</span><br/><input type="text" name="other" class="textBox" />';
	}
	else
	{
		document.getElementById('other').innerHTML = '';
	}
}




