/*!
 * SimpleFlame Content rotator 1.0
 */
(function($){function fisherYates(myArray){var i=myArray.length;if(i===0){return false;}while(--i){var j=Math.floor(Math.random()*(i+1));var tempi=myArray[i];var tempj=myArray[j];myArray[i]=tempj;myArray[j]=tempi;}}var sfRotator=function(el,options){this.settings={'item':'li','activeClass':'active','duration':5000,'autorotate':true,'effectIn':'fadeIn','optionsIn':{},'speedIn':'normal','effectOut':'fadeOut','optionsOut':{},'speedOut':'normal','random':false};$.extend(this.settings,options);this.$container=$(el);this.build();};sfRotator.prototype.build=function(){this.$container.addClass('sf-items');this.$wrapper=$('<div class="sf-rotator" />');this.$container.before(this.$wrapper);this.$wrapper.append(this.$container);this.$controls=$('<ul class="sf-controls" />');this.$wrapper.append(this.$controls);this.$items=this.$container.children(this.settings.item);if(this.settings.random===true){this.$items=this.$items.detach();fisherYates(this.$items);this.$container.append(this.$items);}this.$current=this.$items.index(this.$items.filter('.'+this.settings.activeClass));if(this.$current<0){this.$current=0;}var self=this;this.$items.addClass('sf-item').each(function(index,item){var trigger=$('<li><a href="#">'+parseInt(index+1,10)+'</a></li>');self.$controls.append(trigger);trigger.find('a').data('item',item).bind('click',{self:self},self.trigger);});this.activate(this.$current,true);if(this.settings.autorotate){this.autorotate();}};sfRotator.prototype.trigger=function(event){event.preventDefault();var self=event.data.self;self.stopAutorotate();self.$rotationTerminated=true;var position=self.$items.index($(this).data('item'));self.activate(position);};sfRotator.prototype.activate=function(position){var instant=arguments[1]||false;var activeClass=this.settings.activeClass;var oldItem=this.$items.eq(this.$current);var newItem=this.$items.eq(position);var onHide=function(){oldItem.removeClass(activeClass);};var onShow=function(){newItem.addClass(activeClass).css('zIndex',10);};var effects=['blind','bounce','clip','drop','explode','fold','highlight','puff','pulsate','scale','shake','size','slide','transfer'];if(instant===true){oldItem.removeClass(activeClass).hide();newItem.addClass(activeClass).show();}else{if($.inArray(this.settings.effectOut,effects)>-1){oldItem.hide(this.settings.effectOut,this.settings.optionsOut,this.settings.speedOut,onHide);}else if($.isFunction(oldItem[this.settings.effectOut])){oldItem[this.settings.effectOut](this.settings.speedOut,onHide);}else{throw"Unsupported hide transition";}newItem.css('zIndex',100);if($.inArray(this.settings.effectIn,effects)>-1){newItem.show(this.settings.effectIn,this.settings.optionsIn,this.settings.speedIn,onShow);}else if($.isFunction(newItem[this.settings.effectIn])){newItem[this.settings.effectIn](this.settings.speedIn,onShow);}else{throw"Unsupported show transition";}}this.$controls.find('a').removeClass('active').eq(position).addClass('active');this.$current=position;};sfRotator.prototype.autorotate=function(){this.$rotationTerminated=false;var self=this;this.$container.mouseenter(function(){self.stopAutorotate();});this.$container.mouseleave(function(){self.startAutorotate();});this.startAutorotate();};sfRotator.prototype.startAutorotate=function(){if(this.$rotationTerminated===true){return;}var self=this;this.$rotationInterval=window.setInterval(function(){var next=self.$current+1;if(next===self.$items.length){next=0;}self.activate(next);},this.settings.duration);};sfRotator.prototype.stopAutorotate=function(){if(this.settings.autorotate){window.clearInterval(this.$rotationInterval);}};$.fn.sfRotator=function(options){options=options||{};return this.each(function(){var r=new sfRotator(this,options);});};})(jQuery);

/*!
 * (v) Compact labels plugin
 * Takes one option: labelOpacity [default: true] set to false to disable label opacity change on empty input focus
 */
(function($){$.fn.compactize=function(options){var defaults={labelOpacity:true};options=$.extend(defaults,options);return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){if(options.labelOpacity){if(input.val()===''){label.css('opacity','0.5');}}else{label.hide();}});if(options.labelOpacity){input.keydown(function(){label.hide();label.css('opacity',1);});} input.blur(function(){if(input.val()===''){label.show();} if(options.labelOpacity){label.css('opacity',1);}});window.setTimeout(function(){label.toggle();},50);});};})(jQuery);

/*!
 * (v) 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);

/*!
 * (v) EqualHeights v1.11
 * Sets equal height for all passed elements (keeping padding in mind)
 */
(function($){$.fn.equalHeights=function(){$(window).resize($.proxy(function(){var tallest=0;$(this).css('min-height',0);$(this).each(function(){if($(this).outerHeight()>tallest){tallest=$(this).outerHeight();}});$(this).each(function(){var padding=$(this).outerHeight()-$(this).height();var height=tallest-padding;$(this).css({'min-height':height});if($.browser.msie&&parseInt($.browser.version,10)==6)$(this).height(height);});},this)).triggerHandler('resize');return this;};})(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);
					}
				});
			}
		},
		ui : {
			rotators : function() {
				
				// main banners
				if(jQuery("body.home").length == 1){
					$('div.rotator-a ul').sfRotator();
				}
				
				$('div.rotator-a').append('<span class="roundies lc">.</span><span class="roundies rc">.</span>');
				
				// news
				$('div.newsbar-a ul').each(function() {
					var $root 	= $(this);
					var time 	= 300;
					var cycle 	= 3000;

					newscyclehold = 0;
					
					newscycle = function() {
						if($root.find('li:animated').size() > 0) return false;
						
						if(newscyclehold > 0) {
							if(newscyclehold === 2) newscyclehold = 0;
							return false;
						}						
						
						var $current = $root.find('li:visible');
						var $next = $current.next('li:first');
						if($next.size() === 0) $next = $root.find('li:first');
						
						$current.fadeOut(time,function() {
							$next.fadeIn(time);
						});
					};
					
					$root.mouseenter(function() {
						newscyclehold = 1;
					}).mouseleave(function() {
						newscyclehold = 2;
					});
					
					var newscycleint = setInterval(function() {newscycle();},cycle);
					
				});
				
				//testimonials
				$('div.testimonials-a').each(function() {
					var time 	= 600;				// time of the fading transition
					var cycle	= 8000;				// break between auto cycle, set to 0 to turn off
					
					var $root 	= $(this);
					var count	= $root.find('div.testimonial-a').size();
					
					if(count > 1){
						$root.find('ul').append('<li class="next"><a href="#next">Next</a></li>');
						$root.find('ul li.next a').click(function(){
					        if($root.find('div.testimonial-a:animated').size() > 0) return false;
					        var $visible 	= $root.find('div.testimonial-a:visible');
				        	var $next = $visible.next('div.testimonial-a');
					        if($next.size() == 0) $next = $root.find('div.testimonial-a:first');

					        $visible.fadeOut(time, function(){
					        	$next.fadeIn(time);
					        });

					        return false;
						});

						if(cycle != 0){
					        testimonialRotate = setInterval(function() {
					        	$('div.testimonials-a ul li.next a').trigger('click');
					        }, cycle+time);
						}
					}
				});
				
			},
			labels : function() {
				$('div.combo-a label').compactize();
			}
		},
		design : {
			equalize : function() {
				$('div.triplets-a div.box').equalHeights();
			}
		},
		
		tweaks : {
			activeNav : function(){
					
					if(window.location.href.indexOf("/about-us/") != -1){
							jQuery("li#main-nav-about-us").addClass("selected");
					}
					
					if(window.location.href.indexOf("/consulting-services/") != -1){
							jQuery("li#main-nav-consulting-services").addClass("selected");
					}
					
					if(window.location.href.indexOf("/what-is-loyalty/") != -1){
							jQuery("li#main-nav-what-is-loyalty").addClass("selected");
					}
					
					if(window.location.href.indexOf("/our-clients/") != -1){
							jQuery("li#main-nav-out-clients").addClass("selected");
					}
					
					if(jQuery("div.bioImage").length > 0){
						// handle main nav item
						jQuery("li#main-nav-about-us").addClass("selected");	
						
						
						// dynamically handle bio webapps nav
						jQuery("div.sidenav-a ul li").each(function(){
							var name = jQuery("div#main h2:first").text();
							var text = jQuery(this).find("a").text();
						
							if(name == text){
								jQuery(this).addClass("selected");
							}
						});
						
					}
					
					
			},
			
			bio : function(){
				if(jQuery("div.bioImage").length > 0){
					jQuery("p.photo-a").html(jQuery("div.bioImage").html());	
				}
			}
			
		}// tweaks
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.ui.rotators();
	Engine.ui.labels();
	Engine.design.equalize();
	
	Engine.tweaks.activeNav();
	Engine.tweaks.bio();
	
});
