var count_down = null;

$(document).ready(function(){

				if(is_ie6 === false){
					$('#nav li').hover(function() {
						$('div.nav-highlight',$(this)).animate({height:3}, 90)
						},function() {
						if(!$(this).hasClass('current'))
						$('div.nav-highlight',$(this)).animate({height:0}, 90)
					});
				}

				var current_thumb_position = 0;

				// set opacity of square project thumbs
				$('#main-right li img').animate({opacity: 0.5}, 1);

				// set opacity of related thumbs
				$('#detail-left li img').animate({opacity: 0.5}, 1);

				// featured box slide-up
				$('#services-featured').hover(function(){
					$('div',$(this)).animate({height:85, opacity: 1}, 300);
					},function() {
					$('div',$(this)).animate({height:0, opacity: 0}, 300, function() {
						$(this).hide();
					});
				});

                function initCounter() {
                    if(count_down!=null) return;
                    $('#countdown').data('counter',4);
                    $('#countdown .digit_high, #countdown .digit_high2').css({
                        top: "-82px"
                    });
                    $('#countdown').parents('div').eq(0).find('p,h2').hide();
                    $('#countdown').parents('div').eq(0).find('img').eq(0).css({
                        paddingTop : "60px"
                    });
                    $('#countdown .digit_low').css({
                        top: "-328px"
                    });
                    count_down = setInterval(function() {
                        var d=$('#countdown').data('counter');
                        d-=1;

                        $('.digit_low').animate({
                            top: "+=82px"
                            }, 300);
                        if(d==0) {
                            clearInterval(count_down);
                            count_down=null;
                            $('.digit_high, .digit_high2').animate({
                            top: "+=82px"
                            }, 300);
                            setTimeout(function() {
                                $('#countdown').parents('div').eq(0).find('img').animate({
                                    paddingTop: "0px"
                                },600);
                                setTimeout(function() {
                                    $('#countdown').parents('div').eq(0).find('p,h2').fadeIn();
                                },500);

                            },1000);
                        }

                        $('#countdown').data('counter',d);

                    },1000);
                }

                function slideInit() {
                    var $c = $('.slide').parents('div');
                    var a_index=$c.data('active');
                    var l_index=$c.data('last_active');
                    // destructor
                    if(l_index!==null) {
                        switch($('.slide').eq(l_index).attr('rel')) {
                            case "mobile" :
                                break;
                            case "strategy" :
                                break;
                            case "web-design" :
                                    if(count_down!=null) {
                                        clearInterval(count_down);
                                        count_down=null;
                                        setTimeout(function() {
                                            $('#countdown #four_seconds').css({
                                                paddingTop : "60px"
                                            });
                                            $('#countdown').parents('div').eq(0).find('p,h2').hide();
                                            $('#countdown .digit_high, #countdown .digit_high2').css({
                                                top: "-82px"
                                            });
                                            $('#countdown .digit_low').css({
                                                top: "-328px"
                                            });
                                        },500);
                                    }
                                break;
                            case "agency" :
                                break;
                            default :
                                // deInit
                               // console.log("DeInit : "+$('.slide').eq(l_index).attr('rel'));
                                break;
                        }
                    }

                    // construct
                    if(a_index!== null) {
                        switch($('.slide').eq(a_index).attr('rel')) {
                            case "mobile" :
                                break;
                            case "strategy" :
                                break;
                            case "web-design" :
                                    initCounter();
                                break;
                            case "agency" :
                                break;
                            default :
                                //console.log("Init : "+$('.slide').eq(a_index).attr('rel'));
                                break;
                        }
                    }
                }

                var $container = $('.slide').parents('div');
                if($container.length>0 && $container.find('.slide').length>1) {
                    $container.find('.slide').hide();
                    $container.find('.slide').eq(0).show();
                    $container.data('active',0);
                    $container.data('last_active',null);
                    slideInit();

                    setTimeout(function() {
                            setInterval(function() {
                            var $container = $('.slide').parents('div');
                            var d=$('#home-carousel').data('active');
                            old_d=d;
                            d=(d+1)% $('#home-carousel .slide').length;
                            $container.data('last_active',old_d);
                            $container.data('active',d);
                            $container.find('.slide').eq(old_d).fadeOut(400);
                            $container.find('.slide').eq(d).fadeIn(400);
                            slideInit();
                            return false;
                        }, 10000);
                    }, 500);
                }

                initCounter();

				// project squares yellow textbox slide-up
				$('#main-right li').live('hover',function(){
					// first stop any current animations and slide them back down
					$('#main-right li div:animated').stop().animate({height:0, opacity: 0}, 300);
					$('#main-right li img:animated').stop().animate({opacity: 0.5}, 300);

					$('div.info-box',$(this)).filter(':not(:animated)').animate({height:60, opacity: 1}, 300);
					$('img',$(this)).filter(':not(:animated)').animate({opacity: 1}, 300);
					},function() {
					$('div.info-box',$(this)).animate({height:0, opacity: 0}, 300, function() {
						$(this).hide();
					});
					$('img',$(this)).animate({opacity: 0.5}, 200);
				});

				// related squares yellow textbox slide-up
				$('#detail-left li').hover(function(){
					$('div',$(this)).stop().animate({height:55, opacity: 1}, 300);
					$('img',$(this)).stop().animate({opacity: 1}, 300);
					},function() {
					$('div',$(this)).stop().animate({height:0, opacity: 0}, 300, function() {
						$(this).hide();
					});
					$('img',$(this)).stop().animate({opacity: 0.5}, 200);
				});

				// filter the sqare thumbs
				$('.sub-filter').live('click',function(){

					set_active_thumb_nav(0);

					// reset to page 1 if we're not on it
					$('ul#grid').animate({
					'marginTop' : 0
					});

					// remove the 'current' class from all sibling li's and add it to the clicked one
					$(this).parent().siblings().removeClass('current');
					$(this).parent().addClass('current');

					var show_type = $(this).attr('rel');
					if(show_type == 'all')
						{
							$('#main-right ul li').addClass('needed');
						} else {
							$('#main-right ul li:not(.' + show_type + ')').animate({opacity:0},200);
							$('#main-right ul li:not(.' + show_type + ')').removeClass('needed');
						}

					// quick check, is anything still visible? if not, do the anmimation very quickly!
					var numvisible = $('li.needed:visible').length;
					//alert(numvisible);
					if(numvisible > 0){
						var mvspeed = 800;
					} else {
						var mvspeed = 1;
					}

					// add class 'needed' to any items that are currently hidden but need to be displayed now
					$('#main-right ul li.' + show_type).addClass('needed');

					// make the images 163 px as some may be zero
					$('#main-right ul li.needed img').width(163).height(163);

					$('#main-right ul li.needed').css('display','block');

					$('ul#grid li.needed').each(function(index){
						x = (index% 4) * 169;
						y = Math.floor(index/ 4) * 169;
						$(this).animate({
							left: x,
							top: y
						}, mvspeed, function(){
							$(this).animate({opacity:1},200,function(){
								// pagination for square thumbs
								$('.pagination').html('');
								var thumbs = $('li.' + show_type);
								var number_of_thumbs = thumbs.length;
								if(number_of_thumbs > 1)
								{
									// how many pages?
									var pages = Math.ceil(number_of_thumbs/12);
									if(pages > 1)
									{
										thumb_pagination = '';
										for (i=1;i<=pages;i=i+1)
										{
											thumb_pagination += '<a href="#" class="active" rel="' + i + '">' + i + '</a>';
										}
										$('.pagination').html(thumb_pagination);
										set_active_thumb_nav(0);

										// skip to another page of thumbs
										$('.pagination a').click(function(){
											$('ul#grid').animate({
											'marginTop' : 507*(-$(this).attr('rel'))+507
											});
											move_to_thumb_pos = parseInt($(this).attr('rel'));
											current_thumb_position = move_to_thumb_pos-1;
											set_active_thumb_nav(current_thumb_position);
											return false;
										});
									}
									// reposition the pagination div if necessary
									var rows = Math.ceil(number_of_thumbs/4);
									if(rows < 3)
									{
										//$('.pagination').animate({
										//	'marginTop' : 169*(-rows)-154
										//	});
									}
									// any hidden thumbs, make them zero width and height so the anchor disappears
									$('#main-right ul li:not(.needed) img').width(0).height(0);
									$('#main-right ul li:not(.needed)').css('display','none');
								}
							});
						});

					});

					return false;
				});

				// make the div inside the thumb square clickable
				$('ul#grid li div.info-box').click(function(){
					document.location.href = $(this).prev('a').attr('href');
				});

				// pagination for square thumbs
				var thumbs = $('li.needed');
				var number_of_thumbs = thumbs.length;
				if(number_of_thumbs > 1)
				{
					// how many pages?
					var pages = Math.ceil(number_of_thumbs/12);
					if(pages > 1)
					{
						thumb_pagination = '';
						for (i=1;i<=pages;i=i+1)
						{
							thumb_pagination += '<a href="#" class="active" rel="' + i + '">' + i + '</a>';
						}
						$('.pagination').html(thumb_pagination);
						set_active_thumb_nav(0);
					}
				}

				// skip to another page of thumbs
				$('.pagination a').click(function(){
					$('ul#grid').animate({
					'marginTop' : 507*(-$(this).attr('rel'))+507
					});
					move_to_thumb_pos = parseInt($(this).attr('rel'));
					current_thumb_position = move_to_thumb_pos-1;
					set_active_thumb_nav(current_thumb_position);
					return false;
				});


				// clear newsletter signup box
				$('#home-newsletter-signup #email').click(function(){
					if($(this).val() == 'your email here')
						{
							$(this).val('');
						}
				});

				// newsletter signup
				$('#home-newsletter-signup #email').focus(function(){
					$('#home-newsletter-signup #message').html('');
				});


				$('#home-newsletter-signup').validate({
					//errorLabelContainer: $('#home-newsletter-signup #error'),
					errorPlacement: function(error, element) {
						error.insertAfter(element.next());
						element.parent().addClass('error');
					},
					rules: {email: {required: true, email:true}},
					messages: {email: {required: "Please enter your email address", email: "Please enter a valid email address"}},
					submitHandler: function(form) {
						$(form).ajaxSubmit({
							target: '#home-newsletter-signup #message'
						});
						$('#home-newsletter-signup #message').css('display', 'block');
						$('#email').val('');
					}
				});

				// questionnaire validation
				$('#questionnaire').validate({
					errorPlacement: function(error, element) {
						error.insertAfter(element.parent());
					},
					rules: {
						name: {required: true},
						email: {required: true, email:true},
						telephone: {required: true},
						goal_1: {required: true},
						goal_2: {required: true},
						goal_3: {required: true},
						website_like_1: {required: true},
						website_like_2: {required: true},
						website_like_3: {required: true},
						characterise_vision: {required: true},
						website_notlike_1: {required: true},
						website_notlike_2: {required: true},
						website_notlike_3: {required: true}
					},
					messages: {
						name: {required: "Please enter your name"},
						email: {required: "Please enter your email address", email: "Please enter a valid email address"},
						telephone: {required: "Please enter your telephone number"},
						goal_1: {required: "Please enter a goal"},
						goal_2: {required: "Please enter a goal"},
						goal_3: {required: "Please enter a goal"},
						website_like_1: {required: "Please enter a website"},
						website_like_2: {required: "Please enter a website"},
						website_like_3: {required: "Please enter a website"},
						characterise_vision: {required: "Please tell us about your vision for your project"},
						website_notlike_1: {required: "Please enter a website"},
						website_notlike_2: {required: "Please enter a website"},
						website_notlike_3: {required: "Please enter a website"}
					}
				});

				// clear questionnaire sections box
				$('#questionnaire #sections_required').click(function(){
					if($(this).val() == 'e.g. Home page, About Us, Contact Us, News etc.')
						{
							$(this).val('');
						}
				});
				// repopulate questionnaire sections box if left empty
				$('#questionnaire #sections_required').bind('blur',function(){
					if($(this).val().length < 1)
						{
							$(this).val($(this).attr('rel'));
						}
				});


				// temporarily remove the click from client squares (client page)
				$('.clients-page .square-inner a').click(function(){
					return false;
				});


				function set_active_thumb_nav(position)
				{
					$('.pagination a').removeClass('active');
					//$('.pagination a:eq('+position+')').addClass('active');
					$('.pagination').each(function(){
						$(this).find('a:eq('+position+')').addClass('active');
					});
				}

				$("#is_map").gMap({
					markers: [{
						address : 'NE10 0JP',
						icon: {
							image: "/assets/images/map_flag.png",
							iconsize: [115, 112],
							iconanchor: [76,112]
							}
					}],
					address : 'NE10 0JP',
					zoom : 15
				});

                // Jan 2011 homepage cycle
                /*$('#home-carousel').cycle({
                    fx:         'fade',
                    speed:      1500,
                    timeout:    6000
                });*/

			});
