

/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE TOGGLE
----------------------------------------------------------------- */
jQuery.fn.slideToggle = function(speed, easing, callback) {
	return this.animate({width: 'toggle',opacity: 'toggle'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE IN
----------------------------------------------------------------- */
jQuery.fn.slideIn = function(speed, easing, callback) {
	return this.animate({width: 'show',opacity: 'show'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- SLIDE OUT
----------------------------------------------------------------- */
jQuery.fn.slideOut = function(speed, easing, callback) {
	return this.animate({width: 'hide',opacity: 'hide'}, speed, easing, callback);  
};
/* -----------------------------------------------------------------
	JQUERY CUSTOM FUNCTION -- ANIMATING BACKGROUNDS
----------------------------------------------------------------- */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);
/* -----------------------------------------------------------------
	JQUERY GERENRL FUNCTIONS
----------------------------------------------------------------- */
jQuery(document).ready(function() {
	
	jQuery(document).pngFix();
    
	// LAST CHILD : ADD FIRST / LAST CLASSES
	jQuery("li:last-child").addClass("last");
    jQuery("li:first-child").addClass("first");
	
	// ODD CHILD : ADD ODD CLASS
	jQuery("ul li:odd").addClass("odd");
	// EVEN CHILD : ADD EVEN CLASS
	jQuery("ul li:even").addClass("even");
	
	// HOME BANNER
	if(jQuery("#banner ul").children().length>1){setInterval("HomeBanner()", 5000)};
	jQuery("#header #banner ul li").css("display", "none");
	jQuery("#header #banner ul li:first-child").css("display", "block");
	
	/* Enable product thumbs */
	jQuery('a.thumb').each(function(i) {
		jQuery(this).bind('click', function() {
			var id = jQuery(this).attr('id').split('-');
			id = id[id.length-1];
			
			if((id - 0) == id && id.length > 0) {
				id = '-'+id;
			} else {
				id = '';
			}

			var src = jQuery('img', this).attr('src').replace(/-sm/, '-med');
			var href = jQuery(this).attr('href');
			var alt = jQuery('img', this).attr('alt');
			var title = jQuery(this).attr('title');

			jQuery('#img-src'+id).attr('src', src);
			jQuery('#img-src'+id).attr('alt', alt);
			jQuery('#img-href'+id).attr('href', href);
			jQuery('#img-href'+id).attr('title', title);

			return false;
		});
	});

	/* set HOVER class */
	jQuery("li").hover(function () { 
		jQuery(this).addClass('hover');
	}, function () {
		jQuery(this).removeClass('hover');
	});
	
	/* set NAVIGATION hover */
	jQuery('#navigation ul.level-0 > li > a').css({
		backgroundPosition: "100% -109px"
	});
	jQuery('#navigation ul.level-0 > li')
	.mouseenter(function(){
		jQuery(this).children('a').stop().animate({backgroundPosition:"(100% -153px)"}, {duration:300});
	})
	.mouseleave(function(){
		jQuery(this).children('a').stop().animate({backgroundPosition:"(100% -109px)"}, {duration:300});
	});
	
	jQuery('.shiploader').cycle({delay:  2000, speed:  1000}); 
	
	if(jQuery("#used_equipment").length){setTimeout(animUsedEquipment,2500);};
	if(jQuery("#canceled_projects").length){setTimeout(animCancelledProjects,5000);};
	
	//add round corners to all the descriptions on the featured projects on the homepage
	$(".project_display").find(".display .description").each(function(){
		if(jQuery(this).find(".bottom_right").length==0){
			jQuery(this).wrapInner("<div class=\"bottom_right\"><div class=\"bottom_left\"><div class=\"top_right\"><div class=\"top_left\"><div class=\"wrapper\"></div></div></div></div></div>");
		}
	});
	
	//configure events for clicking on the tabs on the featured projects on the homepage
	$(".project_display").find(".navigation a").click(function(e){
		e.preventDefault();
		//move the right side view
		var $d=$(this).closest(".project_display").children(".display");
		$d.scrollTo($d.children()[$(this).parent().index()],300);
		//set the active class to this element
		$(this).closest("ul").find(".active").removeClass("active");
		$(this).addClass("active");
		//cancel the animation
		if($(this).closest(".project_display").attr('id')=="used_equipment"){doAnimUsedEquipment=false;};
		if($(this).closest(".project_display").attr('id')=="canceled_projects"){doAnimCancelledProjects=false;};
	});
	
	//pause the animation when hovering over the right side
	$(".project_display").find(".display").hover(function(){
		pauseAnim=true;
	},function(){
		pauseAnim=false;
	});
	
});


/* -----------------------------------------------------------------
	HOME BANNER FUNCTION
----------------------------------------------------------------- */
var bannerIndex=1;
function HomeBanner() {
	jQuery("#banner ul li:nth-child("+bannerIndex+")").fadeOut(1500);
	bannerIndex++;
	if(bannerIndex>$("#banner ul").children().length){bannerIndex=1;}
	jQuery("#banner ul li:nth-child("+bannerIndex+")").fadeIn(1500);
};
homeBanner = new HomeBanner();
/* -----------------------------------------------------------------
	SHIPLOADER BANNER
----------------------------------------------------------------- */
var doAnimUsedEquipment = true;
var doAnimCancelledProjects = true;
var pauseAnim=false;

function animUsedEquipment(){
	if(doAnimUsedEquipment){
		if(!pauseAnim){incrTab("#used_equipment");};
		setTimeout(animUsedEquipment,5000);
	}
}
function animCancelledProjects(){
	if(doAnimCancelledProjects){
		if(!pauseAnim){incrTab("#canceled_projects");};
		setTimeout(animCancelledProjects,5000);
	}
}


//generic function for the tabshow_header class
function setBigTab(index){
	switch (index){
		case 0:
			jQuery("#canceled_projects").hide();
			jQuery("#project_map").hide();
			jQuery("#used_equipment").show();
		break;
		case 1:
			jQuery("#used_equipment").hide();
			jQuery("#project_map").hide();
			jQuery("#canceled_projects").show();
		break;
		case 2:
			jQuery("#used_equipment").hide();
			jQuery("#canceled_projects").hide();
			jQuery("#project_map").show();
		break;
	}
	jQuery("#tabshow_header").find(".active").removeClass("active");
	jQuery(jQuery("#tabshow_header").children()[index]).children("a").addClass("active");
	$(window).load();
}
function incrTab(selector){
	//figure out which tab to change to
	var current=$(selector).find(".navigation a.active").parent().index();
	var count=$(selector).find(".navigation a.active").parent().parent().children().length;
	var next=current+1;
	if(next>=count){next=0;};
	var $d=$(selector).children(".display");
	//scroll the right side
	$d.scrollTo($d.children()[next],300);
	
	//set the current tab to active
	$(selector).find(".active").removeClass("active");
	var $newtab=$($(selector).children(".navigation").children()[next]).children("a").addClass("active");
	
	//check to see if the view needs to move before scrolling the left side
	if(($newtab.position().top+$newtab.outerHeight()>$(selector).children(".navigation").scrollTop()+$(selector).children(".navigation").height()+$(selector).children(".navigation").position().top)||($newtab.position().top<$(selector).children(".navigation").scrollTop()+$(selector).children(".navigation").position().top)){
		$(selector).children(".navigation").scrollTo($newtab,300);
	}
	
}

//setTab=deprecated old version of function from before the current homepage
//box with a scroller
//generic function for the tabshow class
//set the index to '++' for an incrementer!
function setTab(selector,index){


	var tabshow=$(selector).children();
	var home_index=0;
	var found=false;
	//figure out the previously active item
	tabshow.each(function(){
		if($(this).hasClass('active')){
			found=true;
		}
		if(found==false){
			home_index++;
		}
		
	});
	//increment the index if thats what's up
	if(index=="++"){
		index=home_index+1;
		if(index>=tabshow.length){
			index=0;
		}
	}else{
		//stop the animation if somebody clicks!
		if(selector=="#used_equipment"){doAnimUsedEquipment=false;}
		if(selector=="#canceled_projects"){doAnimCancelledProjects=false;}
	}

	if(index!=home_index){
		//flatten all the z's
		tabshow.each(function(){
			$(this).children(".content").css("z-index","1");
		});
		//set the previouse z
		$(tabshow[home_index]).removeClass('active').children(".content").css("z-index","15");
		home_index=index;
		//set the new z
		$(tabshow[home_index]).addClass("active").children(".content").fadeTo(0,0).css("z-index","30").fadeTo(500,1);
	}
}

