$(document).ready(function(){
	
	$('#slideshow').cycle({ 
		fx:     'fade', 
		speed:   2000, 
		timeout: 2000
	 });
	
	
	//Preload img for tooltip
	newimage0 = new Image();
	newimage0.src = "assets/templates/default_tpl/images/bubble.png";
	$('div.toolTipWrapper').pngFix();
	//Custom easing animation
	/*-------------------------------------------------------------------------------------*/
	$.easing.custom = function (x, t, b, c, d) { 
		var s = 1.70158;  
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
	}
	$.easing.easeOutExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	};
	$.easing.easeOutBounce = function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	};
	/*----------------------------------------------------------------------------------------*/
	//---------------------------------- Client login and find what you need tabs
	$('#topPanelOne .trigger').animPanel({
		target: '#topPanelOne',
		topStart: '22px',
		topEnd: '-15px',
		imageOpen: 'top_panel_1_trigger.gif',
		imageClose: 'top_panel_1_trigger_close.gif'
	});
	$('#topPanelTwo .trigger').animPanel({
		target: '#topPanelTwo',
		topStart: '22px',
		topEnd: '-15px',
		imageOpen: 'top_panel_2_trigger.gif',
		imageClose: 'top_panel_2_trigger_close.gif'
	});
	
	//---------------------------------- Partners hovers
	$('#partners img').hover(function() {
		var src = $(this).attr("src").match(/[^\.]+/) + "_color.gif";
        $(this).attr("src", src);	
	},function() {
		var src = $(this).attr("src").replace("_color", "");
		$(this).attr("src", src);
	});
	
	//---------------------------------- Home page content scroller
	$("#slider").scrollable({
		size: 1,
		clickable: false,
		easing:	'custom',
		speed: 1000
	}).circular().mousewheel(1000).navigator({ 
        navi: "#flowtabs", 
        naviItem: 'a', 
        activeClass: 'active'
    })

	//Set start at item 2
	if ($('#slider').length) {
		var api = $("#slider").scrollable({api: true});
		api.move(1); 
	}
	//--------------------------------- Set width for content scroller tabs
	var ile = $('#flowtabs li').size();
	$('#flowtabs').width(ile*171);

	//--------------------------------- Main Menu
	$('#mainNav a, #subMenu a').removeAttr('title');

	$("#mainNav ul li a").hoverIntent(function() { 
		$(this).parent().find("ul").animate({ height: "toggle", opacity: "toggle" }, 450, "easeOutExpo");
		$(this).parent().hover(function() {}, function(){ $(this).parent().find("ul").slideUp('slow'); });		 
	}, function() {});
	
	$('#mainNav ul li ul').hover(function() {
		$(this).parents("li").addClass('hateIe');
		$(this).parents("li").find('a').addClass('selected');
	},
	function() {
		$(this).parents("li").find('a').removeClass('selected');
		$(this).parents("li").removeClass('hateIe');
	});

	$('#mainNav ul li').hover(function() {
		$(this).not('#mainNav ul li ul li').addClass('hateIe');
	},
	function() {
		$(this).removeClass('hateIe');
	});	
	

	DD_roundies.addRule('#mainNav ul li ul li a', '5px', true);
	if (!$.browser.msie) {	
		DD_roundies.addRule('#mainNav ul li ul', '0 5px 5px 5px', true);
		DD_roundies.addRule('#mainNav ul li a', '7px 7px 0 0', true);
	} 
	
	//--------------------------------- Sub Menu
	$('#subMenu ul li ul').hide();
	$('#subMenu ul li.active ul').show();

	$('#subMenu ul li:has(ul)').hoverIntent(function(){
		var target = $(this);
		target.addClass('active').find('ul').slideDown();
		$('#subMenu ul li:has(li)').not(target).removeClass('active').find('ul').slideUp();
		return true;
	}, function() {});
		
	//----------------------------------- Panels
	$("select[name='links'], select[name='client_area']").change( function() {
		var link = $(this).val();

		$(this).parents('form').each(function(){
				this.reset();
		});
		if (link.match(/^http/)) {
			window.open(link);
		} else {
			window.location = link;
		}
	});

	//-----------------------------------  Management Team accordion
	
	$('div.desc').eq(0).show();
	$('span.about').eq(0).addClass('active');
	$('div.top').eq(0).css('border-bottom','none');
	
	$('span.about').click(function() {
		$(this).addClass('active');
		$(this).parents('div.top').css('border-bottom','none');
		var target = $(this).parents('div.top').next('div.desc');
		$('div.desc2:visible').slideUp();
		$('span.contact').removeClass('active');
		target.slideDown();
		$('div.desc').not(target).slideUp().prev('div.top').css('border-bottom','1px dotted #b79859');
		$('span.about').not($(this)).removeClass('active');
	});

	$('span.contact').click(function() {
		$(this).addClass('active');
		$(this).parents('div.top').css('border-bottom','none');
		var target = $(this).parents('div.person').find('div.desc2');
		$('div.desc:visible').slideUp();
		$('span.about').removeClass('active');
		target.slideDown();
		$('div.desc2').not(target).slideUp().parents('div.person').find('div.top').css('border-bottom','1px dotted #b79859').find('span.contact').removeClass('active');
	});
	
	//--------------------------------------- News
	$('#news li h2').not('#news li.archives h2').click(function() {
		var target = $(this).parents('li');
		if ( ! target.find('span.content').is(':visible') ) {
			target.find('span.summary').hide();
			target.find('span.content').slideDown();
			$('#news li').not(target).removeClass('active').find('span.content').hide().end().find('span.summary').show();
			target.addClass('active');
		}
	});
	/*----------------------------------------- Add RSS Icon to News and Events heading*/
	$('#content h1:contains("Current News"), #content h1:contains("Events")').append('<a href="feed.rss"><img src="assets/templates/default_tpl/images/feed_big.gif" /></a>');
	$('#wrapperHome #news h2:contains("Whats new?")').append('<a id="show_cal" title="Show Upcoming Events" href="/whats-new/events.htm"><img src="assets/templates/default_tpl/images/calendar_icon.gif" /></a>');
	/*------------------------------------------ hover state for featured links on home page*/
	
	//Tooltips
	$('#show_cal').hover(
		function(e) {
		this.tip = this.title;
		var tipX = e.pageX-90;
		var tipY = e.pageY-90;
		$('body').append('<div class="toolTipWrapper">'+this.tip+'</div>');
		this.title = "";
		this.width = $(this).width();
		$('body').find('.toolTipWrapper').css({left:tipX, top:tipY })
		$('.toolTipWrapper').fadeIn('slow');
	},
	function() {
		$('.toolTipWrapper').fadeOut(100);
		$('body').find('.toolTipWrapper').remove();
		this.title = this.tip;
	});

	$("#show_cal").mousemove(function(e){
		var tipX = e.pageX-90;
		var tipY = e.pageY-90;
		$('.toolTipWrapper').css("left", tipX).css("top", tipY);
	});

	
	$('#featuredArea li').hoverIntent(function() {
		$(this).find('img').fadeTo('slow',0.5);
	},function () {
		$(this).find('img').fadeTo('slow',1);
	});
	
	/*
	$('ul#testimonials li p').each(function (i) {
		var string = $.trim( $(this).text() );
		var result = string.replace(/"/, '<span>"</span>')
		$(this).html(result);
	});
	$('ul#testimonials li p span').css({ color:"#00446a;", fontWeight:"bolder", fontSize: "30px", fontFamily: "Times", fontStyle: "italic" });
	*/
	
	$('table#liveStats tbody tr:odd').addClass('odd');
  	$('table#liveStats tbody tr:even').addClass('even'); 


	//Newsletter form validation
	$("form#subscribeForm").submit(function() {
		if ( ( $("input#name").val() == "") || ($("input#name").val() == "Your name")) {
			//alert('Please type your name');
			$('span#valResult').html('Please type your name');
			return false;
		}
		if ( ( $("input#thfli-thfli").val() == "") || ($("input#thfli-thfli").val() == "Your email address")) {
			//alert('Please type your email address');
			$('span#valResult').html('Please type your email address');
			return false;
		} else {
			var rules  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if ($("input#thfli-thfli").val().match(rules)) {
				return true;
			} else {
				//alert('You povided wrong email address');
				$('span#valResult').html('Email address is not valid');
				return false;
			}
		}
    });

	
}); //Close document.ready

$(window).bind("load", function() {
	$('#topPanelOne form, #topPanelTwo form').show();
	$('.loading').hide();
	$('#slideshowWrapper').fadeIn();
});

(function($) {
	$.fn.animPanel = function(options) {
		var opts = $.extend({}, $.fn.animPanel.defaults, options);
		return this.each(function() {
			$this = $(this);
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;				
			$this.toggle(function() {
				$('#tel').hide();
				$(o.target).animate({ top: o.topStart }, 300, "easeOutBounce", function() { 
					$(o.target).find('img').attr('src','assets/templates/default_tpl/images/'+o.imageClose);		
				});						   
			},function() {
				$('#tel').show();	
				$(o.target).animate({ top: o.topEnd }, 300, "easeOutBounce", function() { 
					$('#topPanelOne ul li ul:visible').slideUp();	
					$(o.target).find('img').attr('src','assets/templates/default_tpl/images/'+o.imageOpen);
				});						   	
			})	
			
		});
	};
})(jQuery);