/** 
 * SVN FILE: $Id: ajax_basket.js 15184 2011-08-19 07:25:05Z jlekse $
 * @version $Revision: 15184 $
 * @lastmodified $LastChangedDate: 2011-08-19 09:25:05 +0200 (pet, 19 avg 2011) $
 */
/**
 * objekt skrbi za risanje košarice in update flash dvorane
 */
function Basket(timeoutDiv, containerDiv) {
	this.venueWindow = null;
	this.venueId = null; // id predstave, storitve...

	this.containerDiv = containerDiv;
	this.timeoutDiv = timeoutDiv;

	this.itemsCount = 0;
	this.itemsById = {};

	this.errors = [];
	
	this.dvorane = []; // array dvoran za odpiranje in zapiranje divov
};

/**
 * Odpre nam FLASH animacijo. V primeru da smo si kliknili na nastavitev abonmaja, se nam abonma popolnoma izbriše
 * če pa smo kliknili dodaj sedež, pa se abonma ohrani in se doda samo sedež
 *
 * @param id - id predstave
 * @param mode - karteri tip vstopnice -> 0 tpack ; 1 navadna
 */
Basket.prototype.openVenue = function (id, mode, url)
{
	ajaxBasket.idFastBuy = id;
	var idframe = 'flashDvorana_'+id;
	if (1 == ajaxBasket.basket.itemsCount) {
		window.location = '#buyStep1';
	} else {
		window.location = '#'+id;
	}
	
	if (url == 'about:blank') // če smo kliknili v dropdownu ----- IZBERITE -----
	{
		// tukaj moramo zapreti flash animacijo in zapreti div
		$('#divDvorana_'+id).toggle();
	} else {
		this.venueWindow = url;
		this.venueId = id;
		$('#'+idframe).attr('src', url);
		
	}
	return false;
};


Basket.prototype.closeVenue = function () {
	// ne naredi nič, včasih je bila ta funckija za zapiranje odprtih oken,
	// v primeru restarta strani.
};


Basket.prototype.refreshFlash = function() {
	// klicano iz flasha, ko se naloži na novo in nima označenih sedežev, ki so v košarici
		// naloži sedeže iz košarice v flash (samo za izbrano dvorano)
	//	alert(this.venueId);
	var item = this.itemsById['i' + this.venueId];
	var seat;
	if (item != 'undefined')
	{
		if (item.seatsById != 'undefined')
		{
			for (var i in item.seatsById) {
				seat = item.seatsById[i];
				seat.updateFlash(1);
			}
			// izprazni lock cache (js in php)
			for (var i in this.itemsById) {
				this.itemsById[i].lockCache = {};
			}
			ajaxBasket.clearLockCache();
		} 
		if (item.segTPackById != 'undefined') {
			// tpacks
			for (var i in item.segTPackById) {
				seat = item.segTPackById[i];
				pL = seat.pItems.length;
				j = 0;
				while (j < pL) {
					if (seat.pItems[j].id) {
						seat.updateTpackFlash(1, seat.pItems[j].id, seat.pItems[j].idI);
					}
					++j;
				}
			}
			ajaxBasket.clearLockCache();
		}
	}
};


Basket.prototype.addItem = function (item) {
	// klicano iz BasketItem, ko ga dodamo
	this.itemsById['i' + item.id] = item;
	++this.itemsCount;
};


Basket.prototype.removeItem = function (item) {
	// klicano iz BasketItem, ko ga sprostimo
	delete this.itemsById['i' + item.id];
	delete item;
	this.itemsCount--;
};

Basket.prototype.setTimeout = function (value) {
	this.timeoutDate = new Date();
	this.timeoutDate = this.timeoutDate.getTime() + value * 1000;
	if (value >= 0) {
		setTimeout('timeToExpireBasket.newTime('+value+');', 0);
	}
};


Basket.prototype.setPayment = function (value) {
	if (this.payment != value) {
		el = document.getElementById('payment' + value);
		if (el && !el.disabled) {
			el.checked = true;
			
			if (this.payment == 68) {
				for (var i in this.itemsById) {
					if (this.itemsById[i].deliveryType == 16) {
						this.itemsById[i].setDeliveryType(0, true);
					}
				}
			}
			this.payment = value;
			
			if (this.payment == 68) {
				for (var i in this.itemsById) {
					this.itemsById[i].setDeliveryType(16, true);
				}
			}
		}
	}
};

Basket.prototype.setCashOnDelivery = function (value) {
	this.setPayment(68);
}


Basket.prototype.loadFromXml = function (xmlDoc) {
	if (true == GET_BASKET_EXPIRE_INFORMATION) {
		this.setTimeout(xmlDoc.timeout);
	}
	this.setPayment(xmlDoc.payment);

	var errors = xmlDoc.errors;
	var i = 0;
	if (errors.length > 0) {
		var s = STR_SERVER_NOTICE+'\r\n\r\n';
		var idx = 0;
		var showMessage = false;
		var reloadPage = false;
		var eL = errors.length;
		while (i < eL) {
			idx = errors[i].id;
			if (typeof(this.errors[idx]) != 'undefined') {
				++i;
				continue;
			}
			m = errors[i].message.split(';');
			eval('this.errors[idx] = ' + m[0] + ';');
			if (this.errors[idx] == STR_BASKET_EXPIRED)
			{
				reloadPage = true;
			}
			showMessage = true;
			
			if (this.errors[idx] == STR_SEAT_ALREADY_TAKEN) {
				ajaxBasket.basket.refreshFlash();
			} else if (this.errors[idx] == STR_PACK_ONLY_FEW) {
				this.errors[idx] = $.sprintf(STR_PACK_ONLY_FEW, m[1], m[2]);
			}
			s += '- ' + this.errors[idx] + '\r\n';
			++i;
		}
		if (showMessage) {
			alert(s);
			if (reloadPage) {
				closeAllDivs();
			}
		}
	}

	var tempIds = new Object();
	var basketItem;
	var xmlItems = xmlDoc.item;

	// dodaj predstave, ki so dodane na server košarici in ne v naši
	i = 0;
	var xL = xmlItems.length;
	while (i < xL) {
		id = xmlItems[i].id;
		mode = xmlItems[i].mode;
		basketItem = this.itemsById['i' + id];
		tempIds['i' + id] = true;
		if (typeof(basketItem) == 'undefined') {
			basketItem = new BasketItem(this, id, mode);
		}

		basketItem.loadFromXml(xmlItems[i]);
		++i;
	}
	if (0==i) window.location.reload(); // no items found, reload page
	
	// odstrani predstave, ki jih ni v server košarici, so pa še vedno pri nas
	for (var k in this.itemsById) {
		if (typeof(tempIds[k]) == 'undefined') {
			this.itemsById[k].remove();
		}
	}
	


	// TODO return posible error or result??
	return true;
};
