function formatNumber(value, fix) {
	fix = fix > 0 ? fix : 0;
	this.value = value.toFixed(fix);
	return (this.value.toString().replace(/\./g, ','));
}
function getFileName(src) {
	var pattern, filename, filepath;
	pattern = /[^\/]*$/;
	filename = pattern.exec(src);
	filepath = src.replace(pattern, '');
	return filename;
}
function getFilePath(src) {
	var pattern, filename, filepath;
	pattern = /[^\/]*$/;
	filename = pattern.exec(src);
	filepath = src.replace(pattern, '');
	return filepath;
}
(function ($) {
	$.fn.slideBart = function (settings) {
		var over, current, next, amount, loadID, completedID, CACHE = 1, SUCCESS = 2, ERROR = 3, TIMEOUT = 4, config = {
			'delay' : 5000,
			'slides' : '.slides',
			'tools' : '.tools'
		};
		if (settings) {
			$.extend(config, settings);
		}
		over = false;
		current = 0;
		return this.each(function () {
			var loadComplete, runSlide, $self = $(this), $slides = $(this).find(config.slides), $tools = $(this).find(config.tools);
			amount = $slides.find('img').size();
			$slides.find(':not(:has(img))').not('img').remove(); // Remove extra elements.
			$tools.hide(); // hide tools, show expected on mouse over.
			$slides.children(':not(:first-child)').hide(); // Initialize: first item as visible, others hidden.
/*		REMOVED BECAUSE LAZYLOAD IS DONE ON ASP.	
			$slides.find('img').each(function(index) {
				$(this).lazyLoad();
				if (index === 0) {$(this).loadImage();}
			});
*/
			function showImage(tgt) {
				if (!tgt.err) {
					$slides.children().eq(current).fadeOut();
					$slides.children().eq(next).fadeIn();
					current = next;
					runSlide();
				}
			}
			function isComplete(tgt) {
				var test_case_amount = 0, TEST_CASE_ATTEMPTS = 50, TEST_CASE_INTERVAL = 500;
				(function check(obj) {
					if (obj.get(0).complete) {
						loadComplete(obj, SUCCESS);
						return null;
					} else if (test_case_amount++ === TEST_CASE_ATTEMPTS){
						loadComplete(obj, TIMEOUT);
						return null;
					} else {
						setTimeout(check, TEST_CASE_INTERVAL, obj);
					}
				}(tgt));
			}
			function loadImage(tgt) {
				var src;
				if ($(tgt).attr('data-lazy-src')) {
//					use oncomplete because of cache files. Load is not reliable.
//					$(this).one('load', function() {
//						$(this).showImage();
//					});
					$(tgt).one('error', function () {
						clearTimeout(completedID);
						loadComplete(tgt, ERROR);
					});
					src = $(tgt).attr('src').replace(getFileName($(tgt).attr('src')), getFileName($(tgt).attr('data-lazy-src')));
					$(tgt).removeAttr('data-lazy-src');
					$(tgt).attr('src', src);
					isComplete(tgt);
					//completedID = setTimeout($(this).isComplete, TEST_CASE_INTERVAL);
				} else {
					loadComplete(tgt, CACHE);
				}
			}
			function nextImage(direction) {
				clearTimeout(loadID);
				if (direction !== -1) {
					direction = 1;
				}
				amount = $slides.find('img').size();
				current = current % amount;
				next = (current + direction) % amount;
				loadImage($slides.find('img').eq(next));
			}
			loadComplete = function (tgt, argument) {
				tgt.err = false;
				switch (argument) {
				case CACHE:
				case SUCCESS:
					showImage(tgt);
					break;
				case ERROR:
					$(tgt).err = true;
					$(tgt).parentsUntil(config.slides).last().remove();
					nextImage(0);
					break;
				case TIMEOUT:
					nextImage();
					break;
				default:
				}
			};
			runSlide = function () {
				if (!over) {
					loadID = setTimeout(nextImage, config.delay);
				}
			};
			function stopSlide() {
				clearTimeout(loadID);
			}
			function lazyLoad() {
				/* $(this).attr('data-lazy-src', $(this).attr('src')).attr('src', "");$(this).hide();
				// removed due to problems with latest version of browsers
				// lazy work around done on ASP by loading the first image on all elements.
				// and keeping the real file name on a data- prefixed attribute
				// used by javascript later. 
				*/
			}
			$self.mouseenter(function () {
				over = true;
				stopSlide(); 
				$tools.show();
			});
			$self.mouseleave(function () {
				over = false;
				runSlide();
				$tools.hide();
			});
			$tools.find('.next').click(function () {
				nextImage(1);
			});
			$tools.find('.prev').click(function () {
				nextImage(-1);
			});
			runSlide();
			$slides.children().css({
				// INHERITED FROM PREVIOUS VERSION. [fail-safe operation.]
				'position' : 'absolute'
			});
		});
	};
}(jQuery));

(function ($) {
	$.fn.dropDown = function (settings) {
		return this.each(function () {
			var $self = $(this);
			$self.find('> li').mouseenter(function () {
				$(this).find('ul').stop(true, true);
				$self.find('li ul:visible').hide();
				$(this).find('ul').show();
			});
			$self.mouseleave(function () {
				$(this).find('ul').delay(400).fadeOut('fast');
			});
		});
	};
}(jQuery));

(function ($) {
	$.fn.placeholder = function (settings) {
		var i = document.createElement('input');
		if (!('placeholder' in i)) {
			return this.each(function () {
				var ph;
				ph = $(this).attr('placeholder');
				if (ph !== '') {
					$(this).val(ph);
					$(this).focus(function () {
						if ($(this).val() === ph) {
							$(this).val("");
						}
					});
					$(this).blur(function () {
						if ($(this).val() === '') {
							$(this).val(ph);
						}
					});
				}  
			});
		}
		$(i).remove();
	};
}(jQuery));

(function ($) {
	$.fn.voucherObject = function (settings) {
		var current_voucher = $('#voucher_list :selected').val();
		function voucherUpdate(target) {
			if ($(target).find(':selected').val() === 'other') {
				$(target).hide();
				$('#voucher-code').val('').fadeIn();
				$('#voucher-code').fadeIn();
				$('#voucher-cancel').show();
				$('#voucher-add').show();
			} else {
				$('#voucher').val('change');
				$('#voucher-form').submit();
			}
		}
		function voucherShowCombo(current_voucher) {
			$('#voucher-code, #voucher-cancel, #voucher-add').hide();	
			$('#voucher_list').fadeIn().val(current_voucher);
		}
		function voucherInput() {
			$(this).find('input').hide();
			$('#voucher_list').fadeIn();
			$('#voucher-cancel').hide();
			$('#voucher-add').hide();
		}
		$('#voucher_list').change(function () {
			voucherUpdate(this);
		});
		$('#voucher-cancel').click(function () {
			$(this).find('p').fadeOut();
			voucherShowCombo(current_voucher);
		});
		$('.voucher-code input, .voucher-code select').click(function () {
			$('.voucher-code p').fadeOut();
		});

		$('#voucher-add').click(function () {
			if ($('#voucher-code').val() !== "") {
				$('#voucher-form').submit();
			}
		});
		$('#voucher_list').change(function () {
			voucherUpdate(this);
		});
		
	};
}(jQuery));

$(document).ready(function () {
	$.fn.nyroModal.settings.closeButton = '<a class="nyroModalClose x">X</a>';
	$.fn.nyroModal.settings.contentError = 'No disponible, por favor intente nuevamente. <a href="" class="nyroModalClose">cerrar</a>';
	
	$('.login-link').each(function(e) {
		$(this).attr('href', $(this).attr('href') + '&t=m').addClass('nyroModal')
	})
	$('.login-link').click(function(e) {
		e.preventDefault();
		if ($('#login-email').size() > 0) {
			$('#login').show();
			$('#register').hide();
			$('#login-email').focus()
		} else {
			$(this).nyroModalManual()
		}
	});
	$('.dropdown').dropDown();
	$('.voucher-code').voucherObject();
	$('input[placeholder]').placeholder();

	$('.product :not(button)').click(function (e) {
		e.stopPropagation();
		window.location.href = $(this).closest('.product').find('a').attr('href');
	});
	$('#form-newsletter').submit(function (e) {
		var submitButton;
		submitButton = $(this).find('button[type="submit"]');
		submitButton.attr('disabled', 'true');
		submitButton.text('aguarde');
		e.preventDefault();
		jQuery.ajax({
			data: $(this).serialize(),
			url: $(this).attr("action"),
			timeout: 2000,
			error: function () {
				submitButton.attr('disabled', 'false');
				submitButton.text('enviar');
				$(this).find('.response').text('intente nuevamente');
			},
			success: function (r) {
				if (r === "Muchas gracias.") {
					$('#form-newsletter').find('input').fadeOut('slow');
					submitButton.fadeOut('slow', function () {
						$('.response').text(r);
					});
				} else {
					submitButton.attr('disabled', '');
					submitButton.text('enviar');
					$('.response').text(r);
				}
			}
		});
		return false;
	});
	$('.fblike').append('<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.sendstar.com.uy%2F&amp;layout=button_count&amp;show_faces=true&amp;width=115&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:115px; height:21px;" allowTransparency="true"></iframe>');
});


