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;
};

/* 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);
};
_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.ImageScrollerContent = new Class({
	Implements: Options,
	
	options:{
		duration:500,
		transition:Fx.Transitions.Sine.easeInOut,
		direction:'ltr',
		link:'cancel',
		show:0,
		onReady:$empty
	},
	
	initialize: function(handler, index, id, options) {
		this.setOptions(options);

		// Make sure element is empty
		this.contentDiv = $(id + '-body');
		this.contentDiv.empty();
		this.contentDiv.set('morph', {
			duration: Mnet.getDuration(this.options.duration), 
			transition: this.options.transition,
			link: this.options.link
		});

		this.directionRtl = (this.options.direction == 'rtl');
		this.handler = handler;
		this.index = index;
		
		this.imageCount = 0;
		this.imageIndex = 0;

		this.imageLoader = new Mnet.SerialImageLoader({ onProgress:this.onProgress.bind(this) });
	},
	
	addImages: function(images) {
		this.imageLoader.addImages(images);
	},

	load: function() {
		this.imageLoader.load();
	},
	
	onProgress: function(image, index, count) {
		this.imageCount++;
		
		// Load 1st image into 1st slot
		if(index == this.options.show) {
			this.imageWidth = image != null ? image.getProperty('width') : this.imageLoader.imageArray[index].width;
			this.loadSlots(index, null, index);
			this.moveSlot(0);
			this.imageIndex = index;
		}
		else if(index == this.options.show + 1) {
			//this.loadSlots(1);

			this.options.onReady.bind(this)();
			
			// Alert handler that we are ready
			if(this.handler)
				this.handler.scrollerReady(this.index);
		}
	},

	getImageDetails: function(index, newIndex) {
		var obj = this.imageLoader.imageArray[index];
		var img = this.imageLoader.images[index];
		
		// Create HTML
		var el;
		if($type(obj) == 'object') {
			if(!obj.loaded) {
				img.setProperty('title', obj.title);
				if(obj.link != '') {
					el = new Element('a', { href:obj.link, title:obj.title });
					el.appendChild(img);
					//el = img;
				}
				else
					el = img;

				this.imageLoader.images[index] = el;
			
				obj.loaded = true;
			}
			else
				el = img;
				
			if(newIndex == index)
				this.options.currentDelay = obj.delay;
		}
		else 
			el = img;

		return el;
	},
	
	loadSlots: function(leftIndex, rightIndex, newIndex) {
		//_d(leftIndex+';'+rightIndex);
		var img1 = this.getImageDetails(leftIndex, newIndex);
		var img2;
		if($defined(rightIndex))
			img2 = this.getImageDetails(rightIndex, newIndex);
		
		// If 1st or 2nd image, only append
		if(!$defined(rightIndex)) {
			this.contentDiv.appendChild(img1);
		}
		else {
			this.contentDiv.empty();
			this.contentDiv.appendChild(img1);
			this.contentDiv.appendChild(img2);
		}
	},
	
	moveSlot: function(index) {
		if(index == 1)
			this.contentDiv.setStyle('left', -this.imageWidth + 'px');
		else
			this.contentDiv.setStyle('left', 0);
	},
	
	moveTo: function(index) {
		return index >= this.imageIndex ? this.next(index) : this.prev(index); 
	},
	
	next: function(index) {
		var thisIndex = this.imageIndex;
		var nextIndex = thisIndex + 1;
		
		if(index != null)
			nextIndex = index;

		if(nextIndex >= this.imageCount)
			nextIndex = 0;
		
		if(thisIndex == nextIndex || thisIndex >= this.imageCount)
			return false;

		// Right to left?
		if(this.directionRtl) {
			this.loadSlots(nextIndex, thisIndex, nextIndex);
			this.moveSlot(1);
		}
		else {
			this.loadSlots(thisIndex, nextIndex, nextIndex);
			this.moveSlot(0);
		}
	
		this.contentDiv.morph({
			'left': this.directionRtl ? 0 : -this.imageWidth
		});	
		
		this.imageIndex = nextIndex;
		
		return true;
	},
	
	prev: function(index) {
		var thisIndex = this.imageIndex;
		var prevIndex = thisIndex - 1;

		if(index != null)
			prevIndex = index;

		if(prevIndex < 0)
			prevIndex = this.imageCount - 1;

		if(thisIndex == prevIndex || thisIndex >= this.imageCount)
			return false;

		// Right to left?
		if(this.directionRtl) {
			this.loadSlots(thisIndex, prevIndex, prevIndex);
			this.moveSlot(0);
		}
		else {
			this.loadSlots(prevIndex, thisIndex, prevIndex);
			this.moveSlot(1);
		}
		
		this.contentDiv.morph({
			'left': this.directionRtl ? -this.imageWidth : 0
		});	
		
		this.imageIndex = prevIndex;
		
		return true;
	}
});

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)) {
			value = this.offsetWidth.toInt() + 
			this.getStyle('marginLeft').toInt() +
			this.getStyle('marginRight').toInt() +
			this.getStyle('paddingLeft').toInt() +
			this.getStyle('paddingRight').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,
		height:'100px',
		width:'auto'
	},
	
	initialize: function(holder, thumbnails, thumbnailHolder, options) {
		this.setOptions(options);
		
		this.images = holder.getElements('img');				
		this.thumbnails = thumbnails.getElements(thumbnailHolder);				
		
		this.currentIndex = -1;
		this.maxIndex = this.images.length - 1;
		
		if(this.maxIndex < 1)
			return;

		// Wrap images in fixed div to keep h/w
		this.holderWrap = new Element('div').setStyles({
			position:'relative',
			height:this.options.height,
			width:this.options.width,
			margin:0,
			padding:0,
			cursor:'pointer'
		}).addEvent('click', this.next.bind(this));

		var img = this.images[0];
		holder.appendChild(this.holderWrap.adopt(holder.childNodes));
		
		if(img.complete) this.wrap(img);
		else img.onload = this.wrap.bind(this, img);
		
		this.images.each(function(item, index) {
			item = item.setStyles({
				position:'absolute',
				left:'0px',	
				top:'0px',
				opacity:0
			}).set('tween', {'link':'cancel','duration': this.options.fadeDuration});
		}, this);
		
		this.thumbnails.each(function(item, index) {
			item
			.setStyles({cursor:'pointer','opacity':this.options.thumbFaded})
			.set('tween', {'link':'cancel','duration': 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);
		
		this.display(this.options.display)
	},
	
	wrap: function(img) {
		this.holderWrap.setStyles({
			'width':img.getFullWidth(),
			'height':img.getFullHeight()
		});
	},
	
	display: function(index) {
		if(this.currentIndex == index)
			return;

		// Hide current image
		/*
		this.images.each(function(item, index) {
			if(index != this.currentIndex)
				item.set({opacity:0});
		}, this);
		*/
						
		if(this.currentIndex >= 0) {
			this.images[this.currentIndex].tween('opacity', 0);
			this.thumbnails[this.currentIndex].tween('opacity', this.options.thumbFaded);
		}
		
		this.images[index].tween('opacity', 1);
		this.thumbnails[index].set('opacity', 1);
		
		this.currentIndex = index;
	},
	
	next: function() {
		var index = this.currentIndex + 1;
		this.display(index > this.maxIndex ? 0 : index);
	}
});


Mnet.FixedSizeImageScroller = new Class({
	Implements: Options,
	
	options:{
		duration:500,
		delay:1000,
		transition:Fx.Transitions.Sine.easeInOut,
		link:'cancel',
		show:0,
		onReady:$empty,
		maxHeight:-1
	},
	
	initialize: function(id, options) {
		this.setOptions(options);

		this.contentWrapper = $(id);
		
		this.contentDiv = $(id + '-body');
		//this.contentDiv.empty();
		this.contentDiv.set('morph', {
			duration: Mnet.getDuration(this.options.duration), 
			transition: this.options.transition,
			link: this.options.link
		});
		this.contentWidth = 0;
		this.contentHeight = 0;
		
		this.images = this.contentDiv.getElements('img');
		this.imageWidths = [];
		this.direction = 1;
				
		this.imageCount = 0;
		this.imageIndex = 0;

		this.imageLoader = new Mnet.SerialImageLoader({ onProgress:this.onProgress.bind(this) });
		this.imageLoader.addImages(this.images);
		this.imageLoader.load();
	},
	
	onProgress: function(image, index, count) {
		this.imageCount++;
		
		image = this.images[index];
		
		var width = $(image).getFullWidth();
		var height = $(image).getFullHeight();
		
		this.imageWidths[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);
			
			if(this.options.maxHeight > 0 && this.contentHeight > this.options.maxHeight)
				this.contentHeight = this.options.maxHeight;
				
			this.contentWrapper.setStyle('height', this.contentHeight);

			this.imagePositions = [];

			var pos = 0;
			for(var i = 0; i < this.imageWidths.length; i++) {
				this.imagePositions.push(pos);
				pos += this.imageWidths[i];
			}
			
			this.timerId = this.next.periodical(this.options.delay, this);
		}
	},
	
	next: function() {
		var thisIndex = this.imageIndex;
		var nextIndex = thisIndex + this.direction;

		if(nextIndex >= this.imageCount-1 || nextIndex < 0) {
			this.direction = this.direction * -1;
			
			nextIndex += this.direction * 2;
		}		

		this.contentDiv.morph({
			'left': -this.imagePositions[nextIndex]
		});	
		
		this.imageIndex = nextIndex;
		
		return true;
	}
});

/* 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(!$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';
	
	if(Mnet.Flash.installed) {
		var o = new Swiff(src, options);
		return o;
	}
	else
		return null;
}
