var Scroller = new Class
({
	options : 
	{
		'container' : ''
		, 'nextLink': ''
		, 'prevLink': ''
		, 'pageWidth': 0
	},
	
	pageCount: 0,
	pageNum: 0,
	fx: null,

	initialize : function(obj)
	{
		// set user defined options
		this.setOptions(obj);

		// setup scrolling effect
		this.fx = new Fx.Scroll(this.options.container,
		{
			duration: 1000
			, wait: false
			, transition: Fx.Transitions.Quad.easeOut
		});
		this.fx.scrollTo(0);

		// number of pages
		//this.pageCount = $(this.options.container).getElements('td').length-1;

		// setup the prev, next links
		if ($(this.options.nextLink)) $(this.options.nextLink).addEvent('click',this.next.bindWithEvent(this));
		if ($(this.options.prevLink)) $(this.options.prevLink).addEvent('click',this.prev.bindWithEvent(this));
	},

	

	getPageCount : function()
	{
		var w = $(this.options.container).getSize().scrollSize.x;
		return Math.ceil(w/this.options.pageWidth)-1;
	},

	next : function(e)
	{
		var cnt = this.getPageCount();

		new Event(e).stop();
		if (++this.pageNum > cnt) this.pageNum = cnt;
		this.fx.scrollTo(this.options.pageWidth*this.pageNum);
		this.setNav();
	},

	prev : function(e)
	{
		new Event(e).stop();
		if (--this.pageNum < 0) this.pageNum = 0;
		this.fx.scrollTo(this.options.pageWidth*this.pageNum);
		this.setNav();
	},

	setNav : function(delay)
	{
		$(this.options.nextLink).setStyle('opacity',0.05);
		$(this.options.prevLink).setStyle('opacity',0.05);

		if (delay) this.setNav.delay(delay,this);
		else
		{
			$(this.options.nextLink).setStyle('opacity',(this.pageNum == this.getPageCount())? 0.05 : 1);
			$(this.options.prevLink).setStyle('opacity',(this.pageNum == 0)? 0.05 : 1);
		}
	},

	reset : function()
	{
		this.pageNum = 0;
		this.fx.scrollTo(0);
	}
});

Scroller.implement(new Options);

var SEOBViewer = new Class
({
	options : 
	{

	},

	data : {},
	scroller : {},

	initialize : function(obj)
	{
		this.data = obj.data;

		this.setupThumbs();
		this.setupGroups();

		this.scroller = new Scroller({'container':'thumb-scroller','pageWidth':662,'nextLink':'next','prevLink':'prev'});
		this.scroller.setNav(500);
	},

	setup : function()
	{
		//$('thumb-scroller').
		//console.log(this.getTypes());
	},

	getTypes : function()
	{
		var types = [];
		var icnt = this.data.length;
		for (var i=0; i<icnt; i++)
		{
			var ocnt = this.data[i].type.length;
			for (var o=0; o<ocnt; o++)
			{
				var type = this.data[i].type[o].split('-').join(' ').capitalize()
				types.include(type);
			}
		}

		return types;
	},

	setupGroups : function()
	{
		new Element('a',
		{
			'href':''
			, 'events':
			{
				'click' : this.sortHandler.bindWithEvent(this,'')
			}
		}).setHTML('All').injectInside('sort-types');
		$('sort-types').appendText(' | ');

		var types = this.getTypes();
		var icnt = types.length;
		for (var i=0; i<icnt; i++)
		{
			if (i)
			{
				$('sort-types').appendText(' | ');
			}

			new Element('a',
			{
				'href':''
				, 'events':
				{
					'click' : this.sortHandler.bindWithEvent(this,types[i])
				}
			}).setHTML(types[i]).injectInside('sort-types');
		}

		$('sort-types').getElement('a').addClass('active');
	},

	sortHandler : function(e,t)
	{
		e = new Event(e).stop();
		this.setupThumbs(t?t.split(' ').join('-').toLowerCase():'');

		$('sort-types').getElements('a').removeClass('active');
		e.target.addClass('active');
	},

	selectFirst : function(type)
	{
		if (!type) 
		{
			this.thumbLinkHandler(0);
			return;
		}

		var icnt = this.data.length;
		for (var i=0; i<icnt; i++)
		{
			if (this.data[i].type == type)
			{
				this.thumbLinkHandler(i);
				return;
			}
		}
	},

	setupThumbs : function(type)
	{
		var items = this.data

		var icnt = items.length;
		if (icnt)
		{
			$('thumb-scroller').empty();
			var table_container = new Element('table',{'border':0,'align':'center'}).injectInside('thumb-scroller');
			var tbody_container = new Element('tbody').injectInside(table_container);
			var tr_container = new Element('tr').injectInside(tbody_container);

			for (var i = 0; i < icnt; i++)
			{
				//if (type && this.data[i].type != type) continue;
				if (type && !this.data[i].type.contains(type)) continue;

				var td_container = new Element('td').injectInside(tr_container);

				new Element('img',
				{
					'src': items[i].thumb
					, 'events':
					{
						'click': this.thumbLinkHandler.bind(this,i)
						, 'mouseenter': this.thumbOnHandler
						, 'mouseleave': this.thumbOffHandler
					}
				}).injectInside(td_container);
			}
		}

		if (this.scroller && this.scroller.reset) 
		{
			this.scroller.reset();
			this.scroller.setNav();
		}

		this.selectFirst(type);
	},

	thumbOnHandler : function(e)
	{
		//this.setStyle('border','2px solid #036');
		this.addClass('highlight');
	},

	thumbOffHandler : function(e)
	{
		this.removeClass('highlight');
	},

	thumbLinkHandler : function(i)
	{
		$('thumb-scroller').getElements('img').removeClass('active');
		$('thumb-scroller').getElements('img[src='+ this.data[i].thumb +']').addClass('active');

		$('view').empty();
		new Element('img',
		{
			'src': this.data[i].view
		}).injectInside('view');

		$('info').empty();
		new Element('h1').setHTML(this.data[i].title).injectInside('info');
		
		new Element('hr').injectInside('info');
		new Element('div').setHTML(this.data[i].description).injectInside('info');

		new Element('hr').injectInside('info');

		if (this.data[i].status == 'active')
		{
			new Element('img',
			{
				'src': '/images/clients/active-dev.gif'
				, 'styles': {'margin-bottom':'5px'}
				, 'alt': 'Active Development'
			}).injectInside('info');
		}				new Element('div').setHTML(this.data[i].services).injectInside('info');
	
		if (this.data[i].link && this.data[i].link.length)
		{
			var ocnt = this.data[i].link.length;

			new Element('hr').injectInside('info');
			for (var o=0; o<ocnt; o++)
			{
				var link = this.data[i].link[o];
				var container = new Element('div',{'class':'link'}).injectInside('info');
				new Element('a',{'href':link.url,'target':'_blank'}).setHTML(link.text).injectInside(container);
			}
		}
	}
});

SEOBViewer.implement(new Options);


var SafeEmail = new Class
({
	options : 
	{
		'user' : ''
		, 'host' : ''
		, 'tld' : '.com'
		, 'subject' : ''
	},

	initialize : function(options)
	{
		this.setOptions(options);
	},

	getMailto : function()
	{
		var mailto = 'mailto:' + this.options.user + '@' + this.options.host + this.options.tld ;
		if (this.options.subject) mailto += '?subject=' + this.options.subject ;
		return mailto;
	}
});

SafeEmail.implement(new Options);
