﻿window.onload = function() {
    //all functions dependent on jquery
    //Initialize all the code that needs to run as the page loads

    //run the emptyText function...this clears out the word 'search' when the keyword field gains focus
    emptyText();
    //run the selectChanger function
    selectChanger();
    toggleFeeds();
}


//toggle the popup box when 'what is this' is hovered on
function toggleFeeds() {
    $('#whatIsThis').hover(function() {
       $('#footer #feedsInfo').show();
   },
   function () {
       $('#footer #feedsInfo').hide();
   });
}

//empty text out ofthe searchfield when it gains focus
function emptyText() {
 var searchForm = document.getElementById('searchForm');
    var input = searchForm.getElementsByTagName('input');
    for(var i=0; i< input.length; ++i) {
        if (input[i].getAttribute('type') == 'text') {
            input[i].onfocus = function(ind) {
                return function() {
                    input[ind].value = '';
                };
            }(i); 
        }
    }
}


//the following code deals with poulating the selects in the search....these arrays hold the data for the selects
var selectOptions1 = ['All Locations-', 'Scotland-1004','Northern Ireland-1005','North East-1006','North West-1007','Wales-1008','Midlands-1009','East-1010','Greater London-1011','South East-1012','South-1013','South West-1014'];
var selectOptions2 = ['All Locations-', 'Belfast-1028','Birmingham-1029','Bristol-1030','Glasgow-1031','Manchester-1032','Newport-1033','Reading-1034','Slough-1035','London-1036'];
var parentOptions1 = ['Face-2-Face Sales','Sales Management','New Media Sales'];
var parentOptions2 = ['All Locations-','Telesales','118 24 7','Customer Services','Marketing','Finance','Information Services','New Media/Digital','HR','Operations'];
	
//listens for a change in the first select and deals with it accordingly
function selectChanger() {
    var parentSelect = document.getElementById('parent');
	parentSelect.onchange = function() {
        var options = parentSelect.childNodes;
		for(var i=0;i<options.length;++i) {
            if(options[i].nodeType == '1') {
                if(options[i].selected == true) {
                    var optionsselected = options[i].innerHTML;
				    if(parentOptions1.inArray(optionsselected)) {
				        emptySelect();
						generateInnerSelect(selectOptions1);
					} else if(parentOptions2.inArray(optionsselected)) {
						emptySelect();
						generateInnerSelect(selectOptions2);
					}
				}
	        }
	    }
	}
}
	
//empties the second select out if it is populated
function emptySelect() {
    var childselect = document.getElementById('child');
	var selectChildren = childselect.childNodes;
	while(childselect.hasChildNodes()){
        childselect.removeChild(childselect.childNodes[0])
    }
}
	
//populates the second select with new options
function generateInnerSelect(optionlist) {
    var childselect = document.getElementById('child');
	for(var i=0; i < optionlist.length; ++i) {
		var opt = document.createElement('option');
		var parts = optionlist[i].split('-');
		opt.value = parts[1];
		opt.innerHTML = parts[0];
		childselect.appendChild(opt);
	}
}
	
Array.prototype.inArray = function (value){
    var i;
	for (i=0; i < this.length; i++) {
	    if (this[i] === value) {
		    return true;
		}
	}
	return false;
};
	
	
	//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);
				});			
		});	
};

DD_belatedPNG.fix('#footer #footerInner .top,#footer #footerInner #main,#footer #footerInner .bottom');

