function cart( email ) {
	this.totalItems 	= 0;
	this.totalPrice	= 0.00;
	this.items 		= new Array();
	this.userEmail = email;
	this.ItemColumns = ['Number','Name','Price','ButtonMinus','Quantity','ButtonPlus','Total'];
	
	this.initialize = function () {
	this.totalItems  = 0;
	this.totalPrice = 0.00;
	this.setUpEvents();
	this.updateCookie();
	this.updatePageElements();
	return;
	};
	
	this.checkOutEvent = function() {
		simpleCart.checkOut();
		return false;
	};
	
	this.emptyEvent = function() {
		simpleCart.empty();
		return false;
	};
	
	this.setUpEvents = function() {
			var x=0,element,elements = getElementsByClassName('simpleCart_total');

			x=0;
			elements = getElementsByClassName('simpleCart_checkout');
			for( x=0;x<elements.length;x++) {
				element = elements[x];
				if( element.addEventListener ) {
					element.addEventListener("click", this.checkOutEvent, false );
				} else if( element.attachEvent ) {
				  	element.attachEvent( "onclick", this.checkOutEvent );
				}
			}
			x=0;
			elements = getElementsByClassName('simpleCart_empty');
			for( x=0;x<elements.length;x++) {
				element = elements[x];
				if( element.addEventListener ) {
					element.addEventListener("click", this.emptyEvent, false );
				} else if( element.attachEvent ) {
				  	element.attachEvent( "onclick", this.emptyEvent );
				}
			}
			return;
	};
	
	this.add = function() {
		newItem = new item();
		var x=0;
		for(x=0;x<arguments.length;x++){
			temp = arguments[x];
			data = temp.split('=');
			newItem.addValue(data[0],data[1]);
		}
		if(!newItem.getValue('name') || !newItem.getValue('price')) {
			alert('Item must have name and price to be added to the cart!');
			return false;
		}
		isnew = true;
		if(!newItem.getValue('quantity')) {
			newItem.addValue('quantity',1);
		}
		this.totalItems = this.totalItems + newItem.getValue('quantity');
		x=0;
		for( x=0;x < this.items.length;x++ ) {
			tempItem = this.items[x];
			if( tempItem.equalTo(newItem) ) {
				tempItem.addValue( 'quantity' , (parseInt(tempItem.getValue('quantity')) + parseInt(newItem.getValue('quantity')) ) );
				this.totalPrice = this.totalPrice + parseFloat( tempItem.getValue('price') );
				isnew = false;
			}
		}
		if( isnew ) {
			this.items[this.items.length] = newItem;
			this.totalPrice = this.totalPrice + parseFloat(newItem.getValue('price'));
		}
		this.updateCookie();
		this.updatePageElements();
		return;
	};
	
	this.addItem = function(newItem) {
		var x=0;
		for(x=0;x<this.items.length;x++) {
			var tempItem = this.items[x];
			if( tempItem.equalTo(newItem) ){
				tempItem.addValue('quantity', parseInt(newItem.getValue('quantity')) + parseInt(tempItem.getValue('quantity')) );
				this.totalItems = this.totalItems + parseInt(newItem.getValue('quantity'));
				this.totalPrice = this.totalPrice + parseInt(newItem.getValue('quantity'))*parseFloat(newItem.getValue('price'));
				return;
			}
		}
		this.items[this.items.length] = newItem;
		this.totalItems = this.totalItems + parseInt(newItem.getValue('quantity'));
		this.totalPrice = this.totalPrice + parseInt(newItem.getValue('quantity'))*parseFloat(newItem.getValue('price'));
		return;
	};
	
	this.updateCookie = function () {
		cookieString = String(this.totalItems) + "&" + String(this.totalPrice);
		x=0;
		for(x=0;x<this.items.length;x++ ) {
			tempItem = this.items[x];
			cookieString = cookieString + "&" + tempItem.cookieString();
		}
		createCookie("simpleCart", cookieString, 30 );
	}
	
	this.empty = function () {
		this.items = new Array();
		this.totalItems = 0;
		this.totalPrice = 0.00;
		this.updateCookie();
		this.updatePageElements();
		return false;
	};
	
	this.deleteItem = function( item ) {  
		found = false;
		var temp = new Array();
		for(x=0; x < this.items.length;x++ ) {
			tempItem = this.items[x];		
			if( tempItem.equalTo(item) ) {
				found = true;
				this.totalItems = this.totalItems - parseFloat(tempItem.getValue('quantity'));
				this.totalPrice = this.totalPrice - parseFloat(tempItem.getValue('price'));
			}
			if( found ) {
				if( x < ( this.items.length - 1 ) ) {
					temp[x] = this.items[x+1];
				} 
			} else {
				temp[x] = this.items[x];
			}
		}
		this.items = temp;
		this.updateCookie();
		this.updatePageElements();
		return false;
	};

	this.options = function() {
		var x=0;
		for(x=0;x<this.items.length;x++) {
			var temp = this.items[x];
			if( temp.optionList() ) {
				return true;
			}
		}
		return false;
	};

	this.updatePageElements = function() {
		var x=0,element,elements = getElementsByClassName('simpleCart_total');
		for( x=0;x<elements.length;x++) {
			element = elements[x];
			element.innerHTML = this.returnTotalPrice();
		}
		x=0;
		elements = getElementsByClassName('simpleCart_quantity');
		for( x=0;x<elements.length;x++) {
			element = elements[x];
			element.innerHTML = String(this.totalItems);
		}
		elements = getElementsByClassName('simpleCart_items');
		for( x=0;x<elements.length;x++) {
			cartTable = elements[x];
			newRow = document.createElement('div');
			var x=0,i=0;
			//delete all current rows
			while (cartTable.childNodes[0]) {
			    cartTable.removeChild(cartTable.childNodes[0]);
			}
			
			for( x=0;x<this.ItemColumns.length;x++) {
				if( this.ItemColumns[x] != 'Options' || this.options() ) {
					tempCell = document.createElement('div');
					tempCell.innerHTML = this.ItemColumns[x];
					tempCell.className = "item" + this.ItemColumns[x];
					newRow.appendChild(tempCell);
				}
			}
			newRow.className = "cartHeaders";
			cartTable.appendChild(newRow);
			
			x=0;
			for( x=0;x<this.items.length;x++ ) {
				tempItem = this.items[x];
				newRow = document.createElement('div');
				i=0;
				for(i=0;i<this.ItemColumns.length;i++) {
					tempCell = document.createElement('div');
					tempCell.className = "item" + this.ItemColumns[i];
					if( this.ItemColumns[i] == 'Image' ) {
						if( tempItem.getValue('image') ) {
							tempCell.innerHTML = '<img src="' + tempItem.getValue('image') + '" />';
						}
					}
					if( this.ItemColumns[i] == 'Name' ) {
						tempCell.innerHTML = tempItem.getValue('name');
					} else if (	this.ItemColumns[i] == 'Price' ) {
						tempCell.innerHTML = this.returnFormattedPrice( tempItem.getValue('price'));
					} else if (	this.ItemColumns[i] == 'Number' ) {
						tempCell.innerHTML = '<div onMouseover="ddrivetip(\'' + tempItem.getValue('name') + '\');" onMouseout="hideddrivetip();">' + tempItem.getValue('number'); + '</div>';
					} else if ( this.ItemColumns[i] == 'Options' && this.options() ) {
						tempCell.innerHTML = tempItem.optionList();
					} else if (	this.ItemColumns[i] == 'ButtonMinus' ) {
						tempCell.innerHTML = '<img src="images/minus_transp.png" alt="-" name="c' + tempItem.getValue('number') +'" width="12" height="12" class="handcursor" id="c' + tempItem.getValue('number') +'" onmouseover="MM_swapImage(\'c' + tempItem.getValue('number') +'\',\'\',\'images/minus.png\',1)" onmouseout="MM_swapImgRestore()" onClick="simpleCart.updateQuantity(' + tempItem.functionString() +',\'new_quantity=' + (tempItem.getValue('quantity') - 1) + '\');return false;" />';
					} else if (	this.ItemColumns[i] == 'ButtonPlus' ) {
tempCell.innerHTML = '<img src="images/plus_transp.png" alt="+" name="b' + tempItem.getValue('number') +'" width="12" height="12" class="handcursor" id="b' + tempItem.getValue('number') +'" onmouseover="MM_swapImage(\'b' + tempItem.getValue('number') +'\',\'\',\'images/plus.png\',1)" onmouseout="MM_swapImgRestore()" onClick="simpleCart.updateQuantity(' + tempItem.functionString() +',\'new_quantity=' + (tempItem.getValue('quantity') + 1) + '\');return false;" />';						
} else if (	this.ItemColumns[i] == 'Quantity' ) {
						tempCell.id = 'i' + tempItem.getValue('number');
						tempCell.innerHTML = tempItem.getValue('quantity');
					} else if (	this.ItemColumns[i] == 'Total' ) {
						tempCell.innerHTML = this.returnFormattedPrice( tempItem.getValue('quantity')* tempItem.getValue('price') );
					}
					newRow.appendChild(tempCell);
				}
				newRow.className = "itemContainer"
				cartTable.appendChild(newRow);
			}
			newRow = document.createElement('div');
			tempCell = document.createElement('div');
			tempCell.innerHTML = "Celkem";
			tempCell.className = "totalPriceInfo";
			newRow.appendChild(tempCell);
			tempCell = document.createElement('div');
			tempCell.innerHTML = this.returnTotalPrice();
			tempCell.className = "totalPrice";
			newRow.appendChild(tempCell);
			newRow.className = "totalRow";
			cartTable.appendChild(newRow);
		}
		return false;	
	};

	this.returnTotalPrice = function() {
		return this.returnFormattedPrice( this.totalPrice );
	};
	

	this.returnFormattedPrice = function( price ) {
		return price + ",-";
	};
	
	this.updateQuantity = function() {
		newItem = new item();
		x=0;
		for(x=0;x<arguments.length;x++){
			temp = arguments[x];
			data = temp.split('=');
			if( data[0] == 'new_quantity') {
				var new_quantity = data[1];
			} else {
				newItem.addValue(data[0],data[1]);
			}
		}
		if( new_quantity < 1 ) {
			this.deleteItem( newItem );
			return;
		}
		newQuan = new_quantity - newItem.getValue('quantity');
		newItem.addValue('quantity', newQuan );
		this.addItem(newItem);
		this.updateCookie();
		this.updatePageElements();
		return false;
	}
	
	this.checkOut = function() { 
	};
}

function item () {
	this.names	= new Array();
	this.values	= new Array();
	
	this.addValue = function(name,value) {
		if( this.names.length != this.values.length ) {
			alert("name and value array lengths do not match for this item!");
			return false;
		}
		found = false;
		var a=0;
		for(a=0;a<this.names.length;a++) {
			if( this.names[a] == name ) {
				this.values[a] = value;
				return;
			}
		}
		if( !found ) {
			this.names[this.names.length]	= name;
			this.values[this.values.length]	= value;
		}
		return;
	};
	
	this.getValue = function(name) {
		var g=0;
		for(g=0;g<this.names.length;g++) {
			if(name==this.names[g]) {
				return this.values[g];
			}
		}
		return null;
	};
	
	this.equalTo = function(item) {
		if(this.getSize() != item.getSize() ) {
			return false;
		} 
		var q=0;
		for(q=0;q<this.names.length;q++) {
			if( this.names[q] != 'quantity' && (item.getValue(this.names[q]) != this.values[q]) ) {
				return false;
			}
		}
		return true;
	};

	this.getSize = function() {
		return this.names.length;
	};
	
	this.cookieString = function() {
		returnString = '';
		var i=0;
		returnString = this.names[i] + "=" + this.values[i];
		i=1;
		for(i=1;i<this.names.length;i++) {
			returnString = returnString + "," + this.names[i] + "=" + this.values[i];
		}
		return returnString;
	};
	
	this.functionString = function() {
		returnString = '\'';
		var f=0;
		returnString = '\'' + this.names[f] + "=" + this.values[f];
		f=1;
		for(f=1;f<this.names.length;f++) {
			returnString = returnString + "','" + this.names[f] + "=" + this.values[f];
		}
		returnString = returnString + '\'';
		return returnString;
	}
	
	this.optionList = function() {
		returnString = '';
		if( this.getSize() < 4 ) {
			return null;
		}
		var o=0;
		for(o=0;o<this.names.length;o++) {
			if(this.names[o] != 'quantity' && this.names[o] != 'price' && this.names[o] != 'name' && this.names[o] != 'number') {
				returnString = returnString + this.names[o] + ':' + this.values[o] + ', ';
			}
		}
		while(returnString.charAt(returnString.length-1)==',' || returnString.charAt(returnString.length-1)==' ' || returnString.charAt(returnString.length)==':'){
			returnString = returnString.substring(0,returnString.length-1);
		}
		return returnString;
	};
}
	
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function createCart(){
	simpleCart.initialize();
	return;
}

window.onload = createCart;
