//document.writeln(location.search);
window.TRANSLATE = function TRANSLATE(obj) {
	return obj[language];
};

var animationDuration = 0;//.5;
var choiceimg, choiceimgCapt, cidx, cidy, choiceimgPn;
function select(index) {
	var img = document.getElementById('choiceimg'+index);
	var dx = img.offsetLeft+img.parentNode.offsetLeft, dy = img.offsetTop+img.parentNode.offsetTop;
	choiceimg = img;
	cidx = dx; cidy = dy;
	choiceimgPn = img.parentNode;

	document.getElementById('navigationbar').appendChild(img);
	img.style.position = 'absolute';
	//img.style.left = dx;
	//img.style.top = dy;
	img.style.webkitTransform = 'translateX('+dx+'px) translateY('+dy+'px)';
	img.style.zIndex = 1000;
	//img.style.opacity = 0.5;
	var capt = document.getElementById('choicecaption'+index);
	choiceimgCapt = capt;
	
	setTimeout(function () {
		choicetable.style.webkitTransition = 'opacity '+animationDuration+'s ease-in-out';
		choicetable.style.opacity = '0';
		img.style.webkitTransition = '-webkit-transform '+animationDuration+'s ease-in-out';
		var dx = 40-capt.offsetLeft;
		img.style.webkitTransform = 'translateX('+(160-dx)+'px) translateY(-40px) scale(0.3)';
		console.log(capt.offsetLeft);
		capt.style.webkitTransition = '-webkit-transform '+animationDuration+'s ease-in-out';
		capt.style.webkitTransform = 'translateX('+(225+dx*3)+'px) translateY(-95px) scale(3)';
		backbutton.style.webkitTransition = 'opacity '+animationDuration+'s ease-in-out';
		backbutton.style.opacity = '.5';
		backbutton.disabled = true;
		document.body.style.pointerEvents = 'none';
	}, animationDuration ? 1 : 0);

	setTimeout(function () {
		choicetable.style.display = 'none';
		document.body.style.pointerEvents = '';
		this.loadSegmentAnimated(index);
		backbutton.disabled = false;
		backbutton.blur();
	}, animationDuration ? (animationDuration*1000+50):0);
}

var currentSegment, currentSegmentIndex, didLoadSegment0Info;
function loadSegmentAnimated(index, animated) {
	currentSegmentIndex = index;
	var seg = document.getElementById('choicedetails'+index);
	seg.style.opacity = '0';
	seg.style.display = 'block';
	setTimeout(function () {
		seg.style.webkitTransition = 'opacity '+animationDuration+'s ease-in-out';
		seg.style.opacity = '1';
	}, animationDuration ? 1 : 0);
	currentSegment = seg;
	if (index === 0) {
		if (!didLoadSegment0Info) {
			didLoadSegment0Info = true;
			exportpathnode.textContent = getInfoValue('lastExportDestination');
			mergecheckbox.checked = getInfoValue('mergeProjects');
			openinxcodecheckbox.checked = getInfoValue('openInXcode');
		}
	}
	if (index === 1) {
		seg.innerHTML = '<iframe onload="this.style.opacity = 1;" style="oapcity:0;-webkit-transition:opacity '+animationDuration+'s ease-in-out" src="https://www.appdoo.info/publisher/assistant/'+location.search+'" id="assistantiframe"></iframe>';
	}
	location.href = 'action:showProceedButton';
}

function goBack() {
	currentSegmentIndex = -1;
	location.href = 'action:hideProceedButton';
	if (!choiceimg) return;
	choicetable.style.display = '';
	setTimeout(function () {
		document.body.style.pointerEvents = 'none';
		currentSegment.style.opacity = 0;
		setTimeout(function () {
			choicetable.style.opacity = '1';
			choiceimg.style.webkitTransform = 'translateX('+cidx+'px) translateY('+cidy+'px)';
			choiceimgCapt.style.webkitTransform = '';
			backbutton.style.opacity = '0';
			setTimeout(function () {
				choiceimgPn.appendChild(choiceimg);
				choiceimg.style.position = '';
				choiceimg.style.webkitTransform = '';
				choiceimg = null; choiceimgCapt = null;
				document.body.style.pointerEvents = '';
				currentSegment.style.display = '';
				currentSegment = null;
			}, animationDuration*1000+50);
		}, animationDuration*1000+50);
	}, animationDuration ? 1 : 0);
}

function preloadImages() {
	var images = ['PublishMenu/back-hover@2x.png','PublishMenu/back-active@2x.png'];
	images.every(function (src) {
		var img = new Image();
		img.src = src;
		return true;
	});
}

var infoDict;
function getInfoValue(key) {
	if (!infoDict) {
		try {infoDict = JSON.parse(decodeURIComponent(location.search.substr('?info='.length)));} catch (error) {console.error('could not parse info:', error);return;}
	}
	return infoDict[key];
}

function translateMenu() {
	lbl_choosedestination.textContent = TRANSLATE({'de':'Wählen …','en':'Choose…'});
	lbl_mergeprojects.textContent = TRANSLATE({'de':'Mit dem Projekt am Zielort zusammenführen','en':'Merge With Project At Destination'});
	lbl_openinxcode.textContent = TRANSLATE({'de':'Sofort in Xcode öffnen','en':'Open with Xcode'});
	lbl_forappstore.textContent = TRANSLATE({'de':'Für App Store','en':'For App Store'});
	lbl_forxcode.textContent = TRANSLATE({'de':'Bereitstellen','en':'Create Xcode project'});
	lbl_targetpath.textContent = TRANSLATE({'de':'Zielpfad','en':'Destination'});
	lbl_options.textContent = LocalizedString('Options');
}

function getExportToXcodeURL() {
	return 'action:exportToXcode?info='+encodeURIComponent(JSON.stringify({openInXcode: openinxcodecheckbox.checked, mergeProjects: mergecheckbox.checked }));
}

var maxExports = Number.MAX_SAFE_INTEGER; // was: 3;
function proceed() {
    // this disables the export limit check and alert;
    location.href = this.getExportToXcodeURL();
	/*
    return;
	if (currentSegmentIndex === 0) {
		var plan = getInfoValue('currentPlan');
		if (plan !== 'free')
			location.href = this.getExportToXcodeURL();
		else {
			var contingent = 0;
			var day = (24*60*60);
			var minDelta = day;
			try {
				var now = ((new Date()).getTime()/1000)|0;
				getInfoValue('lastXcodeExports').every(function (timestamp) {
					var delta = (timestamp+day - now);
					if (delta < 0) {
						contingent++;
					}
					else {
						minDelta = Math.min(minDelta, delta);
					}
					return true;
				});
				contingent += (maxExports-getInfoValue('lastXcodeExports').length);
			} catch (error) {}
			
			//contingent = 2;
			
			var messageText;
			
			
			if (contingent === 0) {
				messageText = TRANSLATE({'de':'Für Xcode bereitstellen','en':'Publish for Xcode'});
				location.href = 'action:alert?info='+encodeURIComponent(JSON.stringify({
					"messageText":messageText,
					"defaultButton":"OK",
					"alternateButton": LocalizedString("Buy appdoo"),
					"otherButton":"",
					"informativeTextWithFormat": LocalizedString("Another Xcode export will be available to you shortly."),
					"defaultButtonAction": "",
					"alternateButtonAction": "location.href=\"external:https://appdoo.info/chooseplan\"",
					"otherButtonAction": "",
					"countdown": minDelta
				}));
			}
			else {

				if (contingent === 1) {
					messageText = TRANSLATE({de:"Es verbleibt 1 kostenloser Xcode-Export.",en:'1 free Xcode export remains.'});
				}
				else {
					messageText = TRANSLATE({de:"Es verbleiben "+contingent+" kostenlose Xcode-Exporte.",en:contingent+' free Xcode exports remain.'});
				}
				location.href = 'action:alert?info='+encodeURIComponent(JSON.stringify({
					"messageText":messageText,
					"defaultButton":LocalizedString('Continue'),
					"alternateButton":"",
					"otherButton":LocalizedString('Cancel'),
					"informativeTextWithFormat": TRANSLATE({de:"In der Basisversion können Sie täglich "+maxExports+" kostenlose Exporte durchführen.",en:'You can do '+maxExports+' free exports every day with the basic version.'}),//.
					"defaultButtonAction": "getInfoValue('lastXcodeExports').push((new Date().getTime()/1000)|0);location.href='"+this.getExportToXcodeURL()+"';",
					"alternateButtonAction": "",
					"otherButtonAction": "",
				}));
			}
			//messageText = getInfoValue('lastXcodeExports');
			//messageText = 'Kontingent aufgebraucht';
		
			//location.href=\"external:https://appdoo.info/chooseplan\"
		}
				
	}
	if (currentSegmentIndex === 1) {
		if (window.assistantiframe)
			window.assistantiframe.proceed();
	}
	*/
}

function setExportDestination(info) {
	exportpathnode.textContent = info.path;
}


//setTimeout(function () { animationDuration = .2; select(0); }, 100);
//window.ondblclick = function () { location.reload(); }

window.language = getInfoValue('language');
setTimeout(function () { translateMenu(); preloadImages();
	//if (!!getInfoValue('lastExportDestination')) {
		//animationDuration = .2;
		select(0);
		setTimeout(function () { document.activeElement.blur(); }, 100);
		setTimeout(function () { document.activeElement.blur(); }, 200);
		setTimeout(function () { document.activeElement.blur(); }, 300);
		setTimeout(function () { document.activeElement.blur(); }, 400);
		setTimeout(function () { document.activeElement.blur(); }, 500);
	//}
 }, animationDuration ? 1 : 0);
