(function(){
	ua = navigator.userAgent.toLowerCase(),	  
	check = function(r){
		return r.test(ua);
	}
	isOpera = check(/opera/),			  
	isIE = check(/msie/),
	isIE7 = isIE && check(/msie 7/),
	isIE8 =	isIE && check(/msie 8/),
	isIE6 =	isIE && !isIE7 && !isIE8,
	isChrome = check(/chrome/),
	isWebKit =check(/webkit/),
	isSafari =!isChrome && check(/safari/),
	isSafari2 =	isSafari && check(/applewebkit\/4/),
	isSafari3 = isSafari && check(/version\/3/),
	isSafari4 = isSafari && check(/version\/4/),
	isGecko = !isWebKit && check(/gecko/),
	isGecko2 = isGecko && check(/rv:1\.8/),
	isGecko3 = isGecko && check(/rv:1\.9/),
	isWindows = check(/windows|win32/),	
	isMac = check(/macintosh|mac os x/),	
	isAir = check(/adobeair/),	
	isLinux = check(/linux/),
	
	
	jQuery.extend(jQuery.browser, {
		isOpera: isOpera,
		isIE:	isIE,
		isIE6: isIE6,
		isIE7: isIE7,
		isIE8: isIE8,
		isChrome: isChrome,
		isWebKit: isWebKit,
		isSafari: isSafari,
		isSafari2: isSafari2,
		isSafari3: isSafari3,
		isSafari4: isSafari4,
		isGecko: isGecko,
		isGecko2: isGecko2,
		isGecko3: isGecko3,
		isWindows: isWindows,
		isMac: isMac,
		isAir: isAir,
		isLinux: isLinux
	});
	
	jQuery.extend({
	  addStyle: function(path){
		var style = document.createElement('link');
		style.setAttribute('rel','stylesheet');
		style.setAttribute('type','text/css');
		style.setAttribute('href',path);
		document.getElementsByTagName('head')[0].appendChild(style)	;	  
	  }, 	  
	  
	  parseQuery: function(url) {
		   var query = url.replace(/^[^\?]+\??/,'');
		   var Params = {};
		   if ( ! query ) {return Params;}// return empty object
		   var Pairs = query.split(/[;&]/);
		   for ( var i = 0; i < Pairs.length; i++ ) {
		      var KeyVal = Pairs[i].split('=');	
		      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
		      var key = unescape( KeyVal[0] );
		      var val = unescape( KeyVal[1] );
		      val = val.replace(/\+/g, ' ');
		      Params[key] = val;
		   }
		   return Params;		  
	  }	  
	});

})();

if ($.browser.isIE) {
	//addStyle('css/stylefix.css');
}
function addStyle(patch){
	var style = document.createElement('link');
	style.setAttribute('rel','stylesheet');
	style.setAttribute('type','text/css');
	style.setAttribute('href',patch);
	document.getElementsByTagName('head')[0].appendChild(style)	;	
}


(function ($) {
    jQuery.fn.defaultText = function (settings) {
        var defaults = {
            text: ''
        };
        settings = $.extend(defaults, settings || {});

        return this.each(function () {
            $(this).val(settings.text);
            $(this).focus(function () {
                var text = $.trim($(this).val());
                if (text == settings.text) {
                    $(this).val('');
                }
            });
            $(this).blur(function () {
                var text = $.trim($(this).val());
                if ('' == text) {
                    $(this).val(settings.text);
                }
            });
        });
    };
})(jQuery);

(function ($) {
    jQuery.fn.regForm = function (settings) {
        var defaults = {
            text: ''
        };
        settings = $.extend(defaults, settings || {});

        return this.each(function () {
			$(this).validate({
				rules: {
					'regsiter_username': {
						required: true
					},
					'register_password': {
						required: true,
						minlength: 6
					},
					'confirm_password': {
						required: true,
						equalTo: $('#register_password'),
						minlength: 6
					},
					'billingEmailAddress': {
						required: true,
						email: true
					},					
					'billingFirstName': {
						required: true
					},
					'billingLastName': {
						required: true
					},
					'billingAddress': {
						required: true
					},
					'billingState': {
						required: true
					},
					'billingCity': {
						required: true
					},
					'billingZipcode': {
						required: true
					},
					'billingCountry': {
						required: true
					},
					'bill_phone': {
						required: true
					}
				},
				errorPlacement: function(error, element) {
					$(element).parents('.x-form-field').addClass('x-form-error');
				},
				success: function(error, element) {
					$(element).parents('.x-form-field').removeClass('x-form-error');
				}
			});
        });
    };
})(jQuery);

(function ($) {
    jQuery.fn.signInForm = function (settings) {
        var defaults = {
            text: ''
        };
        settings = $.extend(defaults, settings || {});

        return this.each(function () {
			$(this).validate({
				rules: {
					'username': {
						required: true
					},
					'password': {
						required: true,
						minlength: 6
					}
				},
				errorPlacement: function(error, element) {
					$(element).parents('.x-form-field').addClass('x-form-error');
				},
				success: function(error, element) {
					$(element).parents('.x-form-field').removeClass('x-form-error');
				}
			});
        });
    };
})(jQuery);

(function ($) {
    jQuery.fn.billBoard = function (settings) {
        var defaults = {
            text: ''
        };
        settings = $.extend(defaults, settings || {});

        return this.each(function () {
			$(this).append('<div class="nav-buttons"></div>');
			var items = $('.panel', this);
			var width  = $(this).width();
			var count = items.length;
			var self = this;
			$('.items', this).width(942 * count);
			$(items).each(function(index){$('.nav-buttons', self).append('<a href="javascript:;">'+index+'</a>');});
			
			$(this).serialScroll({
				axis: 'x',
				target: $('#billboard', this),
				navigation: $('.nav-buttons a', this),
				easing: 'swing',
				interval: 7 * 1000,
				duration: 500,
				items: items,
				onBefore: function(p1, p2, p3, p4, pos){
					$('.nav-buttons a', self).removeClass('active').eq(pos).addClass('active');
				}
			});
			$('.nav-buttons > a:first-child', self).click();
        });
    };
})(jQuery);

(function ($) {
    jQuery.fn.cartForm = function (settings) {
        var defaults = {
            text: ''			
        };

        settings = $.extend(defaults, settings || {});

        return this.each(function () {
			var self = this;								   
			$('form', this).validate({
				rules: {
					'psize': {
						required: true
					},
					'pcolor': {
						required: true
					},
					'qty': {
						required: true
					}
				},
				errorPlacement: function(error, element) {
					$('#cart-error-msg', self).show();
				}
			});
        });
    };
})(jQuery);

(function ($) {
    jQuery.fn.checkStep1Form = function (settings) {
        var defaults = {
            text: ''
        };
        settings = $.extend(defaults, settings || {});

        return this.each(function () {
			$(this).validate({
				rules: {
					'shipping_firstname': {
						required: true
					},
					'shipping_lastname': {
						required: true
					},
					'shipping_address': {
						required: true
					},
					'shipping_city': {
						required: true
					},
					'shipping_country': {
						required: true
					},
					'shipping_state': {
						required: true
					},
					'shipping_zip': {
						required: true
					},
					'shipping_phone': {
						required: true					
					},
					'billing_firstname': {
						required: true
					},
					'billing_lastname': {
						required: true
					},
					'billing_address': {
						required: true
					},
					'billing_city': {
						required: true
					},
					'billing_country': {
						required: true
					},
					'billing_state': {
						required: true
					},
					'billing_zip': {
						required: true
					},
					'billing_phone': {
						required: true					
					}					
					
				},
				errorPlacement: function(error, element) {
					$(element).parents('.x-form-field').addClass('x-form-error');
				},
				success: function(error, element) {
					$(element).parents('.x-form-field').removeClass('x-form-error');
				}				
			});
        });
    };
})(jQuery);

(function ($) {
    jQuery.fn.newsletterForm = function (settings) {
        var defaults = {
            text: ''
        };
        settings = $.extend(defaults, settings || {});

        return this.each(function () {
			$(this).validate({
				rules: {
					'email': {
						required: true,
						email: true
					}					
				},
				errorPlacement: function(error, element) {
					$(element).parents('.x-form-item').addClass('x-form-error');
				},
				success: function(error, element) {
					$(element).parents('.x-form-item').removeClass('x-form-error');
				}
			});
        });
    };
})(jQuery);

(function ($) {
    jQuery.fn.checkoutLoginForm = function (settings) {
        var defaults = {
            text: ''
        };
        settings = $.extend(defaults, settings || {});

        return this.each(function () {
			$(this).validate({
				rules: {
					'username': {
						required: true
					},
					'password': {
						required: true,
						minlength: 6
					}
				},
				errorPlacement: function(error, element) {
					$(element).parents('.ck-form-field').addClass('x-form-error');
				},
				success: function(error, element) {
					$(element).parents('.ck-form-field').removeClass('x-form-error');
				}
			});
        });
    };
})(jQuery);



$(document).ready(function(){		
	//$('#billboard-wrap').billBoard();
	$('#pic-list ul li a').click(function(){
		$('#preview img').attr("src", $(this).attr('href'));
		$('#zoom a').attr("href", $(this).attr('href'));
		return false;										  
	});
	$('#btn-update').click(function(){
		$('#update-cart-form').submit();									
		return false;									
	});
	
	$('#reg_form').regForm();
	$('#login_form').signInForm();
	$('#add-to-cart-form-wrap').cartForm();	
	$('#checkoutstep1_form').checkStep1Form();
	$('#news_letter').newsletterForm();

	$('#checkoutLogin_form').checkoutLoginForm();


});


