 
 /*
 * Common Javascript functions
 */
 

//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
}

 
 
/* parseUri 1.2; MIT License
By Steven Levithan <http://stevenlevithan.com> */

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*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


/* Sort array into a random order  */

function randOrd(){
	return (Math.round(Math.random())-0.5);
}

/* 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 trimToMaxCharWords() by Alex Kearns
*
* This function takes a string of words and returns a string of words that has a max char
* length of maxcharacters. Ie, it trims words off the end of the string so the string is
* below or equal to maxcharacters. It adds a "..." at end if words are trimmed
************************************************************************************************/
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;
}

 
 
 /*
 * Common Javascript called onload
 */


jQuery(document).ready(function() {

 /*
 *
 *  Attach off page content for larger screen resolutions
 *
 */
	
	// attach run off content
	$("div.ft08").before('<p id="run-off"><a href="/radio3/freethinking/2008/">Are you freer than you think?</a></p>');
	$("#run-off").hide();
	
	var wide_screen = 1002;
	var wider_screen = 1098;
	
	var prev_width = $(window).width();
		
	// inititial show
	if(prev_width > wider_screen) {
		$("#run-off")
			.attr("class", "run-off-large")
			.show();
	} else if (prev_width > wide_screen) {
		$("#run-off")
			.attr("class", "run-off-small")
			.show();
	}
	
	var off_page = setInterval(function() {
			
		var new_width = $(window).width();
		
		if(prev_width != new_width) {
			
			// show hide content
			if(new_width > wide_screen) {
				$("#run-off").show("slow");
			} else {
				$("#run-off").hide("slow");
			}
			
			// select small or large content
			if(new_width > wider_screen) {
				$("#run-off").attr("class", "run-off-large");
			} else if(new_width > wide_screen) {
				$("#run-off").attr("class", "run-off-small");
			}
		}	
		
		prev_width = new_width;
		
	}, 1000);
	
	
		

 /*
 *
 * Load random cartoon for homepage header
 *
 
	
	// create a random number for the cartoon
	function get_image_num() {
		var ranNum= Math.floor(Math.random()*6);
		return ranNum;
	}
	
	var bc = $("body").attr("class");
	
	if(bc.indexOf("home") != -1) {
		// assign the random class to body
		var p = "/radio3/freethinking/2007/assets/content/home/cartoons/";
		$("body").addClass("cartoon-"+get_image_num());
	} else {
	
	}
*/		
		
 /*
 *
 * Cycle through homepage intro copy
 *
 
		
		
	// cycle through intro copy
	
	var ifd = $("div.intro-fade");
	
	// add the more button
	$(ifd).after('<p class="link-text intro-fade-control"> <a id="intro-prev" href="#">&lt; Previous</a> &nbsp;<span id="intro-counter"></span>&nbsp; <a id="intro-next" href="#">Next &gt;</a></p>');
	
	var intro_counter = $("p", ifd);
	
	// make position absolute
	$("p", ifd).css("position","absolute");
	
	// show the irst paragrah
	$("p:first-child", ifd).css("display","block");
	
	var intro_fade_select = 0;
	
	$("span#intro-counter").text((intro_fade_select+1)+"/"+intro_counter.length);
	
	var intro_fade_animating = false;
	
	var intro_fade_paras = $("p", ifd).length;
	
	// show the irst paragrah
	$("a#intro-prev").click(function() {
	
			if(intro_fade_animating) {
				return;
			}
			
			intro_fade_animating = true;
			
			if($("p:eq("+intro_fade_select+")",ifd).prev().length > 0) {
				
				$("p:eq("+intro_fade_select+")",ifd).animate({ opacity : 0 }, function() {
					$("p:eq("+intro_fade_select+")",ifd).prev().css("opacity",0).css("display","block").animate({ opacity : 1 }, function() {
						
						$("p:eq("+intro_fade_select+")",ifd).next().css("display","block");
						intro_fade_select --;
						intro_fade_animating = false;
						$("span#intro-counter").text((intro_fade_select+1)+"/"+intro_counter.length);
					});
				});
				
			} else {
				
				
				
			
				$("p:eq("+intro_fade_select+")",ifd).animate({ opacity : 0 }, function() {
					$("p:eq("+(intro_fade_paras-1)+")",ifd).css("opacity",0).css("display","block").animate({ opacity : 1 }, function() {
						
						$("p:eq("+(intro_fade_paras-1)+")",ifd).css("display","block");
						intro_fade_select = (intro_fade_paras-1);
						intro_fade_animating = false;
						$("span#intro-counter").text((intro_fade_select+1)+"/"+intro_counter.length);
					});
				});
				
				
			}
			
			
			
			return false;
			
		});
		
	// show the first paragrah
	$("a#intro-next").click(function() {
	
			if(intro_fade_animating) {
				return;
			}
			
			
			
			intro_fade_animating = true;
			
			
			if($("p:eq("+intro_fade_select+")",ifd).next().length > 0) {
				
				$("p:eq("+intro_fade_select+")",ifd).animate({ opacity : 0 }, function() {
					$("p:eq("+intro_fade_select+")",ifd).next().css("opacity",0).css("display","block").animate({ opacity : 1 }, function() {
						
						$("p:eq("+intro_fade_select+")",ifd).next().css("display","block");
						intro_fade_select ++;
						intro_fade_animating = false;
						$("span#intro-counter").text((intro_fade_select+1)+"/"+intro_counter.length);
					});
				});
				
			} else {
			
				$("p:eq("+intro_fade_select+")",ifd).animate({ opacity : 0 }, function() {
					$("p:eq(0)",ifd).css("opacity",0).css("display","block").animate({ opacity : 1 }, function() {
						
						$("p:eq(0)",ifd).css("display","block");
						intro_fade_select = 0;
						intro_fade_animating = false;
						$("span#intro-counter").text((intro_fade_select+1)+"/"+intro_counter.length);
					});
				});
				
				
			}
			
			
			
			return false;
			
		});
*/	
	
	
 /*
 *
 * check resize for overalaps
 *
 */	
 
 	var ifd = $("div.intro-fade");
 
	if(ifd) {
		var fade_int = setInterval(function() {
		
			if($("p:eq(0)", ifd).height() > 51) {
				$("p", ifd)
					.css("position","relative")
					.css("display", "block")
					.css("opacity",1);
				$(".intro-fade-control").remove();
			} 
			
			
		}, 1000);
	}
	
	$("#newsletter-signup").addClass("non-resized-signup");
	
	var nl_int = setInterval(function() {
		
		if($("#newsletter-signup dd label").height() > 15) {
			
			$("#newsletter-signup").removeClass("non-resized-signup");
			
		} else {
			$("#newsletter-signup").addClass("non-resized-signup");
		}
		
		
	}, 1000);


	
 /*
 *
 * add graded stripes to the comments
 *
 */


	
		// add comment grades
		var c = $(".comment-text p");
		
		if(c) {
		
			$(c[0]).attr("class","comment-1");	
			$(c[1]).attr("class","comment-2");	
			$(c[2]).attr("class","comment-3");
		
		}

 /*
 *
 * homepage contributors carousel
 *
 */
 

 var ctr = $("#promo-contributors > ul > li");
	
	if(ctr.length > 0) {
		
		var aa = new Array();
		
		ctr.each(function() {
		
			var nl = $(this).clone();
			
			aa[aa.length] = nl;
		
		});
		
		aa.sort(randOrd);
		
		$("#promo-contributors > ul").empty().each(function() {
			
			for(var counter = 0; counter < aa.length; counter ++) {
				$(this).append(aa[counter]);
			}

		});
		
		var a_src;
		var i_src;
		var i_alt;
			
		// find the new dom objects
		var ctr = $("#promo-contributors > ul > li").each(function() {
		
			$("p.link-text", this).remove();
			$("p.disclaimer", this).remove();
			
			// get the anchor
			var contrib_anchor = "/radio3/freethinking/2008/contributors/#" + $(this).attr("id");
			
			// all the list items
			$(this).css("display","none");
			$(this).addClass("clearfix");
			
			// all the list items
			
			var contr_home_biog = $("p:eq(0)", this)//.not(".link-text");
			$(contr_home_biog).html(" "+trimToMaxCharWords($("p", this).text(), 150));
			
			$("p:eq(1), p:eq(2)", this).remove()
			
			if($("a.contributor-image", this).attr("href").indexOf("place-holder.jpg") != -1) {
				$(this).remove();
			}
			
			// get the image attributes
			a_src = $("a.contributor-image", this);
			i_src = $(a_src).attr("href");
			i_alt = $(a_src).text();
			
			// remove the link
			$(a_src).remove();
			
			
			$(this).prepend('<img class="contributor-image" src="'+i_src+'" alt="'+i_alt+'" />');
			$(this).append('<ul><li><p class="link-button next-button"><a href="#">Next contributor</a></p></li><li><p class="link-button prev-button"><a href="#">Previous contributor</a></p></li><li><p class="link-button"><a href="/radio3/freethinking/2008/contributors/">All Contributors</a></p></li></ul>')
			
			// assign links
			
			$("a.trim-more-link", this).css("display","inline").click(function() {
				window.location.href = contrib_anchor;
			});
			
			$("img.contributor-image", this).css("cursor","pointer").click(function() {
				window.location.href = contrib_anchor;
			});
			
			$("h3", this).css("cursor","pointer").click(function() {
				window.location.href = contrib_anchor;
			});
			
		});
		
		// create the image
		
		// show the first contribitor
		$(ctr[0]).css("display","block");
 		
 		
 		$("#promo-contributors > ul > li p.next-button a").click(function() {
			
			var pt = $(this).parent().parent().parent().parent();
			
			$(pt).hide();
			
			if($(pt).next().length > 0) {
				$(pt).next().css("display","block");
			} else {
				$(pt).parent().find("li:first-child").css("display","block");
			}
		
			return false;
		});
		
		$("#promo-contributors > ul > li p.prev-button a",this).click(function() {
			
			var pt = $(this).parent().parent().parent().parent();
			
			$(pt).hide();
			
			if($(pt).prev().length > 0) {
				$(pt).prev().css("display","block");
			} else {
				$(pt).parent().find("li:last-child").css("display","block");
			}
		
			return false;
			
		});
 		
 	}
 
 	
 	

 	
 	
 /*
 *
 * abc image replace
 *
 */
 
	var abc_li = $('#abc-childhood li a.abc-img-src');
	
	$(abc_li).each(function() {
		
		var abc_img_src = $(this).attr("href");
		var abc_img_alt = $(this).text();
		
		$(this).parent().prepend('<img src="'+abc_img_src+'" alt="'+abc_img_alt+'" />');
		
		// remove the link
		$(this).remove();
		
	});
	
	
 /*
 *
 * broadcasts table stripe
 *
 */	
	
	var broad_t = $(".broadcast-table");
	
	if(broad_t) {
		
		$("tr:nth-child(odd)", broad_t).addClass("odd");
 	
 	}
 	
 	
 /*
 *
 * embed this focus
 *
 */		
 
 
 	var embed = $("#embed-code textarea");
	
	if(embed) {
		
		$(embed).focus( function() { 			
			$(this).select();
		});
		
			$(embed).click( function() { 			
			$(this).select();
		})
 	
 	}
 	
 /*
 *
 * contributors list enhance
 *
 */

		var contribs_list = $("#contributor-list > ul");
		
		if(contribs_list.length > 0) {
		
			$("> li:nth-child(even)", contribs_list).addClass("even");
			$("> li:nth-child(odd)", contribs_list).addClass("odd");
			
			$(contribs_list).children(":nth-child(odd)").wrap('<li class="clearfix"><ul></ul></li>')
			
			var contribs_even = $(contribs_list).children(".even");
			
			$(contribs_even).each(function(){
				
				$(this).appendTo($(this).prev().children("ul"));
			
			});
			
			
			$("ul > li", contribs_list).each(function() {
			
				a_src = "";
				i_src = "";
				i_alt = "";
												
				// get the image attributes
				a_src = $("a.contributor-image", this);
				i_src = $(a_src).attr("href");
				i_alt = $(a_src).text();
				
				// remove the link
				$(a_src).remove();
				
				
				$(this).prepend('<img src="'+i_src+'" alt="'+i_alt+'" />');			
			
			});
			
			// get url
			var l = window.location.href;
			// find anchor
			var contrib_anchor = parseUri(l).anchor;
			
			// double check it anchors down to the correct position
			if (contrib_anchor) {
				window.scrollTo(0,findPos($("#"+contrib_anchor).get()[0])[1]);
			}
			
			
		
		}

	
});

