// Get array of elements by an arbitrary property
// optional args: parentNode (node), singleResult (boolean)
function getElementsBy(attr, val){
	var parentNode = (arguments.length > 2) ? arguments[2] : document.body;
	var singleResult = (arguments.length > 3) ? arguments[3] : false;
	if (!parentNode.getElementsByTagName){
		return false;
	}
	var elements = [];
	var nodes = parentNode.getElementsByTagName('*');
	for (var i=0; i<nodes.length; i++){
		if (typeof nodes[i][attr] == 'string'){
			if (nodes[i][attr].match(new RegExp("(^|\\s)" + val + "(\\s|$)"))){
				elements.push(nodes[i]);
				if (singleResult){
					break;
				}
			}
		}
	}
	return elements;
}



function getOffset(e, offsetType){
	var h = Number.NaN;
	if (document.getElementById) {
		if (typeof e === 'string'){
			e = document.getElementById(e);
		}
		
		if (e){
			var offset = (offsetType === 'width') ? e.offsetWidth : e.offsetHeight;		
			if (typeof offset !== 'undefined') {
				h = e.offsetHeight;
			}
		}
	}
	return h;
}


// h=height, y=vertical-offset; in px
var cables = {
	h:1380,
    y:-52,
	yShadow:14,
    left:{
        h:527,
        y:0
    },
    right:{
        h:490,
        y:739
    },
	plug:{
		h:590	
	}
};


function resizeHfeed(){
	// Where position-anywhere CSS fails, JS takes over...
	var section1, section2, results, twitter, twitterHeight, hfeed;
	section1 = document.getElementById('section1');
	theSubways = document.getElementById('the-subways');
	twitterGoomusic = getElementsBy('className', 'twitter', section1, true)[0];
	twitterGoomusicHentry = getElementsBy('className', 'hentry', twitterGoomusic, true)[0];
	twitterSubways = getElementsBy('className', 'twitter', theSubways, true)[0];
	twitterHeight = Math.max(getOffset(twitterGoomusicHentry, 'height'), getOffset(twitterSubways, 'height'));
	hfeed = getElementsBy('className', 'hfeed', document.body, true)[0];
	twitterGoomusic.style.height = (twitterHeight + 130) + 'px';
	hfeed.style.paddingTop = (twitterHeight - 30) + 'px';
	return true;
}


function positionCables(){
	var b = document.body;
	
	var docHeight = getOffset(b, 'height');
	if (!docHeight){
		return false;	
	}
	var fullRepeats = Math.floor((docHeight - cables.y - cables.yShadow) / cables.h);
	var fullRepeatsHeight = (fullRepeats * cables.h) + cables.y + cables.yShadow;
	
	var nextCableSide = 'left';
	var cutBody = fullRepeatsHeight - (cables.h - cables.right.h - cables.right.y);	
	
	if (fullRepeatsHeight + cables.left.h + cables.left.y < docHeight) {
		nextCableSide = 'right';
		cutBody = fullRepeatsHeight + cables.left.h + cables.left.y;
	}
	if (fullRepeatsHeight + cables.right.h + cables.right.y < docHeight) {
		nextCableSide = 'left';
		cutBody = fullRepeatsHeight + cables.right.h + cables.right.y;
	}
	
	// CSS for Body element
	b.className = 'js cable-' + nextCableSide;
	b.style.backgroundPosition = nextCableSide + ' ' + (cutBody - cables.plug.h + 70) + 'px';
	
	// New element: 'cables' => CSS
	var e = document.getElementById('cables');
	if (!e){
		e = document.createElement('div');
		e.id = 'cables';
	}
	e.style.height = cutBody + 'px';
	
	
	b.appendChild(e);
	return true;
}
resizeHfeed();
positionCables();




// =====================================



/** 
 *  @fileoverview TextResizeDetector
 *  See http://www.alistapart.com/articles/fontresizing
 * 
 *  Detects changes to font sizes when user changes browser settings
 *  Fires a custom event with the following data:
 * 	iBase  : base font size  	
 *	iDelta : difference in pixels from previous setting
 *  iSize  : size in pixel of text
 *  
 *  @author Lawrence Carvalho carvalho@uk.yahoo-inc.com
 *  @version 1.0
 */

/**
 * @constructor
 */
var TextResizeDetector = function() { 
    var el  = null;
	var iIntervalDelay  = 200;
	var iInterval = null;
	var iCurrSize = -1;
	var iBase = -1;
 	var aListeners = [];
 	var createControlElement = function() {
	 	el = document.createElement('span');
		el.id='textResizeControl';
		el.innerHTML='&nbsp;';
		el.style.position="absolute";
		el.style.left="-9999px";
		var elC = document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID);
		// insert before firstChild
		if (elC)
			elC.insertBefore(el,elC.firstChild);
		iBase = iCurrSize = TextResizeDetector.getSize();
 	};

 	function _stopDetector() {
		window.clearInterval(iInterval);
		iInterval=null;
	};
	function _startDetector() {
		if (!iInterval) {
			iInterval = window.setInterval('TextResizeDetector.detect()',iIntervalDelay);
		}
	};
 	
 	 function _detect() {
 		var iNewSize = TextResizeDetector.getSize();
		
 		if(iNewSize!== iCurrSize) {
			for (var 	i=0;i <aListeners.length;i++) {
				aListnr = aListeners[i];
				var oArgs = {  iBase: iBase,iDelta:((iCurrSize!=-1) ? iNewSize - iCurrSize + 'px' : "0px"),iSize:iCurrSize = iNewSize};
				if (!aListnr.obj) {
					aListnr.fn('textSizeChanged',[oArgs]);
				}
				else  {
					aListnr.fn.apply(aListnr.obj,['textSizeChanged',[oArgs]]);
				}
			}

 		}
 		return iCurrSize;
 	};
	var onAvailable = function() {
		
		if (!TextResizeDetector.onAvailableCount_i ) {
			TextResizeDetector.onAvailableCount_i =0;
		}

		if (document.getElementById(TextResizeDetector.TARGET_ELEMENT_ID)) {
			TextResizeDetector.init();
			if (TextResizeDetector.USER_INIT_FUNC){
				TextResizeDetector.USER_INIT_FUNC();
			}
			TextResizeDetector.onAvailableCount_i = null;
		}
		else {
			if (TextResizeDetector.onAvailableCount_i<600) {
	  	 	    TextResizeDetector.onAvailableCount_i++;
				setTimeout(onAvailable,200)
			}
		}
	};
	setTimeout(onAvailable,500);

 	return {
		 	/*
		 	 * Initializes the detector
		 	 * 
		 	 * @param {String} sId The id of the element in which to create the control element
		 	 */
		 	init: function() {
		 		
		 		createControlElement();		
				_startDetector();
 			},
			/**
			 * Adds listeners to the ontextsizechange event. 
			 * Returns the base font size
			 * 
			 */
 			addEventListener:function(fn,obj,bScope) {
				aListeners[aListeners.length] = {
					fn: fn,
					obj: obj
				}
				return iBase;
			},
			/**
			 * performs the detection and fires textSizeChanged event
			 * @return the current font size
			 * @type {integer}
			 */
 			detect:function() {
 				return _detect();
 			},
 			/**
 			 * Returns the height of the control element
 			 * 
			 * @return the current height of control element
			 * @type {integer}
 			 */
 			getSize:function() {
	 				var iSize;
			 		return el.offsetHeight;
		 		
		 		
 			},
 			/**
 			 * Stops the detector
 			 */
 			stopDetector:function() {
				return _stopDetector();
			},
			/*
			 * Starts the detector
			 */
 			startDetector:function() {
				return _startDetector();
			}
 	}
 }();




function initTextResizeDetector()  {
   var iBase = TextResizeDetector.addEventListener(onFontResize,null);
}
function onFontResize(e,args) {
	// available props:
	// Base font size (px) = args[0].iBase
	// Current font size (px) = args[0].iSize
	// Change from last size (px) = args[0].iDelta;
	resizeHfeed();
	positionCables();
	
}
//id of element to check for and insert control
TextResizeDetector.TARGET_ELEMENT_ID = 'form';
//function to call once TextResizeDetector has init'd
TextResizeDetector.USER_INIT_FUNC = initTextResizeDetector;