j$(document).ready(function(){

    j$("#product_addtocart_form").addClass("niceform");
	j$("#form-validate").addClass("niceform");
	j$("#co-billing-form, #co-shipping-method-form, #co-payment-form").addClass("niceform");
	j$(".form-list").addClass("niceform");
/*
var clonedSource = $('#source1').clone(true); //clone the source
$('#source1').replaceWith($('#source2'));  //replace the source1 in
position with the 'swapped' source
$('#source2Container').append(clonedSource);  //append cloned source1
in the position of source2 
*/

var clonedSource = j$('validation-advice').clone(true);
j$('validation-advice').replaceWith($('jNiceSelectWrapper'));
j$('jNiceSelectWrapper').append(clonedSource); 
});





/*
 * jNice
 * version: 1.0 (11.26.08)
*/
(function(j$){
	j$.fn.jNice = function(options){
		var self = this;
		var safari = j$.browser.safari; /* We need to check for safari to fix the input:text problem */
		/* Apply document listener */
		j$(document).mousedown(checkExternalClick);
		/* each form */
		return this.each(function(){
			j$('input:submit, input:reset, input:button', this).each(ButtonAdd);
			j$('button').focus(function(){ j$(this).addClass('jNiceFocus')}).blur(function(){ j$(this).removeClass('jNiceFocus')});
			j$('input:text:visible, input:password', this).each(TextAdd);
			/* If this is safari we need to add an extra class */
			if (safari){j$('.jNiceInputWrapper').each(function(){j$(this).addClass('jNiceSafari').find('input').css('width', j$(this).width()+11);});}
			j$('input:checkbox', this).each(CheckAdd);
			j$('input:radio', this).each(RadioAdd);
			j$('select', this).each(function(index){ SelectAdd(this, index); });
			/* Add a new handler for the reset action */
			j$(this).bind('reset',function(){var action = function(){ Reset(this); }; window.setTimeout(action, 10); });
			j$('.jNiceHidden').css({opacity:0});
		});
	};/* End the Plugin */

	var Reset = function(form){
		var sel;
		j$('.jNiceSelectWrapper select', form).each(function(){sel = (this.selectedIndex<0) ? 0 : this.selectedIndex; j$('ul', j$(this).parent()).each(function(){j$('a:eq('+ sel +')', this).click();});});
		j$('a.jNiceCheckbox, a.jNiceRadio', form).removeClass('jNiceChecked');
		j$('input:checkbox, input:radio', form).each(function(){if(this.checked){j$('a', j$(this).parent()).addClass('jNiceChecked');}});
	};

	var RadioAdd = function(){
		var j$input = j$(this).addClass('jNiceHidden').wrap('<span class="jRadioWrapper jNiceWrapper"></span>');
		var j$wrapper = j$input.parent();
		var j$a = j$('<span class="jNiceRadio"></span>');
		j$wrapper.prepend(j$a);
		/* Click Handler */
		j$a.click(function(){
				var j$input = j$(this).addClass('jNiceChecked').siblings('input').attr('checked',true);
				/* uncheck all others of same name */
				j$('input:radio[name="'+ j$input.attr('name') +'"]').not(j$input).each(function(){
					j$(this).attr('checked',false).siblings('.jNiceRadio').removeClass('jNiceChecked');
				});
				return false;
		});
		j$input.click(function(){
			if(this.checked){
				var j$input = j$(this).siblings('.jNiceRadio').addClass('jNiceChecked').end();
				/* uncheck all others of same name */
				j$('input:radio[name="'+ j$input.attr('name') +'"]').not(j$input).each(function(){
					j$(this).attr('checked',false).siblings('.jNiceRadio').removeClass('jNiceChecked');
				});
			}
		}).focus(function(){ j$a.addClass('jNiceFocus'); }).blur(function(){ j$a.removeClass('jNiceFocus'); });

		/* set the default state */
		if (this.checked){ j$a.addClass('jNiceChecked'); }
	};

	var CheckAdd = function(){
		var j$input = j$(this).addClass('jNiceHidden').wrap('<span class="jNiceWrapper"></span>');
		var j$wrapper = j$input.parent().append('<span class="jNiceCheckbox"></span>');
		/* Click Handler */
		var j$a = j$wrapper.find('.jNiceCheckbox').click(function(){
				var j$a = j$(this);
				var input = j$a.siblings('input')[0];
				if (input.checked===true){
					input.checked = false;
					j$a.removeClass('jNiceChecked');
				}
				else {
					input.checked = true;
					j$a.addClass('jNiceChecked');
				}
				return false;
		});
		j$input.click(function(){
			if(this.checked){ j$a.addClass('jNiceChecked'); 	}
			else { j$a.removeClass('jNiceChecked'); }
		}).focus(function(){ j$a.addClass('jNiceFocus'); }).blur(function(){ j$a.removeClass('jNiceFocus'); });

		/* set the default state */
		if (this.checked){j$('.jNiceCheckbox', j$wrapper).addClass('jNiceChecked');}
	};

	var TextAdd = function(){
		var j$input = j$(this).addClass('jNiceInput').wrap('<div class="jNiceInputWrapper"><div class="jNiceInputInner"></div></div>');
		var j$wrapper = j$input.parents('.jNiceInputWrapper');
		j$input.focus(function(){
			j$wrapper.addClass('jNiceInputWrapper_hover');
		}).blur(function(){
			j$wrapper.removeClass('jNiceInputWrapper_hover');
		});
	};

	var ButtonAdd = function(){
		var value = j$(this).attr('value');
		j$(this).replaceWith('<button id="'+ this.id +'" name="'+ this.name +'" type="'+ this.type +'" class="niceButton'+ this.className +'" value="'+ value +'"><span><span>'+ value +'</span></span>');
	};

	/* Hide all open selects */
	var SelectHide = function(){
			j$('.jNiceSelectWrapper ul:visible').hide();
	};

	/* Check for an external click */
	var checkExternalClick = function(event) {
		if (j$(event.target).parents('.jNiceSelectWrapper').length === 0) { SelectHide(); }
	};

	var SelectAdd = function(element, index){
		var j$select = j$(element);
		index = index || j$select.css('zIndex')*1;
		index = (index) ? index : 0;
		/* First thing we do is Wrap it */
		j$select.wrap(j$('<div class="jNiceWrapper"></div>').css({zIndex: 100-index}));
		var width = j$select.width() + 30;
		j$select.addClass('jNiceHidden').after('<div class="jNiceSelectWrapper"><div><span class="jNiceSelectText"></span><span class="jNiceSelectOpen"></span></div><ul></ul></div>');
		var j$wrapper = j$(element).siblings('.jNiceSelectWrapper').css({width: width +'px'});
		j$('.jNiceSelectText, .jNiceSelectWrapper ul', j$wrapper).width( width - j$('.jNiceSelectOpen', j$wrapper).width());
        j$('.jNiceSelectText').width(j$('.jNiceSelectText').width()-8);
		j$(".jNiceSelectWrapper").css("width", "auto");
		/* IF IE 6 */
		if (j$.browser.msie && jQuery.browser.version < 7) {
			j$select.after(j$('<iframe src="javascript:\'\';" marginwidth="0" marginheight="0" align="bottom" scrolling="no" tabIndex="-1" frameborder="0"></iframe>').css({ height: j$select.height()+4 +'px' }));
		}
		/* Now we add the options */
		SelectUpdate(element);
		/* Apply the click handler to the Open */
		j$('div', j$wrapper).click(function(){
			var j$ul = j$(this).siblings('ul');
			if (j$ul.css('display')=='none'){ SelectHide(); } /* Check if box is already open to still allow toggle, but close all other selects */
			j$ul.slideToggle();
			var offSet = (j$('a.selected', j$ul).offset().top - j$ul.offset().top);
			j$ul.animate({scrollTop: offSet});
			return false;
		});
		/* Add the key listener */
		j$select.keydown(function(e){
			var selectedIndex = this.selectedIndex;
			switch(e.keyCode){
				case 40: /* Down */
					if (selectedIndex < this.options.length - 1){ selectedIndex+=1; }
					break;
				case 38: /* Up */
					if (selectedIndex > 0){ selectedIndex-=1; }
					break;
				default:
					return;
					break;
			}
			j$('ul a', j$wrapper).removeClass('selected').eq(selectedIndex).addClass('selected');
			j$('span:eq(0)', j$wrapper).html(j$('option:eq('+ selectedIndex +')', j$select).attr('selected', 'selected').text());
			return false;
		}).focus(function(){ j$wrapper.addClass('jNiceFocus'); }).blur(function(){ j$wrapper.removeClass('jNiceFocus'); });
	};

	var SelectUpdate = function(element){
		var j$select = j$(element);
		var j$wrapper = j$select.siblings('.jNiceSelectWrapper');
		var j$ul = j$wrapper.find('ul').find('li').remove().end().hide();
		j$('option', j$select).each(function(i){
			j$ul.append('<li><a href="#" index="'+ i +'">'+ this.text +'</a></li>');
		});
		/* Add click handler to the a */
		j$ul.find('a').click(function(){
			j$('a.selected', j$wrapper).removeClass('selected');
			j$(this).addClass('selected');
			/* Fire the onchange event */
			if (j$select[0].selectedIndex != j$(this).attr('index') && j$select[0].onchange) { j$select[0].selectedIndex = j$(this).attr('index'); j$select[0].onchange(); }
			j$select[0].selectedIndex = j$(this).attr('index');
			j$('span:eq(0)', j$wrapper).html(j$(this).html());
			j$ul.hide();
			return false;
		});
		/* Set the defalut */
		j$('a:eq('+ j$select[0].selectedIndex +')', j$ul).click();
	};

	var SelectRemove = function(element){
		var zIndex = j$(element).siblings('.jNiceSelectWrapper').css('zIndex');
		j$(element).css({zIndex: zIndex}).removeClass('jNiceHidden');
		j$(element).siblings('.jNiceSelectWrapper').remove();
	};


	/* Utilities */
	j$.jNice = {
			SelectAdd : function(element, index){ 	SelectAdd(element, index); },
			SelectRemove : function(element){ SelectRemove(element); },
			SelectUpdate : function(element){ SelectUpdate(element); }
	};/* End Utilities */


	/* Automatically apply to any forms with class jNice */
	j$(function(){j$('.niceform').jNice();	});
})(jQuery);
