// JavaScript Document

var newsitems;
var curritem=0;
var iPause=0;

$(document).ready(function(){
	
	// auto fill in text boxes
	$('.newsletterInput').val('Email address...');
	$('.searchInput').val('Search...');
	
	$('.newsletterInput, .searchInput').focus(function(){
		if( $(this).attr('value') == 'Email address...' || $(this).attr('value') == 'Search...' ){
			$(this).val('');
		}
	});
	$('.newsletterInput').blur(function(){
		if( $(this).attr('value') == ''){
			$(this).val('Email address...');
		}
	});
	$('.searchInput').blur(function(){
		if( $(this).attr('value') == ''){
			$(this).val('Search...');
		}
	});

	// ** begin colourCode **
	// The code below applies to experience profile listing where there is the colour code
	// of services the experience (client) belongs to.
	
	// 1. remove fixed height setting on the .colourCode DIV
	$('.colourCode').css({'height':'auto'}); 
	
	// 2. For each list item, the switch loop gets the data inside and assigns it to a
	// corresponding index slot in two newly created Arrays. This determines the spot 
	// it'll appear. 'positionArray' stores image ID while 'altArray' stores the 
	// image alt and title information. 
	$('.profile').each(function(){
		var positionArray = new Array(7);
		var altArray = new Array(7);
		$('li', this).each(function(){
			var serviceType = $(this).text().replace(/^\s+|\s+$/g, ''); // uses regexp to trim - otherwise IE freaks out
			switch(serviceType){
				case ('Integrated Services'):
					positionArray[0] = 'integrated';
					altArray[0] = serviceType;
					break;
				case('Managed Services'):
					positionArray[1] = 'managed';
					altArray[1] = serviceType;
					break;
				case('Cleaning Services'):
					positionArray[2] = 'cleaning';
					altArray[2] = serviceType;
					break;
				case('Food Services'):
					positionArray[3] = 'food';
					altArray[3] = serviceType;
					break;
				case('Laundry Services'):
					positionArray[4] = 'laundry';
					altArray[4] = serviceType;
					break;
				case('Retail Services'):
					positionArray[5] = 'retail';
					altArray[5] = serviceType;
					break;
				case('Painting Services'):
					positionArray[6] = 'painting';
					altArray[6] = serviceType;
					break;
			}
		}); // end of each list
		
		// 3. Ready to pump out new list HTML
		$('.profile .colourCode span').remove(); // remove the span
		$('ul', this).empty().addClass('relatedServices'); // empty out the existing list, and add class 'relatedServices' to UL
		
		// 4. Loops through array, and checks if the slot index equals null. If not, output the colour code.
		// Otherwise, output a blank image.
		for(i=0; i<positionArray.length; i++){
			if(positionArray[i] != undefined){
				$('ul', this).append('<li><img src="/images/icons/filter-'+ positionArray[i] +'.gif" alt="'+ altArray[i] +'" title="'+ altArray[i] +'" /></li>');
				$(this).addClass(positionArray[i] + ' ');
			}
			else{
				$('ul', this).append('<li><img src="/images/icons/experience-filter-bullet.gif" alt="blank" title="blank" /></li>');
			}
		}
	});
	
	
	// ** begin filter services on Experience Landing page **
	var getClassName;
	$('#experienceFilter li').each(function(){
		$(this).click(function(){
						
			// resets
			$('#experienceLandingBox .profile').each(function(){
				$(this).show().removeClass('selected');
				if(getClassName){
					$(this).removeClass(getClassName+'-selected');
				}
			});
			
			getClassName = $(this).attr('class'); // this determines what services is clicked
			
			$('#experienceLandingBox .profile').each(function(){
				var getProfileClassName = $(this).attr('class');
				if( getClassName == 'all'){
					$(this).show();
				}
				else if( !getProfileClassName.match(getClassName) ){
					$(this).hide();
				}
				else{
					//$(this).hide();
					//$(this).fadeIn(10000);
					$(this).addClass('selected ' + getClassName + '-selected');
				}
			});
			
			$('#experienceFilter li a').removeClass('on'); // remove all on classes
			$('a', this).addClass('on'); // then only add onto the list selected
			
			return false;
		});
	});
	
	/* border rollover for services landing page */
	$('#servicesLandingBox .services').hover(function(){
		$('.image', this).addClass('hover');
	}, function(){
		$('.image', this).removeClass('hover');
	});
	
	// get rid of border of investment announcement div
	$('#investorLandingBox .announcements div:last').css({'background':'none'});
	
	//sIFR.activate(infobook, arial); //active sifr at the end
	
	var getMaxHeight;

	$('.quickFacts li .sIFR-alternate').css({
		width:'173px',
		display: 'none',
		height:'auto',
		position: 'static'
		});

	/* gets maximum height of quickfacts item, and sets it (so page doesn't jump) */
	$('.quickFacts li').css({display:'block'}).each(function(){
		$('.sIFR-flash', this).css({display: 'block'});
		if( $(this).children('.sIFR-flash').attr('height') ){
			var getHeight = parseInt($('.sIFR-flash', this).attr('height'));
		}
		else{
			var getHeight = parseInt($(this).height()+20);
		}
		
		if(!getMaxHeight){
			getMaxHeight = getHeight;
		}
		if(getHeight > getMaxHeight){
			getMaxHeight = getHeight;
		}
		//alert(getMaxHeight);
	});
			
	// sets maximum height on the UL
	if($('.quickFacts li').size() > 1){
		$('.quickFacts ul').css({
			height: getMaxHeight +'px',
			position: 'relative',
			overflow: 'hidden'
		});
		
		$('.quickFacts li').css({position:'absolute', top:0, background:'#fff', height: (getMaxHeight+10)+'px'});
	
	// only rotates if there are more than 1 quickfacts (i.e. on services pages)
	
		$('.quickFacts li').hover( 
			function(){ iPause=1; },
			function(){	iPause=0; });
		newsitems = $('.quickFacts li').size();
		setInterval(ticknews, 5000); //time in milliseconds
	}
	
	// sets first sIFR h3 of wysiwygStandard to be margin:0
	$('.sIFR-active .wysiwygStandard h3:first').css({margin:'0'});
		
});

function ticknews() {
  $('.quickFacts li').hide();
  if (iPause==0){
	 $(".quickFacts ul li:eq("+curritem+")").fadeOut('slow');
	 curritem = ++curritem%newsitems;
	 $(".quickFacts ul li:eq("+curritem+")").fadeIn('slow');
	 }
}