/**
 * sfSlider
 *
 * @version: 1.0
 *
 * Required settings:
 *  display 	- provide number of items displayed at once
 *
 * Other settings:
 *  time 		- transition time
 *  easing 		- easing for the transition
 *  width 		- width of the scrolled area (by default visible area + right margin on the last visible item)
 *  previous	- previous link text
 *  next		- next link text
 *  wrap		- wrap container selector
 *  slider		- items container selector
 *  items		- items selector
 */ 
eval(function($){jQuery.fn.sfSlider=function(options){var defaults={width:0,display:6,time:500,easing:'swing',previous:'Previous',next:'Next',wrap:'div.wrap',slider:'ul.items',items:'ul.items li'};var settings=$.extend({},defaults,options);return this.each(function(){var $root=$(this);var $wrap=$root.find(settings.wrap);var $slider=$root.find(settings.slider);var $items=$root.find(settings.items);var all=$items.size();var pages=Math.ceil(all/settings.display);if($items.size()<=settings.display)return false;var width=settings.width;if(settings.width===0)width=$wrap.width()+parseInt($items.css('margin-right'),10);var current=1;$root.append('<ul class="index"><li class="prev"><a href="#previous" class="off">'+settings.previous+'</a></li><li class="next"><a href="#next">'+settings.next+'</a></li></ul>');var $controls=$root.find('ul.index');var check=function(){if(current===1){$controls.find('li.prev a').addClass('off');}else{$controls.find('li.prev a').removeClass('off');}if(current===pages){$controls.find('li.next a').addClass('off');}else{$controls.find('li.next a').removeClass('off');}};$controls.find('a').click(function(){var direction=$(this).parent().attr('class');if($slider.is(':animated')||(current==1&&direction=='prev')||(current==pages&&direction=='next'))return false;if(direction=='next'){move='-='+width+'px';current++;}else{move='+='+width+'px';current--;}$slider.animate({'marginLeft':move},settings.time,settings.easing);check();return false;});});};})(jQuery);

/*
 * hrefID jQuery extention
 */
$.fn.extend({ hrefId: function() { return $(this).attr('href').substr($(this).attr('href').indexOf('#')); } });

/*
 * In-Field Label jQuery Plugin
 * http://fuelyourcoding.com/scripts/infield.html
 *
 * Copyright (c) 2009 Doug Neiner
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://docs.jquery.com/License
 *
 * @version 0.1
 */
(function($){
	
    $.InFieldLabels = function(label,field, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        
        // Access to jQuery and DOM versions of each element
        base.$label = $(label);
        base.label = label;

 		base.$field = $(field);
		base.field = field;
        
		base.$label.data("InFieldLabels", base);
		base.showing = true;
        
        base.init = function(){
			// Merge supplied options with default options
            base.options = $.extend({},$.InFieldLabels.defaultOptions, options);

			// Check if the field is already filled in
			if(base.$field.val() != ""){
				base.$label.hide();
				base.showing = false;
			};
			
			base.$field.focus(function(){
				base.fadeOnFocus();
			}).blur(function(){
				base.checkForEmpty(true);
			}).bind('keydown.infieldlabel',function(e){
				// Use of a namespace (.infieldlabel) allows us to
				// unbind just this method later
				base.hideOnChange(e);
			}).change(function(e){
				base.checkForEmpty();
			}).bind('onPropertyChange', function(){
				base.checkForEmpty();
			});
        };

		// If the label is currently showing
		// then fade it down to the amount
		// specified in the settings
		base.fadeOnFocus = function(){
			if(base.showing){
				base.setOpacity(base.options.fadeOpacity);
			};
		};
		
		base.setOpacity = function(opacity){
			base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration);
			base.showing = (opacity > 0.0);
		};
		
		// Checks for empty as a fail safe
		// set blur to true when passing from
		// the blur event
		base.checkForEmpty = function(blur){
			if(base.$field.val() == ""){
				base.prepForShow();
				base.setOpacity( blur ? 1.0 : base.options.fadeOpacity );
			} else {
				base.setOpacity(0.0);
			};
		};
		
		base.prepForShow = function(e){
			if(!base.showing) {
				// Prepare for a animate in...
				base.$label.css({opacity: 0.0}).show();
				
				// Reattach the keydown event
				base.$field.bind('keydown.infieldlabel',function(e){
					base.hideOnChange(e);
				});
			};
		};

		base.hideOnChange = function(e){
			if(
				(e.keyCode == 16) || // Skip Shift
				(e.keyCode == 9) // Skip Tab
			  ) return; 
			
			if(base.showing){
				base.$label.hide();
				base.showing = false;
			};
			
			// Remove keydown event to save on CPU processing
			base.$field.unbind('keydown.infieldlabel');
		};
      
		// Run the initialization method
        base.init();
    };
	
    $.InFieldLabels.defaultOptions = {
        fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be
		fadeDuration: 300 // How long should it take to animate from 1.0 opacity to the fadeOpacity
    };
	

    $.fn.inFieldLabels = function(options){
        return this.each(function(){
			// Find input or textarea based on for= attribute
			// The for attribute on the label must contain the ID
			// of the input or textarea element
			var for_attr = $(this).attr('for');
			if( !for_attr ) return; // Nothing to attach, since the for field wasn't used
			
			
			// Find the referenced input or textarea element
			var $field = $(
				"input#" + for_attr + "[type='text']," + 
				"input#" + for_attr + "[type='password']," + 
				"textarea#" + for_attr
				);
				
			if( $field.length == 0) return; // Again, nothing to attach
			
			// Only create object for input[text], input[password], or textarea
            (new $.InFieldLabels(this, $field[0], options));
        });
    };
	
})(jQuery);

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","inherit");
		});
	}
})(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);
					}
				});
			}
		},
		forms : {
			labels : function(){
				var $elements = $('form.newsletter-a p input, form#topform p input');
				$elements.each(function(){
					if($(this).val() !== '') $(this).prevAll('label:first').hide();
				}).focus(function(){
					$(this).prevAll('label:first').hide();
				}).blur(function(){
					if($(this).val() === '') $(this).prevAll('label:first').show();
				});
			}
		},
		fixes : {
			blog : function(){
				// no comments/trackbacks + alternative
				var $comments = $('div.comments-a');
				$comments.each(function(){
					if($(this).find('div.comment').length == 0){
						var fixed = $(this).html().replace(/<\/h2>/i,'</h2><p class="empty">') + '</p>';
						$(this).html(fixed);
					} else {
						$(this).find('div.comment:odd').addClass('alt');
					}
				});
				
				// show/hide comments/trackbacks
				var $links = $('div.post-a p.info a.comments, div.post-a p.info a.trackbacks');
				$links.click(function(){
					$($(this).hrefId()).toggle();
					if($(this).is('.comments')) $($(this).hrefId()).next('div.add-comment-a:first').toggle();
					return false;
				});
				
				// single post (show trackbacks and comments)
				if($('div.post-a').length == 1){
					$('div.comments-a, div.add-comment-a').show();
				}
			},
			separators : function(){
				$('ul[class*=cols]').each(function(){
					var matches = /cols([0-9]+)/i.exec($(this).attr('class'))
					var row = matches[1];
					$(this).find('li').each(function(i){
						if((i+1) % row == 0) $(this).after('<li class="separate"><a href="#top">Back to top</a></li>');
					});
				});
			}
		},
		slider : function(){
			$('#slider ul.items li:nth-child(6n+1)').addClass('start');
			$('#slider').sfSlider({display: 6});
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.forms.labels();
	Engine.fixes.blog();
	Engine.fixes.separators();
	Engine.slider();
	
});




$(document).ready(function(){

		
		//var pathArray = window.location.pathname.split( '/' );
		//var host = pathArray[1];
		//$('body').attr('id' , host);
       //////////////

    	jQuery("#nav ul li.selected ul:first").clone().appendTo("#subNav");
    	jQuery ("#pageContent img:first").addClass("headerImage");
    	//jQuery("ul#nav_761243 li.selected ul:first").clone().appendTo("#subNav");
    	

    	
   		Cufon.replace('#nav ul li a' , {hover: true});
		Cufon.replace('#subNav ul li a' , {hover: true});
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon.replace('h3');
		Cufon.replace('h4');
		Cufon.replace('#quote p');
		Cufon.replace('#addressWrapper');
		Cufon.replace('#homeContact');
		Cufon.replace('.phone');
		
		//$(".columns").equalHeights();
	
		$(function(){
			$('#slides').slides({
				preload: true,
				preloadImage: 'images/template/loading.gif',
				play: 6000,
				pause: 6000,
				hoverPause: true,
				effect: 'fade',
				crossfade: true
				});
		});
		
	//Home sliding form
	jQuery('#homeContact').click(function(){
		jQuery('#homeContactFormWrapper').slideToggle();
	});
			
		
});	
	
	


