var splash_accordion = null;
var footer_scroller = null;
var search_box = null;
var g_drop_downs = null;
var dropdown_menu = null;
var dropdown_menu2 = null;

window.addEvent('domready', function() {
  var accordion_header = $$('.accordion_header');
  if (accordion_header && accordion_header.length) {
    var splash_accordion = new SplashAccordion(accordion_header[0]);
  }
  var rotation_banner = $$('.rotation_banner');
  if (rotation_banner && rotation_banner.length) {
    var usedcar_banner = new UsedCarBanner(rotation_banner[0]);
  }
  dropdown_menu = new DropdownMenu($('nav'), 'nav_tab', $('nav_dropdowns'),  'nav_dropdown');
  dropdown_menu2 = new DropdownMenu($('nav_content'), 'parent', $('nav_content_dropdowns'),  'nav_dropdown', true, 21);

  if ($('mr_contact_box')) {
    var contact_accordion = new ContactAccordion();
  }

  if ($('mr_parts_box')) {
    var parts_accordion = new PartsAccordion();
  }

  if ($('mr_comment_box')) {
    var comment_box = new CommentFormBox();
  }

  if ($('mr_service_box')) {
    var easybooking_box = new EasyBookingBox();
  }
  
  setTimeout('delayed_load()', 250);
});

sIFR.replace(hc77, {
  selector: 'h1',
  css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; font-size:31px; }',
  wmode: 'transparent',
  tuneHeight: -7,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57, {
  selector: '.index_content_right h2',
  css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57, {
  selector: '.usedcar_summary h2, .section_header h2',
  css: '.sIFR-root { background-color: transparent; color: #FFFFFF; font-weight:bold; cursor: pointer; }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57, {
  selector: 'h2.grey',
  css: '.sIFR-root { background-color: transparent; color: #a3a3a3; font-size:18px; }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57, {
  selector: '#experience h2',
  css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; font-size:18px; cursor:pointer; width:135px }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57, {
  selector: '.white_content_main h2',
  css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; font-size:18px; }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57, {
  selector: '.r_long_header',
  css: '.sIFR-root { background-color: transparent; color: #666666; cursor:pointer }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57o, {
  selector: '.pullout_quote',
  css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});
sIFR.replace(hc57, {
  selector: '.main_quote',
  css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57, {
  selector: '.showroom_item h3',
  css: '.sIFR-root { background-color: transparent; color: #464b4d; cursor: pointer;}',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57, {
  selector: '.showroom_item_expanded_content h3',
  css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; cursor: pointer;}',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc57o, {
  selector: '.testimonial_copy',
  css: '.sIFR-root { background-color: transparent; color: #3E4E8E;}',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc77, {
  selector: 'span.testimonial_name',
  css: '.sIFR-root { background-color: transparent; color: #383838; font-size:14px }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});

sIFR.replace(hc77, {
  selector: 'p.testimonial_name, div.testimonial_name',
  css: '.sIFR-root { background-color: transparent; color: #383838; text-align:right; font-size:14px }',
  wmode: 'transparent',
  tuneHeight: -6,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});
sIFR.replace(hc77, {
  selector: '.feature_box .more_link_copy',
  css: '.sIFR-root { background-color: transparent; color: #FFFFFF; }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});
sIFR.replace(hc77, {
  selector: '.more_link_copy',
  css: '.sIFR-root { background-color: transparent; color: #464b4d; cursor:pointer;  }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});
sIFR.replace(hc77, {
  selector: '.service_location_name',
  css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; }',
  wmode: 'transparent',
  tuneHeight: -4,
  ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
});


function delayed_load()
{
  var showroom_items = $$('.showroom_item');
  var showroom_items_expanded = $$('.showroom_item_expanded_content');
  if (showroom_items.length && showroom_items_expanded.length) {
    var showroom_toggler = new ShowroomToggler($$('.showroom_item'), $$('.showroom_item_expanded_content'), $('showroom_reminder_tab'));
  }

  var manufacturer_items = $$('.index_manufacturer');
  if (manufacturer_items) {
    var index_manufacturers = new IndexManufacturers(manufacturer_items);
  }

	var hover_images = $$('.image_hover');
	if (hover_images.length) {
		var hover_img = new HoverImages(hover_images);
	}

	var featured_used_car = $$('.r_used_car_box');
	if (featured_used_car.length) {
		var featured_used_car = new FeaturedUsedCar(featured_used_car[0]);
	}

  if ($('mr_testimonial_box')) {
    var testimonial_rotator = new TestimonialRotator($('mr_testimonial_box'));
  }

  if ($('keema_conscious_scroller')) {
    var footer_scroller = new TextScroller($('keema_conscious_scroller'));
  }

  if ($('why_keema_scroller')) {
    var why_keema = new TextScroller($('why_keema_scroller'));
  }

  new ClearTextInput(document.forms['search'].q);

  var contact_form = document.forms['contact_form'];
  if (contact_form) {
    new ClearTextInput(contact_form.rego);
    new ClearTextInput(contact_form.name);
    new ClearTextInput(contact_form.address);
    new ClearTextInput(contact_form.postcode);
    new ClearTextInput(contact_form.telephone);
    new ClearTextInput(contact_form.email);
    new ClearTextInput(contact_form.friend_email_0);
    new ClearTextInput(contact_form.friend_email_1);
    new ClearTextInput(contact_form.friend_email_2);
    new ClearTextInput(contact_form.friend_email_3);
    new ClearTextInput(contact_form.friend_email_4);
    if (contact_form.promo_code) {
    	new ClearTextInput(contact_form.promo_code);
  	}
  	$(contact_form.purpose).addEvent('change', function(e) {
    	var rel = $(contact_form.purpose.children[contact_form.purpose.selectedIndex]).getAttribute('rel');
    	if (contact_form.promo_code && rel == 1) {
    		$(contact_form.promo_code.parentNode).setStyle('display', 'block');
    		$('information_night_details').setStyle('display', 'none');
    		$(contact_form.email).getPrevious().setStyle('display', 'none');
    	} else if (rel == 2) {
    		$('information_night_details').setStyle('display', 'block');
    		$(contact_form.promo_code.parentNode).setStyle('display', 'none');
    		$(contact_form.email).getPrevious().setStyle('display', 'block');
    	} else {
    		$(contact_form.promo_code.parentNode).setStyle('display', 'none');
    		$('information_night_details').setStyle('display', 'none');
    		$(contact_form.email).getPrevious().setStyle('display', 'none');
    	}
  		
  	});

  }

  
  var service_contact_form = document.forms['easy_booking'];
  if (service_contact_form) {
    new ClearTextInput(service_contact_form.make);
    new ClearTextInput(service_contact_form.model);
    new ClearTextInput(service_contact_form.year);
    new ClearTextInput(service_contact_form.registration);
    new ClearTextInput(service_contact_form.kms);
    new ClearTextInput(service_contact_form.name);
    new ClearTextInput(service_contact_form.phone);
    new ClearTextInput(service_contact_form.email);
    new ClearTextInput(service_contact_form.address);
    new ClearTextInput(service_contact_form.state);
    new ClearTextInput(service_contact_form.suburb);
    new ClearTextInput(service_contact_form.postcode);
    new ClearTextInput(service_contact_form.preferred_time);
    new ClearTextInput(service_contact_form.preferred_date);
    new ClearTextInput(service_contact_form.comments);
  }

  var select_boxes = $$('select');
  if (select_boxes) {
    g_drop_downs = [];
    select_boxes.each(function(el) {
      g_drop_downs[el.form.name+'_'+el.name] = new DropDown(el, el.selectedIndex,{selectText:el.options[el.selectedIndex].text, dropDownImageWidth: 14});
    });
  }

  if (document.forms['used_car_search']) {
    var used_search = new UsedSearch(document.forms['used_car_search']);
  }

  var used_car_items = $$('.used_car_item');
  if (used_car_items) {
    new UsedCarItems(used_car_items);
  }

	if ($('knowledgebank_articles')) {
		new KnowledgeBankToggler();
	}

  var staff_overlays = $$('.staff_overlay');
	if (staff_overlays.length) {
		new StaffPopup(staff_overlays[0]);
	}
}

var IndexManufacturers = new Class({

  manufacturers: null,

  initialize: function(elements)
  {
    this.manufacturers = elements;

    this.manufacturers.each(function(manufacturer) {
      manufacturer.addEvent('mouseenter', function(e) {
        e = new Event(e).stop();
        var tar = $(e.target);
        if (!tar.hasClass('index_manufacturer')) {
          tar = $(tar.parentNode);
        }
        var img = $(tar).getElement('img');
        img.src = img.src.substring(0, img.src.length-5)+'h.gif';
      }.bind(this));
      manufacturer.addEvent('mouseleave', function(e) {
        e = new Event(e).stop();
        var tar = $(e.target);
        if (!tar.hasClass('index_manufacturer')) {
          tar = $(tar.parentNode);
        }
        var img = $(tar).getElement('img');
        img.src = img.src.substring(0, img.src.length-5)+'s.gif';
      }.bind(this));
    }.bind(this));
  }
});

var ClearTextInput = new Class({

	Implements: [Events, Options],

  options: {
/*  onFocus: $function,
    onBlur: $function  */
  },

  element: null,

  initialize: function(element, options)
  {
    this.setOptions(options);
    this.element = $(element);
    this.element.addEvent('focus', function(e) {
      if (this.element.value == this.element.defaultValue) {
        this.element.value = '';
      }
      this.fireEvent('focus', [this.element]);
    }.bind(this));
    this.element.addEvent('blur', function(e) {
      if (this.element.value == '') {
        this.element.value = this.element.defaultValue;
      }
      this.fireEvent('blur', [this.element]);
    }.bind(this));
  }

});

var StaffPopup = new Class({

  container: null,
  bubble:null,
  blurb:null,
  blurb_timeout:null,
  blurb_fx:null,

  initialize: function(element)
  {
    this.container = $(element);
    this.bubble = this.container.getElement('.staff_bubble');
    this.blurb = this.container.getElement('.staff_blurb');

    if (this.bubble && this.blurb) {
      this.bubble.addEvent('mouseenter', function(e) {
        e = new Event(e).stop();
        this.showBlurb();
      }.bind(this));
      this.bubble.addEvent('mouseleave', function(e) {
        e = new Event(e).stop();
        this.startHideBlurb();
      }.bind(this));
      this.blurb.addEvent('mouseenter', function(e) {
        e = new Event(e).stop();
        this.showBlurb();
      }.bind(this));
      this.blurb.addEvent('mouseleave', function(e) {
        e = new Event(e).stop();
        this.startHideBlurb();
      }.bind(this));

      this.blurb_fx = new Fx.Tween(this.blurb, {duration: 1000} );
      this.bubble_fx = new Fx.Tween(this.bubble, {duration: 1000} );
      this.blurb.setOpacity(0);
    }
  },

  showBlurb: function()
  {
    clearTimeout(this.blurb_timout);
    this.blurb_timeout = null;
    this.blurb_fx.cancel();
    this.blurb_fx.start('opacity', 1);
    this.bubble_fx.cancel();
    this.bubble_fx.start('opacity', 0);
    sIFR.replace(hc57o, {
      selector: '.staff_blurb_content',
      css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; font-size:16px; }',
      wmode: 'transparent',
      tuneHeight: -4,
      ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
    });
  },

  hideBlurb: function()
  {
    this.bubble_fx.cancel();
    this.bubble_fx.start('opacity', 1);
    this.blurb_fx.cancel();
    this.blurb_fx.start('opacity', 0);
  },

  startHideBlurb: function()
  {
    clearTimeout(this.blurb_timout);
    this.blurb_timeout = null;
    this.blurb_timout = setTimeout(this.hideBlurb.bind(this), 400);
  }

});

var HoverImages = new Class( {

	images: null,

	initialize: function(images)
	{
		this.images = images;
		this.images.each(function(image) {
			image.addEvent('mouseenter', function(e) {
				e = new Event(e).stop();
				var img = $(e.target);
        if ('img' == img.nodeName.toLowerCase()) {
          img.src = img.src.substring(0, img.src.length-5)+'h.jpg';
        }
        else {
          img = $(img.parentNode);
          var bg_src = img.getStyle('background-image');
          img.setStyle('background-image', bg_src.substring(0, bg_src.length-6)+'h.jpg)');
        }
			});
			image.addEvent('mouseleave', function(e) {
				e = new Event(e).stop();
				var img = $(e.target);
        if ('img' == img.nodeName.toLowerCase()) {
          img.src = img.src.substring(0, img.src.length-5)+'s.jpg';
        }
        else {
          img = $(img.parentNode);
          var bg_src = img.getStyle('background-image');
          img.setStyle('background-image', bg_src.substring(0, bg_src.length-6)+'s.jpg)');
        }
			});
      if ('img' == image.nodeName.toLowerCase()) {
        new Asset.image(image.src.substring(0, image.src.length-5)+'h.jpg');
      }
      else {
        var bg_src = image.getStyle('background-image');
        new Asset.image(bg_src.substring(4, bg_src.length-5)+'h.jpg');
      }
		});
	}

});

var FeaturedUsedCar = new Class( {

	hover_link: null,
  hover_images: null,
  effect1: null,
  effect2: null,

	initialize: function(featured_box)
	{
		this.hover_link = featured_box.getElement('a');
		this.hover_images = featured_box.getElements('div.r_used_car_image');
    this.effect1 = new Fx.Tween(this.hover_images[0]);
    this.effect2 = new Fx.Tween(this.hover_images[1]);

    this.hover_link.addEvent('mouseenter', function(e) {
			e = new Event(e).stop();
      this.effect1.start('opacity',1);
      this.effect2.start('opacity',0);
    }.bind(this));
    this.hover_link.addEvent('mouseleave', function(e) {
			e = new Event(e).stop();
      this.effect1.start('opacity',0);
      this.effect2.start('opacity',1);
    }.bind(this));
	}
});

var DropdownMenu = new Class({
  options: {
	 ignoreSelect: false
   , tabClass: null
   , dropdownClass: null
   , yOffset: 33
  },

  tabs: null,
  dropdowns: null,

  initialize: function(tabContainer, tabClass, dropdownContainer, dropdownClass, ignoreSelect, yOffset) {
    this.tabs = tabContainer.getElements('.' + tabClass);
    this.dropdowns = dropdownContainer.getElements('.' + dropdownClass);
    if (ignoreSelect) {
    	this.options.ignoreSelect = true;
    }
    if (yOffset) {
    	this.options.yOffset = yOffset;
    }
	this.options.tabClass = tabClass;
	this.options.dropdownClass = dropdownClass;

    var i = 0;
    this.tabs.each(function(el) {
      if (this.options.ignoreSelect || !el.hasClass('section')) {
        el.setAttribute('rel', i);
        el.addEvent('mouseover', function(e) {
          e = new Event(e).stop();
          var tar = $(e.target);
          while (!tar.nodeType || !tar.hasClass(this.options.tabClass)) {
            tar = $(tar.parentNode);
          }
          // place the associated dropdown
          var idx = tar.getAttribute('rel');
          this.placeDropdown(idx);
        }.bind(this));
        el.addEvent('mouseout', function(e) {
          e = new Event(e).stop();
          // hide the associated dropdown
        var tar = $(e.target);
          while (!tar.nodeType || !tar.hasClass(this.options.tabClass)) {
          var tar = $(tar.parentNode);
          }
          // place the associated dropdown
          var idx = tar.getAttribute('rel');
          this.hideDropdown(idx);
        }.bind(this));
      }
      i++;
    }.bind(this));
    this.dropdowns.each(function(el){
      el.addEvent('mouseover', function(e) {
        e = new Event(e).stop();
        // show the associated dropdown
        var tar = $(e.target);
        while (!tar.hasClass(this.options.dropdownClass)) {
          tar = $(tar.parentNode);
        }
        var idx = tar.getAttribute('rel');
        this.placeDropdown(idx);
      }.bind(this));
      el.addEvent('mouseout', function(e) {
        e = new Event(e).stop();
        // hide the associated dropdown
        var tar = $(e.target);
        while (!tar.hasClass(this.options.dropdownClass)) {
          tar = $(tar.parentNode);
        }
        var idx = tar.getAttribute('rel');
        this.hideDropdown(idx);
      }.bind(this));
    }.bind(this));

	// load dropdown background
	new Asset.image(base_url+'images/bg_dropdown.png');
  },

  placeDropdown: function(idx) {
    if (idx < this.dropdowns.length) {
      if (!this.dropdowns[idx].getAttribute('rel')) {
        var tab_pos = this.tabs[idx].getPosition();
        var tab_size = this.tabs[idx].getSize();
        var left_offset = 5;
        if (this.tabs[idx].hasClass('first')) {
          left_offset = 2;
        }
        var pos_y = tab_pos.y + this.options.yOffset;
        var pos_x = tab_pos.x+left_offset;
        if (Browser.Engine.trident) {
//          pos_y -= 3;
//          pos_x -= 2;
        }
        this.dropdowns[idx].setStyles({top:pos_y+'px', left:pos_x+'px', width:'125px'});
        this.dropdowns[idx].setAttribute('rel', idx)
      }
      this.dropdowns[idx].setStyle('display' ,'block');
    }
  },

  hideDropdown: function(idx) {
    if (idx < this.dropdowns.length) {
      this.dropdowns[idx].setStyle('display', 'none');
    }
  }

});


var UsedCarItems = new Class({

  items: null,

  initialize: function(items) {
    this.items = items;

    items.each(function(item) {
      item.addEvent('click', function(e) {
        e = new Event(e).stop();
        var item = $(e.target);
        while(!item.hasClass('used_car_item')) {
          item = $(item.parentNode);
        }
        var link = item.getElement('a');
        window.location = link.getAttribute('href');
      });
    });
  }
});

var UsedCarBanner = new Class({

  container: null,
  splash_images: null,
  togglers: null,
  image_timeout: null,
  current: 0,
  next: null,

  initialize: function(container) {
    this.container = container;
    this.splash_images = container.getElements('.rotation_image');
    this.togglers = container.getElements('.usedcar_nav_item');

    this.togglers.each(function(el) {
      el.addEvent('click', function(e) {
        e = new Event(e).stop();
        var toggler = $(e.target);
        var idx = this.togglers.indexOf(toggler);
        this.next = idx;
        this.stop();
        this._changeImage();
      }.bind(this))
    }, this);
    this.start();
  },

  changeImage: function() {
    this.next = this.current+1;
    if (this.next >= this.splash_images.length) {
      this.next = 0;
    }
    this._changeImage();
  },

  _changeImage: function()
  {
    if (this.current != this.next)
    {
      this.setOpaque();
      var effect1 = null;
      var effect2 = null;
      effect1 = new Fx.Tween(this.splash_images[this.next]);
      effect2 = new Fx.Tween(this.splash_images[this.current]);
      effect1.start('opacity',0,1);
      effect2.start('opacity',1,0);
      this.current = this.next;
    }
    this.next = null;
    if (!this.image_interval) {
      this.start();
    }
  },

  start: function() {
    this.image_interval = setInterval(this.changeImage.bind(this), 6000);
  },

  stop: function() {
    clearInterval(this.image_interval);
    this.image_interval = null;
  },

  setOpaque: function ()
  {
    var i = 0;
    this.splash_images.each(function(el) {
      if (i != this.current) {
        el.setOpacity(0);
      }
      if (i != this.next){
        this.togglers[i].removeClass('selected');
      }
      else {
        this.togglers[i].addClass('selected');
      }
      i++;
    }.bind(this));
  }
});

var UsedSearch = new Class({

  manu_dropdown: null,
  make_dropdown: null,
  make_rdropdown: null,

  initialize: function(form)
  {
    new ClearTextInput(form.used_keyword);

    // add event to manufacturer drop down
    this.manu_dropdown = $(form.used_manufacturer_id);
    this.make_dropdown = $(form.used_model_id);
    this.manu_rdropdown = g_drop_downs[this.manu_dropdown.form.name+'_'+this.manu_dropdown.name];
    this.make_rdropdown = g_drop_downs[this.make_dropdown.form.name+'_'+this.make_dropdown.name];

    this.manu_dropdown.addEvent('change', function() {
      // work out which manufacturer was set
      this.setMakeDropdown(this.manu_dropdown.value);
    }.bind(this));
    if (manufacturer_id) {
      this.setMakeDropdown(manufacturer_id);

      // find the index of this id in the select box
      var found = false;
      var idx = 0;
      while (!found && idx < this.manu_dropdown.options.length) {
        if (this.manu_dropdown.options[idx].value == manufacturer_id) {
          this.manu_rdropdown.selectOption(idx, 1);
          found = true;
        }
        idx++;
      }

    }
  },

  setMakeDropdown: function(manufacturer_id)
  {
    this.make_dropdown.options.length = 1;
    // change the contents of the make dropdown
    makes[manufacturer_id].each(function (opt) {
      this.make_dropdown.options[this.make_dropdown.options.length] = new Option(opt.name, opt.id);
    }.bind(this));
    this.make_rdropdown.destroyDropDown();
    this.make_rdropdown.selectOption(0, 1);
  }
});

var TestimonialRotator = new Class({
  options: {
  },

  images: null,
  interval: null,
  current: 0,
  previous: null,

  initialize: function(container)
  {
    this.images = container.getElements('.testimonial_image');
    this.interval = setInterval(this.changeImage.bind(this), 4500);
  },

  changeImage: function()
  {
    var next = this.current + 1;
    if (next >= this.images.length) {
      next = 0;
    }

    if (this.current != next)
    {
      this.setOpaque();
      var effect1 = null;
      var effect2 = null;
      effect1 = new Fx.Tween(this.images[next]);
      effect2 = new Fx.Tween(this.images[this.current]);
      effect1.start('opacity',0,1);
      effect2.start('opacity',1,0);
      this.current = next;
    }
  },

  setOpaque: function()
  {
    for (var i = 0; i < this.images.length; i++) {
      if (i != this.current) {
        this.images[i].setStyle('visibility', 'hidden');
      }
    }
  }

});
TestimonialRotator.implement(new Options);

var ContactAccordion = new Class({

  accordion: null,
  callback_form: null,
  question_form: null,
  phone_number: null,
  name: null,
  postcode: null,
  question_name: null,
  question_email: null,
  question_postcode: null,
  question_textarea: null,
  visible: false,
  img_submit: null,

  initialize: function() {
    this.accordion = new Accordion($$('#accordion_contact_box .contact_box_toggler'), $$('#accordion_contact_box .contact_box_element'),
                                    {display: -1});
    this.callback_form = document.forms['call_me_back'];
    this.phone_number = $(this.callback_form.phone_number);
    this.name = $(this.callback_form.name);
    this.postcode = $(this.callback_form.postcode);
    this.callback_form = $(this.callback_form);
    this.question_form = document.forms['quick_question'];
    this.question_name = $(this.question_form.name);
    this.question_email = $(this.question_form.email);
    this.question_postcode = $(this.question_form.postcode);
    this.question_textarea = $(this.question_form.question);
    this.question_form = $(this.question_form);

    new ClearTextInput(this.phone_number, {
      onFocus: function(e) {
        this.accordion.display(0);
      }.bind(this)
    });
    new ClearTextInput(this.name);
    new ClearTextInput(this.postcode);
    new ClearTextInput(this.question_name);
    new ClearTextInput(this.question_email);
    new ClearTextInput(this.question_postcode);
    new ClearTextInput(this.question_textarea);

    this.callback_form.addEvent('submit', function(e) {
      // check input
      this.accordion.display(0);
      why = '';
      if (this.phone_number.value == '' || this.phone_number.value == this.phone_number.defaultValue) {
        why += "Please enter your phone number\n";
      }
      if (this.name.value == '' || this.name.value == this.name.defaultValue) {
        why += "Please enter your name\n";
      }
      if (this.postcode.value == '' || this.postcode.value == this.postcode.defaultValue) {
          why += "Please enter your postcode\n";
      }
      if (why != '') {
        alert (why);
        return false;
      }
      return true;
    }.bind(this));
    
    this.question_form.addEvent('submit', function(e) {
        why = '';
          if (this.question_name.value == '' || this.question_name.value == this.question_name.defaultValue) {
            why += "Please enter your name\n";
          }
          if (this.question_email.value == '' || this.question_email.value == this.question_email.defaultValue) {
              why += "Please enter your email address\n";
            }
          if (this.question_postcode.value == '' || this.question_postcode.value == this.question_postcode.defaultValue) {
              why += "Please enter your postcode\n";
          }
        if (this.question_textarea.value == '' || this.question_textarea.value == this.question_textarea.defaultValue) {
          why += "Please enter your question\n";
        }
        if (why != '') {
          alert (why);
          return false;
        }
        return true;
    }.bind(this));
  }

});

var PartsAccordion = new Class({

  form: null,
  visible: false,
  img_submit: null,
  sections: null,
  pickup_loc: null,
  heights: [],

  initialize: function() {
    this.sections = $$('#parts_contact_box .contact_box_element');
    this.form = document.forms['parts_enquiry'];
    this.first_name = $(this.form.first_name);
    this.last_name = $(this.form.last_name);
    this.make_model = $(this.form.make_and_model);
    this.model_year = $(this.form.model_year);
    this.company = $(this.form.company);
    this.phone = $(this.form.phone);
    this.email = $(this.form.email);
    this.address = $(this.form.address);
    this.suburb = $(this.form.suburb);
    this.postcode = $(this.form.postcode);
    this.parts_description = $(this.form.parts_description);
    this.vin = $(this.form.vin);
    this.retrieve_methods = this.form.retrieve_method;
    this.pickup_location = this.form.pickup_location;
    this.parts_description = $(this.form.parts_description);
    this.parts_identification = $(this.form.parts_identification);
    this.form = $(this.form);
    this.pickup_loc = this.form.getElement('#pickup_location');
    this.pickup_loc.setStyle('display', 'none');

    for (var i = 0; i < this.sections.length; i++) {
      this.heights[i] = this.sections[i].getSize().y;
      this.sections[i].setStyles({height:'0px',visibility:'hidden',opacity:'0',overflow:'hidden'});
    }

    new ClearTextInput(this.first_name, {
      onFocus: function(e) {
        this.display(0);
      }.bind(this)
    });
    new ClearTextInput(this.last_name, {
      onFocus: function(e) {
        this.display(0);
      }.bind(this)
    });
    new ClearTextInput(this.make_model, {
      onFocus: function(e) {
        this.display(1);
      }.bind(this)
    });
    new ClearTextInput(this.model_year, {
      onFocus: function(e) {
        this.display(1);
      }.bind(this)
    });
    new ClearTextInput(this.company);
    new ClearTextInput(this.phone);
    new ClearTextInput(this.email);
    new ClearTextInput(this.address);
    new ClearTextInput(this.suburb);
    new ClearTextInput(this.vin);
    new ClearTextInput(this.postcode);
    new ClearTextInput(this.parts_description);
    new ClearTextInput(this.parts_identification);

    $(this.retrieve_methods[0]).addEvent('click', function(e) {
      this.pickup_loc.setStyle('display', 'none');
    }.bind(this));
    $(this.retrieve_methods[1]).addEvent('click', function(e) {
      this.pickup_loc.setStyle('display', 'block');
    }.bind(this));

    this.form.addEvent('submit', function(e) {
      // check input
      this.display(0);
      this.display(1);
      why = '';
      if (this.first_name.value == '' || this.first_name.value == this.first_name.defaultValue) {
        why += "Please enter your First name\n";
      }
      if (this.last_name.value == '' || this.last_name.value == this.last_name.defaultValue) {
        why += "Please enter your Last name\n";
      }
      if (this.make_model.value == '' || this.make_model.value == this.make_model.defaultValue) {
        why += "Please enter your Car Make & Model\n";
      }
      if (this.model_year.value == '' || this.model_year.value == this.model_year.defaultValue) {
        why += "Please enter your Year of Model\n";
      }
      if (this.parts_description.value == '' || this.parts_description.value == this.parts_description.defaultValue) {
        why += "Please enter your Parts Description\n";
      }
      if (this.retrieve_methods[1].checked && !this.pickup_location.selectedIndex) {
        why += "Please select where you would like to pick-up this part\n";

      }
      if (why != '') {
        alert (why);
        return false;
      }
      return true;
    }.bind(this));
  },

  display: function(section)
  {
    var fx= new Fx.Morph(this.sections[section]);

    fx.start({
      'height': this.heights[section]+8,
      'opacity': 1
    });
  }

});


var CommentFormBox = new Class({

  form: null,
  sections: null,
  heights: [],
  first_name: null,
  last_name: null,
  email: null,
  make_and_manufacturer: null,
  comments: null,

  initialize: function() {
    this.sections = $$('#mr_comment_box .contact_box_element');
    for (var i = 0; i < this.sections.length-1; i++) {
      this.heights[i] = this.sections[i].getSize().y;
      this.sections[i].setStyles({height:'0px',visibility:'hidden',opacity:'0',overflow:'hidden'});
    }

    this.form = document.forms['comment_form'];
    this.first_name = $(this.form.first_name);
    this.last_name = $(this.form.last_name);
    this.email = $(this.form.email);
    this.make_and_manufacturer = $(this.form.make_and_manufacturer);
    this.comments = $(this.form.comments);
    this.form = $(this.form);

    new ClearTextInput(this.first_name);
    new ClearTextInput(this.last_name);
    new ClearTextInput(this.email);
    new ClearTextInput(this.make_and_manufacturer);
    new ClearTextInput(this.comments, {
      onFocus: function(e) {
        this.display(0);
        this.display(1);
//        this.display(2);
      }.bind(this)
    });

    this.form.addEvent('submit', function(e) {
      // check input
      why = '';
      if (this.first_name.value == '' || this.first_name.value == this.first_name.defaultValue) {
        why += "Please enter your first name\n";
      }
      if (this.last_name.value == '' || this.last_name.value == this.last_name.defaultValue) {
        why += "Please enter your last name\n";
      }
      if (this.make_and_manufacturer.value == '' || this.make_and_manufacturer.value == this.make_and_manufacturer.defaultValue) {
        why += "Please enter the make and manufacturer of your car\n";
      }
      if (this.comments.value == '' || this.comments.value == this.comments.defaultValue) {
        why += "Please enter a comment\n";
      }
      if (why != '') {
        alert (why);
        return false;
      }
      return true;
    }.bind(this));
  },

  display: function(section)
  {
    var fx= new Fx.Morph(this.sections[section]);

    fx.start({
      'height': this.heights[section]+8,
      'opacity': 1
    });
  }

});

var EasyBookingBox = new Class({

  form: null,
  sections: null,
  heights: [],
  name: null,
  phone: null,
  email: null,
  make: null,
  comments: null,
  preferred_time: null,
  preferred_date: null,

  initialize: function() {

    this.form = document.forms['easy_booking'];
    this.name = $(this.form.name);
    this.phone = $(this.form.phone);
    this.email = $(this.form.email);
    this.make = $(this.form.make);
    this.preferred_time = $(this.form.preferred_time);
    this.preferred_date = $(this.form.preferred_date);
    this.form = $(this.form);

    this.form.addEvent('submit', function(e) {
      // check input
      why = '';
      if (this.make.value == '' || this.make.value == this.make.defaultValue) {
        why += "Please enter the make of your car\n";
      }
      if (this.name.value == '' || this.name.value == this.name.defaultValue) {
        why += "Please enter your name\n";
      }
      if (this.preferred_time.value == '' || this.preferred_time.value == this.preferred_time.defaultValue) {
        why += "Please enter your preferred time\n";
      }
      if (this.preferred_date.value == '' || this.preferred_date.value == this.preferred_date.defaultValue) {
        why += "Please enter your preferred date\n";
      }
      if (!this.form.contact_method[0].checked && !this.form.contact_method[1].checked) {
        why += "Please select a method of contact\n";
      }
      if (this.form.contact_method[0].checked &&
        (this.email.value == '' || this.email.value == this.email.defaultValue || !checkEmail(this.email.value)) ) {
        why += "Please enter your email address\n";
      }
      if (this.form.contact_method[1].checked && (this.phone.value == '' || this.phone.value == this.phone.defaultValue)) {
        why += "Please enter your phone number\n";
      }
      if (why != '') {
        alert (why);
        return false;
      }
      return true;
    }.bind(this));
  }

});

var KnowledgeBankToggler = new Class({

  options: {

  },

  togglers: null,
  articles: null,
  showroom_contents: null,
  current: 0,
  next: null,

  initialize: function(showroom_items, showroom_contents, tab)
  {
		this.togglers = $$('.article_toggle_box');
		this.articles = $$('.article_block');

		this.togglers.each(function(toggler) {
			toggler.addEvent('click', function(e) {
				e = new Event(e).stop();
        var item = $(e.target);
        while(!item.hasClass('article_toggle_box')) {
          item = $(item.parentNode);
        }
        var idx = this.togglers.indexOf(item);
				this.clearArticles();
				this.articles[idx].setStyle('display','block');
        item.setStyle('backgroundImage', 'url(images/keema_tip/bg_hover.gif)');

				sIFR.replace(hc57, {
					selector: '.white_content_main h2',
					css: '.sIFR-root { background-color: transparent; color: '+header_colour+';  }',
					wmode: 'transparent',
          tuneHeight: -4,
					ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
				});
				sIFR.replace(hc57o, {
					selector: '.pullout_quote',
					css: '.sIFR-root { background-color: transparent; color: '+header_colour+'; }',
					wmode: 'transparent',
          tuneHeight: -4,
					ratios: [9, 1.27, 14, 1.19, 21, 1.16, 28, 1.13, 38, 1.12, 53, 1.11, 60, 1.1, 61, 1.11, 94, 1.1, 96, 1.09, 101, 1.1, 102, 1.09, 103, 1.1, 108, 1.09, 110, 1.1, 1.09]
				});
			}.bind(this));
		}.bind(this));
	},

	clearArticles: function()
	{
		this.articles.each(function(article) {
			article.setStyle('display','none');
		});
		this.togglers.each(function(toggler) {
			toggler.setStyle('backgroundImage','url(images/keema_tip/bg_normal.gif)');
		});
	}
});

var ShowroomToggler = new Class({

  options: {

  },

  showroom: null,
  showroom_items: null,
  showroom_contents: null,
  current: 0,
  next: null,

  initialize: function(showroom_items, showroom_contents, tab)
  {
    this.showroom_items = showroom_items;
    this.showroom_contents = showroom_contents;

    this.showroom_items.each(function(el) {
      el.addEvent('click', function(e) {
        e = new Event(e).stop();
        var item = $(e.target);
        while(!item.hasClass('showroom_item')) {
          item = $(item.parentNode);
        }
        var idx = this.showroom_items.indexOf(item);
        this.next = idx;
        this.showContent();
      }.bind(this));
    }, this);

    this.hideContents();
    this.tab = tab;
    if (this.tab) {
      setTimeout(this.showTab.bind(this), 2000);
    }
  },

  showContent: function(idx)
  {
    if (this.current != this.next) {
      this.showroom_contents[this.current].setStyle('display', 'none');
      this.showroom_contents[this.next].setStyle('display', 'block');
      sIFR.replace(hc57, {
        selector: '.white_content_main h2.blue',
        css: '.sIFR-root { background-color: transparent; color: #48537d; }',
        wmode: 'transparent',
        tuneHeight: -4
      });
      this.current = this.next;
    }
  },

  hideContents: function()
  {
    for (var i = 0; i < this.showroom_contents.length; i++) {
      if (i != this.current) {
        this.showroom_contents[i].setStyle('display', 'none');
      }
    }
  },

  showTab: function()
  {
    var fx = new Fx.Tween(this.tab, {duration: 2000} );
    fx.start('opacity', 1);
  }

});

var SplashAccordion = new Class({

  header_container: null,
  accordion: null,
  text_scroller: null,
  arrow_fx: null,
  timeout: null,
  interval: null,
  container: null,
  splash_images: null,
  arrow: null,
  current: 0,
  activeCount: 0,

  initialize: function(header_container) {

    this.header_container = header_container;
    this.splash_images = this.header_container.getElements('.rotation_image');

    this.container = this.header_container.getElement('#splash_accordion');
    this.accordion = new Accordion(this.header_container.getElements('#splash_accordion h3'), this.header_container.getElements('#splash_accordion .accordion_content'), {
      onActive: function(toggler, element) {
        toggler.setStyles({color: active_colour, cursor: 'auto'});
        this.resetInterval();
        this.placeArrow();
        this.changeImage();
        this.changeText();
        this.activeCount++;
      }.bind(this),
      onBackground: function(toggler, element){
        toggler.setStyles({color: base_colour, cursor: 'pointer'});
      }.bind(this)
    });

    this.text_scroller = new TextScroller(this.header_container.getElement('#splash_content_scroller'), {autoPlay: false});
    // create timer;
    this.setInterval();
    this.arrow = this.header_container.getElement('#splash_accordion_arrow');
    this.arrow_fx = new Fx.Tween(this.arrow);
  },

  setInterval: function()
  {
    clearTimeout(this.timeout);
    this.timeout = null;
    this.interval = setInterval(this.changeAccordion.bind(this), 6000);
  },

  resetInterval: function()
  {
    clearTimeout(this.timeout);
    clearInterval(this.interval);
    this.interval = null;
    this.timeout = null;
    this.timeout = setTimeout(this.setInterval.bind(this), 2000);
  },

  setSplashInterval: function()
  {
    clearTimeout(this.timeout);
    this.timeout = null;
    this.interval = setInterval(this.changeAccordion.bind(this), 6000);
  },

  changeAccordion: function()
  {
    var curr_index = this.accordion.previous;
    var next_index = curr_index+1;
    if (next_index >= this.accordion.elements.length) {
      next_index = 0;
    }
    this.accordion.display(next_index);
  },

  placeArrow: function()
  {
    clearTimeout(this.arrow_timeout);
    this.arrow_timeout = null;
    this.arrow_timeout = setTimeout(this._placeArrow.bind(this), 500);
  },

  _placeArrow: function()
  {
    clearTimeout(this.arrow_timeout);
    this.arrow_timeout = null;
    // get current location of arrow
    var pos = this.arrow.getPosition();
    // get location of current toggler
    var t_pos = this.accordion.togglers[this.accordion.previous].getPosition();
    var p_pos = this.accordion.togglers[this.accordion.previous].getOffsetParent().getPosition();

    this.arrow_fx.start('top', (t_pos.y-p_pos.y+6)+'px');
    // move arrow if needed
  },

  changeImage: function()
  {
    clearTimeout(this.image_timeout);
    this.image_timeout = null;
    this.image_timeout = setTimeout(this._changeImage.bind(this), 500);
  },

  _changeImage: function()
  {
    var index = this.accordion.previous;

    clearTimeout(this.image_timeout);
    this.image_timeout = null;
    if (this.current != index)
    {
      this.setOpaque();
      var effect1 = null;
      var effect2 = null;
      effect1 = new Fx.Tween(this.splash_images[index]);
      effect2 = new Fx.Tween(this.splash_images[this.current]);
      effect1.start('opacity',0,1);
      effect2.start('opacity',1,0);
      this.current = index;
    }
  },

  changeText: function()
  {
    clearTimeout(this.text_timeout);
    this.text_timeout = null;
    this.text_timeout = setTimeout(this._changeText.bind(this), 500);
  },

  _changeText: function()
  {
    clearTimeout(this.image_timeout);
    this.image_timeout = null;
    if (this.current > 0 || this.activeCount > 1) {
      this.text_scroller.showText(this.current);
    }
  },

  setOpaque: function ()
  {
    var i = 0;
    this.splash_images.each(function(el) {
      if (i != this.current && i != this.accordion.previous) {
        el.setOpacity(0);
      }
      i++;
    }.bind(this));
  }
});

var TextScroller = new Class({

  options: {
    margin: 5,
    autoPlay: true
  },

  container: null,
  scroller: null,
  paragraphs: null,
  paragraph_height: null,
  curr: 0,
  interval: null,
  fx: null,

  initialize: function(container, options)
  {
    this.setOptions(options);
    this.container = container;
    this.scroll = this.container.getElement('div');
    // get paragraphs
    this.paragraphs = this.container.getElements('p');
    if (this.paragraphs.length) {
      this.paragraph_height = this.paragraphs[0].getSize().y;
      if (this.options.autoPlay) {
        this.start();
      }
      this.fx = new Fx.Tween(this.scroll);
    }
  },

  nextText: function()
  {
    var next = this.curr+1;
    if (next >= this.paragraphs.length) {
      next = 0;
    }
    this.showText(next);
  },

  showText: function(idx)
  {
    var marginTop = (this.paragraph_height+this.options.margin)*idx;
    // get offset of next paragraph
    this.fx.start('margin-top', '-'+marginTop+'px');
    this.curr = idx;
  },

  start: function() {
    this.interval = setInterval(this.nextText.bind(this), 6000);
  },

  stop: function() {
    clearInterval(this.interval);
    this.interval = null;
  }
});
TextScroller.implement(new Options);

var DropDown = new Class({

  options: {
    width: null,
    padding: 4,
    dropdownDivClass:'select_div',
    dropdownBoxClass:'select_dropdown_container',
    dropdownClass:'select_dropdown',
    dropdownImageClass:'select_image',
    dropDownImage: base_url+'images/form/butt_arrow.gif',
    dropDownImageWidth: 16,
    dropDownImageHeight: 14,
    selectText: 'Select Option..',
    scrollbarBoxClass:'ddselect_dropdown_slider',
    sliderClass:'ddslider',
    sliderBackgroundClass:'ddslider_knob_bg',
    sliderKnobClass:'ddslider_knob',
    sliderKnobTopClass:'ddslider_knob_top',
    sliderKnobBottomClass:'ddslider_knob_bottom',
    scrollbarUpClass:'ddslider_up',
    scrollbarDownClass:'ddslider_down'
  },

  element: null,
  dropdown_status: false,
  dropdown: null,
  inputDiv: null,
  imageButton: null,
  dropDownOptions: [],
  selectOptions: null,
  coords: null,
  toggled: false,
  dropdownCreated: false,

  initialize: function(el, selectedIndex, options)
  {
    this.setOptions(options);

    this.element = el;
    this.element.style.display = 'inline';
    if (!this.options.width) {
      this.options.width = this.element.getWidth()-this.options.dropDownImageWidth;
    }
    this.element.style.display = 'none';
    this.selectedIndex = selectedIndex;

    this.selectDiv = $(this.element.nextSibling.nextSibling);
    this.inputDiv = $(this.selectDiv.childNodes[0]).set('html',this.options.selectText);
    this.imageButton = $(this.selectDiv.childNodes[1]);

    this.inputDiv.addEvent('click', function(e) {
      e = new Event(e).stop();
      this.toggleDropDown();
      this.toggled = true;
      document.fireEvent('click');
    }.bind(this));
    this.imageButton.addEvent('click', function(e) {
      e = new Event(e).stop();
      this.toggleDropDown();
      this.toggled = true;
      document.fireEvent('click');
    }.bind(this));

    document.addEvent('click', function(e) {
      if (this.dropdown_status && !this.toggled) {
        this.toggleDropDown();
      }
      this.toggled = false;
    }.bind(this));

		new Asset.image(base_url+'images/form/bg_dropdown.png');
  },

  toggleDropDown: function()
  {
    if (!this.dropdownCreated) {
      this.createDropDown();
    }
    if (this.dropdown_status) {
      this.dropdownBox.setStyles({'display':'none','z-index':'1'});
      this.dropdown_status = false;
    }
    else {
      // are any of my parents scrolled?
      var coords = this.selectDiv.getCoordinates();

      this.dropdownBox.setStyles({'display':'block','z-index':'1000','left':coords.left+'px','top':(coords.top+this.inputDiv.offsetHeight)+'px'});
      if (this.element.options.length > 8 && !this.dropdown_contentslider) {
        this.dropdown_contentslider = new ContentSlider(this.dropdown, this.scrollbarBox,
          {max_height: 154,
          sliderTrackClass: 'ddslider',
          sliderUpClass: 'ddslider_up',
          sliderDownClass: 'ddslider_down',
          sliderKnobClass: 'ddslider_knob',
          sliderKnobMin: 31,
          updateWidth: false});
      }
      this.dropdown_status = true;
    }
  },

  selectOption: function(index, show) {
    if (Browser.Engine.name == 'trident') {
      this.inputDiv.innerText = this.element.options[index].text;
    }
    else {
      this.inputDiv.innerHTML = this.element.options[index].text;
    }
    if (!show) {
      this.toggleDropDown();
      this.dropdown.childNodes[this.selectedIndex].setStyles({'background':'#f0f0f0','color':'#2b292a'});
    }
    this.selectedIndex = index;
    this.element.selectedIndex = index;
    this.element.fireEvent('change');
  },

  createDropDown: function() {
    this.dropdownBox = new Element('div', {'class':this.options.dropdownBoxClass,
                                       'styles':{'display':'none','position':'absolute'}});
    this.dropdown = new Element('div', {'class':this.options.dropdownClass,
                                       'styles':{'position':'relative'}});
    for (var i = 0; i < this.element.options.length; i++) {
      var new_li = new Element('div').set('html', this.element.options[i].text);

      new_li.addEvent('click', function(e) {
        e = new Event(e).stop();
        var tar = $(e.target);
        var found = false;
        var j = 0;
        var p = tar.getParent();
        while (!found && j < this.element.options.length) {
          if (tar == p.childNodes[j]) {
            found = true;
          }
          else {
            j++;
          }
        }
        this.selectOption(j);
      }.bind(this));

      new_li.addEvent('mouseenter', function() {
        this.setStyles({'background':'#f0f0f0','color':'#2b292a'});
      });
      new_li.addEvent('mouseleave', function(e) {
        e = new Event(e).stop();
        var found = false;
        var j = 0;
        var tar = $(e.target);
        var p = tar.getParent();
        while (!found && j < this.element.options.length) {
          if (tar == p.childNodes[j]) {
            found = true;
          }
          else {
            j++;
          }
        }
        if (found && j != this.selectedIndex) {
          tar.setStyles({'background':'transparent','color':'#383838'});
        }
      }.bind(this));
      new_li.injectInside(this.dropdown);
      if (this.element.options[i].selected) {
        new_li.setStyles({'background':'#f0f0f0','color':'#2b292a'});
      }
    }
    this.dropdown.inject(this.dropdownBox);
    this.dropdownBox.inject(document.body);


    this.dropdownBox.addEvent('click', function(e) {
      e = new Event(e).stop();
    }.bind(this));

    dropdown_width = (this.options.width+this.options.dropDownImageWidth);
    this.scrollbarBox = new Element('div', {'class':this.options.scrollbarBoxClass});

    var scrollbar_width = 0;
    var scrollbar_height = 0;
    if (this.element.options.length > 8) {
      dropdown_height = '154';
      scrollbar_height = '154';
      scrollbar_width = 10;
      this.dropdownBox.setStyle('height', dropdown_height+'px');

      // add the scrollbar
      this.slider = new Element('div', {'class':this.options.sliderClass});
      this.sliderKnob = new Element('div', {'class':this.options.sliderKnobClass});
      this.scrollbarUp = new Element('img', {'class':this.options.scrollbarUpClass,'src':base_url+'images/form/scroll_up.gif',width:6,height:6});
      this.scrollbarDown = new Element('img', {'class':this.options.scrollbarDownClass,'src':base_url+'images/form/scroll_down.gif',width:6,height:6});

      this.scrollbarUp.inject(this.scrollbarBox);
      this.slider.inject(this.scrollbarBox);
      this.sliderKnob.inject(this.slider);
      this.scrollbarDown.inject(this.scrollbarBox);
      select_width = dropdown_width - 14;
    }
    else {
      select_width = dropdown_width - 4;
    }
    this.dropdown.setStyle('width',select_width+'px');

    this.scrollbarBox.inject(this.dropdownBox);
    this.dropdownBox.setStyle('width', dropdown_width+'px');

    this.scrollbarBox.setStyles({width: scrollbar_width+'px', height: scrollbar_height+'px'});

    this.dropdownCreated = true;
  },

  destroyDropDown: function()
  {
    if (this.dropdownCreated) {
      // clear events
      this.dropdownBox.removeEvent('click');
      this.dropdownBox.getElements('li').each(function(li) {
        li.removeEvents(['click', 'mouseenter', 'mouseleave']);
      });
      if (this.dropdown_contentslider) {
        this.dropdown_contentslider = null;
      }
      this.dropdownBox.destroy();
      this.dropdownCreated = false;
    }
  }

});
DropDown.implement(new Options);

var ContentSlider = new Class({

  options: {
    steps: 100,
    max_height: 400,
    sliderTrackClass: 'slider',
    sliderUpClass: 'sliderUp',
    sliderDownClass: 'sliderDown',
    sliderKnobClass: 'sliderknob',
    sliderKnobMin: 26,
    updateWidth: true,
    fadeInSlider: false
  },

  old_step: 0,
  scroll: null,
  step_delta: 0,
  knobmousedown: false,
  content_box: null,
  sliderContainer: null,
  slider: null,
  sliderUp: null,
  sliderDown: null,
  sliderKnob: null,

  initialize: function(content, sliderContainer, options) {
    this.content_box = content;
    this.sliderContainer = sliderContainer;
    this.setOptions(options);
    if (this.options.fadeInSlider) {
      this.sliderContainer.setOpacity(0);
    }

    this.sliderTrack = this.sliderContainer.getElement('.'+this.options.sliderTrackClass);
    this.sliderUp = this.sliderContainer.getElement('.'+this.options.sliderUpClass);
    this.sliderDown = this.sliderContainer.getElement('.'+this.options.sliderDownClass);
    this.sliderKnob = this.sliderContainer.getElement('.'+this.options.sliderKnobClass);

    box_sh = this.content_box.scrollHeight;
    if (box_sh > this.options.max_height) {
      this.content_box.setStyle('height', this.options.max_height+'px');
      if (this.options.updateWidth) {
        this.content_box.setStyle('width', (this.content_box.getScrollSize().x-40)+'px');
      }
      this.content_box.setStyle('overflow', 'hidden');
      this.sliderTrack.setStyle('display', 'block');
      this.sliderTrack.setStyle('height', (this.options.max_height-2*15)+'px');
      this.sliderTrack.setStyle('margin-bottom', '0px');
      this.sliderUp.setStyle('display', 'block');
      this.sliderDown.setStyle('display', 'block');
      this.sliderknob_h = this.sliderTrack.getStyle('height').toInt() * this.content_box.getStyle('height').toInt() / box_sh;
      if (this.sliderknob_h < this.options.sliderKnobMin) { this.sliderknob_h = this.options.sliderKnobMin; }
      this.sliderKnob.setStyle('height', Math.floor(this.sliderknob_h)+'px');
      if (this.options.fadeInSlider) {
        this.sliderContainer.set('tween', {duration: 1000});
        this.sliderContainer.tween('opacity', 1);
      }
      this.scroll = new Fx.Scroll(this.content_box, {
        wait: false,
        duration: 1000
      });

      this.slider = new Slider(this.sliderTrack, this.sliderKnob, {
        steps: this.options.steps,
        mode: 'vertical',
        onChange:this.sliderChange.bind(this)
      }).set(0);

      this.sliderKnob.addEvent('mousedown', function(e) {if (e.target == this.sliderKnob) {this.knobmousedown = true;}}.bind(this));
      this.sliderKnob.addEvent('mouseup', function(e) {if (e.target == this.sliderKnob) {this.knobmousedown = false;}}.bind(this));

      this.step_delta = this.options.steps * 100/this.content_box.scrollHeight
      this.slider.addEvent('mousewheel', function(e) {
        e = new Event(e).stop();
        var delta_steps = Math.floor(e.wheel * this.step_delta);
        this.slider.set(this.slider.step-delta_steps);
      }.bind(this));

      this.content_box.addEvent('mousewheel', function(e) {
        e = new Event(e).stop();
        var delta_steps = Math.floor(e.wheel * this.step_delta);
        this.slider.set(this.slider.step-delta_steps);
      }.bind(this));

      this.sliderUp.addEvent('click', function(e) {
        e = new Event(e).stop();
        var delta_steps = 2 * this.step_delta;
        this.slider.set(this.slider.step-delta_steps);
      }.bind(this));

      this.sliderDown.addEvent('click', function(e) {
        e = new Event(e).stop();
        var delta_steps = 2 * this.step_delta;
        this.slider.set(this.slider.step+delta_steps);
      }.bind(this));

      this.content_box.getElements('a').each(function(el){
        href = el.getAttribute('href');
        if (href && String.contains(href, '#')) {
          parts = href.split('#');
          if (parts.length > 1 && parts[1] != '') {
            el.addEvent('click', function(e){
              e = new Event(e).stop();
              this.knobmousedown = false;
              var target = el.getAttribute('href').split('#')[1];
              this.scrollToTarget(target);
            }.bind(this));
          }
        }
      }.bind(this));

    }
    else {
      this.content_box.setStyle('height', this.options.max_height+'px');
    }

    if(this.options.initialize) this.options.initialize.call(this);
  },

  changeScrollHeight: function() {
    if (this.content_box.scrollHeight > this.options.max_height) {
      this.sliderknob_h = this.sliderTrack.getStyle('height').toInt() * this.content_box.getStyle('height').toInt() / this.content_box.scrollHeight;
      if (this.sliderknob_h < 26) { this.sliderknob_h = 26; }
      this.sliderKnob.setStyle('height', Math.floor(this.sliderknob_h)+'px');
      this.step_delta = this.options.steps * 100/this.content_box.scrollHeight
    }
  },

  sliderChange: function () {

    if (this.scroll && this.slider) {
      this.scroll.cancel();
      var y = (this.content_box.scrollHeight-this.content_box.getStyle('height').toInt())*this.slider.step/this.options.steps;
        this.content_box.scrollTo(0, y)
      this.old_step = this.slider.step;
    }
  },

  scrollToTarget: function (target) {
    found = false;
    as = this.content_box.getElements('a');
    ao = null;
    j = 0;
    while (!found && j < as.length) {
      if (as[j].getAttribute('name') == target) {
        found = true;
        ao = as[j];
      }
      j++;
    }
    if (ao) {
      this.scroll.cancel();
      this.slider.set(this.options.steps*ao.offsetTop/(this.content_box.scrollHeight-this.content_box.offsetHeight));
    }
  }


});
ContentSlider.implement(new Options);

function checkEmail (strng) {
  var valid = true;
  if (strng == "") {
     valid = false;
  }
  else if (!isRFC822ValidEmail(strng)) {
     valid = false;
  }
  return valid;
}
function isRFC822ValidEmail(sEmail) {

  var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  var sQuotedPair = '\\x5c[\\x00-\\x7f]';
  var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
  var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
  var sDomain_ref = sAtom;
  var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
  var sWord = '(' + sAtom + '|' + sQuotedString + ')';
  var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
  var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
  var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
  var sValidEmail = '^' + sAddrSpec + '$'; // as whole string

  var reValidEmail = new RegExp(sValidEmail);

  if (reValidEmail.test(sEmail)) {
    return true;
  }

  return false;
}
