﻿var collectionLP = {

	intialLoad: true,
	contentPlaceHolderId: '',
	curWatchId: 0,
	curCollectionId: 0,
	collectionRoot: 'http://localhost/RalphLaurenWatches',

	init: function (lpDiv, sliderDiv, collectionId, itemId) {
		collectionLP.curCollectionId = collectionId;
		collectionLP.curWatchId = itemId;
		collectionLP.contentPlaceHolderId = lpDiv;
		watchSlider.init(sliderDiv, collectionLP.curWatchId);
		collectionLP.setHistoryCallBack();
	},

	setHistoryCallBack: function () {

		$(window).bind("hashchange", function (e) {

			var path = $.bbq.getState("page");

			if (path != null) {
				collectionLP.toggleContentPlaceHolder(path);
			}

			if (path == null && !collectionLP.intialLoad) {
				collectionLP.toggleContentPlaceHolder('/' + collectionLP.curCollectionId + 'ajax=true');
			}

			collectionLP.intialLoad = false;

		});

		$(window).trigger("hashchange");

	},

	toggleContentPlaceHolder: function (ajaxPath) {

		$.ajax(
		{
			type: "GET",
			url: ajaxPath,
			success: function (result) {
				$("#" + collectionLP.contentPlaceHolderId).hide().empty().append(result).fadeIn(1000);
			},
			error: function (req, status, error) {

			}

		});

	}

};

var watchSlider = {

	curItemId: 0,
	sliderId: '',
	intervalId: null,
	widthConstant: 800,
	totalWidth: 800,

	init: function (sliderPlaceHolderId, itemId) {

		watchSlider.sliderId = sliderPlaceHolderId;
		curItem = itemId;
		watchSlider.ajaxLinks();
		//watchSlider.setClientSlide();
	},

	setClientSlide: function () {

		watchSlider.totalWidth = $("#divWatchSlideContainer").width();

		$("#divWatchSliderBack").css('cursor', 'pointer').click(function () {
			watchSlider.slideLeft();
		});

		$("#divWatchSliderForward").css('cursor', 'pointer').click(function () {
			watchSlider.slideRight();
		});

	},

	ajaxLinks: function () {

		var slider = $("#" + watchSlider.sliderId);

		$(".slideWatchImage a").bind("click", function () {
			var ajaxPath = $(this).attr("href") + '?ajax=true';
			$.bbq.pushState({ page: ajaxPath });
			return false;

		});
	},

	slideLeft: function () {
		var currentPostion = $("#divWatchSlideContainer").scrollLeft();
		watchSlider.slide(currentPostion - quoteSlider.widthConstant, 'slow');
	},


	slideRight: function () {
		var currentPostion = $("#divWatchSlideContainer").scrollLeft();
		watchSlider.slide(currentPostion + watchSlider.widthConstant, 'slow');
	},

	slide: function (posX, speed) {

		var divSlide = $("#divWatchSlideContainer");
		var imgQuotePrev = $("#divWatchSliderBack");
		var imgQuoteNext = $("#divWatchSliderForward");

		if (posX > 1) {
			imgQuotePrev.hide();
		}
		else {
			imgQuotePrev.show();
		}

		if (posX >= quoteSlider.totalWidth) {
			imgQuoteNext.hide();
		}
		else {
			imgQuoteNext.show();
		}

		divSlide.animate({ scrollLeft: posX }, speed);
	}

}
