//var features = 0;
var imageHeight = 336;

window.addEvent('domready', function() {	
	// find max Feature height
	var features = $$('#featured div.feature div.caption');
	
	// set the inner div width to hold all of the items
	$('featured_inner').setStyle('width', 400 * features.length);
	
	var maxHeight = {
		'index': -1,
		'height': 0
	};
	features.each(function(item, index) {
		if (item.getSize().size.y > maxHeight.height) {
			maxHeight.height = item.getSize().size.y;
			maxHeight.index = index;
		}
	});
	setCaption(maxHeight.height);
	
	// Add an indicator for each feature
	$$('#featured div.feature div.indicator').each(function(item, index) {
		//alert(item.getElement('.indicator'));
		for (var i=0; i<features.length; i++) {
			new Element('img', {
				'src': i == index ? 'http://www.alumni.mtu.edu/techalum/images/feature-on.jpg' : 'http://www.alumni.mtu.edu/techalum/images/feature-off.jpg',
				'alt': i + 1
			}).injectInside(item);
		}
	});
	
	// Add onclick ability to indicators
	$$('#featured div.feature div.indicator img').each(function (item, index) {
		item.addEvent('click', function() {
			new SimpleSlide('featured', {type: 'scroll', goTo: this.getProperty('alt').toInt(), duration: 600});
		});
	});
	
	
	// Add social bookmarking functionality
	$$('img.favorites').each(function(item, index) {
		item.addEvent('click', function() {
			//alert(location.protocol + '//' + location.host + location.pathname);
			var title = '';
			if (this.getParent().getParent().getElement('.articleTitle')) {
				title = this.getParent().getParent().getElement('.articleTitle').getText();
			}
			else {
				title = this.getParent().getParent().getParent().getElement('.articleTitle').getText();
			}
			var locat = location.protocol + '//' + location.host + location.pathname + '#' + this.getParent().getParent().getElement('a').getProperty('name');
			//alert(locat);
			if (window.ie) {
				window.external.addFavorite(locat, title);
			}
			else if (window.gecko) {
				window.sidebar.addPanel(title, locat, '');
			}
		});
	});
	$$('img.googleBookmarks').each(function(item, index) {
		item.addEvent('click', function() {
			var title = '';
			if (this.getParent().getParent().getElement('.articleTitle')) {
				title = this.getParent().getParent().getElement('.articleTitle').getText();
			}
			else {
				title = this.getParent().getParent().getParent().getElement('.articleTitle').getText();
			}
			var locat = location.protocol + '//' + location.host + location.pathname + '#' + this.getParent().getParent().getElement('a').getProperty('name');
		var a=window,c=encodeURIComponent,d=a.open("http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk="+c(locat)+"&title="+c(title),"bkmk_popup","left="+((a.screenX||a.screenLeft)+10)+",top="+((a.screenY||a.screenTop)+10)+",height=420px,width=550px,resizable=1,alwaysRaised=1");a.setTimeout(function(){d.focus()},300);
		});
	});
	$$('img.shareFriend').each(function(item, index) {
		item.addEvent('click', function() {
			var title = '';
			if (this.getParent().getParent().getElement('.articleTitle')) {
				title = this.getParent().getParent().getElement('.articleTitle').getText().clean();
			}
			else {
				title = this.getParent().getParent().getParent().getElement('.articleTitle').getText().clean();
			}
			var text = title;
			$ES('.articleText p', this.getParent().getParent()).each(function(item) {
				text += '%0A%0A';
				var newText = '';
				var temp = item.clone();
				if ($ES('a', temp).length > 0)  {
					$ES('a', temp).each(function(sub_item) {
						sub_item.replaceWith(new Element('span').setText(sub_item.getText() + ' (' + sub_item.getProperty('href') + ')'));
					});
				}
				if ($ES('br', temp).length > 0) {
					$ES('br', temp).each(function(sub_item) {
						sub_item.replaceWith(new Element('span').setText('BREAK'));
					});
				}
				newText += temp.getText().clean();
				text += escape(newText);
			});
			
			text = text.replace(/%u2014/g, "--");
			text = text.replace(/%u2013/g, "-");
			text = text.replace(/%u2019/g, escape("'"));
			text = text.replace(/BREAK/g, '%0A');
			text = text.replace(/%u201C/g, escape('"'));
			text = text.replace(/%u201D/g, escape('"'));
			text = text.replace(/%u2022/g, escape(' *'));
			text = text.replace(/%u00C9/g, escape('e'));
			if (text.length > 1600) {
				text = text.substr(0, 1600);
				if (text.substr(text.length-5, 5).indexOf('%') >= 0) {
					//alert(text.substr(text.length-5, 5).indexOf('%'));
					text = text.substr(0, text.length - 5 + text.substr(text.length-5, 5).indexOf('%'));
				}
				text += '...%0A%0ASee the full article at http://www.alumni.mtu.edu/techalum/';
			}
			
			text += '%0A%0A--------------------------------------------------------%0ATechAlum Newsletter: http://www.alumni.mtu.edu/techalum/%0ADennis Walikainen, Senior Editor, dkwalika@mtu.edu%0A';
			
			location.href = 'mailto:?subject=From%20the%20TechAlum%20Newsletter:%20' + escape(title) + '&body=' + text;
		});
	});
	$$('img.delicious').each(function(item, index) {
		item.addEvent('click', function() {
			var title = '';
			if (this.getParent().getParent().getElement('.articleTitle')) {
				title = $E('.articleTitle', this.getParent().getParent()).getText();
			}
			else {
				title = $E('.articleTitle', this.getParent().getParent().getParent()).getText();
			}
			var locat = location.protocol + '//' + location.host + location.pathname + '#' + this.getParent().getParent().getElement('a').getProperty('name');
			window.open('http://del.icio.us/post?v=4&noui&jump=close&url='+encodeURIComponent(locat)+'&title='+encodeURIComponent(title), 'delicious','toolbar=no,width=700,height=400');
		});
	});
	$$('img.facebook').each(function(item, index) {
		item.addEvent('click', function() {
			var title = '';
			if (this.getParent().getParent().getElement('.articleTitle')) {
				title = this.getParent().getParent().getElement('.articleTitle').getText();
			}
			else {
				title = this.getParent().getParent().getParent().getElement('.articleTitle').getText();
			}
			var locat = location.protocol + '//' + location.host + location.pathname + '#' + this.getParent().getParent().getElement('a').getProperty('name');
			window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(locat)+'&t='+encodeURIComponent(title),'sharer','toolbar=0,status=0,width=626,height=436');
		});
	});
	$$('img.digg').each(function(item, index) {
		item.addEvent('click', function() {
			var title = '';
			if (this.getParent().getParent().getElement('.articleTitle')) {
				title = this.getParent().getParent().getElement('.articleTitle').getText();
			}
			else {
				title = this.getParent().getParent().getParent().getElement('.articleTitle').getText();
			}
			var locat = location.protocol + '//' + location.host + location.pathname + '#' + this.getParent().getParent().getElement('a').getProperty('name');
			location.href = 'http://digg.com/submit?url='+encodeURIComponent(locat)+'&title='+encodeURIComponent(title)+'&media=news';
		});
	});
	
	
	new SimpleSlide('featured', {type: 'scroll', direction: 'forward', duration: 600, auto: 'loop', time: 5000});
});

window.addEvent('load', function() {
	// after images have loaded, make sure the huskylinkslot is not larger than the features
	var diff = $('huskylinkslot').getSize().size.y - $('featured').getSize().size.y;
	if ($('huskylinkslot').getSize().size.y > $('featured').getSize().size.y) {
		$('featured').setStyle('height', $('featured').getSize().size.y + diff - 2);
		$$('#featured div.feature div.caption').each(function(item) {
			item.setStyle('height', item.getSize().size.y + diff-1);
		});
	}
	
	if (location.hash) {
		location.hash = location.hash;
	}
});

// Sets the height of all necessary elements
function setCaption(height) {
	$('huskylinkslot').setStyle('height', height + imageHeight + 8);
	$('featured').setStyle('height', height + imageHeight + 16);
	$$('#featured div.feature div.caption').each(function(item) {
		item.setStyle('height', height);
	});
}