var Rotocop = {
	init: function(container,showNumber,showNav){
		if (container) {
			this.container = container;
			this.itemContainer = this.container.getElement('.roto-items');
			this.items = this.itemContainer.getElements('li');
			this.slideCover = this.container.getElement('.roto-cover')
			this.showNumber = (showNumber) ? showNumber : 3;
			this.showNav = (showNav) ? showNav : false;
			this.seq = 0;
			this.z = Math.floor((this.items.length - 1) / showNumber);
			this.size = this.slideCover.getSize();

			if (this.items.length > this.showNumber){
				this.rotoPrev();
				this.rotoNext();
				if(this.showNav) this.buildNav();
			}
		}
	},
		
	rotate: function(newSeq){
		if(this.showNav) {
			this.rotoNav[this.seq].removeClass('on');
			this.rotoNav[newSeq].addClass('on');
		}
		this.items[this.seq].removeClass('on');
		this.items[newSeq].addClass('on');    
		this.seq = newSeq;
		var shift = newSeq * this.size.x;
		this.itemContainer.tween('margin-left', "-" + shift + "px");
	},
	
	buildNav: function(){
		var navCont = new Element('ul', {'id': 'roto-nav'}).inject(this.container,'bottom');
		for (i=0;i<=this.z;i++) {
			new Element('li', {'html': '<a href="#"></a>'}).inject(navCont,'inside');
		}
		this.rotoNav = $$('#roto-nav li');
		this.rotoNav[0].addClass('on');
		this.rotoNav.each(function(el, i){ 
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				this.rotate(i);
			}.bind(this));
	 	}.bind(this));	
	},
	
	rotoPrev: function(){
		var prevLink = new Element('a', {
		    'href': '#',
		    'class': 'roto-prev',
		    'html': 'Previous'
		}).inject(this.container,'bottom');
		
		prevLink.addEvent('click', function(e){
			e = new Event(e).stop();
			if(this.seq==0) { 
				this.rotate(this.z); 
			} else { 
				this.rotate(this.seq - 1); 
			}
		}.bind(this));	
	},
	
	rotoNext: function(){
		var nextLink = new Element('a', {
			'href': '#',
			'class': 'roto-next',
			'html': 'Next'
		}).inject(this.container,'bottom');
		
		nextLink.addEvent('click', function(e){
			e = new Event(e).stop();
			if(this.seq==this.z) { 
				this.rotate(0); 
			} else { 
				this.rotate(this.seq + 1); 
			}
		}.bind(this));	
    }
}; //Rotocop


//from solutoire.com
function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse) {
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y));
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: 'vertical',
		onChange: function(step){
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
};


function viewChanger() {
	$$('.view-changer').each(function(el, i){ 
		var items = el.getElements('li');
		var target = el.get('rel');
		var views = $$("'." + target + "'");
		
		items.each(function(el2){
			var viewClass = el2.get('class').replace("changer","view");
			
			el2.addEvent('click', function(e){	
				e = new Event(e).stop();
				items.each(function(el3){
					el3.getElements('a').removeClass('on');
				});
				el2.getElements('a').addClass('on');
				
				views.each(function(el4){
					var findClass = el4.get('class').split(' ');
					findClass.each(function(el5){
						if (el5.contains('view')) el4.removeClass(el5);
					});
					el4.addClass(viewClass);
				});
				Cookie.write('viewChanger', viewClass, {duration: 5});
			});
		});
		
		var pastView = Cookie.read('viewChanger');
		if (pastView) {
			items.each(function(el3){
				el3.getElements('a').removeClass('on');
				if (el3.get('class').replace("changer","view") == pastView) {
					el3.getElements('a').addClass('on');
				}

			});

			views.each(function(el4){
				var findClass = el4.get('class').split(' ');
				findClass.each(function(el5){
					if (el5.contains('view')) el4.removeClass(el5);
				});
				el4.addClass(pastView);
			});
		}
	});
};


var Popup = {		
	init: function(){
		this.popUpLink = $$('.popUpLink');
		this.popUpBox = $$('.popUpBox');
		var content = $('content');
		var cord1 = content.getCoordinates().left;

		this.popUpLink.each(function(el, i){
			var cord2 = el.getCoordinates().left;	
			var posLeft =  cord2 - cord1;

			if(el.get('rel') == 'middle') {
				var contWidth = content.getStyle('width').toInt();
				var boxWidth = this.popUpBox[i].getStyle('width').toInt();
				var posLeft = ((contWidth - boxWidth) / 2);
			} 

			el.addEvent('click', function(e){	
				e = new Event(e).stop();				
				this.popUpBox.each(function(ela){
					ela.setStyle('left', '-6000px');
				});
				this.popUpBox[i].setStyle('left', posLeft);
			}.bind(this));
		}.bind(this));
		this.close();
	},

	close: function(){
		$$('.close').each(function(el,i){ 
			el.addEvent('click', function(e){	
				e = new Event(e).stop();				
				el.getParent('.popUpBox').setStyle('left', '-6000px');
			});
		});
	}
};


var hoverBox = function(){
	var hoverLink = $$('.hoverLink');
	var hoverBox = $$('.hoverBox');
	
	hoverLink.each(function(el1, i){
		el1.addEvents({
		    'mouseenter': function(){
				hoverBox[i].addClass('on');
		    },
			'mouseleave': function(){
				hoverBox[i].removeClass('on');
		    }
		});
	});
};


// remove this when applicable
function feedbackPop(email) {
	var load = window.open('http://blogs.zdnet.com/emailform.php?email='+email,'','scrollbars=no,menubar=no,height=640,width=400,resizable=no,toolbar=no,location=no,status=no');
	load.document.close();
}


var Truncate = {
	//add class="truncate length-<value>" to the element you want to truncate
	//value is equal to the number of characters (not words)
	init: function(){
		$$('.truncate').each(function(el){ 
			this.getLength(el);	
		}.bind(this));
	},
	
	getLength: function(el){
		var truncLength = 100;
		el.get('class').split(" ").each(function(elClass){ 
			if (elClass.contains('length-')) {
				truncLength = elClass.slice(7).toInt();
			}
		})
		this.getTrunc(el, truncLength);
	},
	
	getTrunc: function(el, truncLength){
		var text = el.get('text');
		var html = el.get('html').clean();
		if (text.length > truncLength) {
			var hidden = true;
			var trunc = text.substring(0, truncLength);
			trunc = trunc.replace(/\w+$/, '');
			trunc = '<p>' + trunc + ' ...</p>';
			if (!html.substr(0,6).contains('<p>')) {
				html = '<p>' + html + '</p>';
			}
			this.toggle(el,trunc,html,hidden);
		}
	},
	
	toggle: function(el,trunc,html,hidden){
		if (hidden) {
			var contents = trunc;
			var showText = ' +more';
		} else {
			var contents = html;
			var showText = ' -less';
		}
		
		el.set('html',contents);
		new Element ('p', {
			'class': 'more',
			'events': {
				'click': function(e){
					e = new Event(e).stop();
					hidden = (hidden == true) ? false : true;
					Truncate.toggle(el,trunc,html,hidden);
				}
			}
		}).set('html',' <a href="#">'+ showText +'</a>').inject(el,'bottom');
	}	
};

var Tags = {
	truncate: function(amount){
		$$('.tags').each(function(el){
			var links = el.get('html').split(',');
			if (links.length > amount) {
				var hiddenLinks = links.splice(amount,links.length - amount);
				hiddenLinks = hiddenLinks.join(', ');
				links = links.join(', ');
				el.set('html',links);
				var ellipsis = new Element ('span', {'class': 'ellipsis'}).set('html','...').inject(el,'bottom');
				var hiddenCont = new Element ('span', {'class': 'more'}).set('html',',' + hiddenLinks).inject(el,'bottom');
				el.addEvents({
					'mouseenter': function(){
						ellipsis.setStyle('display','none');
						hiddenCont.setStyle('display','inline');
					},
					'mouseleave': function(){
						ellipsis.setStyle('display','inline');
						hiddenCont.setStyle('display','none');
					}
				});
			}
		});
	}
};

/*
Script: Position.js
	A class for positioning Elements relative to one another.
*/

var Position = new Class({
	// TODO: currenty only positions to document - need to make it possible to position relative to objects
	Implements: Options,
	options: {
		offEdge: 5 //pixels off the edge of window
	},
	
	initialize: function(elem,link,options){
		this.setOptions(options);
		this.elem = $(elem); // the element to position
		this.link = $(link); // the link to position it to

		// get measurements
		this.linkCoords = this.link.getCoordinates();
		this.elementSize = this.elem.getSize();
		this.windowSize = $(window).getSize();
		this.windowScroll = $(window).getScroll();

		// max x and y coordinates possible for screen
		this.yMax = this.windowSize.y + this.windowScroll.y;
		this.xMax = this.windowSize.x + this.windowScroll.x;

		// where the element will appear not accounting for screen
		this.yActual = this.linkCoords.bottom + this.elementSize.y;
		this.xActual = this.linkCoords.left + this.elementSize.x;

		// if element will appear off screen move it on, plus amount of pixels in offEdge
		this.top = (this.yActual > this.yMax) ? this.linkCoords.bottom - ((this.yActual - this.yMax) + this.options.offEdge) : this.linkCoords.bottom;
		this.left = (this.xActual > this.xMax) ? this.linkCoords.left - ((this.xActual - this.xMax) + this.options.offEdge) : this.linkCoords.left;

		this.setPosition();
	},

	setPosition : function(){
		this.elem.set('styles', {
			'position': 'absolute', // make sure it's absolute
			'top': this.top,
			'left': this.left
		});
	}
});

window.addEvent('domready', function() {		
	if ($$('.view-changer') != "") viewChanger();
	if ($$('.popUpLink') != "") Popup.init();
	if ($$('.hoverLink') != "") hoverBox();
	if ($$('.truncate') != "") Truncate.init();
	if ($$('.tags') != "") Tags.truncate(5);
});
