﻿$(window).load(function () {
	runCatScroller();
	runFeatureScroller();
	attachFeatureRollover();
	attachRTMclickTags();
});

// Category vertical scroller
function runCatScroller() {
	numItems = $('#catList li').length;
	$totalImgs = $('#catList li');
	listHeight = numItems * 28;

	$('#catList').css({ height: listHeight + 'px' });

	$('#scrollUp').hover(function (e) {
		$('#container').stop();
		posTop = -parseInt($('#catList').css("top"));
		speed = (posTop) * 6;
		$('#catList').animate({ "top": "0px" }, speed);
	}, function () {
		$('#catList').stop();
	});

	$('#scrollDown').hover(function (e) {
		posTop = parseInt($('#catList').css("top"));
		speed = (listHeight + posTop) * 6;
		scrollTop = listHeight - 250;
		$('#catList').stop();
		$('#catList').animate({ "top": -scrollTop + "px" }, speed);
	}, function () {
		$('#catList').stop();
	});
}

// Initialise scrollable together with the circular plugin
function runFeatureScroller() {
	$("#featureContent").scrollable({ circular: true }).autoscroll(6000);
	var scroller = $("#featureContent").data("scrollable");
	scroller.onSeek(function () {
		var scIdx = scroller.getIndex();
		if (scroller.getIndex() < -50) scIdx += 10;
		var idx = (scIdx % 5) + 1;
		$("#featureList li").removeClass('highlight');
		$("#featureList li strong").removeClass('active');
		$("#featureList li.feature" + idx).addClass('highlight');
		$("#featureList li.feature" + idx + " strong").addClass('active');
	});
	$("#featureList li.feature1").addClass('highlight');
	$("#featureList li.feature1 strong").addClass('active');
}

// Handle manual selection of scroller item
function attachFeatureRollover() {
	$("#featureList li").mouseover(function () {
		var featureNum = parseInt($(this).find('strong').attr('class').substr(7, 1));
		var scroller = $("#featureContent").data("scrollable");
		scroller.seekTo(featureNum - 1, 0);
		scroller.stop();
	});

	$("#featureList").mouseout(function () {
		var scroller = $("#featureContent").data("scrollable");
		scroller.play();
	});
}

// Attach RTM click tags
function attachRTMclickTags() {
	// Left hand feature selector clicks
	var featureLIDseed = 773773;
	var featureMseed = 165164;
	$("#featureList a").each(function (index) {
		$(this).mousedown(function () {
			pingRTM((featureLIDseed + index), (featureMseed + index));
		});
//		var curLink = $(this).attr('href');
//		$(this).attr('href', 'http://srx.main.ebayrtm.com/clk?RtmClk&lid=' + (featureLIDseed + index) + '&m=' + (featureMseed + index));
	});

	// Centre feature clicks
	$(".fc").each(function (index) {
		var realIndex = index;
		if (realIndex == 0) realIndex = 6;
		if (realIndex == 6) realIndex = 1;
		realIndex -= 1;
		$(this).find("a,area").each(function (i) {
			$(this).mousedown(function () {
				pingRTM((featureLIDseed + realIndex), (featureMseed + realIndex));
			});
		});
	});

	// Category navigation clicks
	var catLIDseed = 773785;
	var catMseed = 165169;
	$("#catList a").each(function (index) {
		$(this).mousedown(function () {
			pingRTM((catLIDseed + index), (catMseed));
		});
	});
}

// Ping RTM Click tag
function pingRTM(linkId, msgId) {
	var url = "http://srx.main.ebayrtm.com/clk?RtmClk&a=json&redirect=false&m=" + msgId + "&lid=" + linkId;
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.charset = "utf-8";
	document.body.appendChild(script);
	script.src = url;
}
