//Height Balance
//Normalizes the height of the elements passed in,
//based on the number of columns desired
function heightBalance(balanceElements, columns){
	balanceElements.eachSlice(columns, function(balanceElementSlice){
		var sliceHeight = balanceElementSlice.max(function(sliceElement){
			return sliceElement.getHeight();
		});
		
		balanceElementSlice.each(function(balanceElement){
			balanceElement.setStyle({
				height: sliceHeight + 'px'
			});
		});
	});
}

//Handle post organization.
function listingSorter(){
	var posts = $$('div.retiredPost');	
	heightBalance(posts, 2);
	
	var supportNav = $$('#related > ul.supportNav > li');
	heightBalance(supportNav, 2);
}

//First and Last LI Selector
//Note: Prototype Driven
function liFirstLast() {
	var firstLIs =	$$('ul > li:first-child');
	var lastLIs = $$('ul > li:last-child');
	
	firstLIs.each(function(liFirst) {
		liFirst.addClassName('first');
		});
		
	lastLIs.each(function(liLast) {
		liLast.addClassName('last');
	});
}

//Odd Posts - they need special fall-back style, so this is how it's done.
function labelOddPosts() {
	var oddPosts = $$('#main .post:nth-child(2n+1)');
	
	oddPosts.each(function(oddPost){
		oddPost.addClassName('odd');
	});	
}

//Input Clear
//Clears text inputs on a page on focus
//Note: Prototype driven
function inputClear() {
	var textInputs = $$('input[type="text"]');
	
	textInputs.each(function(textInput){
		textInput.initialValue = textInput.value;
		textInput.observe('focus', function(event) {
			if(textInput.value == textInput.initialValue){
				textInput.clear();
			}
		});
		textInput.observe('blur', function(event){
			if(textInput.value.blank() == true) {
				textInput.value = textInput.initialValue;
			}
		});
	});
}

// Cookie Functions
// Set the cookie 
function setCookie(name,value,days) { 
	if (days) { 
		var date = new Date(); 
		date.setTime(date.getTime()+(days*24*60*60*1000)); 
		var expires = ";expires="+date.toGMTString(); 
	} else { 
		expires = ""; 
	} 
	document.cookie = name+"="+value+expires+";"; 
}

// Read the cookie 
function readCookie(name) { 
	var needle = name + "="; 
	var cookieArray = document.cookie.split(';'); 
	for(var i=0;i < cookieArray.length;i++) { 
		var pair = cookieArray[i]; 
		while (pair.charAt(0)==' ') { 
			pair = pair.substring(1, pair.length); 
		} 
		if (pair.indexOf(needle) == 0) { 
			return pair.substring(needle.length, pair.length); 
		} 
	} 
	return null; 
}


//Subtle opacity fluctuation between main area and side bar.
function opacityFlux(){
	var main = $('main');
	var related = $('related');
	
	if(!related) return false;
	
	related.setOpacity(.25);			
	
	related.observe('mouseenter', function(event){
		new Effect.Tween(related, .25, 1, {
			duration: .2
		}, function(value){
			this.setOpacity(value);
		});		
		new Effect.Tween(main, 1, .5, {
			duration: .7
		}, function(value){
			this.setOpacity(value);
		});	
	});
	related.observe('mouseleave', function(event){
		new Effect.Tween(related, 1, .25, {
			duration: .7
		}, function(value){
			this.setOpacity(value);
		});		
		new Effect.Tween(main, .5, 1, {
			duration: .2
		}, function(value){
			this.setOpacity(value);
		});	
	});
}

//Opacity Flux, smarter and now using Scripty 2 instead of Scriptaculous.
function opacityFluxBeta(){
	var main = $('main');
	var related = $('related');
	var opacitySolid = 1;
	var opacityFaded = .25;
	
	if(!related) return false;
	
	related.morph('opacity:' + opacityFaded);
	
	related.observe('mouseenter', function(event){
		related.morph('opacity:' + opacitySolid + '', {
			duration: .25,
			transition: 'easeInOutExpo'
		});
		main.morph('opacity:' + opacityFaded + '', {
			duration: .25,
			transition: 'easeInOutExpo'
		});
	});
	
	related.observe('mouseleave', function(event){
		related.morph('opacity:' + opacityFaded + '', {
			duration: .25,
			transition: 'easeInOutExpo'
		});
		main.morph('opacity:' + opacitySolid + '', {
			duration: .25,
			transition: 'easeInOutExpo'
		});
	});
}

//Replacement for Window Onload - Loads before images, cross-browser
document.observe("dom:loaded", function() {
	//dynamicShadow('/images/global/shadow.png', 'page-container', 16, 0);
	liFirstLast(); // Adds classes 'first' and 'last' to respective LIs
	inputClear();
	listingSorter();
	labelOddPosts();	
	getTwitters('tweet', { 
	  id: 'optikinescant', 
	  count: 3, 
	  enableLinks: true, 
	  ignoreReplies: true, 
	  clearContents: true,
	  template: '<p><strong>"%text%"</strong> <a href="http://twitter.com/%user_screen_name%/statuses/%id%/" class="timestamp">%time%</a></p>'
	});
	opacityFluxBeta();
});
