/************************************************************************
 * 		Tab Saved Tours						 							*
 * 		C 2008 by René Lange & Thomas Müller & Steve Rohrlack @ mindbox	*
 ************************************************************************/
var SavedTours = AbstractTab.extend({

	initialize : function() {
		this.selector = 'savedtours';
		this.title = 'My Saved Tours';
	},
	
	// Inhalte erstellen
	buildContent : function() {
		this.tours = TourPlanner.instance.dh.getData('', this.selector);
		var container = new Element('div',{'id':'tabContent','class':'noBack'});
		
		var ffC = 1;
		this.tours.each(function(tour) {
			if(ffC == 1){
				ffClass = '';
				ffC = 0;
			}else{
				ffClass = ' blueBg';
				ffC = 1;
			}

			var tmpEl = new Element('div',{'class':'rdytour' + ffClass});

			var left = new Element('div',{'id':'left_con'});
			var picture = new Element('div',{'id':'left_pic'});

			var right = new Element('div',{'id':'right_con'});
			var title = new Element('div',{'id':'right_title'});
			var desc = new Element('div',{'id':'right_desc'});
			var text = new Element('div',{'id':'right_text'});
			var summary = new Element('div',{'id':'right_summary'});
		
			var title_1 = new Element('a',{'id':'title_1_'+tour.id_tour, 'name':tour.id_tour, 'class':'rtTitle1'});
			title_1.setHTML(tour.title);

			desc.setHTML(tour.headline?tour.headline:'Short description of only one row');
			text.setHTML(tour.description?tour.description:'A longer description for the tour. Here is described what de description of the tour is, or in other words, this ist the desription of the tour');			
			
			picture.setHTML(this.getFirstPicture(tour));
			
			var tmpElSubText = new Element('div',{'class':'text'});
			tmpElSubText.setText('Summary: ');
			var tmpElSubStrong = new Element('strong');
		
			tmpElSubStrong.setText('Drive time ' + TourPlanner.instance.calcDays(tour));// + ' | Distance ' + tour.distance + ' km | max. ' + TourPlanner.instance.calcMaxPersons(tour) + ' Persons ');
			tmpElSubText.adopt(tmpElSubStrong);
			
			title.adopt(title_1);
			summary.adopt(tmpElSubText);
		
			left.adopt(picture);
		
			right.adopt(title);		
			right.adopt(desc);
			right.adopt(text);
			right.adopt(summary);

			tmpEl.adopt(left);
			tmpEl.adopt(right);	
			tmpEl.adopt(new EditButton(tour.id_tour)) // ID der Tour mitgeben, um danach hinspringen zu können
			tmpEl.adopt(new Clearer());		
			
			container.adopt(tmpEl);		
		}.bind(this));
	
		this.fillContent(container);
		this.addEvents();
	},
	
	// Events an Buttons hängen
	addEvents : function() {		
		$('savedtoursContent').getElements('a').each(function(el){
			el.addEvent('click', function(ev){
				var elClass = el.getProperty('class');
				switch(elClass){
					case 'tpEditTour':
					case 'rtTitle1':
						if (!askForSaving()) { return; } // Aktuelle Tour wurde geändert
					
						TourPlanner.instance.changeCurrentTour(el.getProperty('name'), false);
						TourPlanner.instance.sendMessage('editor', 'changeTour');
						TourPlanner.instance.sendMessage('tourplanner', 'showEditor');
						TourPlanner.instance.sendMessage('tourplanner', 'updatePlannerMap');
						TourPlanner.instance.log('def_saved', '');
						break;
				}
				return false;
			}.bind(this));
		}.bind(this));
		this.stopLinkHopping();
	},
	
	// Erstes Bild eines Entry eintragen
	getFirstPicture : function(ct){
		var i = 0;
		var j = 0;
		while(ct.dcDays[i]){
			cd = ct.dcDays[i];
			while(cd.dcEntries[j]){
				cde = cd.dcEntries[j];
				if(cde.img && cde.category != 'Service'){
					return cde.img;
				}
				j = j+1;
			}
			i = i+1;
		}
		return '<div class="picture"><img src="/templates/img/tourplanner/no_image1.gif" /></div>';
	}
});