/* stop IE 5/6 flicker */
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

/* on document load */
$(document).ready(function() {
	
	// turn any main navigation image with an "active" class to its on-state
	$('#navigation').find('.active img').each(function(i) {
		var suffix = this.src.substring(this.src.lastIndexOf('.')); // .gif, .jpg etc..
		this.src = this.src.substring(0,this.src.lastIndexOf('.')) + "_on" + suffix; // bla.gif -> bla_on.gif
	});
	
	// animate and display our blue box login form
	$(".blue_box_login").click(function() {
		$(".blue_login_box").slideDown('slow');
		return false;
	});
	$(".blue_box_login_close").click(function() {
		$(".blue_login_box").slideUp('slow');
		return false;
	});
	
	// animate and display our blue box login form
	$(".blue_box_login2").click(function() {
		$(".blue_login_box2").slideDown('slow');
		return false;
	});
	$(".blue_box_login_close2").click(function() {
		$(".blue_login_box2").slideUp('slow');
		return false;
	});
	
	/* if the left column is larger than right column */
	if ($("#home #left_column").height() > $("#home #right_column").height()) {
		var leftColumn = $("#home #left_column");
		var rightColumn = $("#home #right_column");
		var leftBox = $("#right_column .dual_box_wrapper .left_box");
		var leftBoxFormElementBox = $("#right_column .dual_box_wrapper .left_box .form_element_box:last-child");
		var rightBox = $("#right_column .dual_box_wrapper .right_box");
		var rightBoxWBar = $("#right_column .dual_box_wrapper .right_box .box_with_bar:last-child");
		var difference1 = leftColumn.height() - rightColumn.height(); // diff between left and right column
		// expand the height of .left_box and .right_box inside .dual_box_wrapper
		if (leftBox.height() > rightBox.height()) {
			var difference2 = leftBox.height() - rightBox.height(); // diff between left and right box
			if ($.browser.msie) { // if ie 5/6/7
				rightBoxWBar.height(rightBoxWBar.height() + difference1 + difference2 + "px"); // increase height of right box
			} else {
				rightBoxWBar.height(rightBoxWBar.height() + difference1 + difference2 + 13 + "px"); // increase height of right box
			};
			leftBoxFormElementBox.height(leftBoxFormElementBox.height() + difference1 + "px"); // increase height of left box			
		} else {
			var difference3 = rightBox.height() - leftBox.height(); // diff between left and right box
			if ($.IE6Below()) { // if ie 5/6
				leftBoxFormElementBox.height(leftBoxFormElementBox.height() + difference1 + difference3 + "px"); // increase height of left box
			} else {
				leftBoxFormElementBox.height(leftBoxFormElementBox.height() + difference1 + difference3 + 5 + "px"); // increase height of left box
			};
			rightBoxWBar.height(rightBoxWBar.height() + difference1 + "px"); // increase height of right box
		}
	} else {
		var leftColumn = $("#home #left_column");
		var rightColumn = $("#home #right_column");
		var leftBox = $("#right_column .dual_box_wrapper .left_box");
		var leftBoxFormElementBox = $("#right_column .dual_box_wrapper .left_box .form_element_box:last-child");
		var rightBox = $("#right_column .dual_box_wrapper .right_box");
		var rightBoxWBar = $("#right_column .dual_box_wrapper .right_box .box_with_bar:last-child");
		var leftBoxWBar = $("#left_column .box_with_bar:last-child");
		var difference1 = rightColumn.height() - leftColumn.height(); // diff between left and right column
		// expand the height of .left_box and .right_box inside .dual_box_wrapper
		if (leftBox.height() > rightBox.height()) {
			var difference2 = leftBox.height() - rightBox.height(); // diff between left and right box
			rightBoxWBar.height(rightBoxWBar.height() + difference2 + "px"); // increase height of right box
			leftBoxFormElementBox.height(leftBoxFormElementBox.height() + "px"); // increase height of left box			
		} else {
			var difference3 = rightBox.height() - leftBox.height(); // diff between left and right box
			if ($.IE6Below()) { // if ie 5/6
				leftBoxFormElementBox.height(leftBoxFormElementBox.height() + difference3 + 13 + "px"); // increase height of left box
			} else {
				leftBoxFormElementBox.height(leftBoxFormElementBox.height() + difference3 + 18 + "px"); // increase height of left box
			};
		};
		// expand the height of the last .box_with_bar inside #left_column
		if ($.browser.msie) {
			leftBoxWBar.height(leftBoxWBar.height() + difference1 + "px"); // increase height of left column box with bar
		} else {
			leftBoxWBar.height(leftBoxWBar.height() + difference1 + 13 + "px"); // increase height of left column box with bar
		};
	};
	
	
	/* add default text in form fields based on title attribute
	   remove default text in form fields on focus */
	$("input[@type='text'],input[@type='password']").each(function(i) {
		// if a title exists, we assume this should be set as the default text
		if (this.getAttribute('title')!=null) {
			if (this.getAttribute('value')=='') {
				this.setAttribute('value', this.getAttribute('title')); // set the default value to the title string
			};
			$(this).focus(function () {
				if (this.value == this.title) this.value = '';
				this.style.color = '#000000';
			});
			$(this).blur(function () {
				if (this.value == '') this.value = this.title;
				this.style.color = '#666666';
			});
		};		
	});
	
	
	
}); // ...end of ondocumentload.






// Return true if IE is 6 or less
$.IE6Below = function() {
	if ($.browser.msie) {
		try {
			if (ScriptEngineMajorVersion() <= 5 && ScriptEngineMinorVersion() < 7) { // ie 6- only
				return true;
			}
		} catch(err) { return false; };
		return false;
	} else {
		return false;
	};
}


/* domain constants */
var at = "\x40";
var velocityelec = "velocityelec";
var dotcom = ".com";

/* obfuscate email addresses */
function show_email_addr(username)
{
	document.write("<a href=\"mailto:" + username + at + velocityelec + dotcom + "\">" + username + at + velocityelec + dotcom + "</a>");
}

function show_email_addr2(username, style)
{
	document.write("<a href=\"mailto:" + username + at + velocityelec + dotcom + "\" class=\"" + style + "\">" + username + at + velocityelec + dotcom + "</a>");
}

function show_email_name(username, name)
{
	document.write("<a href=\"mailto:" + username + at + velocityelec + dotcom + "\">" + name + "</a>");
}

function show_email_name2(username, name, style)
{
	document.write("<a href=\"mailto:" + username + at + velocityelec + dotcom + "\" class=\"" + style + "\">" + name + "</a>");
}


/* rotating images */
var initialized = false;
var current_image_number = 0;
var rotating_images = new Array();
var initial_rotation_delay = 2000; // lower this if you want the first image to fade very soon
var rotation_delay = 4000; // normal delay between image rotations
var fade_speed = 2000; // milliseconds e.g. 1000 or words e.g. "slow"

function rotate_images() {
	// if we haven't initialized let's do that now
	if (! initialized)
	{
		// use jquery to find any image with a class of "rotating_image" and add them to our array
		$(".rotating_image").each(function(i) {
			rotating_images[i] = this;
		});
		// randomize image order (comment this section out if not needed)
		function randomizeArray(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;
		   }
		}
		randomizeArray(rotating_images);
		$(rotating_images[0]).addClass('first_rotating_image');
		initialized = true;
		setTimeout("rotate_images()", initial_rotation_delay);
		return;
	}
	// if the first image doesn't exist, just exit
	if (! rotating_images[0]) return;
	// if the first image has downloaded, continue
	if (rotating_images[0].complete || rotating_images[0].complete == undefined)
	{
		// set the next image we should load
		var next_image_number = current_image_number + 1;
		if (next_image_number >= rotating_images.length) next_image_number = 0;
		// has this image been loaded
		if (rotating_images[next_image_number].complete || rotating_images[next_image_number].complete == undefined)
		{
			// ok, switch to this new image			
			$(rotating_images[current_image_number]).fadeOut(fade_speed);
			$(rotating_images[next_image_number]).fadeIn(fade_speed);
			// change the current image to this one
			current_image_number = next_image_number;
		}
		setTimeout("rotate_images()", rotation_delay);		
	}
	else
	{
		// not downloaded yet, check back later
		setTimeout("rotate_images()", initial_rotation_delay);
	}
}


/* tabbed content */
$.fn.tabs = function(options) {
    // basic stuff
    var ON_CLASS = 'show';
    var OFF_CLASS = 'hidden';
    // options
    var on = options && options.on && (typeof options.on == 'number' && options.on > 0) ? options.on : 1;
    return this.each(function() {
        $(this).find('>div').not(':eq(' + on + ')').addClass(OFF_CLASS);
        $(this).find('.switch_navigation').removeClass(OFF_CLASS);
        $(this).find('.switch_navigation a:eq(' + parseInt(on-1) + ')').addClass(ON_CLASS);
        var container = this;
        $(this).find('.switch_navigation a').click(function() {
            if (!$(this).is('.' + ON_CLASS)) {
                var re = /([_\-\w]+$)/i;
                var target = $('#' + re.exec(this.href)[1]);
                if (target.size() > 0) {
                    $(container).find('>div:visible').addClass(OFF_CLASS);
										$(container).find('.switch_navigation').addClass(ON_CLASS);
                    target.removeClass(OFF_CLASS);
                    $(container).find('.switch_navigation a').removeClass(ON_CLASS);
                    $(this).addClass(ON_CLASS);
                } else {
                    //alert('There is no such container.');
                }
            }
            return false;
        });
    });
};