var cf_interval = 5000;
var selected = 0;
var flip;

$(document).ready(function(){
	
	var navbar = $("#nav-left").offset().left;
	
	$("#navigation li ul").each(function(){
		
		var parent = $(this).parents('li').offset();
		var left = parent.left - navbar;
		$(this).css('left', left);
	
		if($("li span", this).length > 0){
			$(this).remove();
		}
	});

/*
	$("#navigation ul").superfish({
		
	});
*/
	
	//$("input").hint();
	
	initTables();
	initImages();
	initSiteMap();
	initGallery();
	initStickyFoot();
	
});

$(window).load(function(){

	initStickyFoot();
	startCrossfade();
});

function initStickyFoot(){

	var footerHeight = 0,
           footerTop = 0,
           $footer = $("#footer");

       positionFooter();

       function positionFooter() {

                footerHeight = $footer.outerHeight();
                footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";

               if ( ($(document.body).height()+footerHeight) < $(window).height()) {
                   $footer.css({
                        position: "absolute",
                        top: footerTop
                   })
               } else {
                   $footer.css({
                        position: "static"
                   })
               }

       }

       $(window)
               .resize(positionFooter)



}

function initGallery(){
	$("#gallery a.caseStudyImage").click(function(e){
		e.preventDefault();
		var largeImageSrc = $(this).attr('href');
		$(".discription-area p img").attr('src', largeImageSrc);
		//where #mainImage is the ID of the div holding the large image.
	});
}

function initSiteMap(){
	$(".sitemap_0").each(function(){
		$(this).before("<div style='clear: both'></div>");
	});
}

function initImages(){
	$("#center-bar img").each(function(){
		if($(this).attr('alt') != ""){
			var src = $(this).attr("src");
			var alt = $(this).attr("alt");
			var style = $(this).attr("style");
			var newHTML = "<div style='" + style + "' class='imageHolder'><img src='" + src + "' alt='" + alt + "' /><p>" + alt + "</p></div>";
			$(this).before(newHTML).remove();
		}
	});
}

function initCrossfade(){
	
	$("#mygallery div.panel div").each(function(i){
		if(i > 0){
			$(this).css('opacity', 0).css('display', 'block');
			$(this).removeClass('atStart');
		} else {
			$(this).addClass('active');
			$(this).removeClass('atStart');
		}
		
	});
	
}

function startCrossFade(){
	flip = setInterval("crossfade()", cf_interval);
}

function crossfade(){
	var images = $("#mygallery div.panel div");
	var total = images.length;
	
	if(selected + 1 == total){
		nextIndex = 0;
	} else {
		nextIndex = selected + 1;
	}
	
	$("#mygallery div.panel div.active").animate({
		opacity: 0
	}, 'slow', 'swing', function(){
		$(this).css('left', '-10000px');
	});
	
	$("#mygallery div.panel div").removeClass('active');
	var nextImage = $("#banner div.panel div:eq(" + nextIndex + ")");
	
	$(nextImage).css('left', '0px');
	
	$(nextImage).animate({
		opacity: 1
	}, 'slow').addClass('active');
	
	selected = nextIndex;
	
}

function initEqualHeights(selector){
	var maxHeight = 0;
	$(selector).each(function(){
		if($(this).height() > maxHeight){
			maxHeight = $(this).height();
		}
	});
	
	$(selector).height(maxHeight);
}

function initFAQ(){
	$(".faq .question").click(function(e){
		e.preventDefault();
		$(".faq .answer").hide();
		$(this).parents(".faq").find(".answer").show();
	});
	
	$(".question:eq(0)").trigger('click');
}

function initTables(){
	
	$("#body_fundamentals table").each(function(i){
	
		$(this).attr('cellpadding', '0');
		$(this).attr('cellspacing', '0');
	
		$("tr:eq(0)").addClass('table-top');
		$("tr", this).each(function(){
			$("td:eq(0)", this).addClass('company-light');
		});
		
		if(i % 2){
			$(this).after("<div style='clear: both'></div>");
			$(this).addClass('secondTable');
		}
		
	});
	
}

/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }
    
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),
    
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title
      
      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);

/*
 * Superfish v1.4.8 - jQuery menu widget
 * Copyright (c) 2008 Joel Birch
 *
 * Dual licensed under the MIT and GPL licenses:
 * 	http://www.opensource.org/licenses/mit-license.php
 * 	http://www.gnu.org/licenses/gpl.html
 *
 * CHANGELOG: http://users.tpg.com.au/j_birch/plugins/superfish/changelog.txt
 */

;(function($){
	$.fn.superfish = function(op){

		var sf = $.fn.superfish,
			c = sf.c,
			$arrow = $(['<span class="',c.arrowClass,'"> &#187;</span>'].join('')),
			over = function(){
				var $$ = $(this), menu = getMenu($$);
				clearTimeout(menu.sfTimer);
				$$.showSuperfishUl().siblings().hideSuperfishUl();
			},
			out = function(){
				var $$ = $(this), menu = getMenu($$), o = sf.op;
				clearTimeout(menu.sfTimer);
				menu.sfTimer=setTimeout(function(){
					o.retainPath=($.inArray($$[0],o.$path)>-1);
					$$.hideSuperfishUl();
					if (o.$path.length && $$.parents(['li.',o.hoverClass].join('')).length<1){over.call(o.$path);}
				},o.delay);	
			},
			getMenu = function($menu){
				var menu = $menu.parents(['ul.',c.menuClass,':first'].join(''))[0];
				sf.op = sf.o[menu.serial];
				return menu;
			},
			addArrow = function($a){ $a.addClass(c.anchorClass).append($arrow.clone()); };
			
		return this.each(function() {
			var s = this.serial = sf.o.length;
			var o = $.extend({},sf.defaults,op);
			o.$path = $('li.'+o.pathClass,this).slice(0,o.pathLevels).each(function(){
				$(this).addClass([o.hoverClass,c.bcClass].join(' '))
					.filter('li:has(ul)').removeClass(o.pathClass);
			});
			sf.o[s] = sf.op = o;
			
			$('li:has(ul)',this)[($.fn.hoverIntent && !o.disableHI) ? 'hoverIntent' : 'hover'](over,out).each(function() {
				if (o.autoArrows) addArrow( $('>a:first-child',this) );
			})
			.not('.'+c.bcClass)
				.hideSuperfishUl();
			
			var $a = $('a',this);
			$a.each(function(i){
				var $li = $a.eq(i).parents('li');
				$a.eq(i).focus(function(){over.call($li);}).blur(function(){out.call($li);});
			});
			o.onInit.call(this);
			
		}).each(function() {
			var menuClasses = [c.menuClass];
			if (sf.op.dropShadows  && !($.browser.msie && $.browser.version < 7)) menuClasses.push(c.shadowClass);
			$(this).addClass(menuClasses.join(' '));
		});
	};

	var sf = $.fn.superfish;
	sf.o = [];
	sf.op = {};
	sf.IE7fix = function(){
		var o = sf.op;
		if ($.browser.msie && $.browser.version > 6 && o.dropShadows && o.animation.opacity!=undefined)
			this.toggleClass(sf.c.shadowClass+'-off');
		};
	sf.c = {
		bcClass     : 'sf-breadcrumb',
		menuClass   : 'sf-js-enabled',
		anchorClass : 'sf-with-ul',
		arrowClass  : 'sf-sub-indicator',
		shadowClass : 'sf-shadow'
	};
	sf.defaults = {
		hoverClass	: 'sfHover',
		pathClass	: 'overideThisToUse',
		pathLevels	: 1,
		delay		: 800,
		animation	: {opacity:'show'},
		speed		: 'normal',
		autoArrows	: false,
		dropShadows : false,
		disableHI	: false,		// true disables hoverIntent detection
		onInit		: function(){}, // callback functions
		onBeforeShow: function(){},
		onShow		: function(){},
		onHide		: function(){}
	};
	$.fn.extend({
		hideSuperfishUl : function(){
			var o = sf.op,
				not = (o.retainPath===true) ? o.$path : '';
			o.retainPath = false;
			var $ul = $(['li.',o.hoverClass].join(''),this).add(this).not(not).removeClass(o.hoverClass)
					.find('>ul').hide().css('visibility','hidden');
			o.onHide.call($ul);
			return this;
		},
		showSuperfishUl : function(){
			var o = sf.op,
				sh = sf.c.shadowClass+'-off',
				$ul = this.addClass(o.hoverClass)
					.find('>ul:hidden').css('visibility','visible');
			sf.IE7fix.call($ul);
			o.onBeforeShow.call($ul);
			$ul.animate(o.animation,o.speed,function(){ sf.IE7fix.call($ul); o.onShow.call($ul); });
			return this;
		}
	});

})(jQuery);
