if (window.Puntoticket == undefined) {
	window.Puntoticket = {};
}

Puntoticket.Samsung = {
	Banners: function(o) {
		this.options = {
			targetUrl: 'http://bit.ly/pd3Pc3',
			imgBaseUrl: 'banner_samsung/images',
			timePerItem: 7000,
			resolutions: [
				{
					name: '1024',
					dir: '1024',
					minWidth: 112,
					minHeight: 305,
					margin: 10
				},
				{
					name: '1280_inter1',
					dir: '1280_inter1',
					minWidth: 187,
					minHeight: 485,
					margin: 18
				},
				{
					name: '1280',
					dir: '1280',
					minWidth: 200,
					minHeight: 519,
					margin: 20
				}
			],
			items: [
				{left: 'galaxy_mini-S5570', right: 'galaxy_pro-B7510'},
				{left: 'galaxy_ace-S5830', right: 'galaxy_fit-S5670'},
				{left: 'galaxy_s-I9003', right: 'galaxy_sii-I9100'},
				{left: 'galaxy_tab-P1000', right: 'galaxy_tab-P7500'}
			],
			minItemsToRun: 1,
			debug: false
		};
		jQuery.extend(true, this.options, o);
		this._resolutionIndex = -1;
		this._itemsReady = 0;
		this._allAssetsReady = false;
		this._firstRun = true;
		this._running = false;
		this._spriteZindex = 900;
		this._canDisplay = false;
		this._resizeTimer = null;
		this._bannerLeft = {
			container: null,
			imgFondo: null,
			link: null,
			imgLink: null,
			actual: null
		};
		this._bannerRight = {
			container: null,
			imgFondo: null,
			link: null,
			imgLink: null,
			actual: null
		};

		this._repository = null;
		this._spritesets = [];
		this._assets = {
			logo: { img: null, ready: false },
			hayun: { img: null, ready: false },
			samsung: { img: null, ready: false },
			galaxy: { img: null, ready: false },
			paracada: { img: null, ready: false }
		};
		this._debug = {};
	},

	Sprite: function(item, parent) {
		var self = this;
		this._parent = parent;
		this.name = item;
		this.imgloaded = false;
		this.rfxloaded = false;
		this.labelloaded = false;
		this.ready = false;
		this.actualSide = '';

		if ($.cookie(this.name+'_lastSeen') && !isNaN($.cookie(this.name+'_lastSeen'))) {
			this.lastSeen = $.cookie(this.name+'_lastSeen');
		}
		else {
			this.lastSeen = 0;
		}

		this.rfx = document.createElement('img');
		this.rfx.setAttribute('id', 'rfx_'+item);
		//this.rfx.style.zIndex = this._parent._spriteZindex++;
		$(this.rfx).css({'z-index': this._parent._parent._spriteZindex++, position: 'absolute'});
		this._parent._parent._repository.appendChild(this.rfx);
		$(this.rfx).load(function(e) {self.OnRfxLoad();});
		$(this.rfx).hide();
		$(this.rfx).addClass('imgRfx');
		this.rfx.setAttribute('src', '');
		
		this._onSpriteReady = this._parent.SpriteReady;
		this.img = document.createElement('img');
		this.img.setAttribute('id', 'cel_'+item);
		//this.img.style.zIndex = this._parent._spriteZindex++;
		$(this.img).css({'z-index': this._parent._parent._spriteZindex++, position: 'absolute'});
		this._parent._parent._repository.appendChild(this.img);
		this.img.setAttribute('src', '');
		$(this.img).hide();
		$(this.img).addClass('imgCel');
		$(this.img).load(function(e) {self.OnImgLoad();});
		
		this.label = document.createElement('img');
		this.label.setAttribute('id', 'label_'+item);
		//this.label.style.zIndex = this._parent._spriteZindex++;
		$(this.label).css({'z-index': this._parent._parent._spriteZindex++, position: 'absolute'});
		this._parent._parent._repository.appendChild(this.label);
		this.label.setAttribute('src', '');
		$(this.label).hide();
		$(this.label).addClass('imgLabel');
		$(this.label).load(function(e) {self.OnLabelLoad();});
	},
	
	SpriteSet: function(item, parent) {
		var self = this;
		this._parent = parent;
		this.name = item.left;
		this.left = new Puntoticket.Samsung.Sprite(item.left, this);
		this.right = new Puntoticket.Samsung.Sprite(item.right, this);
		this.ready = false;
		
		if ($.cookie(this.name+'_lastSeen') && !isNaN($.cookie(this.name+'_lastSeen'))) {
			this.lastSeen = $.cookie(this.name+'_lastSeen');
		}
		else {
			this.lastSeen = 0;
		}
	},
	
	Asset: function(item, parent) {
		var self = this;
		this._parent = parent;
		this.name = item;
		this.ready = false;

		this.imgLeft = document.createElement('img');
		this.imgLeft.setAttribute('id', 'asset_'+item+'_left');
		//this.imgLeft.style.zIndex = this._parent._spriteZindex;
		$(this.imgLeft).css({'z-index': this._parent._spriteZindex++, position: 'absolute'});
		this._parent._bannerLeft.container.appendChild(this.imgLeft);
		this.imgLeft.setAttribute('src', '');
		$(this.imgLeft).offset({top: -2000, left: 0});
		$(this.imgLeft).hide();
		$(this.imgLeft).animate({opacity: 0}, 0);
		$(this.imgLeft).addClass('asset');

		this.imgRight = document.createElement('img');
		this.imgRight.setAttribute('id', 'asset_'+item+'_right');
		//this.imgRight.style.zIndex = this._parent._spriteZindex++;
		$(this.imgRight).css({'z-index': this._parent._spriteZindex++, position: 'absolute'});
		this._parent._bannerRight.container.appendChild(this.imgRight);
		this.imgRight.setAttribute('src', '');
		$(this.imgRight).offset({top: -2000, left: 0});
		$(this.imgRight).hide();
		$(this.imgRight).animate({opacity: 0}, 0);
		$(this.imgRight).addClass('asset');

		$(this.imgLeft).load(function(e) {self.OnLoad();});
	}

}

Puntoticket.Samsung.Banners.prototype = {
	Init: function() {
		var bv = $.browser.version.match(/^[0-9]+(\.[0-9])?/).length > 0 ? $.browser.version.match(/^[0-9]+(\.[0-9])?/)[0]:0;
		if (!($.browser.safari || $.browser.webkit || $.browser.mozilla || $.browser.opera) && !($.browser.msie && bv >= 9)) {
			return;
		}
		var self = this;
		var tmpEl;
		
		this.dimensions = this._clientCoords();
		//alert(this._clientCoords().width);
		//alert($(document).innerWidth());
		//alert($('#container').offset().left);
		
		// Crear debug
		if (this.options.debug) {
			this._debug = new Puntoticket.DebugWindow({
				container: $('#container')[0],
				height: '200px'
			});
			this._debug.WriteLn('Inicializando');
		}

		// 
		$(window).resize(function(e) { self.OnResize(); });
		
		// Crear repositorio
		this._repository = document.createElement('div');
		this._repository.setAttribute('id', 'bannerSamsungRepo');
		this._repository.setAttribute('style', 'display: none;');
		document.body.appendChild(this._repository);
		
		// Crear contenedor
		// Izquierdo
		this._bannerLeft.container = document.createElement('div');
		this._bannerLeft.container.setAttribute('id', 'bannerSamsungLeft');
		document.body.appendChild(this._bannerLeft.container);
		this._bannerLeft.container.setAttribute('style', 'position: fixed;top:0px;left:0px;z-index:400;');
		$(this._bannerLeft.container).addClass('bannerLateralSamsung');
		
		// Derecho
		this._bannerRight.container = document.createElement('div');
		this._bannerRight.container.setAttribute('id', 'bannerSamsungRight');
		document.body.appendChild(this._bannerRight.container);
		this._bannerRight.container.setAttribute('style', 'position: fixed;top:0px;left:0px;z-index:400;');
		$(this._bannerRight.container).addClass('bannerLateralSamsung');

		// Crear imagen de fondo
		// Izquierdo
		this._bannerLeft.imgFondo = document.createElement('img');
		this._bannerLeft.imgFondo.setAttribute('id', 'bannerLeftFondo');
		this._bannerLeft.container.appendChild(this._bannerLeft.imgFondo);
		$(this._bannerLeft.imgFondo).addClass('imgFondo');
		this._bannerLeft.imgFondo.setAttribute('src', this.options.imgBaseUrl+'/fondobannerizq.png');

		// Derecho
		this._bannerRight.imgFondo = document.createElement('img');
		this._bannerRight.imgFondo.setAttribute('id', 'bannerRightFondo');
		this._bannerRight.container.appendChild(this._bannerRight.imgFondo);
		$(this._bannerRight.imgFondo).addClass('imgFondo');
		this._bannerRight.imgFondo.setAttribute('src', this.options.imgBaseUrl+'/fondobannerder.png');

		// Crear contenedor link
		// Izquierdo
		this._bannerLeft.linkContainer = document.createElement('div');
		this._bannerLeft.linkContainer.setAttribute('id', 'bannerSamsungLeftLink');
		document.body.appendChild(this._bannerLeft.linkContainer);
		this._bannerLeft.linkContainer.setAttribute('style', 'position: fixed;top:0px;left:0px;height:100%;z-index:998;');
		
		// Derecho
		this._bannerRight.linkContainer = document.createElement('div');
		this._bannerRight.linkContainer.setAttribute('id', 'bannerSamsungRightLink');
		document.body.appendChild(this._bannerRight.linkContainer);
		this._bannerRight.linkContainer.setAttribute('style', 'position: fixed;top:0px;left:0px;height:100%;z-index:998;');

		// Crear imagen de link
		// Izquierdo
		tmpEl = document.createElement('a');
		tmpEl.setAttribute('href', this.options.targetUrl);
		tmpEl.setAttribute('target', '_new');
		this._bannerLeft.linkContainer.appendChild(tmpEl);
		this._bannerLeft.imgLink = document.createElement('img');
		this._bannerLeft.imgLink.setAttribute('id', 'bannerLeftLinkImg');
		tmpEl.appendChild(this._bannerLeft.imgLink);
		this._bannerLeft.imgLink.setAttribute('style', 'position: absolute;top:0px;left:0px;height:100%;width:100%;z-index:999;');
		this._bannerLeft.imgLink.setAttribute('src', this.options.imgBaseUrl+'/transparent.gif');

		// Derecho
		tmpEl = document.createElement('a');
		tmpEl.setAttribute('href', this.options.targetUrl);
		tmpEl.setAttribute('target', '_new');
		this._bannerRight.linkContainer.appendChild(tmpEl);
		this._bannerRight.imgLink = document.createElement('img');
		this._bannerRight.imgLink.setAttribute('id', 'bannerRightLinkImg');
		tmpEl.appendChild(this._bannerRight.imgLink);
		this._bannerRight.imgLink.setAttribute('style', 'position: absolute;top:0px;left:0px;height:100%;width:100%;z-index:999;');
		this._bannerRight.imgLink.setAttribute('src', this.options.imgBaseUrl+'/transparent.gif');
		
		
		// Crear imagenes celulares
		var i;
		for(i=0;i<this.options.items.length;i++) {
			var spriteset = new Puntoticket.Samsung.SpriteSet(this.options.items[i], this);
			this._spritesets.push(spriteset);
		}

		// Ordenar segun TS de ultima visualizacion.
		this._spritesets.sort(function(a,b) {return (a.lastSeen > b.lastSeen) ? 1 : ((b.lastSeen > a.lastSeen) ? -1 : 0);} );

		for(i in this._assets) {
			this._assets[i] = new Puntoticket.Samsung.Asset(i, this);
		}
		
		this.OnResize();
	},
	ActualResolution: function() {
		return this.options.resolutions[this._resolutionIndex];
	},
	LoadAssets: function() {
		if (this.options.debug) {this._debug.WriteLn('Load Assets');}
		this._running = false;
		this._itemsReady = 0;
		this._allAssetsReady = false;
		var i;
		for (i in this._assets) {
			$(this._assets[i].imgLeft).hide();
			$(this._assets[i].imgRight).hide();
			this._assets[i].ready = false;
			if (this.options.debug) {this._debug.WriteLn('Cargando '+this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._assets[i].name+'.png');}
			this._assets[i].imgLeft.src = this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._assets[i].name+'.png';
		}
		for(i=0;i<this._spritesets.length;i++) {
			this._spritesets[i].ready = false;
			
			$(this._spritesets[i].left.img).hide();
			$(this._spritesets[i].left.rfx).hide();
			$(this._spritesets[i].left.label).hide();
			this._spritesets[i].left.imgloaded = false;
			this._spritesets[i].left.rfxloaded = false;
			this._spritesets[i].left.labeloaded = false;
			this._spritesets[i].left.ready = false;

			if (this.options.debug) {this._debug.WriteLn('Cargando '+this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._spritesets[i].left.name+'*.png');}
			
			this._spritesets[i].left.img.src = this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._spritesets[i].left.name+'.png';
			this._spritesets[i].left.rfx.src = this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._spritesets[i].left.name+'_rfx.png';
			this._spritesets[i].left.label.src = this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._spritesets[i].left.name+'_label.png';

			$(this._spritesets[i].right.img).hide();
			$(this._spritesets[i].right.rfx).hide();
			$(this._spritesets[i].right.label).hide();
			this._spritesets[i].right.imgloaded = false;
			this._spritesets[i].right.rfxloaded = false;
			this._spritesets[i].right.labeloaded = false;
			this._spritesets[i].right.ready = false;

			if (this.options.debug) {this._debug.WriteLn('Cargando '+this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._spritesets[i].right.name+'*.png');}
			
			this._spritesets[i].right.img.src = this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._spritesets[i].right.name+'.png';
			this._spritesets[i].right.rfx.src = this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._spritesets[i].right.name+'_rfx.png';
			this._spritesets[i].right.label.src = this.options.imgBaseUrl+'/'+this.ActualResolution().dir+'/'+this._spritesets[i].right.name+'_label.png';
		}
	},
	OnResize: function() {
		if (this.options.debug) {this._debug.WriteLn('OnResize');}
		var self = this;
		clearTimeout(this._resizeTimer);
		this._resizeTimer = setTimeout(function() {self.ProcessResize();}, 100);
	},
	ProcessResize: function() {
		if (this.options.debug) {this._debug.WriteLn('ProcessResize');}
		this.dimensions = this._clientCoords();
		this._resolutionIndex = this.options.resolutions.length-1;
		var lastResolutionIndex = this._resolutionIndex;

		while((this._resolutionIndex >= 0) && ((Math.floor((this.dimensions.width-$('#container').width())/2) < this.ActualResolution().minWidth) || (this.dimensions.height < this.ActualResolution().minHeight))) {
			this._resolutionIndex--;
		}
		$(this._bannerLeft.container).css({top: 0, left: 0}).width($('#container').offset().left);
		$(this._bannerRight.container).css({top: 0, left: $('#container').offset().left+$('#container').width()}).width(this.dimensions.width - ($('#container').position().left+$('#container').width()));
		$(this._bannerLeft.linkContainer).offset($(this._bannerLeft.container).offset()).width($(this._bannerLeft.container).width());
		$(this._bannerRight.linkContainer).offset($(this._bannerRight.container).offset()).width($(this._bannerRight.container).width());
		if (this._resolutionIndex >= 0) {
			this._canDisplay = true;
			if (lastResolutionIndex != this.resolutionIndex) {
				this.LoadAssets();
			}
		}
		else {
			this._canDisplay = false;
		}
		this.RelocateItems();

	},
	RelocateItems: function() {
		var i;
		if (this._canDisplay) {
			if (this.options.debug) {this._debug.WriteLn('RelocateItems');}
			window.status='arreglando';
			var guide = Math.round($('#container').offset().left-this.ActualResolution().margin);
			var self = this;
			$(this._assets.logo.imgLeft).css({
				top: ($(self._assets.logo.imgLeft).height()*0.2),//$(document).scrollTop()+($(self._assets.logo.imgLeft).height()*0.2),
				left: guide-Math.round($(self._assets.logo.imgLeft).width()*0.87)
			});
			$(this._assets.hayun.imgLeft).css({
				top: $(self._assets.logo.imgLeft).position().top+$(self._assets.logo.imgLeft).height()+Math.round($(self._assets.logo.imgLeft).height()*0.2),
				left: guide-$(self._assets.hayun.imgLeft).width()
			});
			$(this._assets.samsung.imgLeft).css({
				top: $(this._assets.hayun.imgLeft).position().top+$(this._assets.hayun.imgLeft).height(),
				left: guide-$(this._assets.samsung.imgLeft).width()
			});
			$(this._assets.galaxy.imgLeft).css({
				top: $(this._assets.samsung.imgLeft).position().top+$(this._assets.samsung.imgLeft).height(),
				left: guide-$(this._assets.galaxy.imgLeft).width()
			});
			$(this._assets.paracada.imgLeft).css({
				top: $(this._assets.galaxy.imgLeft).position().top+$(this._assets.galaxy.imgLeft).height(),
				left: guide-Math.round($(this._assets.paracada.imgLeft).width()*0.966)
			});

			guide = this.ActualResolution().margin;//Math.round($(this._bannerRight.container).offset().left+this.ActualResolution().margin);

			$(this._assets.logo.imgRight).css({
				top:$(this._assets.logo.imgLeft).position().top,
				left: guide+Math.round($(this._assets.paracada.imgLeft).width()*0.966)-Math.round($(self._assets.logo.imgLeft).width()*0.87)
			});
			$(this._assets.hayun.imgRight).css({
				top: $(this._assets.hayun.imgLeft).position().top,
				left: guide
			});
			$(this._assets.samsung.imgRight).css({
				top: $(this._assets.samsung.imgLeft).position().top,
				left: guide
			});
			$(this._assets.galaxy.imgRight).css({
				top: $(this._assets.galaxy.imgLeft).position().top,
				left: guide
			});
			$(this._assets.paracada.imgRight).css({
				top: $(this._assets.paracada.imgLeft).position().top,
				left: guide
			});

			var side, sprite, container;

			if (this._bannerLeft.actual != undefined && this._bannerLeft.actual != null) {
				side = 'left';
				sprite = this._bannerLeft.actual;
				container = this._bannerLeft.container;
				$(sprite.img).css({
					top:$(this._assets.paracada.imgLeft).position().top+$(this._assets.paracada.imgLeft).height()+Math.round($(self._assets.logo.imgLeft).height()*0.4),
					left: $(container).width()-this.ActualResolution().margin-$(sprite.img).width()
				});
				$(sprite.rfx).css({
					top:$(sprite.img).position().top+$(sprite.img).height(),
					left:$(sprite.img).position().left+Math.floor(($(sprite.img).width()-$(sprite.rfx).width())/2)
				});
				$(sprite.label).css({
					top:$(sprite.rfx).position().top+Math.round($(sprite.rfx).height()*0.2),
					left:$(sprite.img).position().left+Math.floor(($(sprite.img).width()-$(sprite.label).width())/2)
				});
			}

			if (this._bannerRight.actual != undefined && this._bannerRight.actual != null) {
				side = 'right';
				sprite = this._bannerRight.actual;
				container = this._bannerRight.container;
				$(sprite.img).css({
					top:$(this._assets.paracada.imgRight).position().top+$(this._assets.paracada.imgRight).height()+Math.round($(self._assets.logo.imgRight).height()*0.4),
					left: this.ActualResolution().margin
				});
				$(sprite.rfx).css({
					top:$(sprite.img).position().top+$(sprite.img).height(),
					left:$(sprite.img).position().left+Math.floor(($(sprite.img).width()-$(sprite.rfx).width())/2)
				});
				$(sprite.label).css({
					top:$(sprite.rfx).position().top+Math.round($(sprite.rfx).height()*0.2),
					left:$(sprite.img).position().left+Math.floor(($(sprite.img).width()-$(sprite.label).width())/2)
				});
			}

			for (i in this._assets) {
				$(this._assets[i].imgLeft).show();
				$(this._assets[i].imgRight).show();
				if (this.options.debug) {this._debug.WriteLn(i+' left panel - top, left: '+$(this._assets[i].imgLeft).position().top+','+$(this._assets[i].imgRight).position().left);}
				if (this.options.debug) {this._debug.WriteLn(i+' right panel - top, left: '+$(this._assets[i].imgRight).position().top+','+$(this._assets[i].imgRight).position().left);}
			}
			for(i=0;i<this._spritesets.length;i++) {
				$(this._spritesets[i].left.img).show();
				$(this._spritesets[i].left.rfx).show();
				$(this._spritesets[i].left.label).show();
				
				$(this._spritesets[i].right.img).show();
				$(this._spritesets[i].right.rfx).show();
				$(this._spritesets[i].right.label).show();
			}
		}
		else {
			for (i in this._assets) {
				$(this._assets[i].imgLeft).hide();
				$(this._assets[i].imgRight).hide();
			}
			for(i=0;i<this._spritesets.length;i++) {
				$(this._spritesets[i].left.img).hide();
				$(this._spritesets[i].left.rfx).hide();
				$(this._spritesets[i].left.label).hide();

				$(this._spritesets[i].right.img).hide();
				$(this._spritesets[i].right.rfx).hide();
				$(this._spritesets[i].right.label).hide();
			}
		}
	},
	SpriteSetReady: function(spriteset) {
		if ((++this._itemsReady >= this.options.minItemsToRun) && (this._allAssetsReady) && !this._running) {
			this.Run();
		}
	},
	AssetReady: function(asset) {
		this._allAssetsReady = true;
		for (var i in this._assets) {
			this._allAssetsReady = this._allAssetsReady && this._assets[i].ready;
		}
		if ((this._itemsReady >= this.options.minItemsToRun) && (this._allAssetsReady) && !this._running) {
			this.Run();
		}
	},
	Run: function() {
		this._running = true;
		if (this.options.debug) {this._debug.WriteLn('Run');}
		var self = this;
		/*if (this._firstRun) {
			$(this._assets.logo.imgLeft).animate({opacity: 0}, 0);
			$(this._assets.hayun.imgLeft).animate({opacity: 0}, 0);
			$(this._assets.samsung.imgLeft).animate({opacity: 0}, 0);
			$(this._assets.galaxy.imgLeft).animate({opacity: 0}, 0);
			$(this._assets.paracada.imgLeft).animate({opacity: 0}, 0);

			$(this._assets.logo.imgRight).animate({opacity: 0}, 0);
			$(this._assets.hayun.imgRight).animate({opacity: 0}, 0);
			$(this._assets.samsung.imgRight).animate({opacity: 0}, 0);
			$(this._assets.galaxy.imgRight).animate({opacity: 0}, 0);
			$(this._assets.paracada.imgRight).animate({opacity: 0}, 0);
		}*/
		this.RelocateItems();
		if (this._firstRun) {

			$(this._assets.logo.imgLeft).animate({opacity: 0}, 0);
			$(this._assets.hayun.imgLeft).animate({opacity: 0}, 0);
			$(this._assets.samsung.imgLeft).animate({opacity: 0}, 0);
			$(this._assets.galaxy.imgLeft).animate({opacity: 0}, 0);
			$(this._assets.paracada.imgLeft).animate({opacity: 0}, 0);

			$(this._assets.logo.imgRight).animate({opacity: 0}, 0);
			$(this._assets.hayun.imgRight).animate({opacity: 0}, 0);
			$(this._assets.samsung.imgRight).animate({opacity: 0}, 0);
			$(this._assets.galaxy.imgRight).animate({opacity: 0}, 0);
			$(this._assets.paracada.imgRight).animate({opacity: 0}, 0);
			
			setTimeout(function() {
				$(self._assets.hayun.imgLeft).animate({opacity: 1}, 1200, 'swing');
			}, 0);
			setTimeout(function() {
				$(self._assets.samsung.imgLeft).animate({opacity: 1}, 1200, 'swing');
			}, 100);
			setTimeout(function() {
				$(self._assets.galaxy.imgLeft).animate({opacity: 1}, 1200, 'swing');
			}, 200);
			setTimeout(function() {
				$(self._assets.paracada.imgLeft).animate({opacity: 1}, 1200, 'swing');
			}, 300);
			setTimeout(function() {
				$(self._assets.logo.imgLeft).animate({opacity: 1}, 1000, 'linear', function() {
					self.AnimateNext();
				});
			}, 600);

			setTimeout(function() {
				$(self._assets.hayun.imgRight).animate({opacity: 1}, 1200, 'swing');
			}, 0);
			setTimeout(function() {
				$(self._assets.samsung.imgRight).animate({opacity: 1}, 1200, 'swing');
			}, 100);
			setTimeout(function() {
				$(self._assets.galaxy.imgRight).animate({opacity: 1}, 1200, 'swing');
			}, 200);
			setTimeout(function() {
				$(self._assets.paracada.imgRight).animate({opacity: 1}, 1200, 'swing');
			}, 300);
			setTimeout(function() {
				$(self._assets.logo.imgRight).animate({opacity: 1}, 1200, 'linear');
			}, 1000);
		}
		this._firstRun = false;
	},
	AnimateNext: function() {
		var self = this;
		var spriteset;

		var cnt = 0;
		do {
			spriteset = this._spritesets.shift();
			this._spritesets.push(spriteset);
		} while(spriteset.ready == false && (++cnt < this._spritesets.length));
		this._bannerLeft.actual = spriteset.left;
		this._bannerRight.actual = spriteset.right;

		this._bannerLeft.actual.AnimateIn(this._bannerLeft.container, 'left', function() {});
		this._bannerRight.actual.AnimateIn(this._bannerRight.container, 'right', function() {});
	},
	_clientCoords: function() {
		 var dimensions = {width: 0, height: 0};
		 if (document.documentElement) {
			 dimensions.width = document.documentElement.offsetWidth;
			 dimensions.height = document.documentElement.offsetHeight;
		 } else if (window.innerWidth && window.innerHeight) {
			 dimensions.width = window.innerWidth;
			 dimensions.height = window.innerHeight;
		 }
		 return dimensions;
	}
}


Puntoticket.Samsung.Sprite.prototype = {
	OnImgLoad: function() {
		this.imgloaded = true;
		if (this.imgloaded && this.rfxloaded && this.labelloaded) {
			this.ready = true;
			this._parent.SpriteReady(this);
		}
	},
	OnRfxLoad: function() {
		this.rfxloaded = true;
		if (this.imgloaded && this.rfxloaded && this.labelloaded) {
			this.ready = true;
			this._parent.SpriteReady(this)
		}
	},
	OnLabelLoad: function() {
		this.labelloaded = true;
		if (this.imgloaded && this.rfxloaded && this.labelloaded) {
			this.ready = true;
			this._parent.SpriteReady(this)
		}
	},
	AnimateIn: function(container, side, onComplete) {
		var self = this;
		if (this._parent._parent._canDisplay) {
			container.appendChild(this.img);
			container.appendChild(this.rfx);
			container.appendChild(this.label);
			this.actualSide = side;
			
			$(this.img).css({
				top:$('#asset_paracada_'+side).position().top+$('#asset_paracada_'+side).height()+Math.round($('#asset_logo_'+side).height()*0.4),
				left: (side == 'left' ? $(container).width()-this._parent._parent.ActualResolution().margin-$(this.img).width():this._parent._parent.ActualResolution().margin)
			});
			$(this.rfx).css({
				top:$(this.img).position().top+$(this.img).height(),
				left:$(this.img).position().left+Math.floor(($(this.img).width()-$(this.rfx).width())/2)
			});
			$(this.label).css({
				top:$(this.rfx).position().top+Math.round($(this.rfx).height()*0.2),
				left:$(this.img).position().left+Math.floor(($(this.img).width()-$(this.label).width())/2)
			});

			$(this.label).animate({opacity: 0}, 0);
			$(this.img).animate({opacity: 0, top: '-=20'}, 0).animate({opacity: 1, top: '+=20'}, 800, 'easeOutCirc', function() {});
			$(this.rfx).animate({opacity: 0, top: '+=20'}, 0).animate({opacity: 0.25, top: '-=20'}, 800, 'easeOutCirc', function() {
				$(self.label).animate({opacity: 0}, 0).animate({opacity: 1}, 1000, 'linear', function() {
					if (self.actualSide == 'left') {
						self.lastSeen = Math.round(new Date().getTime() / 1000);
						$.cookie(self.name+'_lastSeen', self.lastSeen);
						self._parent._parent.RelocateItems();
						setTimeout(function(){
							self._parent._parent._bannerRight.actual.AnimateOut();
							self._parent._parent._bannerLeft.actual.AnimateOut();
						}, self._parent._parent.options.timePerItem);
					}
				});
			});
		}
		else {
			setTimeout(function() {self.AnimateIn(container, side, onComplete);},500);
		}
	},
	AnimateOut: function() {
		var self = this;
		$(this.label).animate({opacity: 0},300, 'linear').animate({top: '-2000'}, 0);
		$(this.img).animate({opacity: 0},300, 'linear').animate({top: '-2000'}, 0);
		$(this.rfx).animate({opacity: 0},300, 'linear').animate({top: '-2000'}, 0, function() {
			if (self.actualSide == 'left') {
				self._parent._parent.AnimateNext();
			}
		});
	}
};

Puntoticket.Samsung.SpriteSet.prototype = {
	SpriteReady: function(sprite) {
		if (this.left.ready && this.right.ready) {
			this.ready = true;
			this._parent.SpriteSetReady(this);
		}
	},
};

Puntoticket.Samsung.Asset.prototype = {
	OnLoad: function() {
		this.imgRight.src = this.imgLeft.src;
		this.ready = true;
		this._parent.AssetReady(this);
	}
};

Puntoticket.DebugWindow = function(o) {
	this.options = {
		id: 'PuntoticketDebug',
		container: document.body,
		width: '100%',
		height: '100%'
	};

	jQuery.extend(true, this.options, o);

	this._innerContainer = document.createElement('div');
	this._innerContainer.setAttribute('id', this.options.id);
	this._innerContainer.setAttribute('style', 'display: block; width:'+this.options.width+';height:'+this.options.height);
	this.options.container.appendChild(this._innerContainer);
	this._outputTextbox = document.createElement('textarea');
	this._outputTextbox.setAttribute('style', 'display: block;width:100%;height:100%');
	this._innerContainer.appendChild(this._outputTextbox);
	$(this._innerContainer).addClass('PuntoticketDebugWindow');
	this._innerContainer.style.zIndex = 999;
}

Puntoticket.DebugWindow.prototype = {
	Write: function(text) {
		this._outputTextbox.value += text;
		this._outputTextbox.scrollTop = this._outputTextbox.scrollHeight;
	},
	WriteLn: function(text) {
		this.Write(text+"\n");
	}
};

