/**
Site Manager Global JS Script
Use this to initialize functionality inside the page
*/

/*Tipz in Mootools 1.1:

window.addEvent('domready', function(){
	var thetip = new Tips($$('.tipz'), {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
}); 
*/


/**
Function: fixIE6Hover
Adds "over" class to menu on mouse over for IE6
*/

function fixIE6Hover() {
	if (document.all&&document.getElementById) {
		var lis = $A($('navbar').getElementsByTagName('li'));
		lis.push($('install'));
		for (i=0; i < lis.length; i++) {
			node = lis[i];
			node.onmouseover=function() {
				this.className+=" over";
			}
			node.onmouseout=function() {
				this.className=this.className.replace(" over", "");
			}
		}
	}
}

window.onerror = function(){
}
/**
Function: addTips
Adds Tooltips to UI
*/


/* In Mootools 1.2 - */

function addTips(){
	//store titles and text  
    $$('a.tipz').each(function(element,index) {  
        var content = element.get('title').split('::');  
        element.store('tip:title', content[0]);  
        element.store('tip:text', content[1]);  
    });  
      
    //create the tooltips  
    var tipz = new Tips('.tipz',{  
        className: 'tipz',  
        hideDelay: 5,
        showDelay: 1000  
    });  
   
   
	//fade tips
	tipz.addEvents({
		'show': function(tip) {
			tip.fade('in');
		},
		'hide': function(tip) {
			tip.fade('out');
		}
	});
}

function adjustWidth(){
	//checks to see if each element exists, if it does it takes it's width otherwise, it returns 0
	var firstPaneSize = ($('first_pane') && $('first_pane').getSize().x) || 0;
	var newsPaneSize = ($('news_pane') && $('news_pane').getSize().x) || 0;
	var entryContentSize = ($('entry-content') && $('entry-content').getSize().x) || 0;
	var projectArchiveSize = ($('projects_archive') && $('projects_archive').getSize().x) || 0;
	var newsArchiveSize = ($('news_archive') && $('news_archive').getSize().x) || 0;
	var shualinksArchiveSize = ($('shualinks_archive') && $('shualinks_archive').getSize().x) || 0;
	var relevantSize = ($('relevant') && $('relevant').getSize().x) || 0;
	var moreTagsSize = ($('moretags') && $('moretags').getSize().x) || 0;
	var footerSize = ($('footer') && $('footer').getSize().x) || 0;
	//adds them all up
	var calcwidth = firstPaneSize + newsPaneSize + entryContentSize + projectArchiveSize + newsArchiveSize + shualinksArchiveSize + relevantSize + moreTagsSize + footerSize + 60; //+60 for the margins
	if (calcwidth<window.innerWidth-60){
		calcwidth = window.innerWidth-60;
	} 
	//sets wrapper to the calculated size
	$('wrapper').setStyle('width', calcwidth);
}

function scrolling() {
	var scrollCount = 0;
	
	var scroll = new Fx.Scroll(window, {
			wait: false,
			duration: 500,
			offset: {'x': -60, 'y': -60},
		});
	
	$('imgnext').addEvent('click', function(event) {
		event.stop();
		scrollCount++;
		scroll.toElement('anchor-'+scrollCount);

	});
	
	$('imgprev').addEvent('click', function(event) {
		if(scrollCount>0){
			event.stop();
			scrollCount--;
			scroll.toElement('anchor-'+scrollCount);
		} else {
			event.stop();
			scrollCount=-1;
			scroll.toElement('blog-title');
		}
	});
}

/**
Function: initSite()
	Global function that is called onDomReady event
	Use this function to initialize global behaviour for the site
*/

function initSite(){
	//call addTips
	//addTips();
	//call the wrapper width adjustment
	adjustWidth();
	//calls scrolling
	scrolling();
	//fix IE6 hover for menu
	if(Browser.Engine.trident4)
		fixIE6Hover();
}

//call initSite when DOM is ready
window.addEvent('domready',initSite);