﻿$(document).ready(function() {

    /***************************
     Add autocomplete to form to 
     prevent Google autosuggestions
    ****************************/
    $('.mainForm').attr('autocomplete', 'off'); 

    /****************************
     Setup the fancybox popups.
    ****************************/
    $("a#inline").fancybox({ 'hideOnContentClick': false }); 

    $("a.zoom").fancybox({
        'hideOnContentClick': false,
		'overlayOpacity'	:	0.65,
		'overlayColor'		:	'#452276'
	});
	
	$("a#forwardLink").fancybox({ 'hideOnContentClick': false, 'frameWidth' : 500, 'frameHeight' : 468, 'overlayColor' : '#452276'}); 
	
	$("a.zoomVideo").each(function(i,val) {
	
		// Work out the video dimensions
		var classes = $(this).attr("class").split(/\s+/);
		
		var width = 780;
		var height = 350;
		
		if (classes.length > 1) {
			width = parseInt(classes[1]);
			height = parseInt(classes[2]);
		}
		
		$(this).fancybox({
	    'hideOnContentClick': false,
        'overlayOpacity'	:	0.65,
        'overlayColor'		:	'#452276',
        'frameWidth'        :   width,
        'frameHeight'       :   height
		});
    });
	
	$("img.galleryImg").click(function(event) {
		$(this).parent().find("a.zoom").click();
	});

    // Set equal heights on dependent columns
    $('div.linkContainer').setEqualHeight();
	$('div.gateWrapper').each(function(i,val) {
		$(this).find('div.gatePanel:not(.wide)').setEqualHeight();
	});
    $('div.repeatItem').each(function(i,val) {
        $(this).find('div.itemImageCont, div.itemSummary, div.itemDetailsList').setEqualHeight();
    });

    // Setup the drop down information areas (IE / FF) versions
	if($.browser.msie){
		$('div.relatedInformation div.section a:first').toggle(
			function () {
				$(this).parent().addClass("active");
				return false;
			},
			function () {
				$(this).parent().removeClass("active");
				return false;   
			}
		);
	}else{
		$('div.relatedInformation div.section a:first').click(function(event){
			$(this).parent().toggleClass('active',500);
			event.preventDefault();
		});
	}
    
    //Expand Panel
	$("#searchLink").click(function(){
	    $(this).parent().addClass('searchActive');
		$("div#quickSearch").slideDown("fast", function() {
		    $("div#quickSearch input:first").focus().select();
		});	
	});	
 
	// Collapse Panel
	$("img#closeSearch").click(function(){	
		$("div#quickSearch").slideUp("fast");
		$("#searchLink").parent().removeClass('searchActive');
	});
	
	/****************************
     Setup the FAQ logic functions
    ****************************/
    
	$('div.faq a.switchControl').click(function(event) {
	    $(this).parent().toggleClass('expanded', 500);
		event.preventDefault();
	});
	$('div.faq div.faqContent h4').click(function(event) {
	    $(this).parent().parent().toggleClass('expanded', 500);
		event.preventDefault();
	});
	$('#faqFilterButton').click(function(event) {
    	event.preventDefault();
    	var curSize = 0;
	    $('div.faq').each(function(i, val) {
	        if(
	           $('#faqFilterOptions').attr("selectedIndex") == 0
	           || 
	           $(this).attr('title').indexOf($('#faqFilterOptions').val()) > -1
	          )
            {
	            $(this).slideDown();
	            curSize++;
	        }else{
	            $(this).slideUp();
	        }
        });
        resetFAQPager(curSize);
	});	
	
	var curPage = 1;
	function resetFAQPager(curSize) {
	    
	    // Hide whilst developing
	    $('#faqPager').hide();
	    
	    var pageSize = 1;
	    var pageCount = curSize / pageSize;
	    var startIndex = (curPage-1) * pageSize;
	    
	    var pageList = $('#faqPager ul');
	    pageList.find('li').remove();
	    
	    for(var pageIndex=1; pageIndex<=pageCount; pageIndex++ ){
	        var pageItem = $('<li><a>'+pageIndex+'</a></li>');
	        if ((pageIndex+1) == curPage) {
	            pageItem.find('a').addClass('active');
	        }
	        pageItem.appendTo('#faqPager ul');
	    }
	}
	
	/****************************
     Setup the Listing filter functions
    ****************************/
    $('div.filterList ul li label').each(function(i,val) {
        $(this).parent().find('img').click(function() {
            if ($(this).attr('src').indexOf("checkInactive") == -1){
                $(this).attr('src',"images/icons/checkInactive.gif");
                $(this).parent().find('input').click();
            }else{
                $(this).attr('src',"/images/icons/checkActive.gif");
                $(this).parent().find('input').click();
            }
        });
    });
    $('div.filterList ul li input:checked').each(function(i,val) {
       $(this).parent().find('img').attr('src',"/images/icons/checkActive.gif").attr('alt','checked');
    });
    
    
    /****************************
     Setup the role div clicks to mimic link
     click
    ****************************/
    $('div.roleBox').click(function() {
        $(this).find('a').click();
    });
    
});

/********************************************************
 jQuery extension function to set jQuery selected objects to 
 same height
********************************************************/
jQuery.fn.setEqualHeight=function(o) {
        var maxHeight=0;
        var maxElement=null;
        jQuery(this).each(function(i) {
        if((jQuery(this).height()+parseInt(jQuery(this).css("padding-bottom"))+parseInt(jQuery(this).css("padding-top")))>maxHeight) {
        maxHeight=jQuery(this).height()+parseInt(jQuery(this).css("padding-top"))+parseInt(jQuery(this).css("padding-bottom"));
        maxElement=this;
    }
});
jQuery(this).not($(maxElement)).each(function() {$(this).height(maxHeight-parseInt(jQuery(this).css("padding-top"))-parseInt(jQuery(this).css("padding-bottom")))})
}

/********************************************************
 jQuery based function to hide / show all list items
 based on whether they meet the filter conditions.
********************************************************/
function refreshList() {
    var filterText = "";
    $('div.filterList ul li input:checked').each(function (i, val) {
        filterText+= $(this).parent().find('label').text()+",";
    });
    var filters = filterText.split(',');
    
    $('div.listing').each(function(i,val) {
        
        var blDisplay = (filters.length == 1) ? true : false;
        var filterIndex =0;
        var strTitle = $(this).attr('title');
        
        for (filterIndex=0;filterIndex<filters.length;filterIndex++) {
            if (filters[filterIndex].length > 0 && strTitle.indexOf(filters[filterIndex]) > -1) {
                blDisplay = true;
            }
        }
        if (blDisplay) {
            $(this).slideDown();
        }else{
            $(this).slideUp();
        }
    });
}

/********************************************************
 jQuery based function to auto suggest links based on
 search results
********************************************************/
function quickSearch(inputString){
	if(inputString.length < 4) {
		$('#suggestions').slideUp();
	} else {
	    $('#searchInput').addClass('load');
        $.ajax({  
            type: "POST",  
            url: "/webservices/SiteSearch.asmx/Search",  
            contentType: "application/json; charset=utf-8",  
            data: "{HomeNodeId:'1068', Terms:'"+inputString+"', ResultsLimit:'5'}",  
            dataType: "json",  
            success: function(response) {
                $('#searchInput').removeClass('load');
                if ($('#searchInput').val().length > 3 && response.d.length > 0) {
			        $('#suggestionsList').html(response.d);
			        $('#suggestions').slideDown();
			    }else{
			        $('#suggestions').slideUp();
			    }
            },  
            error: function(msg) {
                $('#searchInput').removeClass('load');
                //alert('fail: '+msg.status + ', ' + msg.statusText +', ' + msg.responseText);   
            }
        });    
	}
}
    
function clearQuickSearch() {
	$('#suggestions').slideUp();
}


