
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

/*--------------------------------------------------------------------------*/ 
// jQuery SWFObject v1.1.1 MIT/GPL @jon_neal
// http://jquery.thewikies.com/swfobject
(function(f,h,i){function k(a,c){var b=(a[0]||0)-(c[0]||0);return b>0||!b&&a.length>0&&k(a.slice(1),c.slice(1))}function l(a){if(typeof a!=g)return a;var c=[],b="";for(var d in a){b=typeof a[d]==g?l(a[d]):[d,m?encodeURI(a[d]):a[d]].join("=");c.push(b)}return c.join("&")}function n(a){var c=[];for(var b in a)a[b]&&c.push([b,'="',a[b],'"'].join(""));return c.join(" ")}function o(a){var c=[];for(var b in a)c.push(['<param name="',b,'" value="',l(a[b]),'" />'].join(""));return c.join("")}var g="object",m=true;try{var j=i.description||function(){return(new i("ShockwaveFlash.ShockwaveFlash")).GetVariable("$version")}()}catch(p){j="Unavailable"}var e=j.match(/\d+/g)||[0];f[h]={available:e[0]>0,activeX:i&&!i.name,version:{original:j,array:e,string:e.join("."),major:parseInt(e[0],10)||0,minor:parseInt(e[1],10)||0,release:parseInt(e[2],10)||0},hasVersion:function(a){a=/string|number/.test(typeof a)?a.toString().split("."):/object/.test(typeof a)?[a.major,a.minor]:a||[0,0];return k(e,a)},encodeParams:true,expressInstall:"expressInstall.swf",expressInstallIsActive:false,create:function(a){if(!a.swf||this.expressInstallIsActive||!this.available&&!a.hasVersionFail)return false;if(!this.hasVersion(a.hasVersion||1)){this.expressInstallIsActive=true;if(typeof a.hasVersionFail=="function")if(!a.hasVersionFail.apply(a))return false;a={swf:a.expressInstall||this.expressInstall,height:137,width:214,flashvars:{MMredirectURL:location.href,MMplayerType:this.activeX?"ActiveX":"PlugIn",MMdoctitle:document.title.slice(0,47)+" - Flash Player Installation"}}}attrs={data:a.swf,type:"application/x-shockwave-flash",id:a.id||"flash_"+Math.floor(Math.random()*999999999),width:a.width||320,height:a.height||180,style:a.style||""};m=typeof a.useEncode!=="undefined"?a.useEncode:this.encodeParams;a.movie=a.swf;a.wmode=a.wmode||"opaque";delete a.fallback;delete a.hasVersion;delete a.hasVersionFail;delete a.height;delete a.id;delete a.swf;delete a.useEncode;delete a.width;var c=document.createElement("div");c.innerHTML=["<object ",n(attrs),">",o(a),"</object>"].join("");return c.firstChild}};f.fn[h]=function(a){var c=this.find(g).andSelf().filter(g);/string|object/.test(typeof a)&&this.each(function(){var b=f(this),d;a=typeof a==g?a:{swf:a};a.fallback=this;if(d=f[h].create(a)){b.children().remove();b.html(d)}});typeof a=="function"&&c.each(function(){var b=this;b.jsInteractionTimeoutMs=b.jsInteractionTimeoutMs||0;if(b.jsInteractionTimeoutMs<660)b.clientWidth||b.clientHeight?a.call(b):setTimeout(function(){f(b)[h](a)},b.jsInteractionTimeoutMs+66)});return c}})(jQuery,"flash",navigator.plugins["Shockwave Flash"]||window.ActiveXObject);


/*--------------------------------------------------------------------------*/ 

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $j(window).height() - this.height() ) / 2+$j(window).scrollTop() + "px");
    this.css("left", ( $j(window).width() - this.width() ) / 2+$j(window).scrollLeft() + "px");
    return this;
}

/*--------------------------------------------------------------------------*/
    
var knockout_jr_ftw = {src: skin_url + 'js/fonts/knockout_jr_ftw.swf'};
var knockout_jr_ltw = {src: skin_url + 'js/fonts/knockout_jr_ltw.swf'};
var knockout_mid = {src: skin_url + 'js/fonts/knockout_mid.swf'};
var clarendon = {src: skin_url + 'js/fonts/clarendon.swf'};
var clarendon_bold = {src: skin_url + 'js/fonts/clarendon_bold.swf'};
var futura = {src: skin_url + 'js/fonts/futura.swf'};
sIFR.activate(clarendon, clarendon_bold, knockout_jr_ftw, knockout_jr_ltw, knockout_mid);

sIFR.replace(clarendon, {
	selector: '.page-title h1',
	wmode: 'transparent',
	tuneWidth: 5,
	forceSingleLine: true,
	css: {
		'.sIFR-root': {'color': '#cf1632', 'font-weight': 'bold', 'font-size': '46px', 'text-decoration': 'none'}
	}
});

sIFR.replace(clarendon, {
	selector: '.page-title .header',
	wmode: 'transparent',
	tuneWidth: 5,
	forceSingleLine: true,
	css: {
		'.sIFR-root': {'color': '#cf1632', 'font-weight': 'bold', 'font-size': '46px', 'text-decoration': 'none'}
	}
});

sIFR.replace(knockout_jr_ltw, {
	selector: '.tout .header',
	wmode: 'transparent',
	tuneWidth: 5,
	forceSingleLine: false,
	css: {
		'.sIFR-root': {'color': '#2f2f2f', 'font-weight': 'normal', 'font-size': '21px', 'text-decoration': 'none'}
	}
});

sIFR.replace(futura, {
	selector: 'h2.lead, h1.lead, #article h1',
	wmode: 'transparent',
	tuneWidth: 5,
	css: {
		'.sIFR-root': {'color': '#828282', 'font-weight': 'bold', 'font-size': '36px'},
		'strong': {'color': '#000000'}
	}
});

sIFR.replace(futura, {
	selector: '.warranties h2, #faqs h2, #main.privacy #main_content h2',
	wmode: 'transparent',
	tuneWidth: 5,
	css: {
		'.sIFR-root': {'color': '#1D1D1D', 'font-weight': 'bold', 'font-size': '18px'}
	}
});

sIFR.replace(futura, {
	selector: '#sitemap h3',
	wmode: 'transparent',
	tuneWidth: 5,
	css: {
		'.sIFR-root': {'color': '#2f2f2f', 'font-weight': 'bold', 'font-size': '14px'},
		'a:link': { 'color': '#2f2f2f', 'text-decoration': 'none'  },
		'a:hover': { 'color': '#cf1632', 'text-decoration': 'none'  }
	}
});



/*--------------------------------------------------------------------------*/
/* Hover Helper */
var HoverHelper = function (el) {
	this.el = $j(el);
	this.setup();	
};
HoverHelper.prototype = {
	setup: function () {
		var self = this;
		this.el.bind('mouseenter', function() {
			self.el.addClass('hover');
		});
		this.el.bind('mouseleave', function() {
			self.el.removeClass('hover');
		});
	}
};

/*--------------------------------------------------------------------------*/
/* Mega Drop Menu */
var MegaDrop = function (container) {
	this.megas = container.find('.mega_drop');
	this.triggers = container.find('li.hover');
	this.setup();
};
MegaDrop.prototype = {
    setup: function () {
    	var self = this;
    	this.triggers.hoverIntent({
    		sensitivity: 1,
            interval: 10,
            over: function () {
            	self.open($j(this).index());
            },
            out: function () {
				self.close($j(this).index());
            }
		});
    },
    open: function (idx) {
			var self = this;
			this.current = idx;
			this.hasGrown = false;
			$j(this.megas[idx]).css("z-index", 150).show();
			$j(this.triggers[idx]).addClass("over");
			this.togglers = $j(this.megas[idx]).find('.mega_menu li');
			this.tabs = $j(this.megas[idx]).find('.mega_content .tab');
			this.menu = $j(this.megas[idx]).find('.mega_menu:first');
			
			$j(this.tabs[0]).show();
			$j(this.togglers[0]).addClass('active');
			
			this.togglers.bind('mouseenter', function () {
				//self.grow(self.menu);
				self.showTab($j(this).index());
			});
    },
    close: function (idx) {
			$j(this.megas[idx]).hide();
			this.tabs.hide();
			this.togglers.removeClass('active');
			$j(this.triggers[idx]).removeClass("over");
			//this.menu.css('width', 120);
    },
    showTab: function (idx) {
	    this.tabs.hide();
    	this.togglers.removeClass('active');
    	$j(this.tabs[idx]).show();
    	$j(this.togglers[idx]).addClass('active');
    },
    grow: function (el) {
    	if (!this.hasGrown){
    		$j(el).animate(
				{width: 616},
				{
					duration: 200,
					easing: 'easeInQuint'	
				}	
			);
    		this.hasGrown = true;
    	} 
    }  
};

/*--------------------------------------------------------------------------*/
/* Slideshow */
var SlideShow = function (container, slides, delay, initial, sifr) {
    this.container = container;
    this.slides = slides;
    this.delay = delay;
    this.current = initial;
    this.togglers = this.container.find('.togglers li');
	this.sifrCache = {};
	this.sifr = sifr;
    this.setup();
};
SlideShow.prototype = {
    setup: function () {
        var self = this;
        this.showOnly($j(this.slides[this.current]));
        $j(this.togglers[this.current]).addClass('active');
        
        this.togglers.bind('click', function () {
            self.pause();
            self.goTo($j(this).index());
        });
		
		if ($j(this.container).find('.hero-tooltip').length > 0) {
			$j(this.container).find('.hero-tooltip .plus, .hero-tooltip .hero-info').bind('mouseenter', function () {
				self.pause();
			});
			$j(this.container).find('.hero-tooltip .plus, .hero-tooltip .hero-info').bind('mouseleave', function () {
				self.start();
			});
		}
		
		this.start();
    },
    showOnly: function (el) {
        this.slides.hide();
        el.show();
    },
    goTo: function (idx) {
        var self = this;
		
        if (idx === this.current) {	
            return;
        }
        $j(this.togglers[this.current]).removeClass('active');
        $j(this.slides[this.current]).fadeOut('slow');
        this.current = idx;
		
        $j(this.togglers[this.current]).addClass('active');
        $j(this.slides[this.current]).fadeIn('slow');
		
        this.start();
    },
    start: function () {
        if (this.timer) {
            return;
        }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1500);
    },
    pause: function () {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        else {
            return;
        }
    },
    next: function() {
        this.timer = false;
        if (this.current === this.slides.length - 1) {
            this.goTo(0);
        }
        else {
            this.goTo(this.current + 1);
        }
        this.start();
    }
};

/*--------------------------------------------------------------------------*/
/* Custom Dropdown */
var DropDown = function (container, callback) {
	this.container = $j(container);
	this.btn = this.container.find('.trigger:first');
	this.menu = this.container.find('.menu:first');
	this.track = this.container.find('.track:first');
	this.scroll_pane = this.container.find('.scroll_pane:first');
	this.loaded = false;
	this.current_pick = this.container.find('input[type="hidden"]:first');
    this.callback = callback;
    this.setup();
};
DropDown.prototype = {
	setup: function () {
		var self = this;
		this.btn.bind('click', function () {
			self.open();
			return false;
		});
	
		$j(document).bind('click', function(ev){
			if (self.active) return;
			self.close();
		});
		
		this.container.bind('mouseenter', function () {
			self.active=true;
		});
		this.container.bind('mouseleave', function () {
			self.active=false;
		});
		
		
	},
	open: function () {
		var self = this;
        
		//$j('.dropdown').removeClass('active');
		//$j('.dropdown .menu').hide();
		
		this.container.addClass('active');
		
		if (this.container.hasClass('slideup')) {
			
			this.menu.animate(
				{ 
					top: -153
				},
				{ 
					duration: 150,
					complete: function () {
						self.menu.show();
						if (!self.isLoaded) {
							self.scroll_pane.jScrollPane({
								scrollbarWidth: 27,
								verticalDragMinHeight: 41,
								verticalDragMaxHeight: 41,
								horizontalDragMinWidth: 27,
								horizontalDragMaxWidth: 27
							});
							self.isLoaded = true;
						}
						self.menu.addClass('active');
						self.container.css('z-index', 103); 
					}
				}
			);
		}
		else {
			this.menu.slideDown(150, function () {
				if (jQuery.browser.msie) {
					if (self.menu.hasClass('choose')) {
						self.menu.addClass('show');
						if (self.menu.hasClass('show')) {
							if ($j("body.catalog-product-compare-index #content").length) {	
								/* ie6 */
								if ($j.browser.msie && $j.browser.version=="6.0") {
									/* for IE6 - add padding to body for drop down menu, otherwise it goes off the screen */
									$j("body").addClass('dropdown-trick'); 
								}
								else {
									/* increase z-index of model choose dropdown, otherwise overridden by country drop down in IE7 */
									$j("body.catalog-product-compare-index #content").css("z-index","140"); 
								}
							}

						}
					}
				}
				if (!self.isLoaded) {
					self.scroll_pane.jScrollPane({
						scrollbarWidth: 27,
						scrollbarWidth: 27,
						verticalDragMinHeight: 41,
						verticalDragMaxHeight: 41,
						horizontalDragMinWidth: 27,
						horizontalDragMaxWidth: 27
					});
					self.isLoaded = true;
				}
				self.menu.addClass('active');
			}).css('z-index', 140);
		}
		
		this.btn.unbind('click');
		this.btn.bind('click', function () {
			self.close();
		});
		
		this.opts = this.menu.find('li a');
		this.opts.unbind('click').bind('click', function (ev) {
			return self.choose($j(this).parents('li:first').find('.val:first').text(), this);
			//return false;
		});
		return false;
	},
	close: function () {
		var self = this;
		this.container.removeClass('active');
		this.menu.slideUp(150, function () {
			$j(self.container).removeClass('active');
			self.menu.css('z-index', 100);
		});
		$j(".slideup").css('z-index','1');
				
		//$j(".dropdown").removeClass("active").children(".menu").slideUp(150).removeClass("active").css("z-index", "100");
		this.btn.unbind('click');
		this.btn.bind('click', function () {
			self.open();
		});
		
		//for IE6 - remove padding to body for drop down menu
		if (jQuery.browser.msie) {
			if (self.menu.hasClass('show')) {
				$j("body.catalog-product-compare-index #content").css("z-index","0");
				self.menu.removeClass('show');
				if(parseInt(jQuery.browser.version) <= 7) {
					$j("body").removeClass('dropdown-trick');
				}
			}
		}
	},
	choose: function (val, node) {
		this.btn.find('span:first').text(val);
		this.current_pick.val(val);
		this.close();
		if (this.callback){
			this.callback.call(this, val, node);
			return false; // prevent devault behavior
		}
		return true; // allow default behavior
	}
};
/*--------------------------------------------------------------------------*/
/* Tooltips */
var ToolTip = function (trigger, pop) {
	this.trigger = $j(trigger);
	this.pop = $j(pop);
	this.setup();
};
ToolTip.prototype = {
	setup: function () {
		var self = this;
		
		this.trigger.hoverIntent({
			sensitivity: 1,
			interval: 50,
			over: function() {
				self.open();
			}, 
			out: function() {
				return;
			}		
		});	
	},
	open: function () {
		var self = this;
		var offset = this.trigger.position();
		
		this.pop.css("top", offset['top']);
		this.pop.css("left", offset['left']);
		this.pop.show();
		
		var close_btn = this.pop.find('.close:first');
		close_btn.bind('click', function () {
			self.close();
		});
	},
	close: function () {
		this.pop.hide();
	}
};

/*--------------------------------------------------------------------------*/

function externalLinks() {
 	if (!document.getElementsByTagName) return;
 	var externals = $j("a[rel='external']");
	
	externals.each(function () {
		if ($j(this).attr('href')) {
			$j(this).bind('click', function (ev) {
				ev.preventDefault();
				window.open($j(this).attr('href'));
			});
		}
	});
}

/*--------------------------------------------------------------------------*/

function adjustStyle(width) {
    width = parseInt(width);
	if (width <= 1140) {
		$j("body:first").attr('id' ,'screen_1024');
	}
	else if (width > 1140) {
		$j("body:first").attr('id' ,'schwinn');
	}
}

/*--------------------------------------------------------------------------*/

var Tabs = function (container) {
	this.container = $j(container);
	this.togglers = this.container.find('.tab');
	this.tabs = this.container.find('.tab-container');
	this.setup();
};
Tabs.prototype = {
	setup: function () {
		var self = this;
		this.show(0);
		
		this.togglers.each(function (i) {
			$j(this).bind('click', function (ev) {
				ev.preventDefault();
				self.show(i);
			});
		});
	},
	show: function (idx) {
		this.togglers.removeClass('active');
		$j(this.togglers[idx]).addClass('active');
		this.tabs.hide();
		$j(this.tabs[idx]).show();
		this.container.css("height", $j(this.tabs[idx]).height());
	}
};

/*--------------------------------------------------------------------------*/


var RidingFilter = function(trigger, hook){
	this.trigger = $j(trigger);
	this.s = $j(hook);
	//this.h = $j('.article:not(:' + hook + ')');
	this.setup();
}

RidingFilter.prototype = {
	setup: function () {
		var self = this;
		this.trigger.bind('click', function (ev) {
			ev.preventDefault();
			$j('#articles .article').hide();
			//self.h.hide();
			if(self.trigger.attr('id')=='show_video') {
				$j('#hidden_video').show();
			} else {
				$j('#hidden_video').hide();
			}
			self.s.show();
			
			$j('#filters ul li').removeClass('active');
			self.trigger.parent('li').addClass('active');
			return;
		});	
	}
};

var RidingFilterAll = function(){
	
	
};

/*--------------------------------------------------------------------------*/

var PopUp = function (trigger, pop) {
	this.trigger = $j(trigger);
	this.pop = $j(pop);
	this.setup();
};
PopUp.prototype = {
	setup: function () {
		var self = this;
		this.trigger.bind('click', function (ev) {
			ev.preventDefault();
			self.open();
		});	
	},
	open: function () {
		if ($j('#modal_overlay')) {
			this.init_modal($j('#modal_overlay'));
			$j("#modal_overlay").css("z-index","900");
		}	
		var self = this;
		this.pop.center();
		this.pop.fadeIn(100);
		this.pop.css("z-index","1500");
		if ($j.browser.msie && $j.browser.version=="6.0" || $j.browser.version=="7.0" ) {
			if ($j.address.value() == '/reviews' || $j.address.value() == '/review-form') {
				$j("body.catalog-product-view #content").css("z-index","1500");
			}
		}
		
		var close_btn = this.pop.find('.close');
		close_btn.bind('click', function () {
			self.close();
		});
	},
	close: function () {
		this.pop.fadeOut(100);
		if ($j('#modal_overlay')) {
			$j('#modal_overlay').hide();
		}
		if ($j.browser.msie && $j.browser.version=="6.0" || $j.browser.version=="7.0" ) {
			if ($j.address.value() == '/reviews' || $j.address.value() == '/review-form') {
				$j("body.catalog-product-view #content").css("z-index","0");
			}
		}
	},
	init_modal: function (el) {
		el.css("height", $j('body:first').height());
		$j(el).show();
	}
};

/*--------------------------------------------------------------------------*/

var YouTubePopUp = function(trigger, pop, width, height){
    this.trigger = $(trigger);
    this.pop = $(pop);
	this.container = this.pop.find('.content');
    this.width = width;
    this.height = height;
    this.matches = this.trigger.attr('href').match(/v=([A-Za-z0-9\-=_]{11})/);
    this.setup();
};
YouTubePopUp.prototype = {
    setup: function() {
        var self = this;
        this.trigger.bind('click', function (ev) {
            ev.preventDefault();
            self.open();
        });
    },
	open: function () {
		var self = this;
		if ($j('#modal_overlay')) {
			this.init_modal($j('#modal_overlay'));
		}
		this.pop.addClass('video');
		this.pop.css("width", this.width + 50);
		this.pop.css("height", this.height + 50);
		this.pop.center();
		this.pop.fadeIn(100, function () {
			self.embed_video();
		});
		
		var close_btn = this.pop.find('.close');
		close_btn.bind('click', function () {
			self.close();
		});
	},
	close: function () {
		var self = this;
		this.pop.fadeOut(100, function () {
			self.container.html('');
		});
		if ($j('#modal_overlay')) {
			$j('#modal_overlay').hide();
		}
	},
	init_modal: function (el) {
		el.css("height", $j('body:first').height());
		$j(el).show();
	},
    embed_video: function (){
        var url = "http://www.youtube.com/v/" + this.matches[1] + "&hl=en&autoplay=1";
		
        this.container.flash({
            swf: url,
            width: this.width,
            height: this.height
        });
    }
};

/*--------------------------------------------------------------------------*/

var BasicSlider = function (container, increment, delay) {
	this.container = $j(container);
	this.increment = increment;
	this.contents = this.container.find('.slides:first');
	this.slides = this.contents.find('.slide');
	this.prevBtn = this.container.find('.prev:first');
	this.nextBtn = this.container.find('.next:first');
	this.slideCount = this.slides.length / this.increment;
	this.offset = this.contents.position()['left'];
	this.width = $j(this.slides[0]).width();
	this.current = 0;
	this.delay = delay | 5;
	this.setup();
};
BasicSlider.prototype = {
	setup: function () {
		var self = this;
		this.prevBtn.bind('click', function (ev) {
			ev.preventDefault();
			$j(this).addClass('active');
			self.back();
		});
		
		this.nextBtn.bind('click', function (ev) {
			ev.preventDefault();
			$j(this).addClass('active');
			self.forward();
		});
		
		this.goTo(0);
		//this.start();
	},
	goTo: function (idx) {
		var self = this;
		if (this.current === idx) {
			return;
		}
		
		if (idx < 0 || idx >= this.slideCount) {
			return;
		}
		this.current = idx;	
		
		$j(this.contents).animate(
			{ left: (-(idx * this.width) + this.offset) },
			{
				//duration: 800,
				duration: 100,
				easing: 'easeOutExpo'
			}
		);
		return this.current;
	},
	start: function () {
        if (this.timer) {
            return;
        }
        var self = this;
        this.timer = setTimeout(function(){
            self.next();
        }, this.delay * 1000);
	},
	pause: function () {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = false;
        }
        else {
            return;
        }
	},
	back: function () {
		this.pause();
        if (this.current === 0) {
          //  return; 
        	this.goTo(this.slides.length - 1);
        }
        else {
            this.goTo(this.current - 1);
        }
        //this.start();	
	},
	forward: function () {
        this.pause();
        this.next();
	},
	next: function () {
        if (this.current+1 === this.slides.length) {
			//this.timer = false;
			//return;			
		//	this.setup();
			this.goTo(0);
        }
        else {
            this.goTo(this.current + 1);
        }
        //this.start();
	}
};

/*--------------------------------------------------------------------------*/

var CategoryNav = function(container) {
	this.togglers = container.find('.trigger');
	this.subcats = container.find('.subcat');
	this.setup();
};
CategoryNav.prototype = {
	setup: function () {
		var self = this;
		
		this.togglers.each(function (i) {
			$j(this).bind('click', function (ev) {
				ev.preventDefault();
				if ($j(this).hasClass('active') ) {
					$j(this).removeClass('active');
					$j(self.subcats[i]).slideUp(300);
					$j(this).parents('li.year').removeClass("open");
				}
				else {	
					$j(this).addClass('active');
					$j(self.subcats[i]).slideDown(300);
					$j(this).parents('li.year').addClass("open");
				}
			});
		});		
	}
};

function whereToBuyHovers() {
	
	new BasicSlider($j('#stores'), 1, 15);
	
	$j('#block-wrapper .buy-block .store').hoverIntent({
		sensitivity: 1,
		interval: 10,
		over: function () {
			var menu = $j(this).find('ul');
			$j(menu).animate(
				{ top: 0 },
				{
					duration: 250
				}
			);
		},
		out: function () {
			var menu = $j(this).find('ul');
			$j(menu).animate(
				{ top: 80 },
				{
					duration: 250
				}
			);
		}
	});	
	
}

/*--------------------------------------------------------------------------*/

var Truncator = function (el, max) {
	this.el = $j(el);
	this.max = max;
	this.setup();
};
Truncator.prototype = {
	setup: function () {
		var txt = $j.trim(this.el.text());
		
		if (txt.length > this.max) {
			var trunc = txt.substring(0, this.max) + '...';
			this.el.text(trunc);
		}
		
	}
};


/*--------------------------------------------------------------------------*/
/* DOM Ready */
$j(document).ready(function () {
	
	//1024 resize
	function content_resize() 
	{
		var w = $j( window );
		var H = w.height();
		var W = w.width();
		
		if (W <= 1140) {
			$j("body:first").addClass('screen_1024');
		}
		else if (W > 1140) {
			 $j("body:first").removeClass('screen_1024');
		}
		
	}
	
	content_resize();
	$j(window).resize(content_resize);
	$j('.product-img-box .enlarge').show();
	
	/* main navigation */
	new MegaDrop($j('#nav'));
	
	new PopUp($j('#trigger_login'), $j('#lightbox_login'));
	
	new ToolTip($j('#bike_toggle'));
	
	new RidingFilter('#show_video', '.type_video');
	new RidingFilter('#show_article', '.type_article');
	new RidingFilter('#show_all', '.article');
	
	/* category side nav */
	if ($j('#aside').length) {
		new CategoryNav($j('#aside'));
	}

	/* registration pop */
	$j("#lightbox_registration .ok, #lightbox_password .ok").click(function() {
		$j(this).parents(".lightbox").hide();
	});
	
	/* locale switcher */
	new DropDown($j('#footer .dropdown'), function(val, node) {
		var old_code = $j('.flag div').attr('class').toLowerCase();
		var new_code = $j(node).parents('li:first').attr('class').toLowerCase();
		var path = window.location.pathname;
		var path_pieces = [];
		if (old_code != 'usa') {
			path_pieces = path.split('/');
			path_pieces.shift();
			path_pieces.shift();
			path = '/' + path_pieces.join('/');
		}
		if (new_code && new_code != 'usa'){
			path = '/' + new_code + path;
		}
		window.location.pathname = path;
	});
	
	/* hero tooltip */
	$j('.hero-tooltip').hoverIntent({
		sensitivity: 1,
		interval: 100,
		timeout: 300,
		over: function () {
			$j(this).find('.hero-info:first').show();
			sIFR.replace(futura, {
				selector: '.hero-info h3',
				wmode: 'transparent',
				css: {
					'.sIFR-root': {'text-transform': 'uppercase', 'font-weight': 'bold'},
					'a:link': { 'color': '#000000', 'text-decoration': 'none'  },
					'a:hover': { 'color': '#cf1632', 'text-decoration': 'none' }
				}
			});	
		},
		out: function () {
			$j(this).find('.hero-info:first').hide();
		}
	});
	
	/* where to buy hovers */
	if ($j('#stores').length) {
		whereToBuyHovers();
	}
	
	/* Support/Company sidebar - opening/closing subcategories*/
	$j("#aside li.archive .header a:first").click(function() {
		var thisList = $j(this).parents("li");
		if (thisList.hasClass("open") == true) {
			thisList.removeClass("open").children(".subcat").slideUp();
			thisList.find('span:first').removeClass('active');
		}
		else {
			thisList.addClass("open").children(".subcat").slideDown();
			thisList.find('span:first').addClass('active');
		}
		return false;
	});
	
	/* check for active state to expand on load */
	var currentPage = window.location.pathname;
	var activeItem = $j('#aside li').has('a[href$='+currentPage+']');
	
	activeItem.addClass('active open');
	activeItem.find('span:first').addClass('active');
	
	$j("#aside li.menu .subcat:first").hide();
	
	$j("#aside li.active .subcat:first").show();
	
	/* sharing */
	$j("#print_share li.print a").click(function() {
		window.print();
	});
	
	/* faq pound downs */
	if($j('#faqs').length && $j('#topics').length) {
		$j('a[href^=#]:not([href=#])').each(function () {
			$j(this).bind('click', function (ev) {
				ev.preventDefault();
				var target = '#' + this.hash.slice(1);
				$j('html, body').animate(
					{ scrollTop: $j(target).offset().top },
					{ duration: 2000, easing: 'easeOutQuart' }
				);
			});
		});
	} 

    // Contacts page
    if ($j('body').hasClass('contacts-index-index')){
        // Scroll top if we display a message
        if($j('#messages_product_view').length){
            $j(window).scrollTop(0);
        }
    }

	/* external links */
	externalLinks();
	
});

