// Hover Behavior for lowpro use
var Hover = Behavior.create();
Object.extend(Hover.prototype, {
  initialize: function(className) {
    this.className = className || 'over';
  },
  onmouseover: function() {
    this.element.addClassName(this.className);
  },
  onmouseout: function() {
    this.element.removeClassName(this.className);
  }
});

var ClickableArea = Behavior.create();
Object.extend(ClickableArea.prototype, {
    onclick: function() {
        link = this.element.down('a'); // Find first link
        if (link) { location.href = link.href; }
    }
});

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];}}
}


// EXTRA LIB

if (Prototype.Browser.IE) Prototype.Browser[window.XMLHttpRequest ? 'IE7' : 'IE6'] = true;

// PRELOAD IMAGES

var BackgroundPreloader = Class.create();

BackgroundPreloader.prototype =
{
	initialize: function()
	{
		if (!arguments || !arguments.length) return;
		
		this.classes = new Array();		
		for (var i = 0; i < arguments.length; i++) this.classes.push(arguments[i]);

		if (document.styleSheets && document.styleSheets.length)
		{	
			for (var i=0; i<document.styleSheets.length; i++)
			{
				var rules = document.styleSheets[i].cssRules ? 
					document.styleSheets[i].cssRules : document.styleSheets[i].rules;
				if (!rules || !rules.length) continue;
		
				for (var j=0; j<rules.length; j++) this.rule(rules[j]);					
			}
		}
	},
	
	rule: function(rule)
	{
		if (!rule.style) return;

		for (var k=0; k<this.classes.length; k++)
		{
			var img = new Image();
			if (rule.selectorText.match(new RegExp(this.classes[k])))
			{
				if (rule.style.cssText) 
				{
					var matches = rule.style.cssText.match(/url\((.*?)\)/);
					if (matches && matches[1])
						img.src = (matches[1].match(/^\.\.\//)) ? 
									matches[1].substr(3) : matches[1];

					if (Prototype.Browser.IE6)
					{
						var matches = rule.style.cssText.match(/src="(.*?)"/);
						if (matches && matches[1]) img.src = matches[1];
					}
				}
			}
		}
	}
};


// CLEAR TEXT INPUTS ONFOCUS

var InputClear = Class.create();

InputClear.prototype =
{
	initialize: function()
	{
		for (var i = 0; i < arguments.length; i++) {
			$$(arguments[i]).invoke('observe', 'focus', function(e) {
				var x = Event.element(e);
				if (x.value == x.defaultValue) x.value = '';
			});			
		}
	}
}

// GLOBAL ACCORDION BOXES

var Accordion = Class.create();

Accordion.prototype =
{
	initialize: function(s)
	{
		var x = $$(s);
		if (!x || !x.length) return;

		this.container = x[0];	
		this.open = false;	
		this.options = this.container.getElementsBySelector('.option');
		this.drawers = this.container.getElementsBySelector('.option .items');
		
		this.options.each(function(x, i) {
			if (x.hasClassName('on')) this.open = i;
			var handle = x.getElementsBySelector('.section a:not(.edit)');
			handle.invoke('observe', 'click', function(e) { this.slide(e, i) }.bind(this));
		}.bind(this));
	},
	
	slide: function(e, i)
	{
		Event.stop(e);		
		if (this.halt || this.open === i) return;
		this.halt = true;
		
		this.options[i].addClassName('on');
		var effects = [new Effect.BlindDown(this.drawers[i], {sync:true})];
		if (this.open !== false) {
			this.drawers[this.open].setStyle({overflow: 'hidden'});			
			effects.push(new Effect.BlindUp(this.drawers[this.open], {sync:true}));
		}

		new Effect.Parallel(effects, {duration: 0.2, afterFinish: function() {
			if (this.open !== false) this.options[this.open].removeClassName('on');			
			this.open = i;
			delete this.halt;
		}.bind(this)});
	}
}

// BROWSER FIXES

if (Prototype.Browser.IE6)
	try {document.execCommand("BackgroundImageCache", false, true);} catch (e){};

// Safari 2 won't style form elements
if (Prototype.Browser.WebKit && !window.devicePixelRatio)
{
	// DOM exceptions any other way so resort to document.write
  	document.write('<link rel="stylesheet" type="text/css" href="/stylesheets/common_safari.css" />');
}

if (Prototype.Browser.WebKit && window.devicePixelRatio)
{
    document.write('<link rel="stylesheet" type="text/css" href="/stylesheets/common_safari3.css" />');
}