function val(el) {
	var tagName = el.attr('tagName').toLowerCase();
	if (tagName == 'input') {
		var type = el.attr('type').toLowerCase();
		if (type == 'checkbox') return el.attr('checked');
		return el.val();
	}
	
	return el.html();
}

var MyTourPackage = {

	updateSubTotal : function(el, factor) {
		var unitPriceId = '#' + el.id + '_unit';
		var subPriceId = '#' + el.id + '_sub';
		var unitPrice = $(unitPriceId).html();
		var value = val($(el));
		if (factor != undefined) {
			value *= factor;
		}
		$(subPriceId).html(value * unitPrice);
	},

	updateTotal : function(elements, totalEl) {
		total = 0;
		$(elements).each( function() {
			total += parseInt($(this).text());
		});
		$(totalEl).html(total + ' AMD');
	},

	updateGrandTotal : function() {
		MyTourPackage.updateTotal(
				'#transfersTotal,#hotelsTotal,#toursTotal',
				'#packageTotal');
	},

	updateTransferTotal : function() {
		var arrival = val($('#transferNumPeopleArrival'));
		var departure = val($('#transferNumPeopleDeparture'));

		var transferTotal = transferPrice(arrival) + transferPrice(departure);
		$('#transfersTotal').html(transferTotal + ' AMD');

		MyTourPackage.updateGrandTotal();
	},

	dayCount : function(fromDate, toDate) {
		if (fromDate == undefined)
			fromDate = val($('#fromDate'));
		if (toDate == undefined)
			toDate = val($('#toDate'));
		var dayCount = Math.floor((new Date(toDate).getTime() - new Date(fromDate).getTime())/(1000*60*60*24));
		return dayCount;
	},

	onTourNumPeopleChange : function(el) {
		MyTourPackage.updateSubTotal(el);
		MyTourPackage.updateTotal('.tour_price', '#toursTotal');
		MyTourPackage.updateGrandTotal();
	},

	onNumRoomsChange : function(el) {
		MyTourPackage.updateSubTotal(el, MyTourPackage.dayCount());
		MyTourPackage.updateTotal('.hotel_price', '#hotelsTotal');
		MyTourPackage.updateGrandTotal();
	},

	updateAll : function() {
		$('.num_people').each(function() {MyTourPackage.updateSubTotal(this);});
		MyTourPackage.updateTotal('.tour_price', '#toursTotal');
		$('.num_rooms').each(function() {MyTourPackage.updateSubTotal(this, MyTourPackage.dayCount());});
		MyTourPackage.updateTotal('.hotel_price', '#hotelsTotal');
		MyTourPackage.updateTransferTotal();
	},

	makeTourLinks : function() {
		$('a.trip').click(function() {
			var link = $(this).html().toLowerCase();
			link = link.replace(/ *\(.*/, '').replace(/  */, '_');
			link = 'sights/' + link + '_eng.html';
			MM_openBrWindow(link, 'sun', 'scrollbars=yes,width=440,height=600');
			return false;
		});
	},
};
