﻿var Slider =
{
	images: [],
	idMapping: new Hashtable(),

	currentStep: 1,
	lastSavedStep: 0,
	handleMouseDownCoordinates: null,
	handleMouseDownOffsetLeft: 0,
	handleMouseDown: false,
	divMouseDown: false,

	confirmChangeUrl: "/",

	token: null,
	serverRequest: new ServerRequest("GET", this.dummy, this.dummy),

	disableFilterConfirmed: false,
	disableFilterConfirmText: "Are you sure you want to see the picture?",

	addImage: function(toggleableEroticImage) {
		this.images.push(toggleableEroticImage);
		this.idMapping.set(toggleableEroticImage.id, toggleableEroticImage)
	},

	removeImage: function(id) {
		for (var i = 0; i < this.images.length; i++) {
			if (this.images[i].id == id) {
				this.images.splice(i, 1);
			}
		}
		
		this.idMapping.remove(id);
	},

	getImageByID: function(id) {
		return this.idMapping.get(id)
	},

	htmlElements:
	{
		div: null,
		handle: null
	},

	windowOnLoad: function() {
		S.Debugger.writeLine(this.currentStep);
		this.lastSavedStep = this.currentStep;

		// Get elements.		
		this.htmlElements.div = S.Get("Slider");
		this.htmlElements.handel = S.Get("SliderHandle");

		if (this.htmlElements.div == null || this.htmlElements.handel == null)
			return;

		S.Event.add(document, "mouseup", this.onDocumentMouseUp.bindAsEventListener(this));
		S.Event.add(this.htmlElements.handel, "mousedown", this.onHandleMouseDown.bindAsEventListener(this));
		S.Event.add(this.htmlElements.div, "mousedown", this.onDivMouseDown.bindAsEventListener(this));
		S.Event.add(document, "mousemove", this.onDocumentMouseMove.bindAsEventListener(this));
	},

	onDocumentMouseUp: function(e) {
		if (this.handleMouseDown || this.divMouseDown) {
			this.snapHandle();

			//if (this.lastSavedStep != this.currentStep)
			this.save();
		}

		this.handleMouseDown = false;
		this.divMouseDown = false;
	},

	save: function() {
		/*S.Debugger.writeLine(this.lastSavedStep + " - " + this.currentStep);
		S.Debugger.writeLine((this.lastSavedStep != this.currentStep && this.currentStep == 3));
		S.Debugger.writeLine((this.lastSavedStep == 3 && this.currentStep != 3));*/

		// Remove or add profiles.
		if ((this.lastSavedStep != this.currentStep && this.currentStep == 3) || (this.lastSavedStep == 3 && this.currentStep != 3)) {
			window.location.href = this.confirmChangeUrl.replace("##RequestedStep##", this.currentStep);
			return;
		}

		this.lastSavedStep = this.currentStep;

		// Update configuration.
		this.serverRequest.send("/Global/Profiles/UpdateSliderStep.aspx", "Value=" + this.currentStep + "&Token=" + this.token);

		if (this.currentStep == 1)
			this.showEroticImages();
		else //if (this.currentStep == 2)
			this.hideEroticImages();
	},

	showEroticImages: function() {
		this.handleImages();
	},

	hideEroticImages: function() {
		this.handleImages();
	},

	handleImages: function() {
		this.images.each(function(toggleableEroticImage) {
			toggleableEroticImage.setSource(this.currentStep != 1);
		} .bind(this));
	},

	handleImageWithConfirm: function(e) {
		var ev = S.Event.wrap(e);

		if (this.currentStep == 2 && !this.disableFilterConfirmed) {
			if (confirm(this.disableFilterConfirmText)) {
				this.disableFilterConfirmed = true;
				this.currentStep = 1;
				this.htmlElements.handel.style.left = "6px";
				this.save();
			}

			ev.preventDefault();
			return false;
		}

		return true;
	},

	snapHandle: function() {
		var currentLeft = (S.Element.getPosition(this.htmlElements.handel).x - S.Element.getPosition(this.htmlElements.div).x - 6);

		// Step 1.
		if (currentLeft <= 21) {
			this.currentStep = 1;
			this.htmlElements.handel.style.left = "6px";
		}

		// Step 2.
		else if (currentLeft <= 48) {
			this.currentStep = 2;
			this.htmlElements.handel.style.left = "33px";
		}

		// Step 3.
		else {
			this.currentStep = 3;
			this.htmlElements.handel.style.left = "60px";
		}
	},

	onDivMouseDown: function(e) {
		if (this.handleMouseDown)
			return;

		this.divMouseDown = true;

		var ev = S.Event.wrap(e);
		var divPositionLeft = S.Element.getPosition(this.htmlElements.div).x + 6;
		var offset = ev.mouseCoordinates.x - divPositionLeft;

		// Step 1.
		if (offset <= 21) {
			this.currentStep = 1;
			this.htmlElements.handel.style.left = "6px";
		}

		// Step 2.
		else if (offset <= 48) {
			this.currentStep = 2;
			this.htmlElements.handel.style.left = "33px";
		}

		// Step 3.
		else {
			this.currentStep = 3;
			this.htmlElements.handel.style.left = "60px";
		}
	},

	onHandleMouseDown: function(e) {
		var ev = S.Event.wrap(e);
		this.handleMouseDownCoordinates = ev.mouseCoordinates;
		this.handleMouseDownOffsetLeft = S.Element.getPosition(this.htmlElements.handel).x;
		this.handleMouseDown = true;
		ev.preventDefault();
	},

	onDocumentMouseMove: function(e) {
		if (!this.handleMouseDown)
			return;

		var ev = S.Event.wrap(e);

		var divPositionLeft = S.Element.getPosition(this.htmlElements.div).x + 6;
		var difference = ev.mouseCoordinates.x - this.handleMouseDownCoordinates.x;
		var newOffsetLeft = this.handleMouseDownOffsetLeft + difference;
		var newLeft = newOffsetLeft - divPositionLeft;

		if (newLeft < 0)
			this.htmlElements.handel.style.left = "0px"
		else if (newLeft > 66)
			this.htmlElements.handel.style.left = "66px"
		else
			this.htmlElements.handel.style.left = newLeft + "px";
	}
};

S.Event.add(window, "load", Slider.windowOnLoad.bind(Slider));

function ToggleableEroticImage(id, onSource, offSource, enableEroticFilterToggleOnClick)
{
	this.id = id;
	this.image = S.Get(id);
	this.onSource = onSource;
	this.offSource = offSource;
	
	Slider.addImage(this);
	
	this.confirmFunction = Slider.handleImageWithConfirm.bindAsEventListener(Slider);
	
	if (enableEroticFilterToggleOnClick && this.onSource.length > 0 && this.onSource != this.offSource)
		S.Event.add(this.image, "click", this.confirmFunction);
	
	if (this.image.src.indexOf(this.offSource) != -1)
	{
		if (blnIE)
		{
			var aParent = S.GetParentByTagName("a", this.image);
			this.image.style.cursor = (aParent != null ? "pointer" : "inherit");
		}
		else
		{
			this.image.style.cursor = "inherit";
		}
	}
	else
	{
		this.image.style.cursor = "pointer";
	}
	
	pcm(this.image);
}

ToggleableEroticImage.prototype.setSource = function(on)
{
	// This means it's a non erotic image.
	if (this.onSource.length == 0)
		return;

	this.image.src = this.getSource(on);
	
	if (!on)
	{
		if (blnIE)
		{
			var aParent = S.GetParentByTagName("a", this.image);
			this.image.style.cursor = (aParent != null ? "pointer" : "inherit");
		}
		else
		{
			this.image.style.cursor = "inherit";
		}
	}
	else
	{
		this.image.style.cursor = "pointer";
	}
}

ToggleableEroticImage.prototype.getSource = function(on)
{
	return (on && this.onSource.length > 0 ? this.onSource : this.offSource);
}

ToggleableEroticImage.prototype.refresh = function()
{
	var useOnSouce = (Slider.currentStep != 1);
	var source = this.getSource(useOnSouce);
	var date = new Date();
	
	if (source.indexOf("&") != -1)
	    source += (source.indexOf("&") != -1 ? "&" : "?") + "RefreshDate=" + date.valueOf();
	    
	this.image.src = source;
	
	if (!useOnSouce)
	{
		if (blnIE)
		{
			var aParent = S.GetParentByTagName("a", this.image);
			this.image.style.cursor = (aParent != null ? "pointer" : "inherit");
		}
		else
		{
			this.image.style.cursor = "inherit";
		}
	}
	else
	{
		this.image.style.cursor = "pointer";
	}
}