Event.observe(document, "dom:loaded", set_container_width);
Event.observe(document, "dom:loaded", make_modules_transparent);
// Event.observe(document, "dom:loaded", browser_support);

Event.observe(window, "load", ie_headlines_fix);
Event.observe(window, "load", set_container_width);
Event.observe(window, "load", add_captions_on_rollover, false);
Event.observe(window, "load", autoscroll_to_myessential);

Event.observe(window, 'load', remove_firefox_focus_links);

function browser_support() {
  if (navigator.appVersion.include('MSIE 6') == true) {
   alert("Please note that this Web site is currently optimized for modern browsers and, unfortunately, this browser is not yet supported. While we are working towards compatibility with older browsers we suggest experiencing the site using using one of the following: Apple Safari 3, Mozilla Firefox 3 or Microsoft Internet Explorer 7."); 
  }
}

function remove_firefox_focus_links() {
	$$("a").each( function(link) {
		Event.observe(link, "focus", function() { link.blur(); });
	});
}

function autoscroll_to_myessential() {
	if (location.hash == "#myessential") {
		scroll_to_myessential();
	}
}

function scroll_to_myessential() {
	var myessential = $$(".module6")[0];
	new Effect.ScrollTo( myessential, { offsetX: -20, duration: 0.25 } );
}

function ie_headlines_fix() {
	if (Prototype.Browser.IE) {
		$$(".module").each(function(e) {
			var w = e.getWidth();
			e.setStyle("width: "+w+"px");
		});
	}
}

function make_modules_transparent() {
	if (Prototype.Browser.IE == false) {
		$$(".module").each(function(e) {
			var rgb = e.getStyle("background-color");
			rgb = rgb.substr(4, rgb.length-5);
			e.setStyle({
				backgroundColor: "rgba("+rgb+", 0.85)"
			});
		});
	}
}

// this is being called from the case study window
function update_myessential(id, save) {
	
	var cs = jaaulde.utils.cookies.get('case_studies');
	if (cs == null) cs = "";
	cs = cs.split(",");
	
	if (save == true) {
		cs.push(id);
	} else {
		cs = cs.without(id);
	}
	
	jaaulde.utils.cookies.set('case_studies', cs.toString());
	
	reload_myessential();
	scroll_to_myessential();
}

function reload_myessential() {
	new Ajax.Request('/update_myessential', {
		method: 'get',
		onSuccess: function(transport) {
			var myessential = $$(".module6")[0];
			myessential.update( transport.responseText );
			set_container_width();
		}
	});
}

function add_captions_on_rollover() {
	$$(".image").each(function(image) {
		if (image.down(".image_caption")) {
			var showCaption = function() {
				image.down(".image_caption").show();
			};
			var hideCaption = function() {
				image.down(".image_caption").hide();
			};
			Event.observe(image, 'mouseover', showCaption);
			Event.observe(image, 'mouseout', hideCaption);
		}
	});
}

function remove_module3_columns(element, id) {
	$(element).up().up().up().previous().down("#"+id).remove(); 
	var siblings = $(element).up('.column').siblings();
	$(element).up('.column').remove();
	if (siblings.length > 0) {
		siblings[0].show();
	}
}

function remove_module4_columns(element, id) {
	$(element).up().up().up().previous().down("#"+id).remove(); 
	var siblings = $(element).up('.column').siblings();
	$(element).up('.column').remove();
	if (siblings.length > 0) {
		siblings[0].show();
	}
}

function show_column(container, id) {
	$$("#"+container+" .column").each(function(column){
		column.hide();
	});
	$$("#"+container+" .column").each(function(column){
		if (column.identify() == id) column.show();
	});
}

function highlight_title(container, id) {
	$$("#"+container+" .description li").each(function(column){
		column.down().setStyle("font-weight: normal;");
	});
	$$("#"+container+" .description #"+id)[0].down().setStyle("font-weight: bold;");
}

function scroll_to_first_module() {
	var first_module = $$('.assigned_module')[0];
	scroll_to_module(first_module.identify());
}

function scroll_to_module(id, src_id) {
	var module = $(id);
	new Effect.ScrollTo( module, { offsetX: -20, duration: 0.5 } );
	
	if (src_id != 'false') {
		$$(".submenu").each(function(e){ e.hide(); });
		var submenu = $$("#submenu_"+src_id);
		if (submenu.length > 0)	{
			submenu[0].show();
		}
	}
}

function set_container_width() {
	var container = $("container");

	if (container) {
		var width = 0;
		$$("#container .assigned_module").each(
			function(module) {
				width += module.getWidth() + 20;
			}
		);
		
		if (Prototype.Browser.IE) width += 2000;

		container.style.width = width+"px";
	}
}

function show_bg_image(orig_src, new_src, txt) {
	var image = $("zoomimage");
	var img = image.down("img");
	var caption = image.down(".caption");
	
	if (image.isInfront == true) {
		image.isInfront = false;
		img.src = "images/blank.png"
		image.hide();
		
		caption.update("");
	} else {
		if (new_src != null) {
			var new_width = 900;

			img.setStyle("width: 900px");
			img.src = orig_src;
			img.src = "images/"+new_src;
			
			if (txt != "") {
				caption.update(txt);
				caption.show();
			} else {
				caption.hide();
			}
			
		}
		
		image.isInfront = true;
		image.show();
	}
}

function print_module(id) {
	$$(".assigned_module").each(function(e){ 
		if (e.identify() != "assigned_module_"+id) {
			e.addClassName("noprint"); 
		}
	});
	
	window.print();
	
	$$(".assigned_module").each(function(e){ 
		e.removeClassName("noprint");
	});
}




// PATCH scrollTo works horizontally now
Effect.ScrollTo = Class.create();
Object.extend(Object.extend(Effect.ScrollTo.prototype, Effect.Base.prototype), {

  initialize: function(element) {
    this.element = $(element);
    this.start(arguments[1] || {});
  },
  
  setup: function() {
    Position.prepare();
    var offsets = Position.cumulativeOffset(this.element);
    if(this.options.offsetX) offsets[0] += this.options.offsetX;

    var maxWidth = window.innerWidth ? window.width - window.innerWidth : document.body.scrollWidth - (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
    this.scrollStartX = Position.deltaX;
    // this.deltaX = (offsets[0] > maxWidth ? maxWidth : offsets[0]) - this.scrollStartX;
	this.deltaX = offsets[0] - this.scrollStartX;
  },
  
  update: function(position) {
    Position.prepare();
    window.scrollTo( this.scrollStartX + (position * this.deltaX), null );
  }
});