/*
 * truncatable 1.2 - jQuery lightwieght text truncation plugin
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.truncatable.js 2009-08-20 $
 * $('.text').truncatable({	limit: 200, more: '.....', less: true, hideText: '[hide]' }); 
 */

(function($){$.fn.truncatable=function(options){var defaults={limit:100,more:'...',less:false,hideText:'[read less]'};var options=$.extend(defaults,options);return this.each(function(num){var stringLength=$(this).html().length;if(stringLength>defaults.limit){var splitText=$(this).html().substr(defaults.limit);var splitPoint=splitText.substr(0,1);var whiteSpace=new RegExp(/^\s+$/);for(var newLimit=defaults.limit;newLimit<stringLength;newLimit++){var newSplitText=$(this).html().substr(0,newLimit);var newHiddenText=$(this).html().substr(newLimit);var newSplitPoint=newSplitText.slice(-1);if(whiteSpace.test(newSplitPoint)){var hiddenText='<span class="hiddenText_'+num+'" style="display:none">'+newHiddenText+'</span>';var setNewLimit=(newLimit-1);var trunkLink=$('<a>').attr('class','more_'+num+'');$(this).html($(this).html().substr(0,setNewLimit)).append('<a class="more_'+num+'" href="#">'+defaults.more+'<a/> '+hiddenText);$('a.more_'+num).bind('click',function(){$('span.hiddenText_'+num).show();$('a.more_'+num).hide();if(defaults.less==true){$('span.hiddenText_'+num).append('<a class="hide_'+num+'" href="" title="'+defaults.hideText+'">'+defaults.hideText+'</a>');$('a.hide_'+num).bind('click',function(){$('.hiddenText_'+num).hide();$('.more_'+num).show();$('.hide_'+num).empty();return false})}});newLimit=stringLength}}}})}})(jQuery);




/**
 * Compact labels plugin
 */
(function($){$.fn.compactize=function(){return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){label.hide();}).blur(function(){if(input.val()===''){label.show();}});window.setTimeout(function(){if(input.val()!==''){label.hide();}},50);});};})(jQuery);

/*
 * hrefID jQuery extention - returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			},
			dropdown: function() {
				$('#nav li:has(ul)').hover(
					function(){ $('ul:first', this).show(); },
					function(){ $('ul:first', this).hide(); }
				);
			},
			labels : function(){
				$('.box label').compactize();				
			}		
		},
		fixes : {
			lastChild: function() {
				if($('.comments').length) {
					$('.comments').each(function(){
						$('li:last', this).addClass('last');
					});
				}
				
				if($('.entry').length) {
					$('.entry:last').addClass('last');
				}
			},
			blog : function(){
				// no comments/trackbacks + alternative
				var $comments = $('div.comments');
				$comments.each(function(){
					if($(this).find('ol>li').length == 0){
						var fixed = $(this).html().replace('</h2>','</h2><p class="empty">') + '</p>';
						$(this).html(fixed);
					} else {
						$(this).find('ol>li:even').addClass('alt');
					}
				});

				// show/hide comments/trackbacks
				var $links = $('p.post-info a.trig-trackbacks, p.post-info a.trig-comments');
				$links.click(function(){
					$($(this).hrefId()).toggleClass('comments-active');
					if($(this).hasClass('trig-comments')) {
						$($(this).hrefId()).parents('div.entry').find('div.add-comment-a').toggleClass('add-comment-a-active');	
					}
					return false;
				});

				// single post (show trackbacks and comments)
				if($('div.entry').length === 1){
					$('div.comments').addClass('comments-active');
					$('div.add-comment-a').addClass('add-comment-a-active');
				}
			}
		},
		
		
		enhancements : {
			accordion : function() {
				$('.accordion').each(function(){
					var 
						$accordion = $(this),
						$title = $('strong', this),
						liWidth = 34,
						aLiWidth = 784,
						duration = 300,
						timeout = 7000,
						quotes = $('.quotes li',this).map(function(){
							return $(this).html();
						});
						
					var getRandomQuote = function() {
						return quotes[Math.floor(Math.random() * quotes.length)];
					};
					
					var next = function() {
						var $a = $('.active', $accordion);

						if($a.is(':last-child')) {
							$('li:first', $accordion).find('strong').click();
						} 
						else {
							$a.next().find('strong').click();
						}
					};

					var play = setInterval(function() {
						next();
					}, timeout);

					$title.click(function(){
						if(!$(this).parent().is('.active')) {

							clearInterval(play);
							play = setInterval(function() {
								next();
							}, timeout);

							$accordion
								.find('.active').removeClass('active').animate({width: liWidth}, duration).end()
								.find('.left').removeClass('left');

							$(this).parent().addClass('active').animate({width: aLiWidth}, duration);

							$(this).parent().find('.quote').html(getRandomQuote());
							$('.active', $accordion).prevAll().addClass('left');
						}
						return false;
					});				

				}).find('li:first strong').click();
			}
		},
		
		tweaks : {
		
		latestBlogz : function(){
			
			// latest blog post 1
	       
			$(".blog-post:nth-child(1)").each(function(){
			        var postHtml = "";
				var postTitle = $(this).find(".entry h3:first").text();
				var postLink = $(this).find(".entry").find("a").attr("href");
				var postBody = $(this).find(".entry .postAction").text();
				var postBody = postBody.substr(0,190);		
			
				 postHtml = "<h6><a href='"+ postLink + "'>" + postTitle + "</a></h6><p><span class='postText'>" + postBody + "...</span></p>";
                                //alert(postHtml);
				$(".latestBlogEntry").html(postHtml);					
			       
			});
	
			// latest blog post 2
			
			$(".blog-post:nth-child(2)").each(function(){
			
				var $postTitle = $(this).find(".entry h3:first").text();
				var $postLink = $(this).find(".entry").find("a").attr("href");
				var $postBody = $(this).find(".entry .postAction").html();
				var $postBody = $postBody.substr(0,100);
				
					
			
				$postHtml = "<h6><a href='"+ $postLink + "'>" + $postTitle + "</a></h6><p><span class='postText'>" + $postBody + "...</span></p>";
		
		
				//$(".latestBlogEntry2").html($postHtml);
				
				//$(".postText").truncatable({	limit: 100, more: '' }); 
			
			
			});
	
			
		}, // latestblogz
		
		addNavClass : function(){
			
			// add classes for Practice Areas
			$("li.p-areas").find("a").addClass("button button-b");
			
			// add classes for home callout navs
			$("li#link-a").find("a").addClass("link-a");
			$("li#link-b").find("a").addClass("link-b");
			$("li#link-c").find("a").addClass("link-c");
			$("li#link-d").find("a").addClass("link-d");
			
			
			
		},
               showSubMenu : function(){
                                $('#nav_842881 li li:has(ul)').mouseover(
					function(){ var currTop = 0; var cw = 0; cw = $(this).css('width'); var offset = $(this).offset(); var offset2 = $(this).children("ul").offset(); var currTop = offset.top-offset2.top; $('ul:first', this).css("margin-left",cw);$('ul:first', this).css("margin-top",currTop); }
				);

                               $('#nav_842881 li li:has(ul)').mouseout(
					function(){ $('ul:first', this).css("margin-left","-9000px");$('ul:first', this).css("margin-top","0px"); }
				);
			
			}, // end 2nd level submenu
		
		sliderTweak : function(){
			
				$(".accordion li").find("a").each(function(){
					$(this).css("cursor","default");
				});
				
				$(".accordion li").find("a").click(function(){
					return false;
				});
			
			} // end sliderTweak
		
	} // end tweaks
	
		
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.utils.dropdown();
	Engine.utils.labels();
	
	Engine.enhancements.accordion(); 
	
	Engine.fixes.lastChild();
	Engine.fixes.blog();
	
	Engine.tweaks.latestBlogz();
	Engine.tweaks.addNavClass();
	Engine.tweaks.sliderTweak();
	Engine.tweaks.showSubMenu();

	
});