var Utilities =
{
	findPos : function(obj) {
    curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return [curleft, curtop];
	},
  /*
    Finds the x position of an object
  */
  findPosX:function(obj){
    var curleft = 0;
    if (obj.offsetParent)
    {
      while (obj.offsetParent)
      {
        curleft += obj.offsetLeft
        obj = obj.offsetParent;
      }
    }
    else if (obj.x)
      curleft += obj.x;
    return curleft;
  },

  /*
    Finds the y position of an object
  */
  findPosY:function(obj){
    var curtop = 0;
    if (obj.offsetParent)
    {
      while (obj.offsetParent)
      {
        curtop += obj.offsetTop
        obj = obj.offsetParent;
      }
    }
    else if (obj.y)
      curtop += obj.y;
    return curtop;
  },

	shiftArray : function(a, p)
	{
		var i;
		var x;
		var l = a.length;

		if (p < 0) {
			p += l;
		}

		for (; p > 0; p = (Math.ceil(l / p) - 1) * p - l + (l = p)) {
	        for (i = l; i > p; i) {
				x = a[--i];
				a[i] = a[i - p];
				a[i - p] = x;
			}
		}
	}
};

var Point = Class.create();
Point.prototype =
{
	initialize : function(x, y)
	{
		this.x = x;
		this.y = y;
	},
	getX : function()
	{
		return this.x;
	},
	getY : function()
	{
		return this.y;
	}
};

var DescriptionObject = Class.create();
DescriptionObject.prototype =
{
	dummy:false,
	initialize : function(obj, dummy)
	{
		this.obj = $(obj);
		this.dummy = dummy ? true : false;
		this.caret = $('slideshow_caret');
	},
	placeObject : function(x, y)
	{
		this.obj.setStyle({ top: y - 10 + 'px', left: x - 5 + 'px' });
		this.caret.setStyle({ top: y + 'px', left: x - 18 + 'px' });
		this.caret.show();
	},
	setObj : function(obj, dummy)
	{
		this.obj = obj;
		this.dummy = dummy ? true : false;
	},
	toggleIsDummy : function()
	{
		this.dummy = !this.dummy;
	},
	getObj : function()
	{
		return this.obj;
	},
	getId : function()
	{
		return this.obj.id;
	},
	isDummy : function()
	{
		return this.dummy;
	},
	hideObject : function()
	{
		if (this.dummy == false) {
			if (typeof(BrowserDetect) != 'undefined' && BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) {
				Element.setStyle($('right_column'), { zIndex: '9901' });
			}
			this.obj.hide();
		}
	},
	showObject : function(t, l)
	{
		if (this.dummy == false) {
			if (typeof(BrowserDetect) != 'undefined' && BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) {
				Element.setStyle($('right_column'), { zIndex: '9900' });
			}		
			Element.setStyle(this.obj, { top: t + 'px', left: l + 'px' });
			this.obj.show();
		}
	}
};

var SliderObject = Class.create();
SliderObject.prototype =
{
	initialize : function()
	{
	},
	setId : function(arg)
	{
		this.id = arg;
	},
	setHtml : function(arg)
	{
		this.html = arg;
	},
	setHref : function(arg)
	{
		this.href = arg;
	},
	setSrc : function(arg)
	{
		this.src = arg;
	},
	setIndex : function(arg)
	{
		this.index = arg;
	},
	setDescriptionObject : function(obj)
	{
		this.description_object = obj;
	},
	getId : function()
	{
		return this.id;
	},
	getHtml : function()
	{
		return this.html;
	},
	getHref : function()
	{
		return this.href;
	},
	getSrc : function()
	{
		return this.src;
	},
	getIndex : function()
	{
		return this.index;
	},
	getDescriptionObject : function()
	{
		return this.description_object;
	}
};

var Slider = Class.create();

Slider.prototype =
{
	which_slider:null,
	current_description_object:null,
	timer:null,

	initialize : function(arg)
	{
		this.id = arguments[0] || null;
		if (this.id == null || $(this.id + '_sliders') == null) {
			return;
		}
		
		this.which_slider = this.id;

		this.event_previous_slider = this.previous_slider.bindAsEventListener(this);
		this.event_next_slider = this.next_slider.bindAsEventListener(this);
		this.event_mouse_over = this.handle_description.bindAsEventListener(this);
		this.event_mouse_over_description = this.handle_mouse_over_description.bindAsEventListener(this);
		this.event_mouse_out = this.handle_mouse_out.bindAsEventListener(this);
		this.event_mouse_out_hyperlink = this.handle_mouse_out_hyperlink.bindAsEventListener(this);
		this.event_mouse_out_description = this.handle_mouse_out_description.bindAsEventListener(this);

		Event.observe($$('div#' + this.id + '_button_slideshow_left img')[0], 'click', this.event_previous_slider);
		Event.observe($$('div#' + this.id + '_button_slideshow_right img')[0], 'click', this.event_next_slider);

		this.current_description_object = new DescriptionObject(new Object(), true);

		this.the_sliders = $(this.id + '_sliders').select('div.slider');
		this.nr_sliders = this.the_sliders.length;
		this.slider_objects = new Array(this.nr_sliders);

		var obj = null;

		var i = 0;
		for (; i < this.nr_sliders; i++) {
			this.slider_objects[i] = new SliderObject();
			this.slider_objects[i].setId(this.the_sliders[i].id);
			this.slider_objects[i].setSrc(this.the_sliders[i].select('img')[0].src);
			this.slider_objects[i].setHref(this.the_sliders[i].select('a')[0].href);
			this.slider_objects[i].setHtml(this.the_sliders[i].select('a')[0].innerHTML);
			this.slider_objects[i].setIndex(i);
		}
	},

	update_sliders : function()
	{
		var count = 0;
		this.slider_objects.each(function(e)
		{
			if (typeof(e) != 'undefined') {
				$(this.id + '_slider_' + count + '_img').src = e.getSrc();
				$(this.id + '_slider_' + count + '_a').innerHTML = e.getHtml();
				$(this.id + '_slider_' + count + '_a').href = e.getHref();
				count++;
			}
		}.bind(this));
	},

	previous_slider : function(event)
	{
		Utilities.shiftArray(this.slider_objects, -1);
		this.update_sliders();
	},

	next_slider : function(event)
	{
		Utilities.shiftArray(this.slider_objects, 1);
		this.update_sliders();
	},

	handle_mouse_out : function(event)
	{
		Event.stop(event);

		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
		}
		this.timer = setTimeout(function()
		{
			this.current_description_object.hideObject();
		}.bind(this), 500);
	},

	handle_mouse_over_description : function(event)
	{
		Event.stop(event);

		if (this.timer) {

			clearTimeout(this.timer);
			this.timer = null;
		}
	},

	handle_mouse_out_description : function(event)
	{
		Event.stop(event);

		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
		}
		this.timer = setTimeout(function()
		{
			this.current_description_object.hideObject();
		}.bind(this), 500);
	},


	handle_mouse_out_hyperlink : function(event)
	{
		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
		}
		if (this.current_description_object) {
			this.current_description_object.hideObject();
		}
	},


	handle_description : function(event)
	{
		Event.stop(event);

		if (this.timer) {
			clearTimeout(this.timer);
			this.timer = null;
		}

		if (Event.element(event).id == '') {
			if (this.current_description_object) {
				this.current_description_object.hideObject();
			}
		}

		var the_id = Event.element(event).id;
		var i = the_id.lastIndexOf('_img');
		the_id = parseInt(the_id.substring(i-1, i));
		var description_object = $(this.slider_objects[the_id].getDescriptionObject().getObj().id + '_description');

		if (description_object) {
			if (!this.current_description_object.isDummy() || description_object == this.current_description_object.getObj()) {
				this.current_description_object.hideObject();
			}
			this.current_description_object.setObj(description_object, false);

			var l = 315;
			var t = 120;
			if (the_id == 1 || the_id - 3 == 1) {
				l += 125;
			}
			if (the_id == 2 || the_id - 3 == 2) {
				l += 250;
			}
			this.current_description_object.showObject(t, l);
			
			if (this.timer) {
				clearTimeout(this.timer);
				this.timer = null;
			}
		}
	}
};


var HighlightObject = Class.create();
HighlightObject.prototype =
{
	initialize : function()
	{
	},
	setId : function(arg)
	{
		this.id = arg;
	},
	setIndex : function(arg)
	{
		this.index = arg;
	},
	getId : function()
	{
		return this.id;
	},
	getIndex : function()
	{
		return this.index;
	}
};


var Highlights = Class.create();

Highlights.prototype =
{
	initialize : function(arg)
	{
		this.id = arguments[0] || null;
		if (this.id == null || $(this.id + '_sliders') == null) {
			return;
		}

		this.the_highlights = $(this.id + '_sliders').select('div.slider');
		this.nr_highlights = this.the_highlights.length;
		this.highlight_objects = new Array(this.nr_highlights);

		var i = 0;
		for (; i < this.nr_highlights; i++) {
			this.highlight_objects[i] = new HighlightObject();
			this.highlight_objects[i].setId(this.the_highlights[i].id);
			this.highlight_objects[i].setIndex(i);
		}

		this.currentIndex = 0;

		this.event_previous_highlight = this.previous_highlight.bindAsEventListener(this);
		this.event_next_highlight = this.next_highlight.bindAsEventListener(this);

		Event.observe($$('div#' + this.id + '_button_left img')[0], 'click', this.event_previous_highlight);
		Event.observe($$('div#' + this.id + '_button_right img')[0], 'click', this.event_next_highlight);
	},

	update_highlight : function()
	{
		var current_obj = $(this.id + '_slider_' + this.currentIndex);
		var new_obj     = $(this.id + '_slider_' + this.highlight_objects[0].getIndex());
		/*
		new Effect.Parallel([
			new Effect.Fade(current_obj, { duration: 0.25, sync : true }),
			new Effect.Appear(new_obj,   { duration: 0.25, sync : true })
		]);
		*/
		current_obj.hide();
		new_obj.show();
		this.currentIndex = this.highlight_objects[0].getIndex();
	},

	previous_highlight : function(event)
	{
		Utilities.shiftArray(this.highlight_objects, -1);
		this.update_highlight();
	},

	next_highlight : function(event)
	{
		Utilities.shiftArray(this.highlight_objects, 1);
		this.update_highlight();
	}
};

var LocationMap = Class.create();

LocationMap.prototype =
{
	initialize : function(div_id)
	{
		this.map = $(div_id);
		if (this.map == null) {
			return;
		}
		this.form = document.forms[0];
		this.zoomIn = $('zoomIn');
		this.zoomOut = $('zoomOut');
		this.hideShowLink = $('hideShowLink');
		this.isMapShown = true;
		this.hiddenField = $('mapZoom');
		this.hideMapField = $('mapHide');
		this.comments = $('LocationDetails_comments');

		this.click_zoom_in = this.handle_click_zoom_in.bindAsEventListener(this);
		this.click_zoom_out = this.handle_click_zoom_out.bindAsEventListener(this);
		Event.observe(this.zoomIn, 'click', this.click_zoom_in);
		Event.observe(this.zoomOut, 'click', this.click_zoom_out);

		this.click_toggle = this.handle_click_toggle.bindAsEventListener(this);
		Event.observe(this.hideShowLink, 'click', this.click_toggle);
		
		var mapHideValue = this.hideMapField.value;
		if (mapHideValue == 'yes') {
			this.hide_map();
		} else if (mapHideValue == 'no') {
			this.show_map();
		}
		
		this.comments_watcher = this.handle_comments_character_limit.bindAsEventListener(this);
		Event.observe(this.comments, 'keypress', this.comments_watcher);
	},
	
	handle_comments_character_limit : function(e)
	{
		var str = this.comments.value;
		if (str && str.length > 256) {
			this.comments.value = str.substring(0, 256);
		}
	},

	handle_click_zoom_in : function(e)
	{
		if (this.hiddenField) {
			this.hiddenField.value = 'in';
		}
		if (this.form) {
			Event.stop(e);
			this.form.submit();
		}
		return false;
	},

	handle_click_zoom_out : function(e)
	{
		if (this.hiddenField) {
			this.hiddenField.value = 'out';
		}
		if (this.form) {
			Event.stop(e);
			this.form.submit();
		}
		return false;
	},

	handle_click_toggle : function(e)
	{
		Event.stop(e);
		Event.element(e).blur();
		
		this.toggle_map();
	},
	
	hide_map : function()
	{
			Element.hide(this.map);
			this.hideShowLink.innerHTML = 'View';
			this.hideMapField.value = 'yes';
			this.isMapShown = false;
	},
	show_map : function()
	{
			Element.show(this.map);
			this.hideShowLink.innerHTML = 'Hide';
			this.hideMapField.value = 'no';	
			this.isMapShown = true;
	},
	
	toggle_map : function()
	{
		if (this.isMapShown == true) {
			this.hide_map();
		} else {
			this.show_map();
		}
		
	}
};

var Prospect =
{
	solutions : null,
	solution_description : null,

	__init__ : function()
	{
		Prospect.solutions = $$('div#index div#solutions div#interactive ul li');
		Prospect.solution_description = $('solution_description');

		if (Prospect.solutions.length > 0) {
			Prospect.solutions.each(function(solution)
			{
				var solution_button = solution.select('img.solution')[0];
				var solution_button = solution.select('a.solution')[0];
				Event.observe(solution_button, 'mouseover', function(e)
				{
					Event.stop(e);
					Prospect.setSolution($(this.parentNode).select('img.solution')[0].id);
				});
			});
		}

		var ServicesSlider = new Slider('services', false);
		var FleetAnalysisSlider = new Slider('fleetanalysis', false);
		var FleetHighlights = new Highlights('highlights', true);

		var printhref = $$('a.print');
		if (printhref.length > 0) {
			printhref = printhref[0];
			Event.observe(printhref, 'click', function(e)
			{
				this.blur();
				//new Ajax.Request(url, { method: 'get' });
				window.print();
				Event.stop(e);
				return false;
			});
		}
		var locationMap = new LocationMap('map');
		
		Prospect.initializeVPGSubmit();	
	},

  initializeVPGSubmit : function()
  {
  	var submit_button = $('vpgsubmit');
  	if (submit_button) {
  		Event.observe(submit_button, 'click', function(e)
  		{
  			Event.stop(e);
          	Prospect.updateVpgUrl();
		   	return false;
          });
      }
    return;
  },
  
  updateVpgUrl: function()
  {
  	var url = 'Home.updateVpgUrl.ajax.action';
  	var params = $('previewGuideDownload').serialize(true);
  	var ajaxOptions = {
  		method:'get',
  		parameters:params,
  		onComplete: function()
  		{
          	var url = $('vehiclePreviewGuideUrl');
          	if (url.value != "") {
			    //url = "http://www.enterprise.com/content/fleets/pdf/fleetGuide_Daimler.pdf";
			    window.location = url.value;
		   	}
		},		
	    onFailure: function(){}
	}
	new Ajax.Updater({success: $('vehiclePreviewGuideUrlDiv')}, url, ajaxOptions);
  },
  
	showServicesDescription : function(id, e, x, y)
	{
		Event.stop(e);

		var caret = $('slideshow_caret');
		var description = $('slideshow_fuel_card_description');

		x = Utilities.findPosX(Event.element(e));
		y = Utilities.findPosY(Event.element(e));
		
		if (caret != null && description != null) {
			caret.setStyle({ top : ((5 + y) + 'px'), left : ((110 + x) + 'px') });
			caret.show();
			description.setStyle({ top : ((0 + y) + 'px'), left : ((124 + x) + 'px') });
			description.show();
		}
	},

	hideServicesDescription : function(id, e)
	{
		Event.stop(e);
		var obj = $(id);
		var caret = $('slideshow_caret');
		obj = $('slideshow_fuel_card_description');

		if (caret != null && obj != null) {
			caret.hide();
			obj.hide();
		}
	},

	setSolution : function(id)
	{
		var selected = $$('div#index div#solutions div#interactive ul li.selected');
		if (selected.length > 0) {
			selected.each(function(obj)
			{
				obj.toggleClassName('selected');
			});
		}
		selected = $('li_' + id);
		selected.toggleClassName('selected');

		var content_div = $('description_' + id);
		Prospect.solution_description.update(content_div.innerHTML);
	}
};

Event.observe(window, 'load', Prospect.__init__, false);