if(window.location.host == "localhost"){
	var hostSite = "http://localhost/enlaguia/";
}else{
	var hostSite = "http://enlaguia.com.uy/";
}
function callSellAll(){
	var chk = $('rsTable').getElements('input[type=checkbox]');
	if(chk != null && chk.length > 0){
		chk.each(function(element){
			($('chkGroupAction').checked)?element.checked=true : element.checked='';
		});
		
	}
}
function checkIfAllSelected(){
	var chk = $('rsTable').getElements('input[type=checkbox]');
	if(chk != null && chk.length > 0){
		var total = chk.length;
		var index = 0;
		chk.each(function(element){
			if(element.id != 'chkGroupAction')
				(element.checked)?index++:'';
		});
		return ( index == (total-1) );
	}
}
function callDelSell(){
	alert('callDelSell');
}
function callEditSell(){
	alert('callEditSell');
}

function readData(where, productsDTOs){
	if(!$(where)){
		alert(where + ' Is not defined');
	}else{
		$(where).empty();
		if(!$('ulProduct')){
			$(where).adopt(new Element('ul',{id:'ulProduct'}));
		}else{
			$('ulProduct').empty();
		}
		productsDTOs.each(function(productDTO){
			$('ulProduct').adopt( createLi(productDTO) );
		});
	}
}

function showNoProduct(where, message){
	if(!$(where)){
		alert(where + ' Is not defined');
	}else{
		$(where).empty();
		if(!$('ulProduct')){
			$(where).adopt(new Element('ul',{id:'ulProduct'}));
		}else{
			$('ulProduct').empty();
		}
		$('ulProduct').adopt(
			new Element('li').setText(message) 
		 );
	}
}


function createLi(productDTO){
	var aaa = new Element('div',{'class':'eachProductData'})
	.adopt(
		new Element('p').setHTML('<label><b>Producto:</b></label> ' + cutWord(productDTO.productName, 50))
	).adopt(
		new Element('p').setHTML('<label><b>C&uacute;digo:</b></label> ' + productDTO.productId)
	);
	var categories = productDTO.productCatList;
	if(categories.length > 0){
		categories.each(function(cat){
			aaa.adopt(
				new Element('p').setHTML( "<label><b>Categor&iacute;a:</b></label> "+cutWord(cat.categorieName,55) )
			);
		});
	}
	categories = null;
	aaa.adopt(
		new Element('p').setHTML( "<label><b>Precio:</b></label> " + productDTO.productPrice )
	).adopt(
		new Element('p').setHTML( "<label><b>Descripci&oacute;n:</b></label> " + productDTO.productDesc )
	);
	if(productDTO.enOferta == true){
		aaa.adopt(new Element('img', {src: site_url("system/application/views/images/oferta.gif")}));
	}
	if(productDTO.regalo == true){
		aaa.adopt(new Element('p').setHTML("Con Regalo"))
	}
	var li = new Element('li',{id: 'liProd' + productDTO.productId, 'class':'eachProductLi'});
	
	li.adopt( 
		new Element('div',{'class':'eachProduct'})
		.adopt(
			new Element('div',{'class':'eachProductImage'})
			.adopt(
				new Element('ul',{'class':'adminProdLinkOptions'})
				.adopt(
				 	new Element('li')
						.adopt(
							new Element(
								new Element('a', {href:'/edit'}).setText('Editar')
								.addEvent('click', function(ev){
									(new Event(ev)).stop();
									callEditProduct(productDTO.productId);
									//productDTO.productName
								})
							)
						)
				).adopt(
				 	new Element('li')
						.adopt(
							new Element(
								new Element('a', {href:'/delete'}).setText('borrar')
								.addEvent('click', function(ev){
									(new Event(ev)).stop();
									callDeleteProduct(productDTO);
								})
							)
						)
				)
			).adopt(
				new Element('img',{src:productDTO.productImge})
			)
		).adopt( aaa)
	);
	li.adopt(
		new Element('hr').setStyle('clear', 'both')
	);
	return li;
}
function callEditProduct(productId){
	var formEdit = new Element('form', {name:'frmEditProd', id:'frmEditProd', method:'POST', action:site_url('admin/editProduct')});
	formEdit.adopt(
		new Element('input',{type:'hidden', name:'hiddProductId', value:productId})
	);
	$$('body')[0].adopt(formEdit);
	$(formEdit).submit();
}
function callDeleteProduct(productDTO){
	if(confirm('Seguro que desea borrar el producto ' + productDTO.productName + '?')){
		var params = {productId:productDTO.productId};
		var myCallback = new Callback(site_url('admin/deleteProductJSON'),params,
		function(decoded){
			paginator.render();
		},
		function(decoded){
			alert(decoded.errormsg);
		}).request();
	}
}
function readProductsData(where, productsDTOs){
	if(!$(where)){
		alert(where + ' Is not defined');
	}else{
		if(!$('ulProduct')){
			$(where).adopt(new Element('ul',{id:'ulProduct', 'class':'productosUl'}));
		}else{
			$('ulProduct').empty();
		}
		productsDTOs.each(function(productDTO){
			$('ulProduct').adopt( createProductLi(productDTO) );
		});
	}
}

function createProductLi(productDTO){

	var imageLinkSrc = 'productos/ver/' + safeUrl(productDTO.productName) + '/' + productDTO.productId; 
	//var imageLinkSrc = '#';
	var imageSrc = productDTO.productImge;
	var categoriesElements = '';
	
	if(productDTO.productCatList != null & productDTO.productCatList.length > 0){
		var conector = '';
		
		var categories = productDTO.productCatList;
		categories.each(function(element){
			categoriesElements = conector + categoriesElements + cutWord(element.categorieName, 59);
			conector = ' ,';
		});
	}
	
	var info =	new Element('div',{'class':'procuctInfo clearfix'}) //div de las categorias
	.adopt(
		new Element('span',{'class':'dataProduct'}).adopt(
			new Element('b').setText('Categoría: ' + categoriesElements)
		)
	);
	
	if(productDTO.productDesc != ''){
		info.adopt(
			new Element('label',{'class':'dataProduct'}).setHTML('<b>Descripci&oacute;n:</b> ' + productDTO.productDesc )
		);
	}
	if(productDTO.productPrice != ''){
		var precio = new Element('label',{'class':'dataProduct'}).setHTML('<b>Precio:</b> ' + productDTO.productPrice +"  ");
		if(productDTO.enOferta == true){
			precio.adopt(new Element('img', {src: site_url("system/application/views/images/oferta.gif")}));
		}
		info.adopt(precio);
	}
	var h3Title = new Element('h3').adopt(
		new Element('a',{href:imageLinkSrc, title: 'Ver '+productDTO.productName}).setText(cutWord(productDTO.productName, 59))
	);
	var li = new Element('li', {'class':'clearfix friendsSmall'})
	.adopt(
		new Element('div',{'class':'productImage'}) //div de la imagen
		.adopt(
			new Element('a',{href:imageLinkSrc, title: 'Ver '+cutWord(productDTO.productName, 53)})
			.adopt(
				new Element('img',{src:imageSrc, title: 'Ver '+productDTO.productName, alt: 'Imagen de '+cutWord(productDTO.productName, 53)})
			)
		)
	).adopt(
		new Element('div',{'class':'productNameGrey clearfix'}) //div de los detalles
		.adopt(h3Title)
	).adopt( info );
	

	
	li.adopt(
		new Element('div',{'class':'productsLiOptions', id:'divLinksOptions'+productDTO.productId})
		.adopt(
			new Element('a',{href:'/' + safeUrl(productDTO.productName), title:'Agregar '+productDTO.productName+' al carrito', 'class':'linkAddCarrito'})
			.setText('Agregar al carrito')
			.addEvent('click', function(ev){
				(new Event(ev)).stop();
				if($('inputAmoun' + productDTO.productId).value > 0)
					callAdd(productDTO.productId, $('inputAmoun' + productDTO.productId).value, 'divLinksOptions'+productDTO.productId);
				else{
					alert('Ingrese una cantidad ');
				}
			})
		).adopt(
			createSelect('inputAmoun' + productDTO.productId, 'inputBox')
		)
	);
	return li;
}
function boxCarritoChange(element, value){
	var actual = $(element).value;
	switch (value){
		case 1:
			actual++;
		break
		case -1:
			actual--;
		break
	}
	if(actual >= 0)
		$(element).value = actual;
}
function createSelect(elementId, className){
	var box = new Element('div', {'class': className});
	box.adopt(
		new Element('input',{id:elementId, value:'0'})
	).adopt(	
		new Element('img', {src:site_url('system/application/views/images/flecha_arriba.jpg'), 'class': 'butonArrowUp'})
		.addEvent('click', function(){
			boxCarritoChange(elementId, 1);
		})
	).adopt(
		new Element('img', {src:site_url('system/application/views/images/flecha_abajo.jpg'), 'class': 'butonArrowDown'})
		.addEvent('click', function(){
			boxCarritoChange(elementId, -1);
		})
	);
	return box;
	/*
	var select = new Element('select', { id: elementId, 'class': className});
	for(var i=0; i<51; i++){
		select.adopt(
			new Element('option')
			.setProperty('value', i)
			.setText(i)
		);
	}
	return select;
	*/
}

function callAdd(productId, amoun, element){
	var params = {productId:productId, cuantity:amoun};
	var myCallback = new Callback(site_url('carritocallback/addProductCallBackJSON'),params,
	function(decoded){
		$(element).empty();
		$(element).adopt(
			new Element('span').setText('Se agregaron '+ amoun)
		);

	},
	function(decoded){
		alert(decoded.errormsg);		
	}
	).request();
}

function safeUrl(url){
	var myChar = url.charAt(0);
search 	= new Array('À', 'Á','Â','Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ý', 'ý', 'þ', 'ÿ', 'Ŕ', 'ŕ', '&', ' ', '.');
found 	= new Array('a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n','o','o','o','o','o','o','u','u','u','u','y','b','s','a','a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','d','n','o','o','o','o','o','o','u','u','u','y','y','b','y','R','r', '', '-', '');
	search.each(function(element, index){
		url = url.replace(new RegExp( element, "" ), found[index]);
	});
	url = url.replace(/\ /gi, "-");
	url = myChar.concat(url);
	return url;
}
