$(document).ready(function() { 
	setToggles();
	setPopupRegions();
	addPopupEvents();
  addCloseEvents();
  addPanelSwitcher();
  addAdminPopup();
	addNationsDropdown();
	addStyleSwitcher();
	addRoundCorners();
	
	$('.msl-recentblogpostdate, p.msl_pubdate, .msl_event_time, .news_footer, dl.forum, .topic_lastpost_date, .forum_post_date, .blog_day, .msl_blog_post_date, .comment_date, .msl-resourceinfo, #resourcescontent p:contains("Uploaded by"), .rss_pubdate, .msl-recentforumpost-posterinfo, .msl_blog_lastpostdate, .msl_blog_lastcommentdate, .msl-recentblogcommentdate, .msl-caleventitem dt, #msl_event #section-content strong, dd.msl_event_time').each(function() {$(this).dateFormat();});	
	
});

function setToggles() {
      $("#contentarea a.toggler").click(function() {
  	$(this).parent().siblings("div.showhide-content").slideToggle("fast");
	return false;
      });
  }

function addPanelSwitcher() {
	$("#displaycal").click(function() {
		$("#eventcalpanel").show();
		$("#eventlistpanel").hide();
		return false;
	});
	$("#displaylist").click(function() {
		$("#eventcalpanel").hide();
		$("#eventlistpanel").show();
		return false;
	});
}

  function setPopupRegions() {
      $("#register").addClass('popupsource');
      $("#registerforupdates").addClass('popupwindow');
      $("#customise").addClass('popupsource');
      $("#colorselections").addClass('popupwindow');
	  $("#customise").show();
  }
	


  function addPopupEvents() {
      $("#wrapper a.popupsource").click(function() {	
		var popup = $(this).parent().siblings('div.popupwindow');
      	$(popup).addClass('showpop').prepend('<div class="blanket"></div>').find('div.optionstext').center().append('<input class="closepopup button" type="button" value="close"/>');
	  return false;
      }); 
	}
	
	jQuery.fn.center = function() {
	    var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
  	  var viewportWidth = $(window).width();
			var popHeight = $(this).outerHeight();
		  var popWidth = $(this).outerWidth();
			$(this).css("top", (viewportHeight - popHeight) / 2 + "px");
			$(this).css("left", (viewportWidth - popWidth) / 2 + "px");

			return this;
	}
		
	function addCloseEvents() {
					  	$('#wrapper div.optionstext').click(function(event) {  
          		var $tgt = $(event.target);
          		if ($tgt.is('.closepopup')) {
           		$(this).parents('div.popupwindow').removeClass('showpop').height('auto').width('auto');
							$('#wrapper div.blanket').remove();
							$tgt.remove();
          		 };
        });
	}
	
	function addEventIcons() {
	 $(".msl_event_types").contents().filter(function() {
          return this.nodeType == 3;
   }).remove();
	    $("#alleventslist dd a:contains('NUS'):first-child").addClass('nus');
		$("#alleventslist dd a:contains('NUS'):not(:first-child)").addClass('hidden');
		$("#alleventslist dd a:contains('amsu')").addClass('amsu');
		$("#alleventslist dd a:contains('nussl')").addClass('nussl');
		$("#alleventslist dd a:contains('featured')").addClass('hidden');
	}

	function addAdminPopup() {
		$('#paneluseradmin').hide();
		$('#panelcontrol').hide();
		$('#paneladmin').hide();
		$('#adminlinks a').click(function() {
			var myid = $(this).attr('id');	
			$('#panel' + myid).toggle(600);
			return false;
		});
	}

	function addStyleSwitcher(){
      var swatchcontainer = $("#sheetswitch");
      var switcher = $("link.switch[rel*='stylesheet']");
      var colors = new Array;
      var titles = new Array;
      var ids = new Array;

				switcher.each(function() {
          colors.push($(this).css("color"));
          titles.push($(this).attr('title'));
          ids.push($(this).attr('id'));
      });
      
      themeslist = "<ul class='themeslist'>"; // This will store our list items
      for (var i=0, l=colors.length; i<l; i++)
      {
            themeslist += "<li><a class='swatch' title='" + titles[i] + "' alt='" + titles[i]  + "' style='background-color:" + colors[i] + ";'></a><p class='colorname'>" + titles[i] + "</p><a href='#' class='preview'>preview</a></li>";
      }
      
      themeslist += "</ul>";
      swatchcontainer.html(themeslist);
      
      swatchcontainer.find("a.preview").click(function() {	
      	switcher.attr("disabled", "true");
      	var userselection = $('a.preview').index(this);
      	switcher.eq(userselection).attr('disabled', '');
      	return false;
      });
      $('#usersetoptions').show();
      var currentselection = $("div.sitecolour").text();
      if (currentselection && currentselection.length){
      var selectedsheet = $('#' + currentselection);};
      if (selectedsheet && selectedsheet.length){
      switcher.attr("disabled", "true");
      $('#' + currentselection).attr('disabled', '');};
	}
	
	function addNationsDropdown(){
					 $('a.itemnations').click(function() {
					 					$('#nationsSelector').slideToggle("fast");
					          return false;
					 });
	}
	
	function addRoundCorners(){
					 if ($('.roundtop').length) {$('.roundtop').corner('top 5px');}
	}
	
(function($){
	$.fn.dateFormat = function () {
		
		return this.each(function() {
		
			if ($(this).hasClass('forum')) {var $this = $(this).find('dd:eq(3)');}
			else {var $this = $(this);}
			var calendar = $(this).parent().hasClass('msl-caleventitem');
			var str = $this.text();
			
			str = str.replace(/mon /gi, "Monday ");
			str = str.replace(/tue /gi, "Tuesday ");
			str = str.replace(/wed /gi, "Wednesday ");
			str = str.replace(/thu /gi, "Thursday ");
			str = str.replace(/fri /gi, "Friday ");
			str = str.replace(/sat /gi, "Saturday ");
			str = str.replace(/sun /gi, "Sunday ");

			if ($(this).hasClass('.msl_event_time')) {
				str = str.replace(/st-|nd-|rd-|th-|-0/gi, "-");
				str = str.replace(/st |nd |rd |th /gi, " ");
				str = str.replace(/\b0(?![0A-Za-z])/gi, "");
				str = str.replace(/\bAugu\b/gi, "August");				
			}
			
			if (calendar) {
				str = str.replace(/ jan/gi, " January");
				str = str.replace(/ feb/gi, " February");
				str = str.replace(/ mar/gi, " March");
				str = str.replace(/ apr/gi, " April");
				str = str.replace(/ jun/gi, " June");
				str = str.replace(/ jul/gi, " July");
				str = str.replace(/ aug/gi, " August");
				str = str.replace(/ sep/gi, " September");
				str = str.replace(/ oct/gi, " October");
				str = str.replace(/ nov/gi, " November");
				str = str.replace(/ dec/gi, " December");
			}
			else {
				str = str.replace(/jan /gi, "January ");
				str = str.replace(/feb /gi, "February ");
				str = str.replace(/mar /gi, "March ");
				str = str.replace(/apr /gi, "April ");
				str = str.replace(/jun /gi, "June ");
				str = str.replace(/jul /gi, "July ");
				str = str.replace(/aug /gi, "August ");
				str = str.replace(/sep /gi, "September ");
				str = str.replace(/oct /gi, "October ");
				str = str.replace(/nov /gi, "November ");
				str = str.replace(/dec /gi, "December ");
			}

			$this.text(str);
			
		});
		
	};
})( jQuery );




