if (!timers)
	var timers = {};
	
if (!e)
	var e = {};

// Add validation
Validation.defaultOptions.immediate = true;
Validation.defaultOptions.addClassNameToContainer = true;

// EaseFromTo (adapted from "Quart.EaseInOut")
Effect.Transitions.EaseFromTo = function(pos) {
if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,4);
return -0.5 * ((pos-=2)*Math.pow(pos,3) - 2);
};
// EaseFrom (adapted from "Quart.EaseIn")
Effect.Transitions.EaseFrom = function(pos) {
return Math.pow(pos,4);
};
// EaseTo (adapted from "Quart.EaseOut")
Effect.Transitions.EaseTo = function(pos) {
return Math.pow(pos,0.25);
};

var Util = {};
Util.onHover = function(){
	if (!this.hasClassName('selected'))	this.addClassName('over');
};
Util.offHover = function(){
	if (!this.hasClassName('selected')) this.removeClassName('over');
};

var prototipOffset = {x: 0, y: 0};
if (Prototype.Browser.IE) {
	if ($$('html.ie7').length) {
		prototipOffset = {x: -695, y: 0};
	}
}

var Autofill = new Class.create({
	initialize: function(element, placeholderText) {
		var el = $(element);
		
		this.id = element;
		this.blurClass = 'blur';
		this.origValue = el.getValue();
		this.placeholder = placeholderText;
		
		// Check for HTML 5
		if (Modernizr.input.placeholder && !el.readAttribute('placeholder')) {
			el.writeAttribute('placeholder', this.placeholder);
			return el;
		}

		this.blur(el.addClassName('autofill'));
		
		el
			.observe('keyup', this.keypress.bind(this))
			.observe('focus', this.focus.bind(this))
			.observe('blur', this.blur.bind(this));
			
		return el;
	},
	focus: function(e) {
		el = e.element();
		el.removeClassName(this.blurClass);
		if (el.getValue() == this.placeholder) {
			el.clear();
		}
	},
	blur: function(e) {
		el = e.element? e.element(): e;
		if (!el.getValue()) {
			el
				.addClassName(this.blurClass)
				.setValue(this.placeholder);
		}
	},
	keypress: function(e) {
		if (e.keyCode == Event.KEY_ESC) {
			e.element().setValue(this.origValue).blur();
		}
	}
});

document.observe('dom:loaded', function(){
	// Simplify home page title for iOS devices so that webclips have a better default title.
	if (document.location.pathname == '/' && Modernizr.touch) {
		document.title = document.title.replace(/\s+.*$/, '');
	}

	if ($('category-header') && false) {
		$('category-header').setStyle({
			opacity: 0,
			display: 'block'
		});
		$$('#category-header img')[0].observe('load', function(el){
			Effect.Fade(this.parentNode, {
				from: 0,
				to: 1
			});
		});
	}
	
	if ($$('.checkout-cart-index #co-shipping-method-form').length) {
		new Form.Observer('co-shipping-method-form', 0.3, function(el){
			el.submit();
		});
	}

	$$('.radio-list li')
		.invoke('observe', 'mouseover', Util.onHover)
		.invoke('observe', 'mouseout', Util.offHover)
		.invoke('observe', 'click', function(e) {
			var input = e.element().getElementsBySelector('input')[0];
			if (input) {
				input.checked = true;
				fireEvent(input, 'click');
			}
		});
	
	if ($('privacy-security')) {
		var psbox;
		var psBoxContent = $('privacy-security').innerHTML;
		$('privacy-security').remove();
		$$('body')[0].insert('<div id="privacy-security" style="display: none"><a href="." id="video-close"><img src="http://cdn.danner.com/skin/frontend/lfi/danner/images/close-btn.png" alt="Close"></a>'+psBoxContent+'</div>');

		if ($('video-close')) {
			$('video-close').observe('click', function(e){
				if (psbox) {
					psbox.close();
				}
				Event.stop(e);
			});
		}

		if ($$('.checkout-cart-index .security').length) {
			psbox = new Control.Modal($$('.checkout-cart-index .security')[0], {
				overlayOpacity: 0.5,
				className: 'modal',
				fade: true,
				width: 722,
				height: 480,
				iframeshim: false,
				beforeOpen: function(){
					$('ps-scroll-content').setOpacity(0);
				},
				afterOpen: function(){
					fleXenv.fleXcrollMain('ps-scroll-content');
					new Effect.Fade('ps-scroll-content', {
						duration: 0.5,
						from: 0,
						to: 1
					});
				}
			});
		
		}
	}
	
	// Main navigation
	// Prototype's CSS selector engine doesn't seem to be able to lookup 'nav > ul > li' in IE7.
	// Instead we will look up these explicitly called out menus.
	var navMenus = $$('li.nav-menu-item');
	if (navMenus.length) {
		navMenus.each(function(t) {
			t.observe('mouseenter', function(){
				var el = this.childElements().last();

				// check for mouseout timeout function
				if (timers[el.identify()+'_out']) timers[el.identify()+'_out'] = clearTimeout(timers[el.identify()+'_out']);
				timers[el.identify()+'_over'] = setTimeout(function(){
					if (e[el.identify()]) return;
					
					if (!Prototype.Browser.IE) {
						e[el.identify()] = new Effect.Fade(el, {
							from: 0,
							to: 0.99,
							duration: 0.2,
							beforeStart: function(ef){
								ef.element.setStyle({
									display: 'block',
									opacity: 0
								});
							}.bind(this)
						
						});
					} else {
						el.setStyle({display: 'block'});
					}

					this.addClassName('over');
					// el.previous().addClassName('over');
				}.bind(this), 300);
				
			});
			t.observe('mouseleave', function(){
				var el = this.childElements().last();
				
				if (timers[el.identify()+'_over']) timers[el.identify()+'_over'] = clearTimeout(timers[el.identify()+'_over']);
				timers[el.identify()+'_out'] = setTimeout(function(){
					if (e[el.identify()]) e[el.identify()].cancel();
					if (!Prototype.Browser.IE) {
						e[el.identify()] = new Effect.Fade(el, {
							from: el.getOpacity(),
							to: 0,
							duration: 0.2,
							queue: {
								scope: el.identify(),
								position: 'end'
							},
							afterFinish: function(){
								e[el.identify()] = false;
								el.writeAttribute('style', 'display: none');
								el.previous().removeClassName('over');
							}
						});
					} else {
						el.setStyle({display: 'none'});
					}

					this.removeClassName('over');
				}.bind(this), 300);
			});
		});
	}
	
	// Action buttons
	var actionButtons = $$('a.button');
	if (actionButtons.length) {
		actionButtons.each(function(t){
			// Leave out buttons with onclicks specified in DOM
			if (t.readAttribute('onclick') || t.readAttribute('href')) return;
			
			t.observe('click', function(ev){
				if (dataForm) {
					dataForm.submit();
				} else {
					t.up('form').submit();
				}
				ev.stop();
			});
		});
	}
		
});

function newWin(url, w, h, winName) {
	window.open(url, "popup", 'width='+w+',height='+h+',scrollbars=yes,location=0,statusbar=0,menubar=0').focus();
}

/**
 * Formats currency using patern
 * format - JSON (pattern, decimal, decimalsDelimeter, groupsDelimeter)
 * showPlus - true (always show '+'or '-'),
 *      false (never show '-' even if number is negative)
 *      null (show '-' if number is negative)
 */

function formatCurrency(price, format, showPlus){
    precision = isNaN(format.precision = Math.abs(format.precision)) ? 2 : format.precision;
    requiredPrecision = isNaN(format.requiredPrecision = Math.abs(format.requiredPrecision)) ? 2 : format.requiredPrecision;

    //precision = (precision > requiredPrecision) ? precision : requiredPrecision;
    //for now we don't need this difference so precision is requiredPrecision
    precision = requiredPrecision;

    integerRequired = isNaN(format.integerRequired = Math.abs(format.integerRequired)) ? 1 : format.integerRequired;

    decimalSymbol = format.decimalSymbol == undefined ? "," : format.decimalSymbol;
    groupSymbol = format.groupSymbol == undefined ? "." : format.groupSymbol;
    groupLength = format.groupLength == undefined ? 3 : format.groupLength;

    if (showPlus == undefined || showPlus == true) {
        s = price < 0 ? "-" : ( showPlus ? "+" : "");
    } else if (showPlus == false) {
        s = '';
    }

    i = parseInt(price = Math.abs(+price || 0).toFixed(precision), 10) + "";
    pad = (i.length < integerRequired) ? (integerRequired - i.length) : 0;
    while (pad) { i = '0' + i; pad--; }

    j = (j = i.length) > groupLength ? j % groupLength : 0;
    re = new RegExp("(\\d{" + groupLength + "})(?=\\d)", "g");

    /**
     * replace(/-/, 0) is only for fixing Safari bug which appears
     * when Math.abs(0).toFixed() executed on "0" number.
     * Result is "0.-0" :(
     */
    r = (j ? i.substr(0, j) + groupSymbol : "") + i.substr(j).replace(re, "$1" + groupSymbol) + (precision ? decimalSymbol + Math.abs(price - i).toFixed(precision).replace(/-/, 0).slice(2) : "");

    if (format.pattern.indexOf('{sign}') == -1) {
        pattern = s + format.pattern;
    } else {
        pattern = format.pattern.replace('{sign}', s);
    }

    return pattern.replace('%s', r).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};

