jQuery.noConflict();
jQuery(function(){
	initClear();
	hoverForIE6('.search-form .btn-submit', 'hover');
//	initAddClass();
	initFocus();
	initPopup();
})

// Language selector dropdown
jQuery(function(){
  jQuery('.dropdown').mouseenter(function(){
  jQuery('.sublinks').stop(false, true).hide();

 var submenu = jQuery('.sublinks');

 submenu.css({
  position:'absolute',
  top: jQuery(this).offset().top + jQuery(this).height() + 'px',
  left: jQuery(this).offset().left + 'px',
  zIndex:1000
  });

 submenu.stop().slideDown(300);

 submenu.parent().mouseleave(function(){
  submenu.slideUp(300);
  });
  });
  });

// Highlight the search icon when field has focus
jQuery('.nav-bar form input.txt').focus(function(){
    jQuery(this).parent().addClass('focus');
}).blur(function(){
    jQuery(this).parent().removeClass('focus');
});

// initPopup
function initPopup () {
	var activeBox = null,
		activeClass = 'btn-block-open', // cart-box-area-open
		holders = jQuery ( '#content .btn-block' ); // .cart-box-area
	
	holders.each ( function () {
		var holder = jQuery ( this ),
			opener = jQuery ( '.cart-box', this ),
			popup = jQuery ( '.cart-popup', this ),
			close = jQuery ( '.close', this );
		
		opener.click ( function () {
			holders.not ( holder ).removeClass ( activeClass );
			if ( holder.hasClass ( activeClass ) ) {
				holder.removeClass ( activeClass );
				activeBox = null;
			}
			else {
				holder.addClass ( activeClass );
				activeBox = holder;
			}
			return false;
		})
		
		close.click ( function () {
			holder.removeClass ( activeClass );
			activeBox = null;
			return false;
		})
	})
	
	jQuery ( document ).bind ( 'click', function ( e ) {
		if ( activeBox ) {
			e = e || window.event;
			
			var target = e.target || e.srcElement;
			var popup = activeBox.get(0);
			
			while ( target.parentNode && target.tagName.toLowerCase () != 'body' ) {
				if ( target == popup ) return;
				
				target = target.parentNode;
			}
			
			activeBox.removeClass ( activeClass );
			activeBox = null;
		}
	})
}

// initaddClass
function initAddClass() {
	addClass({
		tagName:'a',
		tagClass:'btn-link',
		classAdd:'open',
		addToParent:true
	})
}

// hover for IE
function hoverForIE6(_list, _class) {
	var _hoverClass = 'hover';
	if(_class) _hoverClass = _class;
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		jQuery(_list).hover(function(){
			jQuery(this).addClass(_hoverClass);
		},function(){
			jQuery(this).removeClass(_hoverClass);
		});
	}
}

// initFocus
function initFocus(){
    var inputs = document.getElementsByTagName("input");
    var labels = document.getElementsByTagName("label");
    for (var i=0; i<inputs.length; i++){
        if (inputs[i].type == "radio" || inputs[i].type == "radio"){
            inputs[i].onclick = function (){
                for (var j=0; j<labels.length; j++){
                    if(this.id == labels[j].htmlFor){
                        if(labels[j].parentNode.className.indexOf("focus") == -1){
                            labels[j].parentNode.className += " focus";
                        }
                        else if(this.type != "radio")
                        {
                            labels[j].parentNode.className = labels[j].parentNode.className.replace("focus", "");
                        }
                    }
                    else if(this.type == "radio" && this.name == document.getElementById(labels[j].htmlFor).name){
                        labels[j].parentNode.className = labels[j].parentNode.className.replace("focus", "");
                    }
                }
            }
            if(inputs[i].checked == true){
                for (var j=0; j<labels.length; j++)
                {
                    if(inputs[i].id == labels[j].htmlFor)
                        labels[j].parentNode.className += " focus";
                }
            }
        }
    }
}
// addClass
function addClass (_options) {
	var _tagName = _options.tagName;
	var _tagClass = _options.tagClass;
	var _classAdd = _options.classAdd;
	var _addToParent = false || _options.addToParent;
	var _el = document.getElementsByTagName(_tagName);
	if (_el) {
		for (var i=0; i < _el.length; i++) {
			if (_el[i].className.indexOf(_tagClass) != -1) {
				_el[i].onclick = function() {
					if (_addToParent) {
						if (this.parentNode.className.indexOf(_classAdd) == -1) {
							this.parentNode.className += ' '+_classAdd;
						} else {
							this.parentNode.className = this.parentNode.className.replace(_classAdd,'');
						}
					} else {
						if (this.className.indexOf(_classAdd) == -1) {
							this.className += ' '+_classAdd;
						} else {
							this.className = this.className.replace(_classAdd,'');
						}
					}
					return false;
				}
			}
		}
	}
}
// clear inputs
function initClear(){
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: false,
		addClassFocus: "focus",
		addClassFont: 'font',
		filterClass: "default"
	});
}
function clearFormFields(o){
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (o.addClassFont == null) o.addClassFont = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = jQuery('.cart-table input');
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
					if(o.addClassFont && this.className.indexOf(o.addClassFont) == -1) {
						this.className += " " + o.addClassFont;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
						if(o.addClassFont && this.className.indexOf(o.addClassFont) != -1) {
							this.className = this.className.replace(o.addClassFont, "");
						}
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = jQuery('.cart-table textarea');
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass) == -1) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
					if(o.addClassFont && this.className.indexOf(o.addClassFont) == -1) {
						this.className += " " + o.addClassFont;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}

}
