var TP = function() {
	return {
		'isStupidMSIE':(/*@cc_on!@*/false),
		'hasResolution':(screen.width && screen.height),
		'maxw':1800,
		'maxh':1200,
		'minw':800,
		'minh':600,
		'intRegExp':new RegExp("^[0-9]+$"),
		'prop':null,
		'setting':null,
		'hidden':true,
		'html':['',
			'<button id="fPreButton" type="button">Create Wallpaper<\/button>',
			'<form action="/title/generate.php" method="post">'
			+'<p class="tall"><label for="fSetting">Setting:<select id="fSetting" name="fSetting">'+timehtml+'<\/select><\/label>'
			+'<label for="fProp">Scene:<select id="fProp" name="fProp">'+prophtml+'<\/select><\/label><\/p>'
			+'<p id="pDim" class="hide">'
			+'<label for="fWidth">Width:<input id="fWidth" name="fWidth" value="0" maxlength="4" \/><\/label>'
			+'<label for="fHeight">Height:<input id="fHeight" name="fHeight" value="0" maxlength="4" \/><\/label><\/p>'
			+'<div id="err"><\/div>'
			+'<p class="tall"><button type="submit">Go<\/button><button type="button" id="fDim">&nbsp;<\/button><button type="button" id="fCancel">Cancel<\/button><\/p>'
			+'<\/form>'],
		'isInt':function(i) {
			return TP.intRegExp.test(i);
		},
		'$':function(e) {
			return document.getElementById(e);
		},
		'setStyle':function(e,s) {
			e.style.cssText = s;
		},
		'replaceHtml':function(e,h) {
			if (TP.isStupidMSIE) {
				e.innerHTML = h;
				return e;
			} else {
				var ne = e.cloneNode(false);
				ne.innerHTML = h;
				e.parentNode.replaceChild(ne,e);
				return ne;
			}
		},
		'updateDim':function() {
			if (TP.hasResolution) {
				TP.$('fDim').innerHTML = TP.e.width.value+"x"+TP.e.height.value;
			} else {
				TP.changeDimensions();
			}
		},
		'changeDimensions':function() {
			if (!TP.hidden) {
				TP.$('fDim').innerHTML = TP.e.width.value+"x"+TP.e.height.value;
				TP.$('pDim').className = 'hide';
			} else {
				TP.$('fDim').innerHTML = "Hide Dimensions";
				TP.$('pDim').className = '';
			}
			TP.hidden = !TP.hidden;
			return false;
		},
		'checkDimensions':function() {
			if (TP.isInt(TP.$('fHeight').value) && TP.isInt(TP.$('fWidth').value)) {
				if (TP.$('fWidth').value < TP.minw || TP.$('fHeight').value < TP.minh || TP.$('fWidth').value > TP.maxw || TP.$('fHeight').value > TP.maxh) {
					TP.$('err').innerHTML = '<p class="tall">Woops! The wallpaper resolution must be greater than '+TP.minw+'x'+TP.minh+' and less than '+TP.maxw+'x'+TP.maxh+'.<\/p>';
				} else {
					TP.$('err').innerHTML = '';
				}
			} else {		
				TP.$('err').innerHTML = '<p class="tall">Woops! Your resolution must be a number.<\/p>';
			}
		},
		'updateProp':function() {
			TP.prop = TP.$('fProp').value;
			if (TP.isStupidMSIE) {
				TP.setStyle(TP.$('prop'),'background: none; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/images/title/props/'+TP.prop+'.png\');');
			} else {
				TP.setStyle(TP.$('prop'),'background: url(/images/title/props/'+TP.prop+'.png) no-repeat center 0;');
			}
		},
		'updateSetting':function() {
			document.body.style.backgroundImage = 'url(/images/title/hours/'+TP.$('fSetting').value+'.png)';
		},
		'e':{},
		'init':function() {
			TP.replaceHtml(TP.$('dynamic'),TP.html[1]);
			TP.$('fPreButton').onclick = TP.begin;
		},
		'begin':function() {		
			TP.html[0] = TP.$('main').innerHTML;
			TP.$('main').innerHTML = '';
			TP.replaceHtml(TP.$('dynamic'),TP.html[2]);
			
			TP.e.main = TP.$('main');
			TP.e.dynamic = TP.$('dynamic');
			TP.e.width = TP.$('fWidth');
			TP.e.height = TP.$('fHeight');
			TP.e.setting = TP.$('fSetting');
			TP.e.prop = TP.$('fProp');
			TP.e.err = TP.$('err');
			
			if (TP.hasResolution) {
				(TP.$('fWidth')).value = screen.width;
				(TP.$('fHeight')).value = screen.height;
			}
			TP.$('fSetting').onchange = TP.updateSetting;
			TP.$('fProp').onchange = TP.updateProp;
			TP.$('fWidth').onchange = TP.checkDimensions;
			TP.$('fHeight').onchange = TP.checkDimensions;
			TP.$('fDim').onclick = TP.changeDimensions;
			TP.$('fCancel').onclick = TP.cancel;
			TP.updateDim();
		},
		'cancel':function() {
			TP.replaceHtml(TP.$('main'),TP.html[0]);
			TP.init();
		}
	};
}();

window.onload = TP.init;
