var Cartview = Class.create( {
    initialize: function( params ) {
		// we want to store the cart here 
		this.num  = 0;
		this.totalSeatCount = 0;
		this.maxHeight = 84;
		this.hideCartTimer = null;
	},
	
	showDetails: function() {
		$('detailed_cart_view').show();
		$('cart_lid_details').hide();
		$('detailed_cart_view_popup').show();
		$('cart_item_display').show();
		
		
	},
	
	hideDetails: function() {
		$('cart_lid_details').show();
		$('detailed_cart_view_popup').hide();
		$('cart_item_display').hide();
	},
	
	showDetailsSmooth: function() {
		
		//when cart is empty
		if(seatmap.cartView.totalSeatCount < 1){
			$('detailed_cart_view').show();
			seatmap.cartView.showDetails();
			return;
		}
		
		$('cart_lid_details').hide();
		
		if(seatmap.cartView.totalSeatCount > 3)
			$('cart_items_container').setStyle({height: seatmap.cartView.maxHeight+'px', overflowY: 'hidden', overflowX: 'hidden'});		
		
		// variable to prevent the cartSlide function to fire when its already sliding
		cartMoving = true;
		Effect.BlindDown($('detailed_cart_view'), {duration: .3, afterFinish: function(){if(seatmap.cartView.totalSeatCount > 3){$('cart_items_container').setStyle({height: seatmap.cartView.maxHeight+'px', overflowY: 'auto', overflowX: 'hidden'}); cartMoving = false;		
		}}} );
		
	},
	
	hideDetailsSmooth: function() {
		//when cart is empty
		if(seatmap.cartView.totalSeatCount < 1){
			seatmap.cartView.hideDetails();
			return;
		}
		
		
		$('cart_items_container').style.overflow = 'hidden'; 
		//$('detailed_cart_view_popup').hide();
		Effect.BlindUp($('detailed_cart_view'), 
				{
					duration: .3,
					afterFinish: function(){$('cart_lid_details').show();}
				} ); 
		
		//log("hideDetailsSmooth");
	},
	
	showDetailsFinished: function() {
		if(seatmap.cartView.totalSeatCount > 3){
			$('cart_items_container').setStyle({height: seatmap.cartView.maxHeight+'px', overflowY: 'auto', overflowX: 'hidden'});		
		}
	},
	
	render: function() {
		//log("render the cart");
	},

	addSeat: function(seat, eventInfo) {
		this.totalSeatCount++;
		var key = seat.getSeatId();
		var row = $('seat_line_item').cloneNode(true);
		if(seat.isGA()) {
			row.id = "cartViewRow_"+key+"-"+seat.getId();
		} else {
			row.id = "cartViewRow_"+key;
		}
		var removeButton = "<a href=\"#\" onclick =\"seatmap.removeSeatFromCart('"+key+"', "+seat.getId()+",true); return (false);\" class=\"removeButton\"></a>";
		
		var select = $('ticket_type_select').cloneNode(true);
		var index = 0;
		
		if(seat.getPriceCategory() != 'BEST_AVAILABLE') {
			var pricetypes = seatmap.getPriceTypesForSeat(seat);
			pricetypes.each(function(pt) {
				if(!pt.isPasswordProtected || priceTypePasswords.get(pt.code).validated){
					var priceInfo = seatmap.getPriceInfo(seat.getPriceCategory(), pt.number);
					if(priceInfo){
						var desc = pt.name+' - '+priceInfo.priceFormatted;
					
						var opt1 = new Element('option', { 'value': pt.number}).update(desc);
						select.insert(opt1);
				
						if(pt.number == seat.getPriceTypeNumber()) {
							select.selectedIndex = index;
						} 
						
						index++;
					}
				}
			});
	
			select.onchange = function() {
				var selectedPriceType=this.options[this.selectedIndex];
				seatmap.cart.updatePriceType(seat, row.id, selectedPriceType.value);
			};
			select.show();
		}

		
		$('expanded_cart_size').innerHTML=this.totalSeatCount;
		$('cart_size').innerHTML = this.totalSeatCount;

		this.updateTotalPrice();
		
		//if just one pricetype no dropdown
		if(select.childElements().size() == 1){
			select = '<span class="onePriceType">' + select.options[0].innerHTML + '</span>';
		}
		
		var td = new Element('td', { 'class': 'ttype-col' });
		var rowChildren = row.childElements();
		
		// Section is BEST_AVAILABLE if it is a list of sections containing semicolon
		if(seat.getLevelSection().indexOf(';') >= 0) {
			rowChildren[0].insert(TU('BEST_AVAILABLE'));
			rowChildren[1].insert('');
		}
		else {
			rowChildren[0].insert(seatmap.getLevelName(seat.getLevel()));
			rowChildren[1].insert(seatmap.getSectionName(seat.getSection()));
		}

		if(seat.getRow() != undefined)
			rowChildren[2].insert(seat.getRow());
		else
			rowChildren[2].insert("-");
		
		if(seat.getSeat() != undefined)
			rowChildren[3].insert(seat.getSeat());
		else
			rowChildren[3].insert("-");
		
		if(seat.getPriceCategory() != 'BEST_AVAILABLE') {
			rowChildren[4].insert(select);
			rowChildren[5].insert(seatmap.getPriceInfo(seat.getPriceCategory(), seat.getPriceTypeNumber()).priceFormatted);
		} else {
			rowChildren[4].replace('-');
			rowChildren[5].insert('-');
		}
		rowChildren[6].insert(removeButton);
		
		
		
		
		$('cart_items').insert(row);
		row.show();
		
		// If cart was not visible before, it should be hidden again automatically after X seconds
		if ($('cart_lid_details').visible()) {
			this.hideCartTimer = setTimeout('seatmap.cartView.hideDetailsSmooth()', 2000);
		}
		// ....unless user starts using the cart
		$('cart_items_container').onclick = function() {clearTimeout(seatmap.cartView.hideCartTimer);};
		$('cart_items_container').onscroll = function() {clearTimeout(seatmap.cartView.hideCartTimer);};		
		
		

		// Only slide when its hidden and not sliding...
		if (!$('detailed_cart_view').visible() && !cartMoving ){
			seatmap.cartView.showDetailsSmooth();
		} else {
			this.showDetails();
		}
		
		if(this.totalSeatCount == 3)
			var maxHeightTemp = $('cart_items_container').getHeight();		
			if (maxHeightTemp>0)
				this.maxHeight = maxHeightTemp;

		if(this.totalSeatCount > 3)
			$('cart_items_container').setStyle({height: this.maxHeight+'px', overflowY: 'auto', overflowX: 'hidden'});
	
		// add a special class name to the row at the top of the cart
		$$("#cart_items tr")[2].addClassName("topRow");
	},
	
	removeSeat: function(key, gaId) {
		if(gaId) {
			$('cartViewRow_'+key+'-'+gaId).remove();
		} else {
			$('cartViewRow_'+key).remove();
		}
		this.totalSeatCount--;
		$('expanded_cart_size').innerHTML=this.totalSeatCount;
		$('cart_size').innerHTML = this.totalSeatCount;
		if(this.totalSeatCount < 3)
			$('cart_items_container').setStyle({height: 'auto', overflowY: 'hidden', overflowX: 'hidden'});
		this.updateTotalPrice();
	},
	
	empty: function() {
		while($('cart_items').childElements()[2]) {
			$('cart_items').childElements()[2].remove();
		}
		$('cart_items_container').setStyle({height: 'auto', overflowY: 'hidden', overflowX: 'hidden'});
		this.totalSeatCount = 0;
		$('expanded_cart_size').innerHTML = 0;
		$('cart_size').innerHTML = 0;
		//reset the height
		this.updateTotalPrice();
	},
	
	updateTotalPrice: function() {
		var totalPrice = seatmap.cart.getTotalPrice();
		$('cart_total').innerHTML = sprintf(seatmap.eventInfo.config.priceFormat, parseFloat(totalPrice/100), parseFloat(totalPrice%100), seatmap.eventInfo.config.currency);
	}

});
