﻿BenefulMap.ModalAddPhoto = BenefulMap.Modal.extend({
	init: function(modalTitle, modalSlug, modalElements, modalOptions, snippetPath, params) {
		this._super( modalTitle, modalElements, modalOptions, snippetPath, params);
		this.slug = modalSlug;
		this.checkAnchorForSlug();
	},
	getSlug: function() {
		return this.slug;
	},
	setSlug: function(slug) {
		this.slug = slug;
	},
	addModalTargetListener: function() {
		var self = this;
		$(this.targetEl).live('click', function(event) {
			event.preventDefault();
			self.checkIfUserAuthenticated();
		});
	},
	checkIfUserAuthenticated: function() {
		var self = this;
		WagWorld.Web.AjaxUtility.IsAuthenticated( function(result) {
			var isLoggedIn = result.value.Pass;
			if (isLoggedIn == true) {
				self.setFiredTarget(this);
				self.populate();
			} else {
				self.close();
				$(document).trigger("login.open", [self.getSlug()]);
			}
		});
	},
	revertToInitialState: function() {
		this._super();
		var self = this;
		WagWorld.Web.AjaxUtility.IsAuthenticated( function(result) {
			var isLoggedIn = result.value.Pass;
			if (isLoggedIn == true) {
				//window.location.hash = "";	
			}
		});
	},
	checkAnchorForSlug: function() {
		if (window.location.hash == "#" + this.getSlug()) {
			this.open();
		}
	},
	open: function() {
		this.checkIfUserAuthenticated();
	},
	showThankYou: function() {
		this.setTitle("Thank You");
		var params = {};
		params.ph = 2;
		params.id = jQuery.url.attr("file").replace(".aspx", "");
		this.setParams(params);
		this.populate();
	},
	addModalListeners: function() {
		this._super();
		this.embedPhotoSwf();
		this.continueButtonListener();
		$("#see-photo-link").click(function(event)
		{
			var href = window.location.href + '#1';
			window.location = href;
            window.location.reload();
        });
	},
	embedPhotoSwf: function() {
		var swfContainer = "flashcontent";
		var swfPath = root + "_res/swf/TransformTool.swf";
		var expressInstallPath = root + "_res/swf/expressInstall.swf";
		
		var flashvars = {};
		flashvars.BusinessID = $("#FlashBusinessId").val();
		flashvars.endpointURI = $("#FlashEndPointUri").val();
		flashvars.serverLocation = $("#FlashServerLocation").val();
		flashvars.Context = "Admin";
		flashvars.flash_name = "flashcontent";
		
		var params = {};
		params.quality = "high";
		params.allowScriptAccess = "sameDomain";
		params.wmode = "transparent";
		params.base = root + "_res/swf/";
		params.bgcolor = "#ffffff";
		
		var attributes = "";

		swfobject.embedSWF(swfPath, swfContainer, "710", "500", "10", expressInstallPath, flashvars, params, attributes);
	},
	checkResult: function(pass, msg) {
        console.log(pass, msg);
		if (pass == true) {
			if (msg == "promotion" || msg == " promotion") {
			    this.close();	
				$(document).trigger("promotion.open", [0, "photo"]);
			} else {
				this.showThankYou();
			}
        } else {
            this.redisplayFormWithMessage(msg);
        }
	},
	continueButtonListener: function() {
		var self = this;
		$(document).bind("addphoto.close", function() {
			console.log("addphoto.close triggered");
			self.fireConfirmAddPhoto();
		});
	},
	fireConfirmAddPhoto: function() {
		var self = this;
		WagWorld.Web.AjaxUtility.ConfirmPhotoAdd(parseInt($("#FlashBusinessId").val()), function(result) {
			// console.log(result);
			self.checkResult(result.value.Pass, result.value.Reason);
		});
	}
});


