 
/*********************************************************
 *
 * Common Javascript functions
 *
**********************************************************/


// detct if browser is IE
var isIE = $.browser.msie;

// attach style sheet to the head of document; only executed by JS enabled browsers

var headID = document.getElementsByTagName("head")[0];

var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.media = 'screen';
cssNode.href = '/radio4/worldonthemove/assets/css/js.css';

headID.appendChild(cssNode);


if(isIE) { 

	var cssNodeIE = document.createElement('link');
	cssNodeIE.type = 'text/css';
	cssNodeIE.rel = 'stylesheet';
	cssNodeIE.media = 'screen';
	cssNodeIE.href = '/radio4/worldonthemove/assets/css/js-ie.css';

	headID.appendChild(cssNodeIE);
}


/* 
 *	FUNCTION convert anchors to images using the href
 *
 ***************************/



function anchors_to_images(i, a, w, h) {

	$(i).each(function() {
		
		var img_src = $(a, this).attr("href");
		var img_alt = $(a, this).text();
		
		if(img_src) {
				
			$(this).prepend('<img src="'+img_src+'" width="'+w+'" height="'+h+'" alt="'+img_alt+'" />');
			
			// remove the link
			$(a, this).remove();
		
		}
		
	});

}


/* 
 *	FUNCTION match height of elements
 *
 ***************************/


function match_highest(match, target) {
	
	var max = 0;
		
	$(match).each(function() { 
		
		if($(this).height() > max) {
			max = $(this).height()+parseInt($(this).css('padding-top'))+parseInt($(this).css('padding-bottom'));
		}
				
	});
	
	if(isIE) {
		$(target).css("height",max+"px");
	} else {
		$(target).css("min-height",max+"px");
	}
		
}


/* 
 *	FUNCTION open the guides window
 *
 ***************************/

function openGuide(url) {
	var guidewin = openWindow(url,'popup',618,450,'resizable=no,status=no,menubar=no,toolbar=no,scrollbars=yes,left=250,top=150,screenX=250,screenY=150');
	return false
}


 
/* 
 *	FUNCTION parseUri 
 *
 ***************************/

var parseUri = function (source) {
	var o = parseUri.options,
		value = o.parser[o.strictMode ? "strict" : "loose"].exec(source);
	
	for (var i = 0, uri = {}; i < 14; i++) {
		uri[o.key[i]] = value[i] || "";
	}
	
	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});
	
	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q: {
		name: "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


/* 
 *	FUNCTION Sort array
 *
 ***************************/


// no function yet


/* 
 *	FUNCTION Sort array into a random order
 *
 ***************************/

function randOrd(){
	return (Math.round(Math.random())-0.5);
}



/* 
 *	FUNCTION Find an elements X & Y offsets
 *
 ***************************/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	
	return [curleft,curtop];
}




/* 
 * FUNCTION	Trim to max chars
 *
 ***************************/

var trimToMaxCharWords = function(wordstring,maxcharacters) {
    var wordarray = wordstring.split(" ");
    var numwords=wordarray.length;
    var newstring="";
    var laststring="";
    for (var counter=0; counter<numwords; counter++) {
        laststring=newstring
        if (counter!=0) {
            newstring+=(" "+wordarray[counter]);
        }
        else {
            newstring+=wordarray[counter];
        }
        if (newstring.length>maxcharacters) {
            return (laststring + '... <a href="#" class="trim-more-link">full biog</a>');
        }
    }
    return newstring;
}


