//document.observe("dom:loaded", function() {
//	Page.init();
//});

Event.observe(window, 'load', function() {
	Page.init();
});



var obj;
function checkObject(obj) {if (document.getElementById(obj)) {return true;} else {	return false;}}

var column1 = 0;
var column2 = 0;
var column3 = 0;
var w1 = '';
var w2 = '';
var nrinnerw = '';
var colOffset;

var testC = '0';

var Page = Class.create();
Page = {
	init: function(w1,w2) {
//		alert('test');
		Page.setWidth(w1,w2);
		if(typeof stationInit == 'function') {stationInit()} // check for station specific load scripts
		if(typeof pageInit == 'function') {pageInit()} // check for page specific load scripts
		
	},
	setWidth: function(w1,w2) {
		if(colOffset == null) {
			colOffset = 0;
		}
		if($("nrinner") == null) {
			return false;
		}
		//get widths of the things we need
		if(checkObject('c1')) {
			//Page.adjustColumns('c1');
			column1 = $('c1').getWidth();
		}
		if(checkObject('c3')) {
			//Page.adjustColumns('c3');
			column3 = $('c3').getWidth();
		}
		
		nrinnerw = $('nrinner').getWidth();
		if(w1 != null && w1 != '') {
			$('nrouter').setStyle({'width': w1})
			$('c2').setStyle({'width': w2})
		} else {
			$('c2').setStyle({'width': nrinnerw - column1 - column3 - colOffset});
		}
	},
	matchWidth: function(a,b) {
		var c = $(a).getWidth();
		if(c != null && c != '') {
			$(b).setStyle({'width': c})
		}
	},
	adjustColumns: function(column) {
		$(column).descendants().each(function(s) {
			if(parseInt(s.getWidth()) > parseInt($(column).getStyle('width'))) {
				$(column).setStyle({
					width: s.getWidth() + (s.cumulativeOffset().left - $(column).cumulativeOffset().left) * 2
				});
			}
		});
	}
}
//window.onresize = Page.setWidth();

var inputValue;
var inputNode;
var element;
var action;
function inputTextReplace(element,action,inputValue) {
	if(action == 'focus') {
		$(element).value = inputValue;
	} else {
		if($(element).value == '') {
			$(element).value = inputValue;
		}
	}
}

var state;
var hasClass;
var id;
var link;
function toggleId(id,link) {
	state = $(id).style.display;
	hasClass = $(link).className;
	
	if (state == '') {
		// $(id).hide();
		Effect.SlideUp(id);
		if(hasClass !="" && hasClass != null) {
			$(link).className = hasClass.substr(0,hasClass.length-6) + 'closed';
		}
	} else {
		// $(id).show();
		Effect.SlideDown(id);
		if(hasClass !="" && hasClass != null) {
			$(link).className = hasClass.substr(0,hasClass.length-6) + 'opened';
		}
	}
}

var field;
function checkAll(field) {
	for (i = 0; i < field.length; i++)
		field[i].checked = true ;
}

function uncheckAll(field) {
	for (i = 0; i < field.length; i++)
		field[i].checked = false ;
}

var copyURL;
var newwidth;
var newheight;
function popup(copyURL,newwidth,newheight){
  newwidth = newwidth + 15;
  newheight = newheight + 15;
  window.open(copyURL, 'newwin', 'toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,width=' + newwidth)
}

// TOOLTIP STUFF
var tipArray;var tt;var element;var c;

var Tip = Class.create();
Tip = {
	check: function(tipClass){
		var tipArray = $(document).getElementsByClassName(tipClass);
		if(tipArray.length > 0) {
			Tip.observe(tipArray);
		}
	},
	observe: function(tipArray) {
		for(i=0;i<tipArray.length;i++) {
			$($(tipArray[i])).observe('mouseover', Tip.show);
			$($(tipArray[i])).observe('mouseout', Tip.hide);
		}
	}, 
	create: function(element) {
		this.text = $(element).firstChild.innerHTML;
		this.container = new Element('div', {'class': 'tipcontainer' })
		this.container.appendChild(new Element('div', {'class': 'tc-top' }).update(new Element('span')));
		this.container.appendChild(new Element('div', {'class': 'tc-content' }).update(new Element('span').update(this.text)));
		this.container.appendChild(new Element('div', {'class': 'tc-bottom' }).update(new Element('span')));

		return this.container;
	},
	show: function(event) {
		element = event.element();
		this.container = Tip.create(element);
		this.position = Tip.position(event,this.container);
		$(element).appendChild(this.container);
	},
	hide: function(event) {
		element = event.element();
		$($(element).childNodes[$(element).childElements().length - 1]).remove();
	},
	position: function(event,container) {
		element = event.element();
	}
};

var Lightbox = Class.create();
Lightbox = {
	start: function(a) {
		Lightbox.open();
	},
	open: function() {
		b = $(document).firstChild;
		c = b.getDimensions();
		this.container = new Element('div', {'id': 'lightbox'});
		this.container.setStyle({'width': c.width});
		this.container.setStyle({'height': c.height});
		b.appendChild(this.container);
		b.lastChild.observe('click',Lightbox.close);
		Lightbox.content();
		$('lightbox').appendChild(this.content);
	},
	close: function(event) {
		$('lightbox').remove();
	},
	content: function() {
		this.content = new Element('div', {'id': 'lightboxcontent'});
		return this.content;		
	}
};

function image2caption() {
	if($('article-content')) {
		var aiImage = $('article-content').getElementsByClassName('article-image');
		var aiCaption = $('article-content').getElementsByClassName('caption');
		var aiCredit = $('article-content').getElementsByClassName('credit');
	
		if(aiCaption != null) {$(aiCaption[0]).setStyle({'width': aiImage[0].getWidth()})}
		if(aiCredit != null) {$(aiCredit[0]).setStyle({'width': aiImage[0].getWidth()})}
	}
}


// ELECTION TABS
function electionTab(tab,link) {
	for(i=0;i<2;i++) {
		if(i == tab){
			$('election_tab_content_tab' + i).style.display = 'block';
			$('election_tab_content_nav' + i).className = 'on';
		} else {
			$('election_tab_content_tab' + i).style.display = 'none';
			$('election_tab_content_nav' + i).className = 'off';
		}
	}
}


// Temporary
function articleMatchWidth() {
	var a = $$('img.article-image');
	var b = $$('span.caption');
	var c = $$('span.credit');
	
	$(b[0]).setStyle({width: $(a[0]).getWidth()});
	$(c[0]).setStyle({width: $(a[0]).getWidth()});

}


function electionInit() {

}