if(typeof MooTools != "undefined"){
	window.addEvent('domready',generalDomLoaded);
}

function generalDomLoaded(){
	var textInputs = $ES('input.text');
	var textarea = $ES('textarea');
	if(textInputs.length > 0) {
		textInputs.each(initInput); 
	}
	if(textarea.length > 0) {
		textarea.each(initInput); 
	}
	// we probably won't use this after, good code though...
	// checkForErrors();
	enterDiscountCode();
    mySmoothbox();
	findSubmitButtons();
    //run if safari
    if(window.webkit) safariFixes();
}

function updateQuantity() {
	$('event').value = 'cart.update';
	document.cartForm.submit();
}

function initInput(el){
	if(!(el.hasClass('noClearx'))){
	
		var defaultValue = el.getValue();
		
		el.addEvent('focus',function () {
			if(this.getValue() == defaultValue){
				this.value = "";
			}
		});
		el.addEvent('blur',function (){
			if(this.getValue().clean() == "") {
				this.value = defaultValue;
			} else {
				this.value = this.getValue().clean();
			}
		});
	    
	    charCountLimit(el,defaultValue);
    }
    
}

function enterDiscountCode(){
	if($('discountForm')){
		var discountButton =  $('enterDiscountCode');
		var body = $$('body')[0];
		
		var form = $('discountForm');
		form.setStyles({
				'position': 'absolute',
				'z-index': '1010',
				'display': 'block',
				'opacity': 0,
				'height': '1px' 
		});
		
		var applyDiscountBtn = $('showDiscount');
		applyDiscountBtn.addEvent('click',function(e){
				new Event(e).stop();
				form.submit();
		});
		var closeBtn = $ES('a.closeBtn',form)[0];
		closeBtn.addEvent('click',function(e){
				new Event(e).stop();
				removeOverlay();
		});
		var crds = form.getCoordinates();
		var overlay = createOverlay();
		
		
		if(discountButton){
			discountButton.addEvent('click',function(e){
					new Event(e).stop();
					showOverlay(overlay);
					form.injectInside(body);
					centerEl(form);
					
					new Fx.Styles(form,{duration: 500}).start({
							'opacity': [0,1],
							'height': [3,150]
					});
			});
		}
	}
}

function mySmoothbox() {
	if($('showBackofCard')) {
		var showBackofCardBtn =  $('showBackofCard');
		var body = $$('body')[0];
		
		var imgHolder = $('cvv2Help');
			imgHolder.setStyles({
					'position': 'absolute',
					'z-index': '1010',
					'display': 'block',
					'opacity': 0,
					'height': '1px' 
			});
		var closeBtn = $ES('a.closeBtn',imgHolder)[0];
			closeBtn.addEvent('click',function(e){
				new Event(e).stop();
				removeOverlay();
			});
		var crds = imgHolder.getCoordinates();
		var overlay = createOverlay();
		
		if(showBackofCardBtn){
			showBackofCardBtn.addEvent('click',function(e){
					new Event(e).stop();
					showOverlay(overlay);
                    imgHolder.injectInside(body);
					centerEl(imgHolder);
					
					new Fx.Styles(imgHolder,{duration: 500}).start({
							'opacity': [0,1],
							'height': [3,215]
					});
			});
		}
	}	
	
}


function removeOverlay(els) {
	if(typeof els != "undefined"){
		els.each(function (el){
			if(typeof el !="undefined"){
				el.remove();
			}
		});
	} 
	else if($('overlay').getNext()){
	     $('overlay').getNext().setStyles({
	             'opacity':0,
	             'height':'1px'
	     });
    }
	$('overlay').remove();
}

function centerEl(el){
	var body = $$('body')[0];
	var parentCrds = body.getCoordinates();
	var targetCrds = el.getCoordinates();
	var top = (parentCrds.height.toInt() - targetCrds.height.toInt()) / 2; 
	var left = (parentCrds.width.toInt() - targetCrds.width.toInt()) / 2; 
	
	el.setStyles({
			'top': top+"px",
			'left': left+"px"
	})
	
}
function createOverlay(){
	var overlay = new Element('div',{
			styles:{
				'position': 'absolute',
				'width': window.getScrollWidth()+"px",
				'height': window.getScrollHeight()+"px",
				'background-color': "#000000",
				'z-index': "1000",
				'top':0,
				'left':0,
				'opacity': 0
			},
			events:{
				'click':function(){
				   removeOverlay();
				}
			},
			id:'overlay'
	});
	return overlay;
}
function showOverlay(overlay){
	var body = $$('body')[0];
	overlay.injectInside(body);
	overlay.effect('opacity',{duration: 500, onStart: function(){
		if(window.ie6){
			toggleSelect();
		}
	}}).start(0,.5);
}
function toggleSelect(){
	$ES('select').each(function(sel){
		sel.toggleClass('invisible')
		});
}

function checkForErrors(){
	var error;
var body = $$('body')[0];
	//create overlay and a reference to it //
	var overlay = createOverlay();
	
	if($('messageTxt') && $('messageTxt').hasClass('error')){
        
		showOverlay(overlay);
		error = $('messageTxt');
		error.setStyles({
				'cursor':'pointer',
				'position':'absolute',
				'z-index':'1010',
				'background-color':'#FFFFFF',
				'border':'5px solid red',
				'display':'block',
				'width':'300px',
				'font-size': '12px',
				'opacity': 0,
				'height':'175px'
				
		});
		centerEl(error);
		var errorMsg = error.innerHTML;
		var header = "";
		if(error.hasClass('error')){
			header = "There has been an error:";
		}
		else{
			header = "Notice:";
		}
		error.empty();
		
		
		//CREATE HEADER FOR ERROR OR WARNING
		var h2 = new Element('h2',{
				styles:{
					'background-color':'#E9E9E9',
					'padding':'5px 0',
					'font-size': '12px',
					'color':'#333333'
				}
		});
		h2.setHTML(header);
		h2.injectInside(error);
		
		
		//CREATE PARAGRAPH FOR MESSAGE
		var p = new Element('p');
		p.setHTML(errorMsg);
		p.injectInside(error);
		
		centerEl(error);
		error.injectInside(body);
		
		(function(){error.effect('opacity',{duration: 500}).start(0,1)}).delay(600);
		
		error.addEvent('click',function(){
				removeOverlay([error, overlay]);
		})
		
	}
}

function charCountLimit(el,defaultValue){
    if(el.hasClass('charCounted') && el.getAttribute('maxlength')){
           var i = 0;
           var body = $ES('body')[0];
           
           var elCrds = el.getCoordinates();
           var top = elCrds.top.toInt();
           var left = elCrds.right.toInt();
           
           var charLimit = el.getAttribute('maxlength');
           var counter = new Element('div',{
                   styles:{
                       'position': 'absolute',
                       'z-index': '50',
                       'border': '1px solid #333',
                       'background-color': '#FFFFFF',
                       'opacity':0,
                       'top': top+"px",
                       'left': (left+10)+"px"
                   }
           });
           counter.injectInside(body);
           
           el.addEvent('keypress',function(e){
                   //el.getValue().length.
                   var event = new Event(e);
                   var value = el.getValue();
                   
                   if(value.length == 0){
                       counter.effect('opacity',{duration: 500}).start(1,0);
                   }
                   else if(value.length == 1){
                       counter.effect('opacity',{duration: 500}).start(0,1);
                   }
                   if(value == defaultValue.clean()){
                       counter.effect('opacity',{duration: 500}).start(1,0);
                       counter.innerHTML = "0 / "+charLimit;
                   }
                   counter.innerHTML = value.length +" / "+ charLimit;
                   if(value.length < charLimit){
                       el.removeClass('error');
                   }
                   if(value.length >= charLimit){
                       if(value.length > charLimit){
                           el.addClass('error');
                       }
                       if(event.key == "backspace" || event.key == "delete"){
                       }
                       else if(event.key !="backspace" || event.key !="delete"){
                           event.stop();
                           //alert('stop')
                       }
                   }
                    
           });
    }
}


function safariFixes(){
    if($('home_hand_tout')){
        var hand = $('home_hand_tout');
        var frame = $('frame');
        var frameCrds = frame.getCoordinates();
        var frameBottom = frameCrds.bottom;
        
        hand.setStyle('top',(frameBottom-210)+"px");
    }
        
        
        
}
function findSubmitButtons(){
	var allForms = $ES("form");
	allForms.each(function(xForm) {
		var submits = xForm.getElementsBySelector('a.submit_btn');
		submits.each(function(xSubmit) {
			xSubmit.setProperty('parentForm',xForm.id);
			xSubmit.addEvent('click',function() {
				// just grab the next model-glue event from the anchor title
				var parentForm = $(this.getProperty('parentForm'));
				var buttonEvent = this.id;
				$('event').value = buttonEvent;
				parentForm.submit();
			});
		});
	});
}		
/*		var veryNice = xForm;
		submits.each(function(subButton,veryNice) {
			console.log(arguments);
			var myVar1 = veryNice;
			subButton.addEvent('click',function(myVar1) {
				// just grab the next model-glue event from the anchor title
				console.log(arguments);
				var mybtn = subButton;
				var currentForm = xForm;
				var currentEvent = $('event',xForm);
				if(currentEvent) {
					currentEvent.value = mybtn.id;
					blah = currentForm;
					asdfasdf= blah;
					//document.getElementById(currentForm[0].id).submit();
					//currentForm[0].submit();
				}
			});
		})*/
