// var to turn off custom scrollbars
var ReplaceScrollbars = true;
var inScrollBox = false;
var bodyScrollTop = 0;

jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function(){
	// Preload menu images											 
	$.preloadImages("images/rimnetics-logo-over.jpg","images/btn_about-us-over.jpg","images/btn_structural-enclosures-over.jpg","images/btn_encapsulation-over.jpg","images/btn_engineering-over.jpg","images/btn_contact-over.jpg","images/btn_cd-request-over.jpg");

	// set menu button actions/effects
	var submenuTimeout;
	var submenuID;
	$(".menuBtn").each(function() {
		// automatically change to hover images on menu buttons
		$("#"+$(this).attr("id")).hover(function() {
			if ($(this).attr("src").indexOf("-over.jpg") == -1 && $(this).attr("src").indexOf("cd-request") > -1)
				$(this).attr("src",$(this).attr("src").replace(".jpg","-over.jpg"));
			clearTimeout(submenuTimeout);
			if ($("#"+$(this).attr("id").replace("btn","submenu")).css("display") == "none")
				$("#"+$(this).attr("id").replace("btn","submenu")).slideDown(200);
			submenuID = $(this).attr("id").replace("btn","submenu");
			$(".menuBtn").each(function() {
				if ($(this).attr("id") != submenuID.replace("submenu","btn") && $("#"+$(this).attr("id").replace("btn","submenu")).css("display") != "none")
					submenuFadeOut($(this).attr("id").replace("btn","submenu"));
			});
		},
		function(){
			clearTimeout(submenuTimeout);
			if ($(this).attr("src").indexOf("-over.jpg") != -1)
				$(this).attr("src",$(this).attr("src").replace("-over.jpg",".jpg"));
			if ($("#"+$(this).attr("id").replace("btn","submenu")).css("display") != "none")
				submenuTimeout = setTimeout("submenuFadeOut('"+$(this).attr("id").replace("btn","submenu")+"')",1000);
		});
	});

	// set submenu div actions/effects
	$(".subMenuDiv").each(function() {
		$(this).hover(function(){
			clearTimeout(submenuTimeout);
		},function(){
			if ($(this).css("display") != "none")
				submenuTimeout = setTimeout("submenuFadeOut('"+$(this).attr("id")+"')",1000);
		});
	});

	// highlight cuurent section
	highlightSection();

	/* COMMENTED - COULDN'T RESOLVE ISSUE WHERE BODY SCROLLS ALONG WITH BOX
		 CUSTOM BODY OVERFLOW FIX DOESN'T WORK IN FIREFOX
	// run function to add slider/integrated mousewheel support on all mousewheelslider divs
	if (!$.getUrlVar('noscroll')) {
		if (ReplaceScrollbars) {
			$(".mousewheelslider").each(function() {
				scrollThisDiv($(this).attr("id"));
				$(this).scrollTop(0);
			});
		}
	}
	*/

	/* Write flash text into title div */
	var so = new SWFObject("common/FlashTitle.swf?cacheBuster=000002", "FlashTitle", "600", "115", "9");
	var title = $("#FlashTitleDiv").html();
	title = title.replace("<!-- InstanceBeginEditable name=\"FlashTitle\" -->","");
	title = title.replace("<!-- InstanceEndEditable -->","");
	so.addVariable("text",title);
	so.addParam("menu","false");
	so.addParam("quality","best");
	so.addParam("scale","noscale");
	so.addParam("wmode","transparent");
	so.addParam("allowScriptAccess","always");
	so.addParam("swliveconnect","true");
	if (so.write("FlashTitleDiv")){
		$("#FlashTitleDiv").css({left:"0px",top:"0px"});
	}
	$("#FlashTitleDiv").css({display:"block"});

});

// used by menu code to custom fade out a specific id and highlight current section again
function submenuFadeOut(id) {
	$("#"+id).customFadeOut(200);
	highlightSection();
}

// highlights current section if a section is defined by adding/removing "-over".jpg
function highlightSection() {
	if (typeof(section)!="undefined") {
		if ($("#btn"+section).attr("src").indexOf("-over") == -1)
			$("#btn"+section).attr("src",$("#btn"+section).attr("src").replace(".jpg","-over.jpg"));
	}
}

// ScrollThisDiv
// Copyright 2009 Forest Walz
// requires ui.slider, dimensions
function scrollThisDiv(id) {
	var theScrollHeight = $("#"+id)[0].scrollHeight;
	var theActualHeight = $("#"+id).innerHeight();
	
	if (theScrollHeight > theActualHeight) {
		
		$("#"+id).css({overflow:"hidden"});
		$("#"+id).width($("#"+id).width()-15);
		$("#"+id).after("<div id='"+id+"Control'></div>");
		$("#"+id+"Control").css({
			position:"absolute",
			width:"10px",
			height:($("#"+id).height() - 40) +"px",
			left:($("#"+id).position().left + $("#"+id).width() + 25) + "px",
			top:($("#"+id).position().top + 20) + "px"
		});
		
		$("#"+id+"Control").slider({
			animate: true,
			orientation: 'vertical',
			min: 0,
			max: theScrollHeight,
			value: theScrollHeight,
			slide: function(event, ui) {
				scrollPositionScale = 1-ui.value/theScrollHeight;
				scrollDistance = theScrollHeight - theActualHeight;
				scrollPosition = scrollDistance*scrollPositionScale;
				$("#"+id).scrollTop(scrollPosition);
			}
		});
	
		$("#"+id+",#"+id+"Control").mousewheel(function(event, delta) {
			if (delta > 0) {
				if (delta > 7)
					delta = 7;
				$("#"+id).scrollTop($("#"+id).scrollTop()-18*delta);
			}
			else if (delta < 0) {
				if (delta < -7)
					delta = -7;
				$("#"+id).scrollTop($("#"+id).scrollTop()+18*(delta*-1));
			}
			scrollDistance = theScrollHeight - theActualHeight;
			scrollPosition = theScrollHeight * (1/scrollDistance) * $("#"+id).scrollTop();
			scrollPosition = (scrollPosition * -1) + theScrollHeight ;
			$("#"+id+"Control").slider("option","value",scrollPosition);
		});

	}
}

// allows addition on onload events without replacing the original
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else 
      window.onload = fnc;
  }
}

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});