var MotoListDebug = true;
var MotoList = function(){}
MotoList.popupWindow = null;//the popuop window in which the model is previewed
MotoList.init = function(JSONMessage)
{
	try
	{
		MotoList.ListMessage = JSONMessage.evalJSON();
//		$('linkNavL').onclick = function(){MotoList.navigate('L')}
//		$('linkNavR').onclick = function(){MotoList.navigate('R')}
		
		var arrNumbers = document.getElementsByName('linkNumber');
		for (var i = 0; i< arrNumbers.length; i++)
		{
			if (arrNumbers[i].className == 'linkNav')
			{
				arrNumbers[i].onclick = function(){
					MotoList.ListMessage.CurrentPage = this.innerHTML;
					CM.loadModule('Modele', MotoList.ListMessage);
				}
			}
		}
		
		var DisplayedMotos = document.getElementsByClassName('smallBox')
		for (var i = 0; i< DisplayedMotos.length; i++)
		{
			DisplayedMotos[i].onclick = function(){MotoList.motoDetails(this.getAttribute('motoId'))}
		}
		//display first moto details
		//if (DisplayedMotos.length) MotoList.motoDetails(DisplayedMotos[0].getAttribute('motoId'));
	}
	catch(Ex)
	{
		if (MotoListDebug)
		alert(Ex.message+' on MotoList.init function')
	}
}

MotoList.navigate = function(direction)
{
	try
	{
		switch(direction)
		{
			case 'L':
				if (MotoList.ListMessage.LinkLInactive) return;
				MotoList.ListMessage.CurrentPage --;			
			break;
			case 'R':
				if (MotoList.ListMessage.LinkRInactive) return;
				MotoList.ListMessage.CurrentPage ++;
			break;
			default:
			break;
		}
		CM.loadModule('Modele', MotoList.ListMessage);
	}
	catch(Ex)
	{
		if (MotoListDebug)
		alert(Ex.message+' on MotoList.navigate function')
	}
}

MotoList.motoDetails = function(motoId)
{
	try
	{
		showOverlay()
		new PAjax.Request('model.php?Id='+motoId,{
			method: 'POST',
			onSuccess: function(t)
			{
				$('SecondContent').update(t.responseText)
				Images = $('MotoJSONImages').value.evalJSON();
				for (var i=0; i<Images.length; i++)
				{
					Images[i] = 'http://marcusmoto.ro/images/uploaded/thumbs/'+Images[i];
				}
				var IG = new Gallery;
				IG.initialize(Images);
			}
		});
		return;
		MotoList.popupWindow = window.open('model.php?Id='+motoId, '_blank', 'width=631, height=388')			
		return;
		if (!motoId) return alert('an error occured in motoDetails function');
		var Message = new Object();
		Message.RequestModule = 'motoDetails';
		Message.motoId = motoId;
		new PAjax.Request('./index.php',{
			method: 'POST',
			parameters: 'Message='+Object.toJSON(Message),
			onSuccess: function(transport)
			{
				$('presentationBox').innerHTML = transport.responseText;
				//instantiate images gallery
				Images = $('MotoJSONImages').value.evalJSON();
				for (var i=0; i<Images.length; i++)
				{
					Images[i] = 'images/uploaded/thumbs/'+Images[i];
				}
				var IG = new Gallery;
				IG.initialize(Images);
				initLightbox();
			},
			onFailure: function(){
				alert('Failure')
			}
		});
	}
	catch(Ex)
	{
		if (MotoListDebug)
		alert(Ex.message+' on motoDetails function')
	}
}
