﻿//Custom Accordion menu
//if the persistent option is set to true then the last visited tab is re-opened when the user goes back in their browser history
//if openByDefault is set then if a tab has not been previously selected then the first tab is opened

function accordion(speed, persistent, openByDefault) {
    $('.accordioncontent').hide();
    var y = $('ul#accordion li a');
    y.click(
        function() {
            var current = $(this).next();
            if ((current.is('div')) && (!current.is(':visible'))) {
                y.removeClass('selected');
                $(this).addClass('selected');
                $('.accordioncontent:visible').slideUp(speed);
                current.slideDown(speed);
            } else if ((current.is('div')) && (!current.is(':hidden'))) {
                $(this).removeClass('selected');
                current.slideUp(speed);
            }
        }
    );
    if (persistent) {
        if (location.href.indexOf('.aspx#') !== -1) {
            var str = location.href.split('.aspx#');
            var i = $('#content' + str[1]).slideDown('normal');
            i.siblings('a').addClass('selected');
        }
    }
    if (openByDefault) {
        if (location.href.indexOf('.aspx#') === -1) {
            var i = $('#content1').slideDown('normal');
            i.siblings('a').addClass('selected');
        }   
    }
}

//Jquery function to deal with the scroller - courtesy of 
	//gcmingati.net
	//This function is initialised at the bottom of the template
	//http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html
	
	jQuery.fn.liScroll = function(settings) {
	     
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
		      
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).width();
				});
				$strip.width(stripWidth);			
				var defTiming = stripWidth/settings.travelocity;
				var totalTravel = stripWidth+containerWidth;								
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};


//Locations map
var locationsMap = {
    //'Global' vars
    currentlySelected : 'all',
    data : '',
    
    //Setup funcion
    init : function() {
        //Show the map and hide the on js content
        $('#mapArea, #leftCol ul').show();
        $('#mapAreaAlt').hide();
        //Hijack the leftCol Navlinks and stop them from performing the default action
        $("#leftCol ul li ul li a").bind("click",function() {
            return false;
        });
        
        locationsMap.handleOverlay();
        
        //create an ajax request and grab the json
        locationsMap.createRequest();
   	    request.open("GET", '../Javascript/mapData.js', true);
        request.onreadystatechange = locationsMap.handleMapData;
        request.send(null);   
    },
    
    //A basic AJAX request function
    createRequest : function() {
        try {
            request = new XMLHttpRequest();
        } 
        catch (trymicrosoft) {
            try {
                request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (othermicrosoft) {
                  try {
                     request = new ActiveXObject("Microsoft.XMLHTTP");
                  } 
                     catch (failed) {
                     request = null;
                  }
            }
        }
    },
    
    //plot the pin points on the map according to which business area has been clicked on
    handleMapData : function() {
        if(request.readyState == 4) {
            locationsMap.data = eval("(" + request.responseText + ")");
            var datalength = locationsMap.data.mapData.length;
            $("#leftCol ul li ul li a").each(function(i) {
                $(this).addClass('businessArea'+i);   
            });
            $("#leftCol ul li ul li a").bind("click",function() {
                $('#mapOverlayContainer').hide();
                $("#leftCol ul li ul li a").removeClass('selected');
                $(this).addClass('selected');
                locationsMap.currentlySelected = this;
                $('.pin').remove();
                for(var i=0; i < datalength; ++i) {
                    if(locationsMap.data.mapData[i].Area == locationsMap.currentlySelected.innerHTML) {
                        var locationsLength = locationsMap.data.mapData[i].locations.length;
                        var map = document.getElementById('mapArea')
                        for(var r=0; r < locationsLength; ++r) {
                            var pin = document.createElement('div');
                            pin.className = 'pin';
                            pin.id = locationsMap.data.mapData[i].locations[r].title;
                            var pinTitle = document.createElement('a');
                            pinTitle.href="#";
                            pinTitle.id = locationsMap.data.mapData[i].locations[r].title;
                            var texty = document.createTextNode(locationsMap.data.mapData[i].locations[r].title);
                            pinTitle.appendChild(texty);
                            pin.appendChild(pinTitle);
                            map.appendChild(pin);
                        }
                    }
                }
                locationsMap.handleOverlay();
            });
        }
    },
    
    //handle clicks made on the pins and show the overlay accordingly
    handleOverlay : function() {
        $('#mapArea a').click(function() {
        //alert(locationsMap.currentlySelected);
             $('#mapOverlay').empty();
             var id = this.id;
             var currentBusinessArea;
             if(locationsMap.currentlySelected == 'all') {
                 currentBusinessArea = 'all';
             } else {
                 currentBusinessArea = locationsMap.currentlySelected.className.replace(' selected', '');
             }
             for(var i=0;i<locationsMap.data.OfficeInfo.length; ++i) {
                 if(locationsMap.data.OfficeInfo[i].businessArea == currentBusinessArea) {
                     for(var t = 0;t < locationsMap.data.OfficeInfo[i].locations.length;++t) {
                         if(locationsMap.data.OfficeInfo[i].locations[t].locationIdent == id) {
                             var container = document.createElement('div');
                             container.className = 'office';
                             var h3 = document.createElement('h3');
                             var h3Text = document.createTextNode(id + ' offices');
                             h3.appendChild(h3Text);
                             container.appendChild(h3);
                             for(var p =0; p<locationsMap.data.OfficeInfo[i].locations[t].office.length; ++p) {
                                 var h4 = document.createElement('h4');
                                 var h4Text = document.createTextNode(locationsMap.data.OfficeInfo[i].locations[t].office[p].heading);
                                 h4.appendChild(h4Text);
                                 container.appendChild(h4);
                                 var address = document.createElement('p');
                                 address.className = 'addressText';
                                 var addressText = document.createTextNode(locationsMap.data.OfficeInfo[i].locations[t].office[p].address);
                                 address.appendChild(addressText);
                                 container.appendChild(address);
                                 var blurb = document.createElement('p');
                                 blurb.className = 'blurb';
                                 var blurbText = document.createTextNode(locationsMap.data.OfficeInfo[i].locations[t].office[p].text);
                                 blurb.appendChild(blurbText);
                                 container.appendChild(blurb);
                                 var mapOverlay = document.getElementById('mapOverlay');
                                 mapOverlay.appendChild(container);
                                 locationsMap.formatText('addressText',',',',<br />');
                                 locationsMap.formatText('blurb','\n', '<br /><br />');
                             }
                             $('#mapOverlayContainer').show();
                             $('a#close').click(function() {
                                 $('#mapOverlayContainer').hide();
                             });
                         }
                     }
                 }    
             }
        });
    },
    
    //format the text - add line breaks on the comma
    formatText : function(classIdent,seperator,breakFormat) {
        var pAddress = document.getElementsByTagName('p');
        for(var i=0; i < pAddress.length; ++i) {
            if(pAddress[i].className == classIdent) {
                if(pAddress[i].className.indexOf('formatted') == -1) {
                    var innerText = pAddress[i].innerHTML;
                    var splitInnerText = innerText.split(seperator);
                    var st = '';
                    for(var q=0;q<splitInnerText.length;q++) {
                        if(q == (splitInnerText.length -1)) {
                            st += splitInnerText[q] + '<br />';
                        } else {
                            st += splitInnerText[q] + breakFormat;
                        }
                    }
                }
                pAddress[i].innerHTML = st;
                pAddress[i].className += 'formatted'; 
            }
        }
    } 
}

         //code to run on page load
         $(function(){
             //hot jobs ticker
             $('#hotJobs').show();
             var tickerList = $("ul#ticker"); 
             var children = tickerList.children();
             if(children.length === 0) {
                 var latestJobs = $('#hotJobs p');
                 var defaultList = $('<span>There are new opportunities coming soon</span>'); 
                 defaultList.insertAfter(latestJobs);
             } else {
                 $('#hotJobs ul#ticker').css("width", "6000px");
                 $("ul#ticker").liScroll({travelocity: 0.04}); 
             }
            //accessibility font sizing - some fudging here to stop the increased size breaking layout
            var accessibility = $('#accessibility a').click(function() {
                if(this.className == 'smaller') {
                   $('body').css({'font-size': '75%'});
                   $('#hotJobs .tickercontainer').css({'width': '310px'});
                    $('#navigation ul li a').css({'padding-left': '11px','padding-right': '11px'});
                    $('#navigation ul li.About a').css({'padding-left': '16px'});
                    $('#navigation ul li.Search a').css({'padding-right': '16px'});
                    $('#navigation ul li.Search').css({'padding-right' : '0px'}); 
                   
                }
                else if(this.className == 'larger') {
                   $('body').css({'font-size': '85%'});
                   $('#hotJobs .tickercontainer').css({'width': '290px'});
                   $('#navigation ul li a').css({'padding-left': '4px','padding-right': '3px'});
                   $('#navigation ul li.Search').css({'padding-right' : '5px'}); 
                }
            }); 
         }); 
         
         
     // clear the quick search field when it's clicked on   
    function clickclear(thisfield, defaulttext) {
      if (thisfield.value == defaulttext) {
          thisfield.value = "";
      }
    }
 
     
    

   		
   	
   	

