$(function(){

	$('.nav_global').superfish({
		delay:0,
		speed:'fast',
		autoArrows:false,
		dropShadows:false
	});

	$("#callback_form").validate();
	$(".whitepaper_form").validate();
		
	$('.list_whatwedo .list_slider_inner ul').easyListSplitter({ colNumber: 2 });
	
	$('form.search input[type="text"]').clearField();
	$('.callback_form input[type="text"]').clearField();
	$("form.search_jobs select").selectBox();
	$(".selectbox").selectBox();
	$(".callback_form .selectBox").selectBox();

	$('.list_panels').cycle({
		activePagerClass: 'active',
		fx: 'scrollLeft',
		timeout: 12000,
		prev: '#nav_cycle_prev',
		next: '#nav_cycle_next',
		pager: '#nav_cycle',
		pagerAnchorBuilder: function(idx, slide) {
			var slide_title = jQuery(slide).attr('title');
			return '<li><a href="#">'+slide_title+'</a></li>'; 
		}
	});

	$('.section_slide .list_clients').cycle({ 
		activePagerClass: 'active',
		fx: 'scrollLeft', 
		speed: 800,
		pager: '.nav_1'
	});

	$('.list_clients_cycle_callout').cycle({ 
		activePagerClass: 'active',
	    fx: 'scrollLeft', 
	    speed: 800,
	    pager: '.nav_callout'
	});

    $('.list_clients_cycle_1').cycle({ 
		activePagerClass: 'active',
        fx: 'scrollLeft', 
        speed: 800,
        pager: '.nav_1'
    });
    $('.list_clients_cycle_2').cycle({ 
		activePagerClass: 'active',
        fx: 'scrollLeft', 
        speed: 800,
        pager: '.nav_2'
    });
    $('.list_clients_cycle_3').cycle({ 
		activePagerClass: 'active',
        fx: 'scrollLeft', 
        speed: 800,
        pager: '.nav_3'
    });

	$('.content_header .sidebar .list').superfish({
		animation: {opacity:'show'},
		speed: 'fast'
	});

	$(".selectbox").change(function(){
		window.location = $(this).val();
		return false;
	});

	$('.group .content_header .sidebar li a').hover(function(){
		//$(this).parent().find('.list_subnav:first').addClass('hover').animate({top:0},300);
		$subnav = $(this).next('ul');
		$subnav_height = $subnav.height();
		$subnav_height = $subnav_height / 2 + 8;
		$subnav.css({top:'-'+$subnav_height+'px'});
	},
	function(){
		//$(this).parent().find('.list_subnav:first').removeClass('hover').animate({top:-$height-70+'px'},300);
	});

	$('.content_header .sidebar li a.subnav').hover(function(){
		//$(this).parent().find('.list_subnav:first').addClass('hover').animate({top:0},300);
		$subnav = $(this).next('.list_subnav');
		$subnav_height = $subnav.height();
		$subnav_height = $subnav_height / 2 + 8;
		$subnav.css({top:'-'+$subnav_height+'px'});
	},
	function(){
		//$(this).parent().find('.list_subnav:first').removeClass('hover').animate({top:-$height-70+'px'},300);
	});

	$('.tab_research .trigger').click(function(){
		if($('.tab_research').hasClass('active')){
			$('.tab_research').removeClass('active');
			$('.tab_research').animate({top:'-307px'},300);
		}
		else{
			$('.tab_research').addClass('active');
			$('.tab_research').animate({top:'-3px'},300);
		}
		return false;
	});

	$('.home .list_sites').equalHeights();
	$('.list_children.list_cols').equalHeights();
	$('.list_videos').equalHeights();
	
	$(".list_slider").hide();
	$('.list_slider_tab').click(function(){
		if($(this).hasClass('active')){
			$(this).parent().parent().next('.list_slider').slideUp(400);
			$(this).removeClass('active');
		}
		else{
			$(this).parent().parent().next('.list_slider').slideDown(400);
			$(this).addClass('active');
		}
		return false;
	});

	//$("a.popup").fancybox();
	$("a[rel*=leanModal]").leanModal({top : 200});

	$( "#slider-range" ).slider({
		range: 'min',
		step: 10000,
		min: 20000,
		max: 120000,
		value: 20000,
		slide: function( event, ui ) {
			$( "#amount" ).val( "£" + addCommas(ui.value));
			$( "#amount1" ).val( ui.value);
		}
	});
	$( "#amount" ).val( "£" + addCommas($( "#slider-range" ).slider( "value" )));
	$( "#amount1" ).val($( "#slider-range" ).slider( "value" ));

	/*
	$( "#slider-range" ).slider({
		range: true,
		min: 25,
		step: 10,
		max: 175,
		values: [ 25, 175 ],
		slide: function( event, ui ) {
			$( "#amount" ).val( "£" + ui.values[ 0 ] + "k - £" + ui.values[ 1 ] + "k" );
			$( "#amount1" ).val( ui.values[ 0 ]);
			$( "#amount2" ).val( ui.values[ 1 ]);
			$( "#amount3" ).val( ui.values[ 0 ]);
			$( "#amount4" ).val( ui.values[ 1 ]);
		}
	});
	$( "#amount" ).val( "£" + $( "#slider-range" ).slider( "values", 0 ) + "k - £" + $( "#slider-range" ).slider( "values", 1 ) + "k" );
	$( "#amount1" ).val($( "#slider-range" ).slider( "values", 0 ));
	$( "#amount2" ).val($( "#slider-range" ).slider( "values", 1 ));
	$( "#amount3" ).val($( "#slider-range" ).slider( "values", 0 ));
	$( "#amount4" ).val($( "#slider-range" ).slider( "values", 1 ));
	*/
});

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
	    x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

$.fn.clearField = function(){
	return this.focus(function() {
		if( this.value == this.defaultValue){
			this.value = "";
		}
	}).blur(function(){
		if( !this.value.length){
			this.value = this.defaultValue;
		}
	});
};

// leanModal v1.0 by Ray Stone - http://finelysliced.com.au
(function($){$.fn.extend({leanModal:function(_1){var _2={top:100,overlay:0.5};_1=$.extend(_2,_1);return this.each(function(){var o=_1;$(this).click(function(e){var _3=$("<div id='lean_overlay'></div>");var _4=$(this).attr("href");$("body").append(_3);$("#lean_overlay").click(function(){_5(_4);});var _6=$(_4).outerHeight();var _7=$(_4).outerWidth();$("#lean_overlay").css({"display":"block",opacity:0});$("#lean_overlay").fadeTo(200,o.overlay);$(_4).css({"display":"block","position":"fixed",opacity:0,"z-index":11000,"left":50+"%","margin-left":-(_7/2)+"px","top":o.top+"px"});$(_4).fadeTo(200,1);e.preventDefault();});});function _5(_8){$("#lean_overlay").fadeOut(200);$(_8).css({"display":"none"});};}});})(jQuery);
