jQuery.url=function(){var segments={};var parsed={};var options={url:window.location,strictMode:false,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};var parseUri=function(){str=decodeURI(options.url);var m=options.parser[options.strictMode?"strict":"loose"].exec(str);var uri={};var i=14;while(i--){uri[options.key[i]]=m[i]||""}uri[options.q.name]={};uri[options.key[12]].replace(options.q.parser,function($0,$1,$2){if($1){uri[options.q.name][$1]=$2}});return uri};var key=function(key){if(!parsed.length){setUp()}if(key=="base"){if(parsed.port!==null&&parsed.port!==""){return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/"}else{return parsed.protocol+"://"+parsed.host+"/"}}return(parsed[key]==="")?null:parsed[key]};var param=function(item){if(!parsed.length){setUp()}return(parsed.queryKey[item]===null)?null:parsed.queryKey[item]};var setUp=function(){parsed=parseUri();getSegments()};var getSegments=function(){var p=parsed.path;segments=[];segments=parsed.path.length==1?{}:(p.charAt(p.length-1)=="/"?p.substring(1,p.length-1):path=p.substring(1)).split("/")};return{setMode:function(mode){strictMode=mode=="strict"?true:false;return this},setUrl:function(newUri){options.url=newUri===undefined?window.location:newUri;setUp();return this},segment:function(pos){if(!parsed.length){setUp()}if(pos===undefined){return segments.length}return(segments[pos]===""||segments[pos]===undefined)?null:segments[pos]},attr:key,param:param}}();

/*--------------------------------------------------------------------------*/

var baskerville = { src: skin_url + 'js/fonts/baskerville.swf' };
sIFR.activate(baskerville);
/*
sIFR.replace(baskerville, {
	selector: '#navbar li.hover',
	wmode: 'transparent',
	tuneWidth: 5,
	forceSingleLine: true,
	css: {
		'.sIFR-root': { 'color': '#ffffff', 'font-weight': 'bold', 'letter-spacing': -1.2, 'text-transform': 'uppercase' },
		'a:link': { 'color': '#ffdd00', 'text-decoration': 'none' },
		'a:hover': { 'color': '#ffdd00' }
	}
});

sIFR.replace(baskerville, {
	selector: '#navbar li.level-top',
	wmode: 'transparent',
	tuneWidth: 5,
	forceSingleLine: true,
	css: {
		'.sIFR-root': { 'color': '#ffffff', 'font-weight': 'bold', 'letter-spacing': -1.2, 'text-transform': 'uppercase' },
		'a:link': { 'color': '#bbbbbb', 'text-decoration': 'none' },
		'a:hover': { 'color': '#ffdd00' }
	}
});
*/
sIFR.replace(baskerville, {
	selector: 'h1.riders',
	wmode: 'transparent',
	css: {
		'.sIFR-root': { 'color': '#ffffff', 'font-weight': 'normal', 'letter-spacing': -1.2, 'text-transform': 'uppercase' },
		'h1.archiveH1': { 'color': '#ffffff', 'font-style': 'normal' }
	}
});

sIFR.replace(baskerville, {
	selector: 'h1',
	wmode: 'transparent',
	css: {
		'.sIFR-root': { 'color': '#ffffff', 'font-weight': 'bold', 'letter-spacing': -1.2, 'text-transform': 'uppercase' },
		'a': { 'color': '#ffffff', 'font-weight': 'bold', 'letter-spacing': -1.2, 'text-transform': 'uppercase', 'text-decoration': 'none' },
		'a:hover': { 'color': '#ffdd00' },
		'em': { 'color': '#ffdd00', 'font-style': 'normal' }
	}
});

/*--------------------------------------------------------------------------*/

var BasicSlider = function (container, increment, delay) {
	this.container = $j(container);
	this.increment = increment;
	this.contents = this.container.find('.slides');
	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;
	
		$j(window).bind('resize', function () {
			self.width = $j(self.slides[0]).width();
			self.goTo(0);
		});
		
		this.prevBtn.click(function () {
			$j(this).addClass('active');
			self.back();
		});
		
		this.nextBtn.click(function () {
			$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: 500
				//easing: 'easeOutExpo'
			}
		);
	},
	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 === this.slides.length) {
			this.timer = false;
			this.goTo(0);
        }
        else {
            this.goTo(this.current + 1);
        }
        this.start();
	}
};

/*--------------------------------------------------------------------------*/
//center element
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;
}

/*--------------------------------------------------------------------------*/

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'));
			});
		}
	});
}

/*--------------------------------------------------------------------------*/
//product compare
var ProductCompare = function (trigger, pop) {
	this.trigger = $j(trigger);
	this.pop = $j(pop);
	this.loader = $j('#loader_sm');
	this.path = "/catalog/product_compare/index/";
	this.setup();
};
ProductCompare.prototype = {
    setup: function () {
        var self = this;
        this.trigger.bind('click', function () {
			self.dispatch();
        });
		this.cache = {};
    },
	dispatch: function () {
		if ($j('#compare-items')) {
			if ($j('#compare-items').find('li').length > 5) {
				alert("You can only compare up to 5 products");
				return;
			}
			else {
				this.loader.show();
				this.open();
			}
		}
	},
    open: function () {
		var self = this;
		this.pop.load(this.path, function () {
			self.loader.hide();
			self.pop.center();
			self.pop.show();
			
			self.pop.find('.close').bind('click', function () {
				self.close();
			});
		});
    },
    close: function () {
        this.pop.hide();
		$j('#modal_overlay').hide();
    },
	init_modal_overlay: function (el) {
		el.css("height", $j('body:first').height());
		$j(el).show();
	}

};

/*--------------------------------------------------------------------------*/

var PrintPage = function (trigger, content) {
	this.trigger = $j(trigger);
	this.content = $j(content);
	this.setup();
};
PrintPage.prototype = {
	setup: function () {
		var self = this;
		this.trigger.bind('click', function () {
			self.open();
		});
	},
	open: function () {
		var pop = window.open("");
		pop.document.open("text/html", "replace");
		
		var contents = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>";
		contents += "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>";
		contents += "<head><title>" + $j('title:first').html() + "</title>";
		contents += "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
		contents += "<link rel='stylesheet' type='text/css' href='" + $j('#print_stylesheet').attr('href') + "' media='all' />";
		contents += "</head>";
		contents += "<body class='print'><div class='col-main'>";
		contents += this.content.html();
		contents += "</div></body></html>";
		
		pop.document.write(contents);
		pop.document.close();
	}
};

/*--------------------------------------------------------------------------*/

var VerticalScroller = function (container, increment) {
	this.container = $j(container);
	this.increment = increment;
	this.contents = this.container.find('.scroller');
	this.slides = this.contents.find('li');
	this.prevBtn = this.container.find('.prev:first');
	this.nextBtn = this.container.find('.next:first');
	
	this.slideCount = Math.round(this.slides.length / this.increment);
	
	this.offset = this.contents.position()['top'];
	this.height = this.contents.find('li:first').height();
	//this.height = 142;
		
	this.current = 0;
	this.setup();
};
VerticalScroller.prototype = {
	setup: function () {
		var self = this;

		this.prevBtn.click(function () {
			$j(this).addClass('active');
			self.prev();
		});
		
		this.nextBtn.click(function () {
			$j(this).addClass('active');
			self.next();
		});
		
		this.setActive();
	},
	setActive: function () {
		var self = this;
		var current = jQuery.url.segment(4);
		this.slides.each(function (i) {
			if ($j(this).hasClass(current)) {
				setTimeout(function () {
					self.goTo(i);
				}, 500);
				$j(this).addClass('active');
			}
		});
	},
	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(
			{ top: (-(idx * this.height) + this.offset) },
			{
				duration: 300
				//easing: 'easeOutExpo'
			}
		);
	},
	prev: function () {
        if (this.current === 0) {
			return;
            //this.goTo(this.slides.length - 1);
        }
        else {
            this.goTo(this.current - 1);
        }	
	},
	next: function () {
        if (this.current === this.slides.length - 1) {
            //this.goTo(0);
			return;
		}
        else {
            this.goTo(this.current + 1);
        }
	}
};

/*--------------------------------------------------------------------------*/

/*var ShowMore = function (contents) {
	this.contents = $j(contents);
	this.slideContent = this.contents.find('.content:first');
	this.toggler = this.contents.find('.view:first');
	this.max = 600;
	this.setup();
};*/
/*ShowMore.prototype = {
	setup: function () {
		var self = this;
		var origHeight = this.slideContent.height();
		
		if (this.slideContent.height() > this.max) {
			this.slideContent.css("height", this.max);
		}
		
		this.toggler.toggle(
			function () {
				self.slide(self.slideContent, origHeight);
				self.toggler.html('View Less &raquo;');
			},
			function () {
				self.slide(self.slideContent, self.max);
				self.toggler.html('View More &raquo;');
			}
		);
	},
	slide: function (el, h) {
		$j(el).animate(
			{ height: h },
			{ duration: 300 }
		);
	}
};*/

/*--------------------------------------------------------------------------*/

function adjustStyle(width) {
    width = parseInt(width);
	if (width <= 1140) {
		$j("body:first").addClass("screen_1024");
	}
	else if (width > 1140) {
		$j("body:first").removeClass("screen_1024");
	}
}

/*--------------------------------------------------------------------------*/

var CountrySwitcher = function (container) {
	this.container = $j(container);
	this.dropdown = this.container.find('select:first');
	this.flag = this.container.find('.flag div:first');
	this.setup();
};
CountrySwitcher.prototype = {
	setup: function () {
		var self = this;
		$j(this.dropdown).bind('change keyup keydown', function () {
			var old_code = $j(self.flag).attr('class');
			var new_code = self.dropdown.find('option:selected').attr('value');
			self.switchCountry(old_code, new_code);
			var path = window.location.pathname;
			var path_pieces = [];
			old_code = old_code.toLowerCase();
			new_code = new_code.toLowerCase();
			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;
			
		});
	},
	switchCountry: function (current, next) {
		if (next == '') { return; }
		else {
			$j(this.flag).removeClass(current);
			$j(this.flag).addClass(next);
			var txt = this.dropdown.find('option:selected').text();
			_gaq.push(['_trackEvent', 'Country Change', 'Switch', txt]);
			$j(this.flag).attr('title', txt);
			$j(this.flag).html(txt);
		}
	}
};

/*--------------------------------------------------------------------------*/

$j(document).ready(function(){
	if ($j.browser.msie && $j.browser.version=="6.0") {
		try {
			document.execCommand('BackgroundImageCache', false, true);
		} catch (e) {}
	}	
	
	
	//adjustStyle($j(window).width());
	//$j(window).resize(function() {
	//	adjustStyle($j(window).width());
	//});
	
	/* country switcher */
	new CountrySwitcher($j('#locale'));
	
    /* homepage slideshow */
    if ($j('#hero').length) {
		new BasicSlider($j('#hero'), 1, 10);
    }

    /* fancybox */
    if ($j("a.fancy").length) {
		$j("a.fancy").fancybox({
			'hideOnContentClick': true,
			'overlayShow':	true,
			'speedIn': 5,
			'speedOut': 5
		});
    }
    if ($j("a.fancySwf").length) {
		$j("a.fancySwf").fancybox({
		'scrolling': 'no',
		'titleShow': true,
		'overlayShow':	true
		});
    }

	
    /* product compare */
    if ($j('.btn_compare').length) {
		new ProductCompare($j('.btn_compare'), $j('#product_compare'));
    }

	/* product zoom */
    if ($j('a.zoom').length) {
        var zoom_opts = {
            title: false,
            zoomWidth: 400,
            zoomHeight: 400,
            xOffset: 10,
            yOffset: 0,
            position : 'right',
			showPreload: false
        };
        $j('.zoom').jqzoom(zoom_opts);
    }
	
	/* tab container height adjustment */
	/*
	if ($j('.collateral-tabs').length){
		
		function forceHeight() {
			var wHeight = $j('.wrapper').height();
			var cHeight = $j('#collateral-tabs').height();
			$j('.wrapper').css('height', cHeight + wHeight - 200 + 'px'); 
		}
		$j('#collateral-tabs .tab').each(function(){
			$j(this).bind('click', function(){
				forceHeight();
			});
		});
		forceHeight();
	}
	*/
	
	/* printer friendly product page */
	if ($j('li.print').length) {
		new PrintPage($j('li.print'), $j('.col-main:first'));
	}
	
	if ($j('.riderSlider').length) {
		$j('.riderSlider li a').each(function(){
			$j(this).bind('mouseenter', function(){
				$j(this).addClass('hover');			
			});
			$j(this).bind('mouseleave', function(){
				$j(this).removeClass('hover');			
			});
		});
		
		new VerticalScroller($j('.riderSlider:first'), 3);
	}
	
	externalLinks();
});

