﻿function MyNewzoo()
{
}

MyNewzoo.initialize = function()
{
	Section.setup();
	ExpandableList.setup();
	LookingForAlerts.setup();

	$('a.wizardPreview').click(onWizardPreviewButtonClicked);
}


function LookingForAlerts(container)
{
	this.container = container;
	this.bindClickHandler();
}

LookingForAlerts.prototype.bindClickHandler = function()
{
	var $this = this;
	this.container.bind("click", function(evt)
	{
		var target = evt.target;
		if (target.nodeName == "INPUT")
			setTimeout(function()
			{
				$this.updateCheckboxRow($(target));
			}, 0);
	});
}

LookingForAlerts.prototype.updateCheckboxRow = function(target)
{
	var row = target.parent().parent();
	var checked = $(target).attr("checked");
	if (target.hasClass("interested") && !checked)
		$("input.alert", row).attr("checked", false);
	if (target.hasClass("alert") && checked)
		$("input.interested", row).attr("checked", true);
}

LookingForAlerts.setup = function()
{
	var container = $("#lookingfor-alerts");
	if (container.length != 0)
		LookingForAlerts.instance = new LookingForAlerts(container);
}


function ExpandableList(tableId, addItemId, template)
{
	this.tableId = tableId;
	this.addItemId = addItemId;
	this.template = template;

	this.bindAddLink();
}

ExpandableList.FieldNameFinder = /^([^_]+_)(name)?\d+$/;
ExpandableList.setup = function()
{
	new ExpandableList("#link-list", "#link-add", '<tr><td><input type="text" name="{0}name{1}"/></td><td><input type="text" name="{0}url{1}"/></td></tr>');
	new ExpandableList("#client-list", "#client-add", '<tr><td><input type="text" name="{0}{1}"/></td></tr>');
}

ExpandableList.prototype.bindAddLink = function()
{
	var table = $(this.tableId);
	if (table.length == 0)
		return;

	var $this = this;		
	$(this.addItemId).bind("click", function() { $this.addItem(); });
}

ExpandableList.prototype.addItem = function()
{
	var table = $(this.tableId);
	var fieldName = this.getLinkFieldName(table);
	var count = $("tr", table).length;
	var html = this.template;
	html = html.format(fieldName, count);
	table.append(html);
}

ExpandableList.prototype.getLinkFieldName = function(table)
{
	var field = $("tr:last input:first", table);
	ExpandableList.FieldNameFinder.exec(field.attr("name"))
	return RegExp.$1
}

function Section()
{
	this.bind("advertising-possibilities");
	this.bind("advertising-casestudy");
	this.bind("brandingoptions");
	this.bind("primary-result");
	this.bind("secondary-result");
}

Section.prototype.bind = function(section)
{
	var $this = this;
	var selector = $("#{0}-yes, #{0}-no".format(section));
	if (selector.length == 2)
	{
		selector.bind("click", function()
		{
			var value = $(selector[0]).attr("checked") == true;
			setTimeout(function()
			{
				$this.toggle(section, value)
			}, 1);
		});
		$(selector).filter(":checked").click();
	}
}

Section.prototype.toggle = function(section, value)
{
	var element = $("#section-{0}".format(section));
	if (value)
		element.removeClass("hide");
	else
		element.addClass("hide");
}

Section.instance = null;
Section.setup = function()
{
	if (Section.instance == null)
		Section.instance = new Section();
}


function ImageUpload(name, container, maximumImageCount)
{
	this.name = name;
	this.container = container;
	this.maximumImageCount = maximumImageCount;
	this.imagecontainer = $(".image-existing", this.container);
	this.uploadcontainer = $(".image-uploads", this.container);
	this.uniqueImageCounter = this.currentImageCount();
	
	this.bindAddButtons();
	this.bindDeleteButtons();
	this.updateAddVisibility();
}

ImageUpload.templates = [
	'<li><span><input class="formType1 inputText choose-screenshot" type="file" name="{0}" /> (<a href="javascript:;">Cancel</a>)</span></li>',
	'<li><span><input class="formType1" type="text" name="{0}" /> (YouTube video ID) (<a href="javascript:;">Cancel</a>)</span></li>'
];

ImageUpload.prototype.bindAddButtons = function()
{
	var $this = this;
	$(".btnUploadImage", this.uploadcontainer).bind("click", function() { $this.addUpload(0); });
	$(".btnYouTube", this.uploadcontainer).bind("click", function() { $this.addUpload(1); });
}

ImageUpload.prototype.bindDeleteButtons = function()
{
	var $this = this;
	$(".btn-screenshot-delete", this.imagecontainer).bind("click", function() { $this.removeUpload(this); });
}

ImageUpload.prototype.bindCancelButton = function(elem)
{
	var $this = this;
	$("a", elem).bind("click", function()
	{
		$(this).parent().parent().remove();
		$this.updateAddVisibility();
	});
}

ImageUpload.prototype.addUpload = function(index)
{
	var uploadbutton = this.getUploadButton();
	var fieldname = this.generateFieldName();
	var html = ImageUpload.templates[index].format(fieldname);
	var elem = uploadbutton.before(html).prev();

	this.bindCancelButton(elem);
	this.updateAddVisibility();
	$("input", elem).focus();
}

ImageUpload.prototype.removeUpload = function(elem)
{
	$(elem).parent().remove();
	this.updateAddVisibility();
}

ImageUpload.prototype.updateAddVisibility = function()
{
	var button = this.getUploadButton();
	if (this.currentImageCount() < this.maximumImageCount)
		button.removeClass("hide");
	else
		button.addClass("hide");
}

ImageUpload.prototype.currentImageCount = function()
{
	return this.imagecontainer.children().size()
		+ this.uploadcontainer.children().size() -1;
}

ImageUpload.prototype.generateFieldName = function()
{
	return this.maximumImageCount == 1
		? this.name
		: this.name + "" + this.uniqueImageCounter++;
}

ImageUpload.prototype.getUploadButton = function()
{
	return $("li:last", this.uploadcontainer);
}

ImageUpload.instances = new Object;
ImageUpload.register = function(name, maximumImageCount)
{
	var container = document.getElementById("image-upload-" + name);
	if (isUndefined(ImageUpload.instances[name]) && container)
		ImageUpload.instances[name] = new ImageUpload(name, container, maximumImageCount);
}


function tryPublish()
{
	$("#trypublish").val("1");
	$("#solutionform").submit();
}

function onWizardPreviewButtonClicked()
{
	return openPopup();
}

$(document).ready(MyNewzoo.initialize);