var lmui = function() {
	return {
		initialize: function(init) {
			if (init['pKey']) {
				lmajax.initialize(init['cKey'], init['pKey']);
			} else {
				lmajax.initialize(init['cKey']);
			}
			var pathname = window.location.href;
			lmajax.fn = init['fn'];
			lmajax.ln = init['ln'];
			lmajax.em = init['em'];
			lmajax.cKey = init['cKey'];
			lmajax.pKey = init['pKey'];
			lmui.sp = init['sp'];
			lmui.pubn = init['publisherName'];
			lmui.cn = init['checkoutName'];
			lmui.con = init['contractName'];
			lmui.domain = init['domain'];
			lmui.domainDisplay = init['domainDisplay'];
			lmui.currStep = '';
			lmui.currCat = 18;
			lmui.olcnt = 0;
			document.getElementById('hiw').style.display = '';
			if ((init['fn'] != '') && (init['ln'] != '') && (init['em'] != '') && (pathname.indexOf("#") > -1)) {
				lmui.update(pathname.substring(pathname.indexOf("#")+1));
			} else {
				lmui.update('login');
			}
		},
		displayCats: function() {
			var catList = document.getElementById("offer_nav");
			if (catList.innerHTML == '' && lmajax.offers) {
				var categories = lmajax.offers.distribution.categories;
				var first = true;
				for (x in categories) {
					if (categories[x].name && categories[x].categoryId) {
						var catId = 'cat' + categories[x].categoryId;
						if (first) {
							catList.innerHTML += '<li id="'+catId+'" class="active"><a href="#" onClick="lmui.update(\'offers/'+categories[x].categoryId+'\'); return false;"><strong>'+categories[x].name+' ('+categories[x].cnt+')</strong></a></li>';
							first = false;
						} else {
							catList.innerHTML += '<li id="'+catId+'"><a href="#" onClick="lmui.update(\'offers/'+categories[x].categoryId+'\'); return false;">'+categories[x].name+' ('+categories[x].cnt+')</a></li>';
						}
					}
				}
				catList.innerHTML += '<li id="cat0"><a href="#" onClick="lmui.update(\'offers/0\'); return false;"><em>All Offers</em></a></li>';
			}
		},
		showCat: function(catId) {
			var catOffers = lmajax.getCatOffers(catId);
			var offerDiv = '';
			for (var i = 0; i < catOffers.length; i++) {
				offerDiv = offerDiv + lmui.getOfferDesc(lmajax.getOfferInfo(catOffers[i]));
			}
			document.getElementById('offer_list').innerHTML = offerDiv;
			lmui.highlightCat(catId);
			lmajax.hitCat(catId);
		},
		highlightCat: function(catId) {
			var catList = document.getElementById("offer_nav");
			if (catList.innerHTML == '') {
				lmui.displayCats();
			}
			var catListEls = document.getElementById("offer_nav").getElementsByTagName("li");
			for (var i = 0; i < catListEls.length; i++) {
				catListEls[i].className = '';
			}
			document.getElementById('cat'+catId).className = 'highlight';
		},
		getOfferDesc: function(offerInfo) {
			var offerDesc = '<div id="'+offerInfo["id"]+'" class="offerbox boxcontainer"><div class="hd"><h2>'+offerInfo["name"]+'</h2><span class="wildcoins">Earn '+lmajax.offers.distribution.offers[offerInfo["id"]].value+' WildCoins for Free</span></div>' + 
				'<div class="bd"><a href="#" onClick="lmui.showOfferInsMW('+offerInfo["id"]+'); return false;"><img alt="'+offerInfo["name"]+'" class="offer_thumb" src="'+lmui.sp+offerInfo["thumbnail"]+'"/></a><p>'+offerInfo["desc"]+'</p><span class="credits">Credits '+offerInfo["delay"]+'</span>' + 
				'<a href="#" onClick="lmui.showOfferInsMW('+offerInfo["id"]+'); return false;"><img alt="Select Offer" class="select_offer" src="'+lmui.sp+'/images/flows/135/btn-continue.gif"/></a></div>' + 
				'<div class="ft"><span class="bottom"></span></div></div>';
			return offerDesc;
		},
		getOfferIns: function(offerInfo) {
			var offerIns = '<h2>'+offerInfo["name"]+'</h2>' + 
				'<a href="#" onClick="lmui.launchOffer(\''+offerInfo["oid"]+'\'); return false;"><img alt="'+offerInfo["name"]+'" class="offer_thumb" style="float: left;" src="'+lmui.sp+offerInfo["thumbnail"]+'"/></a>' +
				'<div class="instructions"><h3>To Complete Offer:</h3><ul>';
			if (lmajax.em && (lmajax.em != '')) {
				offerIns = offerIns + '<li><span>Confirm your email: <p id="emailText">' + 
				'<strong><span id="emailStr">'+lmajax.em+'</span></strong> <span class="changeEmail">(<a href="#" onclick="lmui.toggleEmail(); return false;">change</a>)</span></p>' + 
				'<span id="emailForm" style="display:none;"><form onsubmit="lmajax.submitEmail(this); return false;"><input id="cInput" name="c" type="hidden" value="'+lmajax.cKey+'" />' + 
				'<input id="pInput" name="p" type="hidden" value="'+lmajax.pKey+'" /><input id="emailInput" name="email" type="text" value="'+lmajax.em+'" />' + 
				'<input type="submit" name="submit" value="Update >"/><span id="emailUpdateError" class="error"></span></form></span></span></li>';
			}
			if (!lmajax.offers.distribution.offers[offerInfo["id"]].hosted) {
				offerIns = offerIns + '<li><span>Click below to visit our partner\'s site.</span></li>';
			}
			if (offerInfo['ins1']) {
				offerIns = offerIns + '<li><span>'+offerInfo['ins1']+'</span></li>';
			}
			if (offerInfo['ins2']) {
				offerIns = offerIns + '<li><span>'+offerInfo['ins2']+'</span></li>';
			}
			if (offerInfo['ins3']) {
				offerIns = offerIns + '<li><span>'+offerInfo['ins3']+'</span></li>';
			}
			if (offerInfo['ins4']) {
				offerIns = offerIns + '<li><span>'+offerInfo['ins4']+'</span></li>';
			}
				offerIns = offerIns + '<li><span>You will receive an email when your offer completion has been confirmed.</span></li></ul>' + 
				'<a href="#" onClick="lmui.launchOffer(\''+offerInfo["oid"]+'\'); return false;"><img alt="Continue To Offer" class="offerbutton" src="'+lmui.sp+'/images/flows/135/btn-continuetooffer.gif"/></a></div>';
			
			return offerIns;
		},
		showOfferDesc: function(offerId) {
			document.getElementById(offerId).innerHTML = lmui.getOfferDesc(offerId);
		},
		showOfferIns: function(offerId) {
			var offerListDivs = document.getElementById('offer_list').getElementsByTagName('div');
			for (var i = 0; i < offerListDivs.length; i++) {
				if (offerListDivs[i].getAttribute('id')) {
					lmui.showOfferDesc(lmajax.getOfferInfo(offerListDivs[i].id));
				}
			}
			document.getElementById(offerId).innerHTML = lmui.getOfferIns(offerId);
		},
		showOfferInsMW: function(offId) {
			lmui.currOffer = offId;
			if (lmajax.em && (lmajax.em != '')) {
				document.getElementById('launchCont').style.width = '550px';
				launchCont.innerHTML = '<div class="hd"></div><div class="bd"><a class="close" href="#" onclick="mo.hideLaunchBox(); return false;">[x] close</a>' + lmui.getOfferIns(lmajax.getOfferInfo(offId)) + '</div><div class="ft"></div></div>';
				mo.showLaunchBox();
				lmajax.hitGoogle(lmui.pubn,lmui.cn,lmui.con,'offer');
			} else {
				launchCont.innerHTML = launchContCopy;
				document.getElementById('launchCont').style.width = '630px';
				mo.showLaunchBox();
				document.getElementById('birthYear').style.visibility = 'visible';
				if (document.getElementById('firstName')) {document.getElementById('firstName').focus();}
			}
		},
		toggleEmail: function() {
			var textSpan = document.getElementById('emailText');
			var formSpan = document.getElementById('emailForm');
			
			if(textSpan && formSpan) {
				if (textSpan.style.display == 'none') {
					textSpan.style.display = '';
				} else {
					textSpan.style.display = 'none';
				}	
				if (formSpan.style.display == 'none') {
					formSpan.style.display = '';
				} else {
					formSpan.style.display = 'none';
				}
			}
		},
		changeEmail: function() {
			var ref = document.getElementById('emailStr');
			if (ref) {
				ref.innerHTML = lmajax.em;
			}
			ref = document.getElementById('emailUpdateError');
			if (ref) {
				ref.innerHTML = '';
			}
			lmui.toggleEmail();
		},
		launchOffer: function(oid) {
			lmui.oid = oid;
			lmui.olcnt = lmui.olcnt + 1;
			var wt = 'coffer' + lmui.olcnt;
			if (lmajax.offers.distribution.offers[lmui.currOffer].hosted) {
				window.location = 'http://'+window.location.hostname+'/checkout/hostedOffer?c='+lmajax.cKey+'&o='+oid+'&fsn=1';
			} else {
			url = '/checkout/launch?o='+oid+'&c=' + lmajax.cKey;
				if (lmajax.pKey) {url = url + '&p=' + lmajax.pKey;}
			var params = "status=1,toolbar=1,menubar=1,resizable=1,location=1,scrollbars=1,left=0,top=0";
			if (screen.availWidth || screen.width)	{
				params = params + ",width=" + (screen.availWidth || screen.width) + ",height=" + (screen.availHeight || screen.height);
			}
			var w = window.open(url, wt, params);
			launchCont.innerHTML = '<div class="hd"></div>' + 
				'<div class="bd"><h2>'+lmajax.fn+', did you try or purchase a partner offer to receive your free WildCoins?</h2><a href="#" onclick="lmui.chooseAnother(); return false;"><img alt="Choose Another Offer" class="choose" src="'+lmui.sp+'/images/flows/mw/btn-purple1l-choose.gif"/></a><a href="#" onclick="lmui.update(\'confirmation\'); return false;"><img alt="Show Confirmation" src="'+lmui.sp+'/images/flows/mw/btn-purple1r-confirm.gif"/></a></div>' + 
				'<div class="ft"></div></div>';
			mo.showLaunchBox();
			}
		},
		chooseAnother: function() {
			mo.hideLaunchBox();
		},
		message: function() {
			switch (arguments[0]) {
				case 'login':
					document.getElementById('firstNameError').innerHTML = arguments[1][0];
					document.getElementById('lastNameError').innerHTML = arguments[1][1];
					document.getElementById('emailError').innerHTML = arguments[1][2];
					if (arguments[1][3]) {document.getElementById('birthYearError').innerHTML = arguments[1][3];}
					if (arguments[1][4]) {
						document.getElementById('birthYearError').innerHTML = 'We apologize, but this service is not available to you at this time. Please close your browser and return with a parent.';
						document.getElementById('modalclose').style.visibility = 'hidden';
						document.getElementById('submit').disabled = true;
						document.getElementById('submit').style.cursor = 'not-allowed';
						document.getElementById('birthYear').disabled = true;
						document.getElementById('firstName').disabled = true;
						document.getElementById('lastName').disabled = true;
						document.getElementById('email').disabled = true;
					}
					if (!arguments[1][3] && !arguments[1][4]) {document.getElementById('birthYearError').innerHTML = '';}
					break;
				case 'emailChange':
					if (arguments[1]) {
						document.getElementById('emailUpdateError').innerHTML = arguments[1];
					} else {
						document.getElementById('emailUpdateError').innerHTML = 'Error';
					}
					break;
				case 'confirmStatus':
					if (arguments[1]) {
						document.getElementById('confirmStatus').innerHTML = '<div id="message">' + arguments[1] + '</div>';
					} else {
						var messagePending = '<p>There was an error retrieving your status.' + 
							'</p><p>If you would like to contact customer service directly, please visit <a href="/about/support" target="_blank">http://www.'+lmui.domain+'/about/support</a> to create an account and open a job ticket.' + 
							'</p><p>Sincerely,<br/>' + lmui.domainDisplay + 
							'</p><p><br/>Didn\'t sign up? <a href="#" onclick="lmui.update(\'offers\'); return false;">Click here to choose an offer</a>.</p>';
						document.getElementById('confirmStatus').innerHTML = '<div id="message">' + messagePending + '</div>';
						break;
					}
				default:
			}
		},
		update: function(newLoc, historyData) {
			/*if ((newLoc != 'login') && (lmajax.fn == '' || lmajax.fn === undefined || lmajax.ln == '' || lmajax.ln === undefined || lmajax.em == '' || lmajax.em === undefined)) {
				lmui.update('login');
			} else {*/
				if (!newLoc) {newLoc = '';}
				newLoc = newLoc.split('/');
				lmui.currStep = newLoc[0];
				switch (newLoc[0]) {
					case 'login':
						if (!lmajax.offers) {
							setTimeout("lmui.update('login');", 500);
						} else {
							lmui.showCat(18);
							document.getElementById('offers').style.display = '';
							document.getElementById('confirm').style.display = 'none';
							launchCont.innerHTML = launchContCopy;
							document.getElementById('launchCont').style.width = '630px';
							mo.showLaunchBox();
							document.getElementById('birthYear').style.visibility = 'visible';
							if (document.getElementById('firstName')) {document.getElementById('firstName').focus();}
							dhtmlHistory.add('login',null);
							lmajax.hitGoogle(lmui.pubn,lmui.cn,lmui.con,lmui.currStep);
						}
						break;
					case 'loggedin':
						if (mo.isVisible()) {
							mo.hideLaunchBox();
						}
						if (lmui.currOffer) {
							lmui.showOfferInsMW(lmui.currOffer);
						} else {
							//lmui.update('offers/18');
						}
						document.getElementById('offers').style.display = '';
						document.getElementById('confirm').style.display = 'none';
						break;
					case 'offers':
						if (newLoc[1]) {lmui.currCat = newLoc[1];}
							else if (lmui.currCat) {lmui.currCat = lmui.currCat;}
							else {lmui.currCat = 18;}
						if (!lmajax.offers) {
							document.getElementById('loading').innerHTML = '<img src="'+lmui.sp+'/images/waiting.gif"/><p>Loading Offers</p></div>';
							setTimeout("lmui.update('offers');", 500);
						} else {
							document.getElementById('loading').innerHTML = '';
							var loc = 'offers/' + lmui.currCat;
							var hd = [];
								hd['catId'] = lmui.currCat;
							dhtmlHistory.add(loc,hd);
							lmui.showCat(lmui.currCat);
						}
						document.getElementById('offers').style.display = '';
						document.getElementById('confirm').style.display = 'none';
						document.getElementById('hiw').style.display = '';
						lmajax.hitGoogle(lmui.pubn,lmui.cn,lmui.con,lmui.currStep);
						break;
					case 'offer':
						lmui.currOffer = newLoc[1];
						lmui.showCat(lmui.currCat);
						lmui.showOfferIns(lmui.currOffer);
						lmajax.hitGoogle(lmui.pubn,lmui.cn,lmui.con,lmui.currStep);
						break;
					case 'confirmation':
						if (mo.isVisible()) {
							mo.hideLaunchBox();
						}
						document.getElementById('offers').style.display = 'none';
						document.getElementById('confirm').style.display = '';
						document.getElementById('hiw').style.display = '';
						document.getElementById('confirmStatus').innerHTML = '<div id="waiting"><img src="'+lmui.sp+'/images/waiting.gif"/><p>Please Wait</p></div>';
						lmajax.showConfirmation(lmui.oid);
						lmajax.hitGoogle(lmui.pubn,lmui.cn,lmui.con,'complete');
						break;
					default:
						break;
				}
			//}
		}
	};
}();