// JavaScript Document

var sColour = '#30AFA4';
var returnColour = '#000000';
var bIsMoving = false;
var sRegionSelected = 'worldmap';

$(document).ready( function() {
	
	if ($('#section_results ul').length > 0 ) {
		//alert('yes');
		//$('body').animate({ scrollTop: $('#section_results').offset().top }, 1000);
		$('#article_main').hide();
		$('#aside_main').css({'position' : 'absolute', 'left' : '830px', 'padding-top' : '100px'});
		$('#section_results').css({'padding-top' : '45px', 'min-height' : '650px'});
		$('#section_results ul li.activeTab a').trigger('click');
	}

	//Ben's super awesome map starts here
	$('article#article_main map area').hover( function() {
		$('article#article_main img.img_worldmap').attr('src', '/etc/designs/cc2014/images/' + $(this).attr('class') + '_blue.jpg');
	}, function() {
		if ($(this).data('selected') != 'true') {
		$('article#article_main img.img_worldmap').attr('src','/etc/designs/cc2014/images/' + sRegionSelected + '.jpg');
		}
	});
	
	$('article#article_main map area').click( function() {
		$('body').animate({ scrollTop: $('article#article_main header').offset().top }, 1000);
		sRegionSelected = $(this).attr('class');
		$('article#article_main img.img_worldmap').attr('src', '/etc/designs/cc2014/images/' + $(this).attr('class') + '_blue.jpg');
		$('article#article_main map area').data('selected', 'false');
		$(this).data('selected', 'true');
		var thisRegion = $(this).data('region');
		$('article#article_main ul#ul_offices li a').each( function() {
			if ($(this).parent().data('region') != thisRegion ) {
				$(this).animate({'opacity' : '0.3'});
			} else {
				$(this).animate({'opacity' : '1.0'});
			}
		});
		
		$('article#article_main ul#ul_regions li a').each( function() {
			if ($(this).text() == thisRegion ) {
				$(this).animate({'color' : '#009fda'});
			} else {
				$(this).animate({'color' : '#000000'});
			}
		});
		
		return false;
	});
	
	// Make a new ul 
	$('article#article_main h1#h1_regions').before('<h1 id="h1_offices" class="pageColour_text">Offices</h1><ul id="ul_offices"></ul>');
	$('article#article_main ul#ul_regions ul li').children().children().clone().sort( function(a, b) {
   		return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
	}).appendTo('article#article_main ul#ul_offices');
	
	var nAcc = 0
	var nCountFilter = Math.ceil($('ul#ul_offices li').length / 3);
	//alert(nCountFilter + nCountFilter);
	
	$('article#article_main ul#ul_offices li').each( function(i) {
		//$(this).addClass('li_' + i);
		if (i >= nCountFilter) {
			$(this).css({'margin-left' : '170px'});
			if (i >= (nCountFilter + nCountFilter)) {
				$(this).css({'margin-left' : '340px'});
				if (i >= (nCountFilter + nCountFilter + nCountFilter)) {
					$(this).css({'margin-left' : '510px'});
				}
			}
		} else {
			nAcc += $(this).outerHeight(true);	
		}
		if (i == nCountFilter ) {
			$(this).css({'margin-top' : '-' + nAcc + 'px'});
		}
		
		if (i == (nCountFilter + nCountFilter)) {
			$(this).css({'margin-top' : '-' + nAcc + 'px'});
		}
				
		if (i == (nCountFilter + nCountFilter + nCountFilter)) {
			$(this).css({'margin-top' : '-' + nAcc + 'px'});
		}
	}); 
	
	
	
});
