var Util = {};

Util.preload = function (url) {
    var img = new Image();
    img.src = url;
};

var AllSets = new Object();

function TdButtonSet (name) {
    this.name = name;
    this.selected = null;
}

TdButtonSet.ensure = function (name) {
    if (!AllSets[name]) {
	AllSets[name] = new TdButtonSet(name);
    }

    return AllSets[name];
};

TdButtonSet.prototype.select = function (button) {
    if (this.selected) {
	this.selected.unSelect();
    }
    this.selected = button;
};


var AllButtons = new Object();

function TdButton (elt) {
    this.selected = false;
    this.elt = elt;
}

TdButton.ensure = function (elt) {
    if (!AllButtons[elt.id]) {
	AllButtons[elt.id] = new TdButton(elt);
    }

    return AllButtons[elt.id];
};

TdButton.prototype.setName = function () {
    var name = this.elt.id;
    var index = name.indexOf("-");
    return name.substr(0, index);
};

TdButton.prototype.fieldName = function () {
    return 'f' + this.setName();
};

TdButton.prototype.fieldValue = function () {
    var name = this.elt.id;
    var index = name.indexOf("-");
    return name.substr(index + 1);
};

TdButton.prototype.set = function () {
    var name = this.setName();
    return TdButtonSet.ensure(name);
};

TdButton.prototype.hover = function () {
    if (!this.selected) {
	this.elt.style.background = 'url(http://static.tinytags.wigflip.com/img/select-hover.png) no-repeat';
    }
};

TdButton.prototype.unHover = function () {
    if (!this.selected) {
	this.elt.style.background = '';
    }
};

TdButton.prototype.select = function () {
    if (!this.selected) {
	this.selected = true;
	this.elt.style.background = 'url(http://static.tinytags.wigflip.com/img/selector.png) no-repeat';
	this.set().select(this);
	$(this.fieldName()).value = this.fieldValue();
    }
};

TdButton.prototype.unSelect = function () {
    if (this.selected) {
	this.selected = false;
	this.elt.style.background = '';
    }
};
	

function tdMouseOver (elt) {
    TdButton.ensure(elt).hover();
}

function tdMouseOut (elt) {
    TdButton.ensure(elt).unHover();
}

function tdMouseClick (elt) {
    TdButton.ensure(elt).select();
}

function $ (name) {
    return document.getElementById(name);
}


window.onload = function () {
    if (!State) {
	State = {color: 'color-red',
		 rotation: 'rotation-left',
		 size: 'size-small'};
    }

    TdButton.ensure($(State.color)).select();
    TdButton.ensure($(State.rotation)).select();
    TdButton.ensure($(State.size)).select();

    Util.preload('http://static.tinytags.wigflip.com/img/selector.png');
    Util.preload('http://static.tinytags.wigflip.com/img/select-hover.png');

    $('tagtext').focus();
    if (window.top != window) {
	window.top.location.replace('http://wigflip.com/tinytags/');
    }
};



function makeShirt () {
    $('makeShirt').style.display = 'none';
    $('shirtSpinner').style.display = 'block';

    var pdash = function (string) {
	var index = string.indexOf('-');
	return string.substr(index + 1);
    };

    window.open('http://wigflip.com/tinytags/creating-shirt', 'zazzle');

    var callback = function (obj) {
	$('shirtSpinner').style.display = 'none';
	$('shirtDownloader').style.display = 'block';
	window.open(obj.url, 'zazzle');
	$('shirtLink').href = obj.url;
	$('mainZazzle').href = obj.url;
    };

    var args = {color: pdash(State.color),
		rotation: pdash(State.rotation),
		text: State.text,
		size: 'small'};

		
    ajaxCall('/tinytags/shirtxml', args, callback);
}
