var $j = jQuery.noConflict();

(function($j) {
	$j.fn.loupe = function(options) {
		if (!this.length) { return this; }
		options = $j.extend({ 'loupe':'loupe', 'width':200, 'height':150 }, options || {});

		this.each(function() {
			var $this = $j(this), loupe = null, big, small = null, time = null,
			hide = function() { loupe.hide(); },
			move = function(e) {
				var os = small.offset(), sW = small.outerWidth(), sH = small.outerHeight(), oW = options.width/2, oH = options.height/2;

				if (e.pageX > sW + os.left + 10 || e.pageX < os.left - 10 ||
					e.pageY > sH + os.top + 10 || e.pageY < os.top - 10) {
					return hide();
				}
				if (time && clearTimeout(time)) { time = null; }
				loupe.css({ 'left':e.pageX - oW, 'top':e.pageY - oH });
				big.css({
					'left':-(((e.pageX - os.left) / sW) * big.width() - oW)|0,
					'top':-(((e.pageY - os.top) / sH) * big.height() - oH)|0
				});
			};

			$this.mouseenter(function(e) {
				if (!small) { small = $this.is('img') ? $this : $this.find('img:first'); }
				loupe = (loupe || (loupe = $j('<div></div>').addClass(options.loupe)
					.css({
						width:options.width, height:options.height,
						position:'absolute', overflow:'hidden'
					})
					.append(big = $j('<img src="' + $this.attr($this.is('img') ? 'src' : 'href') + '" />').css({position:'absolute'}))
					.mousemove(move).appendTo('body')
				)).show(); move(e);
			}).mouseout(function() { time = setTimeout(hide, 10); });
		});

		return this;
	};
})(jQuery);

$j(document).ready(function(){
		
	var isIE = false;
	if ($j.browser.msie && $j.browser.version.substr(0,1)<7) {
			isIE = true;
	    $j("img[src*='.png']").each(function(){
	        DD_belatedPNG.fixPng(this);
	    });
	    $j("#logo, div, li, a").each(function(){
	        if ($j(this).css('backgroundImage').indexOf('.png') != -1 ) {
	            //alert($j(this).css('backgroundImage'));
	            DD_belatedPNG.fixPng(this);
	        };
	    });
	};
	$j('.form-search #search').click(function(){
		if ($j(this).val() == 'SEARCH'){
			$j(this).val('');
		}
	});
	$j('.form-search #search').blur(function(){
		if ($j(this).val() == ''){
			$j(this).val('SEARCH');
		}
	});
	
	$j('#newsletter-validate-detail').submit(function(){
		var re = new RegExp(/\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/gi);
		var address = $j(this).find('input#newsletter').val();
		if (address.match(re)){
			//alert('good email');
		}else{	
			var x = $j("input#newsletter").offset().left;		
			//alert('bad email');

			$j(this).find('input#newsletter').after('<div class="error-msg"><h3>Oh no! Something\'s not right.</h3>A legit-sounding email address is required. Give it another try!</div>');
			$j('div.error-msg').css({left: x-($j(this).width()/2)+'px', width: ($j(this).parents('ul').width()+22)+'px'}).animate({left: 0, opacity: 1}, 500, function() {
				var el = this;
			  	setTimeout(function() {dismiss(el)}, 5000);
				$j('input#newsletter').focus(function() {
					setTimeout(function() {dismiss(el)}, 100);
				});
				
			});

			return false;
		}
	});
	$j('.parent').hover(function(){
		
			$j(this).addClass('over');
			if (isIE) {
				$j(this).children('ul').addClass('shown-sub');
				$j('select').css('visibility','hidden');
			}

			if ($j('.autocomplete:visible').length>0) {
				console.log('blurring');
				$j('#search').focus().blur();
			};
		},function(){
			$j(this).removeClass('over');
			if (isIE) {
				$j(this).children('ul').removeClass('shown-sub');
				$j('select').css('visibility','visible');
			}
	});
	if($j('a.loupe').length>0){$j('a.loupe').loupe({width: 250, height:250, loupe: 'loupe'})};
	
	setTimeout(function() {
		//alert($j('#ads').children('param').length);
		if ($j('#grip').children('param').length<1) {
			$j('#grip').prepend('<div class="no-flash" style="-webkit-border-radius: 16px;-moz-border-radius: 16px;border-radius: 16px;text-align:left;font-size:22px;padding:30px;width:412px;height:412px;background: url(/store/skin/frontend/default/lizardskins/images/grip-alt.jpg);"><br /><br />If you had Flash installed you\'d be seeing a nice preview of your new custom '+$j('.product-view').attr('id')+' grips!<br /><br />You can still customize your grips with the options to the right, we\'ll make sure you get what you want!<div>');
		};
		if ($j('#ads').children('param').length<1) {
			$j('#ads').append('<div class="pane"><a href="/store/custom-lock-on-grips" ><img src="/store/skin/frontend/default/lizardskins/images/design-your-own-grips.png" alt="Design your own grip lock-ons!" /></a></div><div class="pane omega"><a href="/riders/steve-romaniuk/romo-promo"><img src="/store/skin/frontend/default/lizardskins/images/new-video-romo.png" alt="Watch the new video from Steve Romaniuk" width="317" height="125" /></a></div>');
		};
	}, 1000);

	
	$j('.short-description a,.full-description a').click(function() {
		$j('.full-description').toggle();
		$j('.short-description').toggle();
	})

});

		
		function isHTML5 () {
			var i = document.createElement("input");
			i.setAttribute("type", "color");
			return i.type !== "text";
		}		

		function dismiss(el) {
			$j(el).animate({left: '500px', opacity: 0},300, function() {
				$j(this).remove();
			});
		}

// function toggleMenu(el,over){
//    if (over) {
//    	$j(el).addClass('over');
//    }
//    else {
//    	$j(el).removeClass('over');
//    }
// }


if (typeof(decorateTable) == "undefined"){
	function decorateTable(table, options){
		//console.log('lets decorate a table!');
		$j('#'+table).find('tr:first').addClass('first').end().find('tr:even').addClass('even').end().find('tr:odd').addClass('odd').end().find('tr:last').addClass('last');
		$j('#'+table+' tr').find('td:first').addClass('first').end().find('td:even').addClass('even').end().find('td:odd').addClass('odd').end().find('td:last').addClass('last');
	}
}
if (typeof(decorateList) == "undefined"){
	function decorateList(list, nonRecursive){
		//console.log('lets decorate a list!');
		$j('#'+list).find('li:first').addClass('first').end().find('li:even').addClass('even').end().find('li:odd').addClass('odd').end().find('li:last').addClass('last');
	}
}
if (typeof(setLocation) == "undefined"){
	function setLocation(url){
	    window.location.href = url;
	}
}

function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}
