function ShoppingCar() {
	
	this.shopItemId = null;
	this.timeInterval = null;		//variable what control timer variable
	this.elementProgressBar = null;
	this.elementTextShoppping = null;
	this.elementNumberOrders = null;
	this.percent = null;
	this.urlProgressBar = null;
	this.progressBarRefreshTime = null;
	this.urlItemUpdate = null;
	this.elementItemUpdate = null;
	
	
	/*Empty Basket*/
	this.timeInterval2 = null;
	this.difference = null;
	this.urlRevisEmptyBasket = null;
	this.reviseEmptyBasketTime = null;
	this.elementTimeUpdate = null;
	this.urlEmptyBasket = null;
	
	
	this.configureProgressBar = function(shopItemId, elementProgressBar, elementTextShoppping, elementNumberOrders, urlProgressBar, progressBarRefreshTime, elementItemUpdate, urlItemUpdate) {
		this.shopItemId = shopItemId;
		this.elementProgressBar = elementProgressBar;
		this.elementTextShoppping = elementTextShoppping;
		this.elementNumberOrders = elementNumberOrders;
		this.urlProgressBar = urlProgressBar;
		this.progressBarRefreshTime = progressBarRefreshTime;
		this.urlItemUpdate = urlItemUpdate;
		this.elementItemUpdate = elementItemUpdate;
	}
	
	this.setProgressBarStatus = function (percent, numOrders, numMin, numMax) {
//		alert(percent + "-" + numOrders + "-" + numMin + "-" + numMax);
		this.percent = percent;
		var current = this;
		jQuery(function($) {
			current.elementProgressBar.progressbar({ value: percent});
			current.elementNumberOrders.html(numOrders + " personas se han apuntado");
			if(numOrders < numMin) {
				current.elementTextShoppping.html("No se ha completado el mínimo de compradores");
			} else {
				if(numOrders < numMax) {
					current.elementTextShoppping.html("Se ha llegado al mínimo de compradores");
				} else {
					current.elementTextShoppping.html("Se ha completado el numero máximo de compradores");
				}
			}
		});
	}
	
	this.startRefresh  = function () {
		if (this.percent >= 100) {
			return; 	//not start refresh
		}
		var current = this;
		this.timeInterval = setInterval(function () {
	      	jQuery.ajax({
				type: "POST",
				url: current.urlProgressBar,
				dataType: 'json',
				data: {shopItemId: current.shopItemId},
				success: function(data) {
					var percent = data["percentageOrders"]; 
					current.setProgressBarStatus(percent, data["numberOrders"], data["numMin"], data["numMax"]);
					if (percent >= 100) {
							//cancel actual timer and refresh product div
						clearInterval(current.timeInterval);
						current.updateActiveItem(); 
						return;
					}
				}
			});
		}, this.progressBarRefreshTime);

	}
	
	this.updateActiveItem = function () {
		var current = this;
		jQuery(function($){
	      	jQuery.ajax({
				type: "POST",
				url: current.urlItemUpdate,
				data: {shopItemId: current.shopItemId},
				success: function(data){
				  	current.elementItemUpdate.html(data);
				}
			});
		});
	}
	
	this.runCountdown = function (dateUntil,dateDiference,idElement, url) {
		var current = this;
		var layoutDays='';
		if(dateDiference>=86400)
			layoutDays='{dnn}{dl} {sep} ';//if time > 1 day add day to countdown
		
		jQuery(function ($) {
			$('#'+idElement).countdown({
				labels: ['y', 'mo', 'w', 'd', 'h', 'm', 's'], 
				labels1: ['y', 'm', 'w', 'd', 'h', 'm', 's'],
				layout: layoutDays+"{hnn}{hl} {sep} {mnn}{ml} {sep} {snn}{sl}",
				until: dateUntil ,
				serverSync: current.serverTime(url)}); 
		});
	}

	this.serverTime = function (url) { 
		jQuery(function ($) {
			var time = null; 
			$.ajax({url: url, 
					type: "POST",
				    async: false, 
				    dataType: 'text', 
				    success: function(text) { time = new Date(text); }, 
				    error: function(http, message, exc) { time = new Date(); }
			});
			return time;
		});
	}
	
	this.validateComment = function (url, comment, item_id) {
		var current = this;
		jQuery(function($){
			if(comment == ""){
				$("#divErrorComments").html("Por favor, ingresa un comentario");
            	setTimeout(function() { $("#divErrorComments").empty() }, 15000);
			}
			else{
				current.saveComment(url, comment, item_id);
			}
		});

	}
	
	this.saveComment = function (url, comment, item_id) {
		jQuery(function($){
	      	jQuery.ajax({
				type: "POST",
				async: false,
				url: url,
				data: {comment: comment,
	      			   item_id: item_id},
				success: function(data){
				  	$("#divListComments").html(data);
				  	$("#newComment").val("");
				}	
			});
		});

	}
	
	this.configureReviseEmptyBasket = function(urlRevisEmptyBasket, reviseEmptyBasketTime, elementTimeUpdate, urlEmptyBasket) {
		this.urlRevisEmptyBasket = urlRevisEmptyBasket;
		this.reviseEmptyBasketTime = reviseEmptyBasketTime;
		this.elementTimeUpdate = elementTimeUpdate;
		this.urlEmptyBasket = urlEmptyBasket;
	}
	
	this.setEmptyBasketStatus = function (difference) {
		this.difference = difference;
		var current = this;
		jQuery(function($) {
			current.elementTimeUpdate.html("Quedan " + current.difference + " minutos para realizar la compra, antes de que se vacíe el carrito.");
		});
	}
	
	this.startRefreshEmptyBasket  = function () {
		var current = this;
		this.timeInterval2 = setInterval(function () {
	      	jQuery.ajax({
				type: "POST",
				url: current.urlRevisEmptyBasket,
				dataType: 'json',
				data: {},
				success: function(data) {
					current.setEmptyBasketStatus( data["difference"]);
					if (data["difference"] < 1) {
						clearInterval(current.timeInterval2);
						current.EmptyBasket();
						return;
					}
				}
			});
		}, this.reviseEmptyBasketTime);
	}
	
	this.EmptyBasket = function () {
		var current = this;
		jQuery(function($){
	      	jQuery.ajax({
				type: "POST",
				url: current.urlEmptyBasket,
				data: {},
				success: function(data){
					current.elementTimeUpdate.html("Su carrito de compras se ha vaciado.");
				}
			});
		});
	}
	
	this.showMap = function (idElement,latitude,longitude,address,zoom) {
		var latlng = new google.maps.LatLng(latitude, longitude);
	    var myOptions = {
	      zoom: zoom,
	      center: latlng,
	      mapTypeControl: false,
	      mapTypeId: google.maps.MapTypeId.ROADMAP,
	      navigationControlOptions: {   
	    	    style: google.maps.NavigationControlStyle.SMALL   
	    	  }  
	    };
	    
	    var map = new google.maps.Map(document.getElementById(idElement),myOptions);
	    var contentString = address;
	    
	    var infowindow = new google.maps.InfoWindow({
	        content: contentString
	    });
	    
	    var marker = new google.maps.Marker({
	        position: latlng,
	        map: map,
	        visible:true,
	        title: contentString
	    });
	    
	    infowindow.open(map, marker); 
	    
	    google.maps.event.addListener(marker, 'click', function() {
	      infowindow.open(map,marker);
	    });
	}
}

var shoppingCar = new ShoppingCar();
