﻿// MAIN AJAX FUNCTION START

// this function makes an ajax call to the ajaxUrl passing the requestParameters in the form scope
// it then replaces the html element specified by destinationId with the html element specified by sourceId
// it will perform extra tasks before and after the ajax call, if these are not required simply pass in a blank function call like this:
// function() {}
function replacePaginationHtml(ajaxUrl, repeaterId, index, direction)
{
    // Check to see if the browser is MSIE v6 and then we want to break
//	if(jQuery.browser.msie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"])
//	    return true;
	    
	jQuery.get(
        ajaxUrl,
		{},
		function(data) {

		    var new_slider = jQuery("#"+repeaterId, data);
		    var cur_slider = jQuery("#"+repeaterId);
		    
		    // Header pagination
		    var header_id = "#"+repeaterId+"header";
		    var header = jQuery(header_id, cur_slider);
		    
		    if(header) {
				var html = jQuery(header_id, new_slider);
				header.replaceWith(html);
			}
			
			// Footer pagination
			var footer_id = "#"+repeaterId+"footer";
		    var footer = jQuery(footer_id, cur_slider);
		    
		    if(footer) {
				var html = jQuery(footer_id, new_slider);
				footer.replaceWith(html);
			}
			
		    var holder = jQuery(".ajax_pagination_slider", cur_slider)
		    var cur_slide = jQuery(".ajax_pagination_slide", cur_slider);
		    var new_slide = jQuery(".ajax_pagination_slide", new_slider);
		   
		    //new_slide.hide();
		    
		    var width = holder.width();
		    
		    if(direction > 0)
				width = -width;
		    
		    new_slide.css({
				position	: "absolute",
				top			: 0,
				left		: -width,
				width		: Math.abs(width)
		    });
		    

		    
		    holder.animate(
				{ left : width },
				1000, 
				null,
				function() {
					holder.css("left", 0);
					
					new_slide.css({
						position	: "static",
						top			: 0,
						left		: 0
					});
					
					cur_slide.remove();
				}
			);
		    
		    //cur_slide.remove();
		    holder.append(new_slide);
		    
		    var height = Math.max(cur_slide.height(), new_slide.height());
		    
		    holder.css("height", height);
		    
		    //new_slide.fadeIn();
		    
		    // Fix lightwindow on new slide 
		    myLightWindow._setupLinks(repeaterId);
		},
		"html"
    );		
}

function doAjaxGet(ajaxUrl, requestParameters)
{
	jQuery.get(ajaxUrl, requestParameters, function(data) {}, "html");
}

function replaceHtml(ajaxUrl, sourceId , destinationId, requestMethod, requestParameters, preProcessing, postProcessing)
{
    function replace(data)
    {
		if(!destinationId)
			destinationId = sourceId;
        
		preProcessing();
		
		// this fixes a bug in safari with jQuery
		if(jQuery.browser.safari)
		{
		    var frag = document.createElement("div");
		    frag.innerHTML = data;
		    data = frag;
		}
		
        jQuery("#" + destinationId).replaceWith(jQuery("#" + sourceId, data));
								
	    postProcessing();
    }
	
	if (requestMethod && requestMethod.toUpperCase() == "POST")
		jQuery.post(ajaxUrl, requestParameters, replace, "html");
	else
		jQuery.get(ajaxUrl, requestParameters, replace, "html");
		
}

// MAIN AJAX FUNCTION END




// WHATS ON CALENDAR FUNCTIONS START

function dateClick(popSpanId, calendarDate) 
{
	// fade out any that are already open    
    jQuery('.calPopup').hide();
    jQuery('.calPopUp').fadeOut(700);
    
    // fade in the selected date
    jQuery("#" + popSpanId).hide();
	jQuery("#" + popSpanId).fadeIn(700);
	
	return false;
}

function dateCloseClicked(popSpanId)
{
	// fade out any open calendar dates
    jQuery('.calPopUp').fadeOut(700);
    return false;
}

function nextPreviousClick(calendarDate)
{
    // Check to see if the browser is MSIE v6 or less, if so return forcing a new page call
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{ 
		//test for MSIE x.x;
		var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion <= 6)
			return true;
	}
	
	replaceHtml(document.location.href, "divCalendarHolder", "divCalendarHolder", null, {"calendarDate":calendarDate}, function() {}, function() {});
	return false;
}

// WHATS ON CALENDAR FUNCTIONS END


// CUSTOMISE FUNCTIONS START
function customiseHomepage_click()
{
	jQuery(".customiseHomepage").click(
		function()
		{
			jQuery(".divHomepageCustomise").show("slide", { direction: "up" }, 1000);
			return false;
		});
}

function customiseHomepageClose_click()
{
	jQuery(".divHomepageCustomiseClose .closeMe").click(
		function()
		{
			jQuery(".divHomepageCustomise").hide("slide", { direction: "up" }, 700, function() {});
			return false;
		});
}

function customiseMyWNO_click()
{
	jQuery(".customiseMyWNO").click(
		function()
		{
			jQuery(".divMyWNOCustomise").show("slide", { direction: "up" }, 1000);
			return false;
		});
}

function customiseMyWNOClose_click()
{
	jQuery(".divMyWNOCustomiseClose .closeMe").click(
		function()
		{
			jQuery(".divMyWNOCustomise").hide("slide", { direction: "up" }, 700, function() {});
			return false;
		});
}



// CUSTOMISE FUNCTIONS END


// LOGIN FUNCTIONS START
function login_click()
{
	// this function handles the login button click
	// do an ajax call to get the html for the login bar
	// post processing is to show the bar with a nice effect and reassociate button events
	jQuery(".loginbtn").click(
		function()
		{
			jQuery(".loginBar").show("slide", { direction: "up" }, 1000);
			return false;
		});
}

function loginClose_click()
{
	// this function handles the login close button click
	// simply hide the login bar, no need to do an ajax call
	jQuery(".close").click(
		function()
		{
			jQuery(".loginBar").hide("slide", { direction: "up" }, 1000);
			return false;
		});
}


function loginSubmit_click()
{
	// this function handles the login submit button click
	// do an ajax request to submit the login details then reassociate all button click events (as you dont know whats coming back)
	jQuery(".loginbar_submit").click(
		function()
		{
			// Check to see if the browser is MSIE v6 or less, if so return forcing a new page call
			if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
			{ 
				//test for MSIE x.x;
				var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
				if (ieversion <= 6)
					return true;
			}
				
			// the variables usernameId, passwordId, chkRememberId and ajaxLoginSuccess must be set in the calling page before the ajax call is made
			replaceHtml(
						document.location.href,
						"divReplacement",
						"divReplacement",
						"POST",
						{btnLoginSubmit_ajax:"Submit",username:jQuery(usernameId).val().toString(),password:jQuery(passwordId).val().toString(),rememberMe:(jQuery(chkRememberId + ':checked').val() != null).toString()},
						function() {},
						function() {
										if (jQuery(ajaxLoginSuccess).html() != null)
										{
											jQuery(".loginBar").hide("slide", {direction: "up"}, 700);
											jQuery(".welcome").show("slide", {direction: "up"}, 700).animate({opacity: 1.0}, 3000).hide("slide",{ direction: "up" }, 700, function() { $(this).remove(); });
										}
										associateButtonClicks();
									}
						);
		    return false;
		});
}

function logout_click()
{

	// this function handles the logout button click
	// do an ajax request to logout the user then reassociate button click events
	jQuery(".logoutbtn").click(
		function()
		{
			replaceHtml(
						document.location.href,
						"divReplacement",
						"divReplacement",
						null,
						{btnLogout_ajax:"Submit"},
						function() {},
						function() { associateButtonClicks(); }
						);
			return false;
		});
}

// LOGIN FUNCTIONS END


function associateButtonClicks()
{
    // Check to see if the browser is MSIE v6 and then we want to break
//	if(jQuery.browser.msie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) == 6 && !window["XMLHttpRequest"])
//	    return true;

	login_click();
	loginClose_click();
	loginSubmit_click();
	logout_click();
	customiseHomepage_click();
	customiseHomepageClose_click();
	customiseMyWNO_click();
	customiseMyWNOClose_click();
}