$(document).ready(function() {
	$('#btnSearch').click(function() {
		if ($("#mySearch").valid()) {
			return true;
		}
		return false;
	});

	// Bookmark site
	if(window.opera) {
		if ($("a.bookmark").attr("rel") != ""){
			$("a.bookmark").attr("rel","sidebar");
		} 
	}

	$("a.bookmark").click(function(e){
		e.preventDefault();
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { 
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { 
			window.external.AddFavorite( url, title);
		} else if(window.opera) { 
			return false; 
		} else { 
			 alert('Sorry, your browser does not support the requested action,'
			 + ' please add this page manually to your bookmarks.');
		}
	});

	$('.search').focus(function() {
		if (this.value == 'Search') { this.value=''; };
	});

	// print page function
	$('.print').click(function(e){e.preventDefault;window.print()});
	
	// sub menu - expand on click
	$('.folder').click(function() {
		$('#sub'+$(this).attr('id')).slideToggle();
	});
	
	// sub menu - expand parent element if child is selected
	// $('#panel ul li a.selected').parent().css('display', 'normal');
	// $("p").parent(".selected").css("background", "yellow");
	$("ul").parent("a.selected").css("padding-right", "10px");

	// if blog panel exists move it to the panel
	if ($('#blog-panel').length) {
		$('#blog-panel').prependTo($('#panel'))
		$('#blog-panel').show()
	}

	// headline scroll content
	var headline_count;
	var headline_interval;
	var old_headline = 0;
	var current_headline = 0;
	headline_count = $("div.blog-scroll").size();
	$("div.blog-scroll:eq("+current_headline+")").css('top','0px');
	headline_interval = setInterval(headline_rotate,5000); //time in milliseconds

	function headline_rotate() {
		current_headline = (old_headline + 1) % headline_count; 
		$("div.blog-scroll:eq(" + old_headline + ")").animate({top: -205},"slow", function() {
		$(this).css('top','210px');
		});
		$("div.blog-scroll:eq(" + current_headline + ")").show().animate({top: 0},"slow");  
		old_headline = current_headline;
	}

	$('#GuyQuote-link').fancyZoom({width:800, height:400});
	$('#GrahamQuote-link').fancyZoom({width:800, height:540});
	$('#SallyQuote-link').fancyZoom({width:800, height:800});
	$('#AnnetteQuote-link').fancyZoom({width:800, height:400});
	$('#ChristineQuote-link').fancyZoom({width:800, height:400});
	$('#JustinQuote-link').fancyZoom({width:800, height:400});

	$('.check').click(function(){
		var id = $(this).val();
		var checkEle = $('#checkThis'+id).length;
		var thisques = $(this).attr('name');
		var check = $('input[name$='+thisques+']:checked').length;
		if (check == 0) {
			$('input[name$='+thisques+']').addClass('required');
		} else {
			$('input[name$='+thisques+']').removeClass('required');
		}
		if ((checkEle) && ($('#id'+id+':checked').length == 1)) {
			$('#checkThis'+id).addClass('required');
		} else {
			$('#checkThis'+id).removeClass('required');
		}
	});

    jQuery('#mycarousel').jcarousel({
        scroll: 1,
		auto: 10,
        wrap: 'last'
    });

	if ($(".slidetabs").length) {
		$(".slidetabs").tabs(".slideshow > .myshow", {
			effect: 'fade',
			fadeOutSpeed: 'slow',
			rotate: true
		}).slideshow({autoplay: true,interval:8000,clickable:false });
	}

	$('.hover').hover(
		function() {
			$(this).children('img').fadeIn(300);
		},
		function() {
			$(this).children('img').fadeOut(300);
		}
	);

	$('#na').fancyZoom({width:800, height:400});
	$('#sa').fancyZoom({width:800, height:400});
	$('#eu').fancyZoom({width:800, height:400});
	//$('#af').fancyZoom({width:800, height:400});
	$('#as').fancyZoom({width:800, height:400});

	$('.continent').bind('click', function(e){
		e.preventDefault();
		var where = $(this).children('img').attr('alt').replace(' ','-').toLowerCase();
		$(this).siblings().children('img').removeClass('selected');
		$(this).children('img').addClass('selected');
		$('#popup').empty();
		$('#popup').load('http://www.berkshire.co.uk/where-we-work/'+where+' #selectedContent');
	});

	if ($('#map').length) {
		$('#inner-content').css({'height':'782px'});
	}

});

Cufon.replace('h1')('.inner-content h2')('.post h2')('h3')('#phone');

