﻿

var leadGeneration = function(settings) {
	this.popupUrl = settings.popupUrl;
	this.loadVideoUrl = settings.loadVideoUrl;
	this.adVideoPlayUrl = settings.adVideoPlayUrl;
	this.hardLeadUrl = settings.hardLeadUrl;
	this.sendingText = settings.sendingText;
	this.loadingOptionsText = settings.loadingOptionsText;

	this.bindEvents();
}

leadGeneration.prototype = {
	bindEvents: function() {
		var environment = this;
		$(document).ready(function(e) {
			$(".leadGeneration").live("click", $.proxy(environment.showLeadOptions, environment));
			$(".hardLeadRequestAnchor").live("click", $.proxy(environment.hardLead, environment));
		});
	},

	showLeadOptions: function(e) {
		var currentTarget = $(e.currentTarget);
		this.openLeadPanel(e, {
			autoClickThrough: currentTarget.attr("autoClickThrough"),
			targetId: currentTarget.attr("targetId"),
			leadType: currentTarget.attr("leadType"),
			webSectionsId: currentTarget.attr("webSectionsId"),
			usageHitId: currentTarget.attr("usageHitId")
		});
	},

	openLeadPanel: function(e, attributes, clickThroughUrl) {
		this.targetId = attributes.targetId;
		this.leadType = attributes.leadType;
		this.webSectionsId = attributes.webSectionsId;
		this.hitId = attributes.usageHitId;

		if (attributes.autoClickThrough == "0")
			e.preventDefault();
		else {
			if (!clickThroughUrl && e.currentTarget)
				clickThroughUrl = $(e.currentTarget).attr("href");

			if (e.currentTarget && e.currentTarget.tagName.toUpperCase() == "A")
				return;

			e.preventDefault();

			window.location.href = clickThroughUrl;

			return;
		}

		var environment = this;
		this.openedDiv = $("<div>" + environment.loadingOptionsText + "</div>");
		this.openedDiv.dialog({
			title: "",
			autoOpen: true,
			autoResize: false,
			width: 635,
			modal: true,
			close: function() {
				environment.openedDiv.remove();
				environment.targetId = null;
				environment.hitId = null;
				environment.leadType = null;
				environment.webSectionsId = null;
			}

		});

		$.ajax(
		{
			type: "GET",
			url: this.popupUrl,
			data: {
				targetId: this.targetId,
				leadType: this.leadType,
				webSectionsId: this.webSectionsId,
				hitId: this.hitId
			},
			context: this,
			cache: false,
			success: function(html) {
				this.openedDiv.html(html);
				this.loadVideo();
			}
		});
	},

	loadVideo: function() {
		$.ajax(
		{
			type: "GET",
			url: this.loadVideoUrl,
			dataType: "text",
			data: { scheduleId: this.targetId },
			context: this,
			cache: false,
			success: function(html) {
				if (html != '')
					$("#leadVideoContainer").html(html);
				else
					$("#leadVideoContainer").remove();
			}
		});
	},

	adVideoPlay: function(e) {
		$.ajax(
		{
			type: "POST",
			url: this.adVideoPlayUrl,
			dataType: "json",
			data: {
				SchedulesRef: this.targetId,
				HitsRef: this.hitId
			},
			context: this,
			cache: false,
			success: function(bool) {
				var test = bool;
			}
		});
	},

	hardLead: function(e) {
		e.preventDefault();

		var currentTarget = $(e.currentTarget);

		currentTarget.text(this.sendingText).css({ textDecoration: 'none' });

		var targetId = currentTarget.attr("targetId");
		var leadType = currentTarget.attr("leadType");
		var webSectionsId = currentTarget.attr("webSectionsId");
		var hitId = currentTarget.attr("usageHitId");

		$.ajax(
		{
			type: "POST",
			url: this.hardLeadUrl,
			data: {
				targetId: targetId,
				leadType: leadType,
				webSectionsId: webSectionsId,
				hitId: hitId
			},
			context: this,
			cache: false,
			success: function(html) {
				$("#modalPopupInner").html(html);
			}
		});
	}
};
