$(document).ready(function() {
	setBackgroundText("#header_username", "Username");
	setBackgroundText("#header_password", "Password");
	setBackgroundText("#header_search", "Enter your search here...");
	
	$(".login-element").bind("keypress", submitLoginForm);
	$("a.login-element").bind("click", submitLoginForm);
	$(".search-element").bind("keypress", submitSearchForm);
	$("a.search-element").bind("click", submitSearchForm);
	
	MM_preloadImages('images/submit_ro.jpg','images/login_ro.jpg','images/search_ro.jpg','images/icons/company_ro.jpg','images/icons/solutions_ro.jpg','images/icons/industry_ro.jpg','images/icons/services_ro.jpg','images/icons/media_ro.jpg','images/icons/support_ro.jpg','images/icons/contact_ro.jpg','images/icons/login_ro.jpg');

	var installLink = $("#mxclient_installer");
	installLink.data('_href', installLink.attr('href')).removeAttr('href');
	$("input[name=accept_terms]").click(function() {
		if (this.checked) {
			installLink.attr('href', installLink.data('_href'));
		} else {
			installLink.removeAttr('href');
		}
	});
});

var ENTER_CHAR = 13;

function submitLoginForm(e) {
	if (e.type == 'keypress' && e.which != ENTER_CHAR)
		return;
	return submitJSForm({
					 url: 'login',
					 method: 'post',
					 elements: $("input.login-element")
				 });
}

function submitSearchForm(e) {
	if (e.type == 'keypress' && e.which != ENTER_CHAR)
		return;
	return submitJSForm({
					 url: 'search',
					 method: 'get',
					 elements: $("input.search-element")
				 });
}

function submitJSForm(opts) {
	var url = opts.url || '',
		method = opts.method.toLowerCase() == 'get' ? 'get' : 'post',
		elements = opts.elements || $();
	
	var form = $('<form action="'+url+'" method="'+method+'"></form>');
	elements.each(function(index, element) {
		element = $(element);
		var value = element.val();
		if (typeof value != 'undefined') {
			$('<input type="hidden" name="'+$(element).attr('name')+'" value="'+value+'" />').appendTo(form);
		}
	});
	form.appendTo($("body")).submit();
	return false;
}

function setBackgroundText(input, defaultText) {
	if ($(input).val() == "") {
		$(input).val(defaultText);
	}
	$(input).focus(function() {
		if (this.value == defaultText)
			this.value = "";
	});
	$(input).blur(function() {
		if (this.value == "")
			this.value = defaultText;
	});
}

function shuffle(array) {
	var length = array.length;
	for(var i = length - 1; i > 0; i--) {
	    var j = Math.floor(Math.random() * (i + 1));
	    var tmp = array[i];
	    array[i] = array[j];
	    array[j] = tmp;
	}
	return array;
}

function rotateLogos(logos, container) {
	if (logos.length == 0)
		return;

	logos = shuffle(logos);

	var images = [];
	var max_image_height = 0;
	// Preload all images and append them to the container.
	for (var i = 0; i < logos.length; i++) {
		var image = $("<div />")
			.attr({ "class": "image_rotate_container" })
			.append($("<img />")
				.attr({src: logos[i]})
			)
			.appendTo(container);
		images.push(image);

		if (image.height() > max_image_height) {
			max_image_height = image.height();
		}
	}

	var images_per_slide = 1;//Math.floor($(container).height() / (max_image_height + 10));

	i = 0;
	var count = 0;
	(function displayLogos() {
		window.setTimeout(function() {
			var speed = 1000;

			// Fade out all the images currently showing.
			for (var j = 1; j < images_per_slide; j++) {
				images[prev(i - j)].fadeOut(speed);
			}

			// Fade in all the images for the next slide
			images[prev(i)].fadeOut(speed, function() {
				for (var j = 0; j < images_per_slide; j++) {
					images[i].fadeIn(speed);
					i = next(i);
				}
			});

			// Call the function again to set the timer for the next slide.
			displayLogos();
		}, count++ ? 6000 : 0);
	})();

	// prev() and next() allow us to treat the images array as circular.
	function prev(i) {
		i--;
		while (i < 0) {
			i += images.length;
		}
		return i;
	}
	function next(i) {
		return (i + 1) % images.length;
	}
}

// Dreamveaver functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document;if(d.images){if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){d.MM_p[j]=new Image;d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;if(!d) d=document;if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n];for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;document.MM_sr=new Array;for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;if(!x.oSrc) x.oSrc=x.src;x.src=a[i+2];}
}

function getSTMMenu() {
stm_bm(["menu2fbb",900,"","images/blank.gif",0,"","",0,0,0,0,50,1,0,0,"","",0,0,1,1,"default","hand","",1,25],this);
stm_bp("p0",[0,4,0,0,1,4,0,10,100,"progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=1,motion=forward,enabled=0,Duration=0.60)",5,"progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=1,motion=reverse,enabled=0,Duration=0.60)",4,50,0,0,"#FFCC00","transparent","",3,1,1,"#CCCCCC"]);
stm_ai("p0i0",[0,"Company","","",-1,-1,0,"company","_self","","","","",0,0,0,"images/060420arrow2.gif","images/060420arrow2.gif",10,5,0,0,1,"#333333",0,"#333333",0,"","",3,3,1,1,"#CCCCCC","#999999","#FFFFFF","#FFFFFF","bold 8pt Verdana","bold 8pt Verdana",0,0,"","","","",0,0,0],120,20);
stm_bp("p1",[1,4,0,0,1,4,0,10,100,"progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=1,motion=forward,enabled=0,Duration=0.35)",5,"progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=1,motion=reverse,enabled=0,Duration=0.35)",4,75,0,0,"#FFCC00","transparent","",3,0,0,"#000000"]);
stm_aix("p1i0","p0i0",[0,"About EDI Gateway","","",-1,-1,0,"about-edi-gateway","_self","","","","",0,0,0,"","",0,0,0,0,1,"#333333",0,"#e57700",0,"","",3,3,1,1,"#CCCCCC","#CCCCCC","#FFFFFF","#FFFFFF","bold 7pt Verdana","bold 7pt Verdana"],0,20);
stm_aix("p1i1","p1i0",[0,"Management","","",-1,-1,0,"management","_self","","","","",0,0,0,"images/060420arrow2.gif","images/060420arrow2.gif",10,5],0,20);
stm_bpx("p2","p1",[1,2,0,0,1,4,0,0,100,"progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=0,motion=forward,enabled=0,Duration=0.30)",6,"progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=0,motion=reverse,enabled=0,Duration=0.30)",7,80]);
stm_aix("p2i0","p1i0",[0,"Investor Relation","","",-1,-1,0,"investor-relation"]);
stm_ep();
stm_aix("p1i2","p1i1",[0,"Career","","",-1,-1,0,"career"],0,20);
stm_bpx("p3","p2",[]);
stm_aix("p3i0","p1i0",[0,"Advantages","","",-1,-1,0,"advantages"]);
stm_aix("p3i1","p1i0",[0,"Job List","","",-1,-1,0,"joblist"]);
stm_aix("p3i2","p1i0",[0,"Job Description","","",-1,-1,0,"job-description"]);
stm_aix("p3i3","p1i0",[0,"Form","","",-1,-1,0,"form"]);
stm_ep();
stm_aix("p1i3","p1i1",[0,"Policy","","",-1,-1,0,"policy"],0,20);
stm_bpx("p4","p2",[]);
stm_aix("p4i0","p1i0",[0,"Partnership","","",-1,-1,0,"partnership"]);
stm_aix("p4i1","p1i0",[0,"Benefits / Application","","",-1,-1,0,"benefits-application"]);
stm_ep();
stm_aix("p1i4","p1i1",[0,"Retailers","","",-1,-1,0,"retailers"],0,20);
stm_bpx("p5","p2",[]);
stm_aix("p5i0","p1i0",[0,"Trading Partners","","",-1,-1,0,"trading-partners"]);
stm_aix("p5i1","p1i0",[0,"Partner Techno","","",-1,-1,0,"partner-techno"]);
stm_ep();
stm_aix("p1i5","p1i1",[0,"Associations","","",-1,-1,0,"associations"],0,20);
stm_bpx("p6","p2",[]);
stm_aix("p6i0","p1i0",[0,"Referral Program","","",-1,-1,0,"referral-program"]);
stm_ep();
stm_aix("p1i6","p1i0",[0,"Directory / List","","",-1,-1,0,"directory-list"],0,20);
stm_aix("p1i7","p1i0",[0,"Testimonials","","",-1,-1,0,"testimonials"],0,20);
stm_ep();
stm_aix("p0i1","p0i0",[0,"Solutions / Tools","","",-1,-1,0,"solutions-tools"],120,20);
stm_bpx("p7","p1",[]);
stm_aix("p7i0","p1i1",[0,"Description","","",-1,-1,0,"description"]);
stm_bpx("p8","p2",[1,2,0,0,1,4,0,0,100,"progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=0,motion=forward,enabled=0,Duration=0.30)",6,"progid:DXImageTransform.Microsoft.Wipe(GradientSize=1.0,wipeStyle=0,motion=forward,enabled=0,Duration=0.30)",6]);
stm_aix("p8i0","p1i0",[0,"By Business Size","","",-1,-1,0,"business-size"]);
stm_ep();
stm_aix("p7i1","p1i0",[0,"By Product","","",-1,-1,0,"product"]);
stm_aix("p7i2","p1i0",[0,"Case Studies","","",-1,-1,0,"case-studies"]);
stm_aix("p7i3","p1i0",[0,"White Page","","",-1,-1,0,"white-page"]);
stm_aix("p7i4","p1i1",[0,"Demo","","",-1,-1,0,"demo"]);
stm_bpx("p9","p8",[]);
stm_aix("p9i0","p1i0",[0,"Quick Setup (1)","","",-1,-1,0,"quick-setup"]);
stm_aix("p9i1","p3i3",[]);
stm_ep();
stm_aix("p7i5","p1i0",[0,"Download Information","","",-1,-1,0,"download-information"]);
stm_aix("p7i6","p1i0",[0,"Brochure Request","","",-1,-1,0,"brochure-request"]);
stm_ep();
stm_aix("p0i2","p0i0",[0,"Industry","","",-1,-1,0,"industry"],120,20);
stm_bpx("p10","p1",[1,4,0,0,1,4,0,0]);
stm_aix("p10i0","p1i0",[0,"EDI for Retailers","","",-1,-1,0,"edi-retailers"]);
stm_aix("p10i1","p1i0",[0,"EDI for Food Vendors","","",-1,-1,0,"edi-food-vendors"]);
stm_aix("p10i2","p1i0",[0,"EDI for Suppliers","","",-1,-1,0,"edi-suppliers"]);
stm_aix("p10i3","p1i0",[0,"EDI for Manufacturers","","",-1,-1,0,"edi-manufacturers"]);
stm_ep();
stm_aix("p0i3","p0i0",[0,"Services","","",-1,-1,0,"services"],120,20);
stm_bpx("p11","p10",[]);
stm_aix("p11i0","p1i0",[0,"Professional Services","","",-1,-1,0,"professional-services"]);
stm_aix("p11i1","p1i0",[0,"Outsourcing","","",-1,-1,0,"outsourcing"]);
stm_aix("p11i2","p1i0",[0,"Training","","",-1,-1,0,"training"]);
stm_aix("p11i3","p1i0",[0,"Integration","","",-1,-1,0,"integration"]);
stm_aix("p11i4","p1i0",[0,"Rollouts","","",-1,-1,0,"rollouts"]);
stm_aix("p11i5","p1i0",[0,"Register","","",-1,-1,0,"register-form"]);
stm_ep();
stm_aix("p0i4","p0i0",[0,"Media","","",-1,-1,0,"media"],120,20);
stm_bpx("p12","p1",[]);
stm_aix("p12i0","p1i1",[0,"Press Release","","",-1,-1,0,"press-release"]);
stm_bpx("p13","p2",[]);
stm_aix("p13i0","p1i0",[0,"Articles","","",-1,-1,0,"articles"]);
stm_ep();
stm_aix("p12i1","p1i1",[0,"News","","",-1,-1,0,"news"]);
stm_bpx("p14","p2",[]);
stm_aix("p14i0","p1i0",[0,"Archives","","",-1,-1,0,"archives"]);
stm_ep();
stm_aix("p12i2","p1i1",[0,"Event / Calendar","","",-1,-1,0,"event-calendar"]);
stm_bpx("p15","p2",[]);
stm_aix("p15i0","p1i0",[0,"Tradeshow","","",-1,-1,0,"tradeshow"]);
stm_aix("p15i1","p1i0",[0,"Webcast","","",-1,-1,0,"webcast"]);
stm_ep();
stm_aix("p12i3","p1i0",[0,"Newsletter (5)","","",-1,-1,0,"newsletter"]);
stm_aix("p12i4","p1i1",[0,"Option / Brochures / Form","","",-1,-1,0,"option-brochures-form"]);
stm_bpx("p16","p2",[]);
stm_aix("p16i0","p14i0",[0,"Archives (pdf / html)"]);
stm_aix("p16i1","p1i0",[0,"Logo","","",-1,-1,0,"logo"]);
stm_ep();
stm_aix("p12i5","p1i0",[0,"Award / Recognition","","",-1,-1,0,"award-recognition"]);
stm_aix("p12i6","p1i0",[0,"RSS","","",-1,-1,0,"rss"]);
stm_aix("p12i7","p1i0",[0,"EDI Resources Links","","",-1,-1,0,"edi-resources-links"]);
stm_aix("p12i8","p1i0",[0,"Media Kit","","",-1,-1,0,"media-kit"]);
stm_ep();
stm_aix("p0i5","p0i0",[0,"Support","","",-1,-1,0,"support"],120,20);
stm_bpx("p17","p1",[]);
stm_aix("p17i0","p1i0",[0,"Technical","","",-1,-1,0,"technical"]);
stm_aix("p17i1","p1i0",[0,"FAQ","","",-1,-1,0,"faq"]);
stm_aix("p17i2","p1i0",[0,"Download","","",-1,-1,0,"download"]);
stm_aix("p17i3","p1i1",[0,"Glossary","","",-1,-1,0,"glossary"]);
stm_bpx("p18","p2",[]);
stm_aix("p18i0","p3i3",[]);
stm_aix("p18i1","p1i0",[0,"Live Help","","",-1,-1,0,"live-help"]);
stm_ep();
stm_ep();
stm_ai("p0i1",[0,"Contact","","",-1,-1,0,"contact","_self","","","","",0,0,0,"","",10,5,0,0,1,"#333333",0,"#333333",0,"","",3,3,1,1,"#CCCCCC","#999999","#FFFFFF","#FFFFFF","bold 8pt Verdana","bold 8pt Verdana",0,0,"","","","",0,0,0],120,20);
stm_ai("p0i1",[0,"Login","","",-1,-1,0,"login","_self","","","","",0,0,0,"","",10,5,0,0,1,"#333333",0,"#333333",0,"","",3,3,1,1,"#CCCCCC","#999999","#FFFFFF","#FFFFFF","bold 8pt Verdana","bold 8pt Verdana",0,0,"","","","",0,0,0],120,20);
stm_ep();
stm_em();
}
