var Mnet = new Class();			
	
// Use this method for any duration to allow transitions to be turned off
Mnet.showTransitions = true;
Mnet.getDuration = function(duration) {
	return Mnet.showTransitions && $defined(duration) ? duration : 0;
};
	
/* Flash class */
Mnet.Flash = new Class();
Mnet.Flash.installed = (Browser.Plugins.Flash.version >= 6);
Mnet.Flash.object = function(container, src, width, height, params, vars, events) {
	if(!Mnet.Flash.installed)
		return null;

	if(!$defined(Mnet.rootUrl))
		Mnet.rootUrl = '';

	if(!$defined(params)) params = {};
	if(!$defined(vars)) vars = {};

	var options = {
	    width: width,
	    height: height,
		container: container,
	   	params: params, 
	    vars: vars,
		events: events
	};

	if(!src.match(/^\w+:\/\/|^\//))
		src = Mnet.rootUrl + 'flash/' + src + '.swf';
	
	var o = new Swiff(src, options);
	return o;
}



/* Debug class */
Mnet.Debug = new Class();

Mnet.Debug.init = function() {
	if(!$defined(Mnet.Debug.el)) {
		Mnet.Debug.el = new Element('div', { id:'debug', style:'position:fixed; top:0px; left:0px; background-color:#FFFFFF; color:#000; ' +
			' width:200px; height:12px; z-index:10; text-align:left; line-height:12px; font-size:10px;' }); 
			
		var elClose = new Element('a', { href:'javascript:Mnet.Debug.clear();', html:'[X]', style:'position:absolute; right:0px; color:#000;' });
		Mnet.Debug.el.appendChild(elClose);

		Mnet.Debug.elText = new Element('div', {  });
		Mnet.Debug.el.appendChild(Mnet.Debug.elText);
			
		document.body.appendChild(Mnet.Debug.el);
	}
	else
		Mnet.Debug.el.setStyle('visibility', 'visible');
}
	
Mnet.Debug.append = function(txt) {
	Mnet.Debug.init();	
	var ctxt = Mnet.Debug.elText.get('html');
	if(ctxt != '')
		txt = ctxt + '<br/>' + txt;
		
	Mnet.Debug.el.setStyle('height', Mnet.Debug.elText.getStyle('height').toInt() + 12);
	
	Mnet.Debug.set(txt);
};
var _d = Mnet.Debug.append;

Mnet.Debug.set = function(txt) {
	Mnet.Debug.init();
	Mnet.Debug.elText.set('html', txt);
};

Mnet.Debug.clear = function() {
	Mnet.Debug.init();
	Mnet.Debug.set('');
	Mnet.Debug.el.setStyle('height', 12);
	Mnet.Debug.el.setStyle('visibility', 'hidden');
};

Mnet.SerialImageLoader = new Class({
	imageIndex: 0,
	
	initialize: function(options) {
		if(!$defined(options))
			options = {}

		this.onProgress = $defined(options.onProgress) ? options.onProgress : $empty;
		this.onComplete = $defined(options.onComplete) ? options.onComplete : $empty;
	},
	
	addImages: function(images) {
		if(!$defined(this.imageArray))
			this.imageArray = [];
		
		this.imageArray.extend(images);
	},
	
	load: function() {
		this.imageCount = this.imageArray.length;
		this.imageIndex = -1;
		
		// Call onload method - first time will init and load first image
		this.onLoad();
	},
	
	onLoad: function() {
		// First load call? init images array and skip event calling
		if(!$defined(this.images))
			this.images = [];
		else {
			this.onProgress(this.images[this.imageIndex], this.imageIndex, this.imageCount);
		}
		
		// More images to load?
		if(++this.imageIndex < this.imageCount) {
			var image = this.imageArray[this.imageIndex];
			var src = ($type(image) != 'string') ? image.src : image;
			this.images.push(new Asset.image(src, {onload:this.onLoad.bind(this)}));
		}
		else
			this.onComplete(this.imageCount);
	}
});

Element.implement({
	getFullHeight: function() {
		var value = this.retrieve('Element:fullHeight');
		if(!$defined(value)) {
			value = this.offsetHeight.toInt() + 
				this.getStyle('marginTop').toInt() +
				this.getStyle('marginBottom').toInt() +
				this.getStyle('paddingTop').toInt() +
				this.getStyle('paddingBottom').toInt();
			this.store('Element:fullHeight', value);
		}
		return value;
	},
	
	getFullWidth: function() {
		var value = this.retrieve('Element:fullWidth');
		if(!$defined(value)) {
			//_d(this.getStyle('borderRightWidth'));
			value = this.offsetWidth.toInt() + 
			this.getStyle('marginLeft').toInt() +
			this.getStyle('marginRight').toInt() +
			this.getStyle('paddingLeft').toInt() +
			this.getStyle('paddingRight').toInt()/* +
			this.getStyle('borderLeftWidth').toInt() +
			this.getStyle('borderRightWidth').toInt()*/;
			this.store('Element:fullWidth', value);
		}
		return value;					
	}
});

	
Mnet.Gallery = new Class({
	Implements: Options,
	
	options: {
		holderClass:'image-holder',
		thumbnailsClass:'thumbnails',
		fadeDuration:700,
		display:0,
		thumbFaded:0.5,
		thumbFadeDuration:200,
		addHolderLink:true,
		imageElements:'img',
		align:'left'
	},
	
	initialize: function(holder, thumbnails, thumbnailHolder, options) {
		this.setOptions(options);
		
		this.holder = holder;
		this.images = holder.getElements(this.options.imageElements);
		
		if($defined(thumbnails))				
			this.thumbnails = thumbnails.getElements(thumbnailHolder);				
		
		this.currentIndex = -1;
		this.maxIndex = this.images.length - 1;
		
		if(this.maxIndex < 0)
			return;
		
		var align = (this.options.align == 'right') ? 'right' : 'left';
		
		this.images.each(function(item, index) {
			item = item.setStyles({
				position:'absolute',
				//left:'0px',	
				top:'0px',
				opacity:0
			});
			item.setStyle(align, '0px');
			item.set('tween', {'link':'cancel','duration': Mnet.getDuration(this.options.fadeDuration)});
			
			if(item.getParent().get('tag') == 'a')
				this.options.addHolderLink = false;
		}, this);
		
		// If images don't already have links on them, add next() link to holder
		if(this.options.addHolderLink) {
			this.holder.setStyles({
				cursor:'pointer'
			}).addEvent('click', this.next.bind(this));
		}
				
		if($defined(this.thumbnails)) {
			this.thumbnails.each(function(item, index) {
				item
				.setStyles({cursor:'pointer','opacity':this.options.thumbFaded})
				.set('tween', {'link':'cancel','duration': Mnet.getDuration(this.options.thumbFadeDuration)})
				.addEvent('click', function() {
					this.display(index);	
				}.bind(this))
				.addEvent('mouseover', function() {
					item.tween('opacity', 1);
				}.bind(this))
				.addEvent('mouseout', function() {
					if(index != this.currentIndex)
						item.tween('opacity', this.options.thumbFaded);
				}.bind(this));
			}, this);
		}
		
		// Make sure image is loaded before displaying
		var img = this.images[this.options.display];
		if(img.complete) this.display(this.options.display);
		else img.onload = this.display.bind(this, this.options.display);
	},
	
	display: function(index) {
		if(this.currentIndex == index)
			return;
		
		// Update holder height		
		this.holder.setStyle('height', this.images[index].getFullHeight());
				
		if(this.currentIndex >= 0) {
			this.images[this.currentIndex].tween('opacity', 0);
			if($defined(this.thumbnails))
				this.thumbnails[this.currentIndex].tween('opacity', this.options.thumbFaded);
		}
		
		this.images[index].tween('opacity', 1);
		if($defined(this.thumbnails))
			this.thumbnails[index].set('opacity', 1);
		
		this.currentIndex = index;
	},
	
	prev: function() {
		var index = this.currentIndex - 1;
		this.display(index < 0 ? this.maxIndex : index);
	},
	
	next: function() {
		var index = this.currentIndex + 1;
		this.display(index > this.maxIndex ? 0 : index);
	},
	
	link: function() {
		var item = this.images[this.currentIndex];
		var parent = item.getParent();
		
		if(parent.get('tag') == 'a') {
			if(parent.hasEvent('click'))
				parent.fireEvent('click');
			else {
				var href = parent.href;
				if($type(href) == 'function')
					(href)();
				else if($type(href) == 'string' && href != '')
					document.location.href = href;
			}
		}
	}
});


Mnet.ExpandCollapseButton = new Class({
	Implements: Options,
	
	options:{
		duration:500,
		transition:'pow:out',
		mode:'vertical',
		link:'ignore',
		classActive:'selected',
		classMouseOver:'hover'
	},
	
	initialize: function(button, box, options) {
		this.setOptions(options);

		if($type(button) == 'array') {
			this.elButton = [];
			button.each(function(item) { this.elButton.push($(item)); }.bind(this));
		}
		else
			this.elButton = [$(button)];
			
		this.elBox = $(box);
		if(!this.elBox || !this.elButton[0])
			return;

		this.slide = new Fx.Slide(box, {
			mode: this.options.mode,
			transition: this.options.transition,
			duration: Mnet.getDuration(this.options.duration),
			link: this.options.link
		});
		
		if(!$defined(this.options.expanded))
			this.options.expanded = this.elButton[0].hasClass(this.options.classActive);
		
		if(!this.options.expanded)
			this.slide.hide();
		else	
			this.slide.show();

		this.elBox.setStyle('visibility','visible');		
		this.elBox.setStyle('display','block');
		
		// Add events to button(s)
		this.elButton.each(function(item) { 
			item.setStyle('cursor', 'pointer');

			item.store('Mnet.ExpandCollapseButton.options', {
				'slide':this.slide,
				'expanded':this.options.expanded,
				'classActive':this.options.classActive,
				'onExpand':this.options.onExpand,
				'onCollapse':this.options.onCollapse
			});
			
			item.addEvent('click', function() { return Mnet.ExpandCollapseButton.toggle(item); });

			item.addEvent('mouseover', function() {
				if(!this.options.expanded) {
					this.elButton.each(function(item) {
						item.addClass(this.options.classMouseOver);
					}.bind(this));
				}
			}.bind(this));

			item.addEvent('mouseout', function() {
				if(!this.options.expanded) {
					this.elButton.each(function(item) {
						item.removeClass(this.options.classMouseOver);
					}.bind(this));
				}
			}.bind(this));
		}, this);
	}
});

Mnet.ExpandCollapseButton.expand = function(item) {
	if(!item) item = this;
	var options = $(item).retrieve('Mnet.ExpandCollapseButton.options');
	
	// Still working?
	if(options.slide.timer)
		return false;

	options.slide.slideIn();
	item.addClass(options.classActive);
	options.expanded = true;
	
	if($type(options.onExpand) == 'function')
		options.onExpand(item);
		
	return false;
}

Mnet.ExpandCollapseButton.collapse = function(item) {
	if(!item) item = this;
	var options = item.retrieve('Mnet.ExpandCollapseButton.options');
	
	// Still working?
	if(options.slide.timer)
		return false;

	options.slide.slideOut();
	item.removeClass(options.classActive);
	options.expanded = false;

	if($type(options.onCollapse) == 'function')
		options.onCollapse(item);
	
	return false;
}

Mnet.ExpandCollapseButton.toggle = function(item) {
	if(!item) item = this;
	var options = item.retrieve('Mnet.ExpandCollapseButton.options');
	
	// Still working?
	if(options.slide.timer)
		return false;
		
	if(!options.expanded)
		return Mnet.ExpandCollapseButton.expand(item);
	else
		return Mnet.ExpandCollapseButton.collapse(item);
}

Mnet.ShoppingBasket = new Class({
	Implements: Options,
	
	options: {

	},

	initialize: function(holder, toggle, options) {
		this.setOptions(options);
		
		this.makingRequest = false;
		this.requestUrl = Mnet.rootUrl + 'home/itsagroup/basket/index.php';
		this.checkoutUrl = Mnet.rootUrl + 'home/itsabagel/checkout/';
		
		this.holder = $(holder);
		this.tableBody = this.holder.getElement('table > tbody');
		this.totalHolder = $(this.holder.id + '-total-count');
		this.totalPriceHolder = $(this.holder.id + '-total-price');
		this.toggle = $(toggle);
	},
	
	makeRequest: function(action, options) {
		// If in process of adding a request, ignore
		if(this.makingRequest)
			return;
		
		try {
			var req = new Request.JSON({
				url: this.requestUrl,
				onSuccess: function(responseJSON, responseText) {
					this.makingRequest = false;
					//alert(responseText);
					
					if(!responseJSON || responseJSON.error != null) {
						var error =  responseJSON ? responseJSON.error : 'Invalid return data';
						alert('Unable to perform ' + action + ': ' + error);
					}
					else {
						options.onResponse(responseJSON);
					}
				}.bind(this),
				onFailure: function(xhr) {
					this.makingRequest = false;
					alert('Unable to perform ' + action + ': ' + xhr.statusText);
				}.bind(this)
			});
			
			if(!options.args)
				options.args = {};
			options.args.poutput = 'xml';
			options.args.act = action;
			
			this.makingRequest = true;
			req.post(options.args);
		}
		catch(e) {
			alert('Unable to perform ' + action + ': ' + e.message);
		}
	},

	addItemFromForm: function(form) {
		var post = {};
		for(var i = 0; i < form.elements.length; i++)
			post[form.elements[i].name] = Mnet.Form.itemValue(form.elements[i]);
			
		this.addItem(post);
	},

	addItem: function(item) {
		if(!Mnet.basketOpen) {
			this.expand();
			return;
		}
		
		item.return_items = true;
		this.makeRequest('add', {
			args: item,
			onResponse: this.addItemResponse.bind(this)	
		});
	},
	
	addItemResponse: function(responseJSON) {
		this.fill(responseJSON.cart);
		this.expand();
	},
	
	loadItems: function(id) {
		if(!Mnet.basketOpen)
			return;

		this.makeRequest('list', {
			onResponse: this.loadItemsResponse.bind(this)	
		});
	},
	
	loadItemsResponse: function(responseJSON) {
		this.fill(responseJSON.cart);
	},
		
	clear: function() {
		this.makeRequest('clear', {
			onResponse: function() { this.tableBody.empty(); }.bind(this)	
		});
	},
	
	appendItem: function(item) {
		var html = '<td valign="top"><a href="javascript:basket.removeItem(%id%);">X</a></td><td valign="top">%title%</td><td class="info" valign="top">%toasted%</td><td class="info" valign="top">%glutenfree%</td><td class="price" valign="top">%price%</td>'; //this.options.itemHtml;
		var textToasted = 'Toasted';
		var textGlutenFree = 'G/W Free';

		html = html.replace(/%id%/, item.id);
		
		if(item.signature)
			html = html.replace(/%title%/, item.title);
		else
			html = html.replace(/%title%/, item.title + '<br/>on a ' + item.type + ' bagel');
			
		html = html.replace(/%price%/, item.price);
		html = html.replace(/%toasted%/, item.toasted ? textToasted : '');
		html = html.replace(/%glutenfree%/, item.glutenfree ? textGlutenFree : '');
		
		var row = new Element('tr', {id:'basket-item-row-' + item.id});
		
		this.tableBody.appendChild(row);
		row.set('html', html);
	},
	
	fill: function(cart) {
		this.tableBody.empty();
		
		for(var i = 0; i < cart.items.length; i++) {
			this.appendItem(cart.items[i]);
		}
		
		// Update totals
		this.totalHolder.innerHTML = cart.totalcount;
		this.totalPriceHolder.innerHTML = cart.totalprice;
	},
	
	removeItem: function(id) {
		this.makeRequest('remove', {
			args: {
				id: id
			},
			onResponse: this.removeItemResponse.bind(this)	
		});
	},
	
	removeItemResponse: function(responseJSON) {
		// Find item and remove it
		var node = $('basket-item-row-' + responseJSON.removeditemid)
		if(node)
			this.tableBody.removeChild(node);
	},
	
	expand: function() {
		Mnet.ExpandCollapseButton.expand(this.toggle);	
		if(!this.loaded) {
			this.loadItems();
			this.loaded = true;
		}
	},

	collapse: function() {
		Mnet.ExpandCollapseButton.collapse(this.toggle);	
	},
	
	submit: function() {
		document.location.href = this.checkoutUrl;
	}
});

Mnet.Form = new Class();
Mnet.Form.itemValue = function(item) {
	if(!item)
		return null;
	else if(item.options)
		return (item.selectedIndex >= 0) ? item.options[item.selectedIndex].value : "";
	else if(item.length) {
		i = 0;
		while(i < item.length && !item[i].checked)
			i++;
		return (i < item.length) ? item[i].value : "";
	}
	else if(item.type == 'checkbox' || item.type == 'radio')
		return item.checked ? item.value : '';
	else
		return item.value;
};

Mnet.Slider = new Class({
	Implements: Options,
	
	options: {
		duration: 1000,
		transition: Fx.Transitions.Pow.easeOut,
		delay:-1,
		link: 'cancel',
		reflect: {
			opacity:0.6,
			height:0.33
		}
	},
	
	initialize: function(slider, items, options) {
		this.setOptions(options);
		
		this.elSlider = $(slider);
		this.elFrame = this.elSlider.getElement('.slider');
		this.elBody = this.elSlider.getElement('.slider-body');
		this.elTracker = this.elSlider.getElement('.slider-tracker');
		this.elKnob = this.elSlider.getElement('.slider-knob');
		this.elLeft = this.elSlider.getElement('.slider-left');
		this.elRight = this.elSlider.getElement('.slider-right');
		
		// Calc width
		if($type(items) == 'string')
			items = this.elBody.getElements(items);
		
		this.items = items;
		this.widths = {};
		this.positions = {};
		this.loaded = 0;
		
		//this.bodyWidth = 0;
		
		this.scroller = new Fx.Morph(this.elBody, { 
			link: this.options.link,
			duration: Mnet.getDuration(this.options.duration), 
			transition: this.options.transition
		});

		// Go through each item and get width + add onload event to image
		this.items.each(function(item, index) {
			// Find image
			var img = item.getElement('.image img');
			if(!img.complete) 
				new Asset.image(img.src, { onload: this.onImageLoad.bind(this, [img, index]) });
			else {
				//this.widths[index] = img.width;
				this.onImageLoad(img, index);
			}
		}, this);
	},
	
	onImageLoad: function(img, index) {
		this.loaded++;

		//_d(index);
		//this.items[index].setStyle('width', this.widths[index]);
		this.widths[index] = this.items[index].getFullWidth('width');
		
		//this.bodyWidth += this.widths[index];
		//this.elBody.setStyle('width', this.bodyWidth);
		
		if(this.options.reflect)
			img.reflect(this.options.reflect);
		
		if(this.loaded == this.items.length)
			this.onLoaded();
	},
	
	// All loaded, set up slider
	onLoaded: function(reload) {
		// Get index max
		var widthCont = this.elSlider.getStyle('width').toInt();
		var width = 0;

		var i = this.items.length - 1;
		if(i > 0) {
			// Remove any right margin on last item
			//this.items[i]
			
			do { 
				width += this.widths[i];
				
				if(width <= widthCont)
					i--;
					
				//_d(i + ":" + this.widths[i] + " + " + width + " :" + widthCont);
			} while(width <= widthCont && i > 0);
		}
		
		// Calc positions
		if(!reload) {
			var pos = 0;
			for(var j = 0; j < this.items.length; j++) {
				var w = this.widths[j];
				this.positions[j] = pos;
				pos += w;
			}
		}
				
		// Set knob width
		var count = this.items.length;
		var max = i + 1;
		width = (count - max) / count;

		//_d(count + ":" + max);
		
		this.elKnob.setStyle('width', Math.round(width * 100) + '%');
		
		var step = (reload) ? this.slider.step : 0;
		this.slider = new Slider(this.elTracker, this.elKnob, {
			range: [0, max],
			wheel: true,
			snap: true,
			//onStart: this.onStart.bind(this),
			//onTick: this.onTick.bind(this),
			//onComplete: this.onComplete.bind(this),
			onChange: this.onChange.bind(this)
		});
		this.slider.set(Math.min(step, this.slider.options.range[1]));
		
		// Add links to left / right buttons
		if(!reload) {
			if(this.elLeft)
				this.elLeft.addEvent('click', this.onStep.bind(this, -1));
			if(this.elRight)
				this.elRight.addEvent('click', this.onStep.bind(this, 1));
		}
		
		this.ready = true;
		//if(this.options.delay > 0)
		//	this.play();
	},
	
	reload: function() {
		var play = this.playing();
		if(play)
			this.pause();
		this.onLoaded(true);
		if(play)
			this.play();
	},
	
	/*
	onStart: function() {
		//this.borderFx = this.borderFx || this.element.tween('border').start('#ccc');
		//Mnet.Debug.append('s');
	},
	onTick: function(pos) {
		_d(pos);
		//Mnet.Debug.append(this.slider.step);
		this.elKnob.setStyle('left', pos);
	},
	onComplete: function() {
		//alert(this.step + ":");
		//this.element.morph('left').start(10);
		//Mnet.Debug.append('d');
	},
	*/
	onChange: function(pos) {
		var item = this.items[this.slider.step];
		pos = this.positions[this.slider.step];
		
		this.scroller.start({left:-pos});
	},
	onStep: function(step, wrap) {
		var newStep = this.slider.step + step;
		if(wrap) {
			var min = this.slider.options.range[0];
			var max = this.slider.options.range[1];
			
			if(newStep > max)
				newStep = min;
			else if(newStep < min)
				newStep = max;
		}
		this.slider.set(newStep);
	},

	playPause: function() {
		if(this.playing())
			this.pause();
		else
			this.play();
	},
	
	playing: function() { return (this.timerId != null); },
	
	play: function() { 
		if(!this.ready) return;
		
		// Only scroll if needed
		if(!this.playing())
			this.timerId = this.onStep.periodical(this.options.delay, this, [1, true]);
	},

	pause: function() { 
		if(!this.ready) return;
			
		if(this.playing()) {
			$clear(this.timerId);
			this.timerId = null;
		}
	}
});


Mnet.VariableSizeObjectLoopScroller = new Class({
	Implements: Options,
	
	options:{
		duration:500,
		delay:1000,
		transition:Fx.Transitions.Sine.easeInOut,
		link:'chain',
		show:0,
		onReady:$empty,
		maxHeight:-1,
		objects:'img',
		direction:1
	},
	
	initialize: function(id, options) {
		this.setOptions(options);

		this.contentWrapper = $(id);
		
		this.contentDiv = $(id + '-body');
		
		this.mover = new Fx.Morph(this.contentDiv, {
			duration: Mnet.getDuration(this.options.duration), 
			transition: this.options.transition,
			link: this.options.link
		});
 		
		this.contentWrapperWidth = this.contentWrapper.getStyle('width').toInt();
		this.contentWidth = 0;
		this.contentHeight = 0;
		
		this.items = this.contentDiv.getElements(this.options.objects);
		this.itemWidths = [];
				
		this.itemCount = 0;
		this.itemIndex = 0;

		if(this.options.objects == 'img') {
			this.imageLoader = new Mnet.SerialImageLoader({ onProgress:this.onProgress.bind(this) });
			this.imageLoader.addImages(this.items);
			this.imageLoader.load();
		}
		else {
			this.items.each(function(item, index) {
				this.onProgress(item, index, this.items.length);
			}.bind(this));
		}
	},
	
	onProgress: function(item, index, count, reload) {
		this.itemCount++;
		
		item = this.items[index];
		
		var width = $(item).getFullWidth();
		//var height = $(item).getFullHeight();
		
		this.itemWidths[index] = width;
		this.contentWidth += width;
		
		//if(height > this.contentHeight)
		//	this.contentHeight = height;
		
		// Ready? set width and start scroll
		if(index == count - 1) {
			this.contentDiv.setStyle('width', this.contentWidth + 1000);
			
			if(!reload) {
				this.contentHeight = this.contentWrapper.getFullHeight();
				
				if(this.options.maxHeight > 0 && this.contentHeight > this.options.maxHeight)
					this.contentHeight = this.options.maxHeight;
					
				// Set wrapper height and make content movable
				this.contentWrapper.setStyle('height', this.contentHeight);
				this.contentDiv.setStyle('position', 'absolute');
				
				//_d(this.contentWrapper.getStyle('height'))
				//this.contentWrapper.setStyle('height', this.contentHeight);

				this.ready = true;
				this.play();
			}
		}
	},
	
	reload: function() {
		this.contentWidth = 0;
		this.itemCount = 0;
		
		var count = this.items.length;
		for(var i = 0; i < count; i++) {
			this.onProgress(null, i, count, true);
		}
	},
	
	playPause: function() {
		if(this.playing())
			this.pause();
		else
			this.play();
	},
	
	playing: function() { return (this.timerId != null); },
	
	play: function() { 
		if(!this.ready) return;
		
		// Only scroll if needed
		if(!this.playing() && this.contentWidth > this.contentWrapperWidth)
			this.timerId = this.next.periodical(this.options.delay, this);
	},

	pause: function() { 
		if(!this.ready) return;
			
		if(this.playing()) {
			$clear(this.timerId);
			this.timerId = null;
		}
	},
	
	prev: function() { this.move(-1); },
	next: function() { this.move(1); },
	
	move2: function() {
		//_d(this.prevIndex);
		var item = this.items[this.prevIndex];
		
		// Get node right under content div
		while(item.parentNode != this.contentDiv)
			item = item.parentNode;
				
		this.contentDiv.appendChild(this.contentDiv.removeChild(item));
		
		// Reset div position		
		this.contentDiv.setStyle('left', 0);
		
		this.moving = false;
	},

	move: function(direction) {
		if(!this.ready || this.moving) return false;
		
		this.moving = true;
		
		direction *= this.options.direction;
		
		var thisIndex = this.itemIndex;
		var nextIndex = (thisIndex + direction + this.itemCount) % this.itemCount;

		this.prevIndex = thisIndex;
		this.itemIndex = nextIndex;

		if(direction > 0) {
			this.mover.start({
	    		'left': -this.itemWidths[thisIndex]
			}).chain(
				function() { this.move2(); }.bind(this)
			);
		}
		else {
			//_d(this.prevIndex);
			var item = this.items[nextIndex];
			
			// Get node right under content div
			while(item.parentNode != this.contentDiv)
				item = item.parentNode;
			
			//_d(nextIndex);
			
			var i = this.contentDiv.removeChild(item);		
			i.inject(this.contentDiv, 'top');
			
			// Reset div position		
			this.contentDiv.setStyle('left', -this.itemWidths[nextIndex]);
			
			// Scroll
			this.mover.start({
	    		'left': 0
			}).chain(
				function() { this.moving = false; }.bind(this)
			);
		}

		return true;
	}
});


var basket;

Mnet.init = function() {
	// Hide flash titles
	if(Browser.Plugins.Flash.version >= 6) {
		document.write(
			'<' + 'style type="text/css" media="screen">' +
			'	.flash-titles,' +
			'	.flash-subTitles { visibility:hidden; }' +
			'<' + '/' + 'style>'
		);
	}

	// Hide images while loading
	document.write(
		'<' + 'style type="text/css" media="screen">' +
		'	#images-holder { height:0px; overflow:hidden; } ' +
		'<' + '/' + 'style>'
	);
						
	window.addEvent('domready', function() {
		// Skip transitions on ipod 
		if(Browser.Platform.ipod)
			Mnet.showTransitions = false;
	
		basket = new Mnet.ShoppingBasket('basket','top-menu-item-itsagroup-basket');
		//basket.loadItems();
		
		new Mnet.ExpandCollapseButton('top-menu-item-itsagroup-basket', 'basket', {onExpand:basket.expand.bind(basket)});
	});
	
	/*
	window.addEvent('load', function() {
		
	});
	*/
};




