/*
	Download Portal Ajax Version
	
	WebService URL Examples
	
	VENDORS
	http://oo5:8080/AppGeneratorService/services/AppGeneratorService/getMobileDeviceVendors
	
	MOBILE PHONES FOR VENDOR
	http://oo5:8080/AppGeneratorService/services/AppGeneratorService/getMobilePhoneNamesForVendor
	http://oo5:8080/AppGeneratorService/services/AppGeneratorService/getWebDownloadableMobilePhoneNamesForVendor
	
	TIMETABLES-LINK
	http://oo5:8080/DownloadPortalService/services/DownloadPortalService/getConfigStep?productName=JeNah-Guide&productVersionName=4.1&configStepIndex=0

	DOWNLOAD-LINK
	http://oo5:8080/AppGeneratorService/services/AppGeneratorService/getGeneratedApplicationUrlWithConfigString?vendorName=Nokia&modelName=E65&configData=c1%3dJNVG&productName=JeNah-Guide&versionName=4.1&locale=de&configDataSeparatorChar=,
	
	IMAGES-LINK
	http://oo5:8080/ContentImportService/images.jsf?url=http://oo5:8080/AppGeneratorService/services/AppGeneratorService&namespace=http://ws.sbl.appgenerator.services.server.tafmobile.de&&method=getMobilePhoneImageByName&attribute=HTC,Hertz

*/




var selections = new Object();
selections.vendor = "";
selections.model = "";
selections.timetable = "";
		
var fillBG = true;
		

/*
	This function executes the UI changes and background processes to show 
	the Download Process Page
*/
function requestVendorList() {

	new Effect.ScrollTo('Page', {duration: 0.4, offset: -10, queue: 'end'});
	Element.hide('BottomLeftBox'); 
	Element.hide('BottomCenterBox');
	Element.update('mainContentBox', downloadProcessPage);	
	
	new Effect.Highlight('mainContentBox',	
		{startcolor: '#dddddd', endcolor: '#ffffff', duration: 1.0, queue: 'end'}
	);
	
	new Ajax.Request(
		"http://" + window.location.host + "/AppGeneratorService/services/AppGeneratorService/getMobileDeviceVendors",
		{
			onComplete: handleVendorListResponse
		}
	);	
}	

function requestModelList(vendor) {

	new Ajax.Request(
		"http://" + window.location.host + "/AppGeneratorService/services/AppGeneratorService/getWebDownloadableMobilePhoneNamesForVendor?vendor=" + escape(vendor),
		{
			onComplete: handleModelListResponse
		}
	);
}	

function requestTimetableList() {
	new Ajax.Request(
		"http://" + window.location.host + "/DownloadPortalService/services/DownloadPortalService/getConfigSteps?productName=JeNah-Guide&productVersionName=4.1&configStepIndex=0",
		{
			onComplete: handleTimetableListResponse
		}
	);
}

function requestApp() {
	
	//alert('issue request for:' + selections.vendor + " " + selections.model + "  " + selections.timetable);
	
	new Effect.ScrollTo('Page', {duration: 0.4, offset: -10, queue: 'end'});
	
	new Effect.Highlight('mainContentBox',	
		{startcolor: '#dddddd', endcolor: '#ffffff', duration: 1.0, queue: 'end'}
	);
	
	Element.update('mainContentBox', downloadProcessFinishedPage);
	Element.update('zipURL', '<img src="img/progress.gif"/>');
	Element.update('jadURL', '<img src="img/progress.gif"/>');

	new Ajax.Request(
		"http://" + window.location.host + "/AppGeneratorService/services/AppGeneratorService/getGeneratedApplicationUrlWithConfigString?vendorName=" + escape(selections.vendor) + "&modelName=" + escape(selections.model) +"&configData=c1%3d" + escape(selections.timetable) + "&productName=JeNah-Guide&versionName=4.1&locale=de&configDataSeparatorChar=,",
		{
			onComplete: handleAppResponse
		}
	);  			

}
			
function handleVendorListResponse(transport) {
	
	$("vendorContainer").innerHTML = "";
	fillBG = true;

	$A(transport.responseXML.getElementsByTagName('array')).each(
		function(child){
			var vendorName = child.firstChild.nodeValue;
			var vendorListItem = document.createElement("li");
			vendorListItem.innerHTML = vendorName;
						
			$(vendorListItem).addClassName("dlp");
			if(fillBG) $(vendorListItem).addClassName("alternateBG");
			fillBG = !fillBG;						
						
			$('vendorContainer').appendChild(vendorListItem);
			Element.hide(vendorListItem);

			Event.observe(
				vendorListItem,
				'click',
				function(event) {
					selections.vendor = vendorName;
					requestModelList(vendorName);
				}
			);
		}
	);
				
	applyVisualEffects($("vendorContainer").getElementsByTagName("li"), "vendorBox");

}


function handleModelListResponse(transport) {
	Element.show('modelBox');
	Element.hide('imgTooltipBox');
	Element.hide('timetableBox');
	$("modelContainer").innerHTML = "";
	fillBG = true;
				
	$A(transport.responseXML.getElementsByTagName('array')).each(
		function(child){
			var modelName = child.firstChild.nodeValue;
			var modelListItem = document.createElement("li");
			modelListItem.innerHTML = modelName;
			
			$(modelListItem).addClassName("dlp");
			if(fillBG) $(modelListItem).addClassName("alternateBG");
			fillBG = !fillBG;						
						
			$('modelContainer').appendChild(modelListItem);
			Element.hide(modelListItem);
			
/*
			Event.observe(
				modelListItem,
				'mouseover',
				function(event) {	
				
					// TODO call WS for Image	
					var call = 'http://' + window.location.host + '/ContentImportService/images.jsf?url=http://www.jenah-guide.de/AppGeneratorService/services/AppGeneratorService&namespace=http://ws.sbl.appgenerator.services.server.tafmobile.de&&method=getMobilePhoneImageByName&attribute=';
					
					
					call = call + selections.vendor + ',' + escape(modelName);
					
//					alert(selections.vendor + ',' + escape(modelName));

					$('imgTooltip').src = call;
					
					//$('imgTooltip').src = "img/" + escape(modelName) + ".jpg";	
																
					var element = Event.element(event);
					var topPos = Position.cumulativeOffset(element)[1];
							
					$('imgTooltipBox').setStyle({
  						top: (topPos-180) + 'px'
					});
					
					Element.show('imgTooltipBox');
					
				}
			);
*/
			
/*
			Event.observe(
				modelListItem,
				'mouseout',
				function(event) {
					Element.hide('imgTooltipBox');
				}
			);
*/
			
			Event.observe(
				modelListItem,
				'click',
				function(event) {
					selections.model = modelName;
					
					var element = Event.element(event);
					var topPos = Position.cumulativeOffset(element)[1];
							
					$('timetableBox').setStyle({
  						top: (topPos-220) + 'px'
					});
					
					requestTimetableList();
				}
			);
		}
	);
				
	applyVisualEffects($("modelContainer").getElementsByTagName("li"), "modelBox");
	
}


function handleTimetableListResponse(transport) {

	Element.hide('imgTooltipBox');
	Element.show('timetableBox');
	$("timetableContainer").innerHTML = "";
	fillBG = true;

	$A(transport.responseXML.getElementsByTagName('options')).each(
		function(child){
			var ttName = child.firstChild.nodeValue;
			var ttListItem = document.createElement("li");
			ttListItem.innerHTML = ttName;
			
			$(ttListItem).addClassName("dlp");
			if(fillBG) $(ttListItem).addClassName("alternateBG");
			fillBG = !fillBG;						
						
			$('timetableContainer').appendChild(ttListItem);
			Element.hide(ttListItem);

			Event.observe(
				ttListItem,
				'click',
				function(event) {
					selections.timetable = ttName;
					requestApp();
				}
			);

		}
	);
				
	applyVisualEffects($("timetableContainer").getElementsByTagName("li"), "timetableBox");
	
}



function handleAppResponse(transport) {
	
	var baseURL = transport.responseXML.getElementsByTagName('return')[0].firstChild.nodeValue;
	
	Element.update('zipURL', '<a href="' + baseURL + '.zip">' + baseURL + '.zip</a>');
	Element.update('jadURL', baseURL + ".jad");
	
	new Effect.Highlight('zipURL',	
		{startcolor: '#dddddd', endcolor: '#ffffff', duration: 1.0}
	);
		new Effect.Highlight('jadURL',	
		{startcolor: '#dddddd', endcolor: '#ffffff', duration: 1.0}
	);
}




function applyVisualEffects(elements, box) {
			
	$A(elements).each(
		function(element){
			new Effect.toggle(element, 'appear', { from:0, to:1, duration:0.05, 
													queue: {position: 'end', scope: box}
												 }
			);	
		}
	);

	new Effect.Highlight(box,	
		{startcolor: '#dddddd', endcolor: '#ffffff', duration: 1.0, queue: 'end'}
	);
}

