$(document).ready(function(){
    initialize();
});

function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, true); 
		return true; 
	}else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	}else{ 
		return false; 
	} 
}

function hideFocusBorders(){
var theahrefs = document.getElementsByTagName("a");
	if (!theahrefs){return;}
		for(var x=0;x!=theahrefs.length;x++){
		theahrefs[x].onfocus = function stopLinkFocus(){this.hideFocus=true;};
	}
}

addEvent(window, 'load', hideFocusBorders);

function initialize()
{
    $('#header_carousel .slidetabs').tabs('.items > div', {
		effect: 'fade',
		fadeInSpeed: 1000,
		fadeOutSpeed: 1000,
		rotate: true
	}).slideshow({
		interval: 6000,
		clickable: false
	});

	$('#header_carousel .slidetabs').data('slideshow').play();


	$('#main_menu').find('> li').hover(function(){
		$(this).children('a').toggleClass('hovered');
		$(this).find('ul').stop(true, true).slideToggle('fast');
	});


	var map_timeout = 0;

	$('#map a.link').hover(function(){
		clear_all_boxes();

		var div = $(this).attr('class');
		div = parseInt(div.replace('link office_', ''));

		$(this).addClass('active');
		var div = $('#map #office_'+div);

		var position = $(this).position();
		var displacement = div.height()/2 + 60;
		div.css({top: (position.top - displacement), left: (position.left + 5)});
		div.show();
	}).mouseleave(function() {
		map_timeout = setTimeout(function() {
			clear_all_boxes();
		}, 100);
	});

	$('#map .office').hover(function(){
		clearTimeout(map_timeout);
	}).mouseleave(function() {
		clear_all_boxes();
	});

	function clear_all_boxes(){
		clearTimeout(map_timeout);
		$('#map a.link').removeClass('active');
		$('#map .tooltip').hide();
	}

    jQuery.fn.extend({
         accordion: function() {
            return this.each(function() {

            	var $ul = $(this);

				if($ul.data('accordiated'))
					return false;

				$.each($ul.find('ul, li > div, li > ins'), function(){
					$(this).data('accordiated', true);
					$(this).hide();
				});

				$.each($ul.find('a'), function(){
					$(this).click(function(e){
						activate(this);
						return false;
					});
				});

				var active = (location.hash)?$(this).find('a[href=' + location.hash + ']')[0]:'';

				if(active){
					activate(active, 'toggle');
					$(active).parents().show();
				}

				function activate(el,effect){
					$(el).parent('li').toggleClass('active').siblings().removeClass('active').find('ul, div, ins').slideUp('fast');
					$(el).siblings('ul, div, ins')[(effect || 'slideToggle')]((!effect)?'fast':null);
				}

            });
        }
    });

    $('#faq_list').accordion();

    $('select.styled').wrap('<span class="sh" />');
}
