(function($) {
 $.fn.tooltip = function(string, opts) {
		var o = (typeof string == 'string') ? opts || {} : string;
		var options = $.extend({x:20,y:10}, o);
	    return this.each(function() {
			var str = (typeof string == 'string') ? string : false; var self = $(this), tt;
			var t = self.attr('title')
			if (!str) { str = t; }
			self.data('title_attr', str).mouseover(function(e) {
				e.stopPropagation(); if (str == '') { return; }
		        tt = $('body').append('<div class="tooltip tooltip_'+$(this).attr('class')+'">' + str+ '</div>')
					.find('.tooltip').css({'top': e.pageY + options.y, 'left': e.pageX + options.x }).fadeIn('500');
					self.attr('title','');
		    }).mousemove(function(e) {  
				e.stopPropagation(); 
		        $('.tooltip').css({'top': e.pageY + options.y, 'left': e.pageX + options.x });
				if (!string && self.attr('title') != '') { /* monitor title changes */
					self.data('title_attr', self.attr('title'))
					$('.tooltip').html(self.attr('title'))
				}
		    }).mouseout(function() {  
				if (tt) { self.attr('title', self.data('title_attr')); tt.remove(); }
		    }).click(function(e) { self.trigger('mousemove'); /* for ff */ })
			.attr('title', '');
		});
	}
})(jQuery);

$.fn.reverse = [].reverse;
$.extend( $.easing, { easeOutExpo: function (x, t, b, c, d) { return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; } });

var SlideShow = {
	index: 0,
	delay: 12000,
	total: 0,
	container: '',
	timer: '',
	is_animating: false,
	progress_int: 0,
	init: function() {
		// combine
		var slides = $('.slide');
		this.total = slides.length;
		var _first = this.container = slides.first();
		var nav = $('<div class="slide_nav"></div>').appendTo(_first.parent())
		slides.each(function() {
			var self = $(this), i = self.index();
			self.attr('id', 'slide__'+i);
			self.children().each(function() {
				var child = $(this), c = child.attr('class').split(' ')[0];
				child.addClass('_'+i).data('original_width', child.width())
				if (i > 0) {
					_first.find('.'+c+':last').after(child);
					child.hide();
				}
			});
			if (i > 0) { self.hide() }	
		});
		_first.find('.i img').each(function(i) {
			$('<a href="#" class="_'+i+'" title="'+_first.find('.tg._'+i).text()+'"><span class="pt"><span></span></span><img src="'+ $(this).attr('src') +'" /></a>').bind('click', function(e) {
				e.preventDefault(); e.stopPropagation();  SlideShow.goto(parseInt($(this).attr('class').split('_')[1]))
			}).appendTo(nav).tooltip({x:-20, y:-40})
		})
		setTimeout($.proxy(this.next, this), this.delay);
		this.show_progress();
	},
	goto: function(indx) {
		if(this.index == indx) return this.set_timer();
		this.index = indx; this._next();
	},
	set_timer: function() {
		if (this.timer) { clearTimeout(this.timer) }
		this.timer = setTimeout($.proxy(this.next, this), this.delay);
	},
	_next: function() { 
		var transition = function() { 
			this.container.unbind('complete');
			this.transition(this.set_timer);
		}
		if (!this.is_animating) { transition.apply(this)
		} else { this.container.bind('complete', $.proxy(transition, this)) }
		this.show_progress();
    },
	next: function() {
		this.index += 1;
		if (this.index >= this.total) { this.index = 0 }
		this._next();
	},
	transition: function(cb) {
		var self = this;
		self.is_animating = true;
		var container = $('.slide:first');
		container.children().not('._'+this.index).fadeOut('slow');
		container.find('._'+this.index).reverse().each(function(i) {
			var c = $(this).css({'z-index':1+i});
			$(this).width(150).css({'opacity':0}).show().animate({'width':c.data('original_width'), 'opacity':1}, 500*(i+1), 'easeOutExpo', function() { 
				if (i>=3) { 
					self.is_animating = false; self.container.trigger('complete');
				}
			});
		})		
		if (cb) { cb.apply(self, [this]) }
	},
	show_progress: function() {
		var date = new Date()
		var start_time = date.getTime();
		this.container.parent().find('.slide_nav a').removeClass('active')
			.filter('._'+this.index).addClass('active')
			
		var progress_bar = this.container.parent().find('.slide_nav a._'+this.index+' span span').show();
		progress_bar.parent().css({'width': 0}).animate({width:'100%'}, 1000, 'easeOutExpo');
			
		if (this.progress_int) clearInterval(this.progress_int);
		this.progress_int = setInterval($.proxy(function() {
			var t = this.delay, n = (new Date).getTime() - start_time
			var p = Math.round(n/t*100);
			progress_bar.css({'width':p+'%'})
			if (((new Date).getTime() - start_time) >= this.delay) {
				//this.show_progress();
			}
		}, this), 200)
	}
	
	
}






var App = {
	init_pa_menu: function() {
		var images = {
			1:'appellate', 2:'business', 3:'estate', 4:'quitam', 5:'health', 6:'litigation', 7:'labor', 8:'probate', 9:'realestate', 10:'surity', 11:'tax'
		}
		var img = $('#pa_image');
		$('#topnav .practiceareas .subnav li a').bind('mouseover', function() {
			var li = $(this).parent();
			img.attr('src', 'images/pa/'+images[li.attr('class').split('_')[1]]+'_str.jpg') 
		}).bind('mouseout', function() {
			img.attr('src', 'images/pa/main_str.jpg') 
		})
	},
	init_slideshow: function() {
		
	},	
	init: function() {
		this.init_pa_menu();
		SlideShow.init();
	}
}


$(function() {
	App.init();
})






