
$(document).ready(function(){	
						   
	if (document.location.href.indexOf("http://www") == -1 ){
		home_page = "http://ciut.fm/";		
	} else {
		home_page = "http://www.ciut.fm/";		
	}
						   
	//home_page = "http://www.ciut.fm/";
	m33_theme_dir = home_page + "wp-content/themes/m33_CIUT/";
	m33_CIUT_xml_file = m33_theme_dir + "m33_CIUT_xml.xml"	;
	
	//dave=$.get(m33_CIUT_xml_file);
	//alert(dave.responseXML);
	
	get_topnav_graphics();		
	do_shows_menu();
	
	do_tickets();
	
	//need to filter out if it's the homepage or not -- don't want to call these if it's not the homepage
	////////////////////////////////////////////////////////////////	
	do_homepage_content_box_scrollers("all");
	
	ajax_gets();////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////
	
	do_show_img();//can't really figure out what's a show page via javascript...so this just stays in all the time (I could maybe put it in the markup?)
		
	pre_load_over_imgs();
	
});



function do_side_bar_tabs(){

if ($.browser.msie){

	$('#m33_sidebar').accordion({	
		header: '.widgettitle',
		event: 'click',
		fillSpace: false,
		animated: 'easeslide'
	});		

} else {

	$('#m33_sidebar').accordion({	
		header: '.widgettitle',
		event: 'click',
		fillSpace: false,
		animated: 'easeslide',
		autoheight: false
	});	
	
	$(".textwidget").append("<br/><br/><br/>");
}

	var side_bar_tabs = $("#m33_sidebar li.widget h2");

	for (var i=0; i < side_bar_tabs.length; i++){
		$(side_bar_tabs[(side_bar_tabs.length - 1) - i]).css("z-index", i);
	}	
	
	$("#sidebar_top10_ul").parent(".textwidget").css("margin-left", "0px");
	$("#sidebar_top10_ul").parent(".textwidget").css("width", "152px");
	
	if ($.browser.msie){
		$("#sidebar_top10_ul").css("margin-left", "35px");
	} else {
		$("#sidebar_top10_ul").css("margin-left", "31px");
	}
	
}

function get_topnav_graphics(){
	
	var top_nav_items_wp = $("#top_nav_items li a");		

	$.get(m33_CIUT_xml_file, function (xml) {

		$("top_nav_item", xml).each(function (i) {
			var top_nav_item_name = new Array();
			var top_nav_item_icon_location = new Array();
			
			top_nav_item_name[i] = $(this).attr("item_name");
			top_nav_item_icon_location[i] = home_page + $(this).attr("item_icon_location");				

			for (j = 0; j < top_nav_items_wp.length; j++){
				if (top_nav_items_wp[j].innerHTML == top_nav_item_name[i]){
					$(top_nav_items_wp[j]).prepend("<img class='top_nav_img' src='"+top_nav_item_icon_location[i]+"'/>")
				}
			}

		});
		
	});		
}

function do_shows_menu(){
	var shows_array = $("#top_nav_items li:eq(1) ul li");
	
	var shows_table = "<table id='shows_menu_table'>"
	for (var b = 0; b < shows_array.length; b++){
		if (b == 0 || (b/4) % 1 == 0 ){
			shows_table+="<tr>";	
		}
		shows_table+="<td>";
		shows_table+= shows_array[b].innerHTML;
		shows_table+="</td>";
		if ( ((b+1)/4) % 1 == 0 || b == shows_array.length) {
			shows_table+="</tr>";
		}
	}
	shows_table+="<tr><td COLSPAN='4' id='dont_go' style='background-color:#333;' align='right'><span id='shows_menu_hide'>Hide &#187;</span></td></tr></table>";
	
	$("#page").prepend(shows_table);


	if ($("#shows_menu_table tr").length % 2 != 0){
		$("#shows_menu_table tr:even").css("background-color", "#444");
	} else {
		$("#shows_menu_table tr:odd").css("background-color", "#444");
	}
	
	$("#shows_menu_table").css("opacity", "0.98");

	$("#shows_menu_table td").bind("click", function(e){
		if($(this).attr("id")!='dont_go'){
			window.open($(this).children("a").attr("href"));
			//window.location.href =$(this).children("a").attr("href");			
		} else {
			$("#shows_menu_table").fadeOut("fast");
		}
	});

	$("#top_nav_items li:eq(1) a").attr({
		href: "javascript:void(0);"
	});
	
	$("#top_nav_items li:eq(1) a").bind("click", function(e){
		if ($("#shows_menu_table").css("display") == "none"){
			$("#shows_menu_table").fadeIn("fast");			
		}
	});
	
	$("#top_nav_items li:eq(0) a").attr({
		href: home_page
	});	
	
	
}



function do_homepage_content_box_scrollers(which_one){

	if (!$.browser.msie){
		$("#take5_homepage_box_master_wrapper").css({"margin-top": "6px"});
	}

	if (which_one=="1" || which_one=="all"){
		$("#ciut_homepage_box_post_controller").jFlow({
			slides: "#ciut_homepage_box_post",
			controller: ".jFlowControl", // must be class, use . sign
			slideWrapper : "#jFlowSlide", // must be id, use # sign
			selectedWrapper: "jFlowSelected",  // just pure text, no sign
			width: "100%",
			height: "235px",
			duration: 400,
			prev: ".jFlowPrev", // must be class, use . sign
			next: ".jFlowNext" // must be class, use . sign
		});	
	}
	
	
	if (which_one=="2" || which_one=="all"){	
		$("#take5_homepage_box_post_controller").jFlow({
			slides: "#take5_homepage_box_post",
			controller: ".jFlowControl2", // must be class, use . sign
			slideWrapper : "#jFlowSlide2", // must be id, use # sign
			selectedWrapper: "jFlowSelected2",  // just pure text, no sign
			easing: "swing",
			duration: 400,
			height: "300px",
			width: "381px",
			prev: ".jFlowPrev2", // must be class, use . sign
			next: ".jFlowNext2" // must be class, use . sign
		});
	}
	if (which_one=="3" || which_one=="all"){	
		$("#take5_homepage_box_feature_controller").jFlow({
			slides: "#take5_homepage_box_feature",
			controller: ".jFlowControl3", // must be class, use . sign
			slideWrapper : "#jFlowSlide3", // must be id, use # sign
			selectedWrapper: "jFlowSelected3",  // just pure text, no sign
			easing: "swing",
			duration: 400,
			height: "300px",
			width: "381px",
			prev: ".jFlowPrev3", // must be class, use . sign
			next: ".jFlowNext3" // must be class, use . sign
		});	
	}
}

function ajax_gets (){

	$.get(m33_CIUT_xml_file, function (xml) {

		$.get(home_page + $("charts", xml).text(), function(data){
			$("#sidebar_top10_ul").html($(data).find(".charts_page ol li:lt(10)"));
			$("#sidebar_top10_ul li strong").append("<br /");
			do_side_bar_tabs();
			$("#sidebar #sidebar_loader").css("visibility", "hidden");			
			$("#sidebar #sidebar_loader").css("display", "none");
			$("#sidebar div").css("visibility", "visible");
			$("#sidebar #side_bar_shadow_top").css("visibility", "visible");
			$("#sidebar ul").css("visibility", "visible");
			$("#sidebar span").css("visibility", "visible");
			if (document.location.toString().indexOf("/shows-2/take-5/") == -1 ) {set_side_bar_height();}
			
		});	

		$.get(home_page + $("featured_show", xml).text(), function(data){
			$("#bottom_homepage_box_featured_show_content").html($(data).find("#content .post").html());
			$("#bottom_homepage_box_featured_show_content p.audioplayer_container").html($(data).find("#content .post p.audioplayer_container").html());
			$("#bottom_homepage_box_featured_show_content h2:eq(0)").wrap("<a class='feat_show_anchor' href='"+$("featured_show", xml).text()+"'></a>");
			$("#bottom_homepage_box_featured_show_content img").css("display", "none");
		});
		
		$.get(home_page + $("special_programming", xml).text(), function(data){
			$("#bottom_homepage_box_special_programming_content").html($(data).find("#content .post").html());
			//$("#bottom_homepage_box_special_programming_content .entry h2:eq(0)").wrap("<a class='spec_prog_anchor' href='"+$("special_programming", xml).text()+"'></a>");
		});
			
	});	
	
	if (document.location.toString().indexOf("/shows-2/take-5/") != -1 ) {
		$("#show_page_content_wrapper .post h2:eq(0)").css("visibility", "hidden");		
		$.get(home_page, function(data){
			$("#content").append("<div id='take5_homepage_box_master_wrapper'>" + $(data).find("#take5_homepage_box_master_wrapper").html() + "</div>");
			$("#take_5_heading_img").css("top", "245px");
			$("#take_5_heading_img").css("left", "30px");
			$("#take_5_heading_right_text").css("display", "none");
			$("#take5_homepage_box_heading").append("<span style='float:left; margin:5px 0px 0px 12px; font-weight:bold; font-size:1.4em;'>RECENT NEWS</span>");
			do_homepage_content_box_scrollers("2");
			do_homepage_content_box_scrollers("3");
			set_side_bar_height();
		});
	}
	
}

function do_show_img(){
	$("#show_page_content_wrapper #show_img").attr("src", $("#show_page_content_wrapper .entry img").attr("src"));
}

function do_tickets (){
	$("#donate_today_hit").bind("mouseover", function(e){
		$("#donate_today").attr("src", m33_theme_dir+"images/donate_today_over.png");
	});
	$("#donate_today_hit").bind("mouseout", function(e){
		$("#donate_today").attr("src", m33_theme_dir+"images/donate_today.png");
	});	
	
	$("#donate_today_hit").css("opacity", "0");		
	$("#donate_today_hit").css("background-color", "black");
	
	$("#listen_live_hit").css("opacity", "0");		
	$("#listen_live_hit").css("background-color", "black");	
	
	$("#listen_live_hit").bind("mouseover", function(e){
		$("#listen_live").attr("src", m33_theme_dir+"images/listen_live_over.png");
	});
	$("#listen_live_hit").bind("mouseout", function(e){
		$("#listen_live").attr("src", m33_theme_dir+"images/listen_live.png");
	});	
	
	$("#donate_today_hit").bind("click", function(e){
		window.location.href = home_page + "index.php/about/membership/";
	});

	$("#listen_live_hit").bind("click", function(e){
		window.location.href = home_page + "index.php/about/listen-live/";
	});	
}



function set_side_bar_height(){
	
	if ($.browser.msie){
		$("#content").css("height", $("#content").height()+8);	
	}
	
	if ($("#content").height() > $("#sidebar").height()){
		$("#sidebar").css("height", $("#content").height()+9);
	} else {
		if ($.browser.msie){
			$("#content").css("height", $("#sidebar").height()+38);
		} else {
			$("#content").css("height", $("#sidebar").height()+30);
		}
		$("#sidebar").css("height", $("#content").height()+9);
	}
	
	$("#side_bar_shadow").css("height", $("#content").height()-56);
}

function pre_load_over_imgs(){
	var location_of_over_imgs=m33_theme_dir+"images/";
	var img1=new Image();
	img1.src=location_of_over_imgs + "side_bar_slide_heading_bg_over.png";
	var img2=new Image();
	img2.src=location_of_over_imgs + "ciut_post_box_next_over.png";
	var img3=new Image();
	img3.src=location_of_over_imgs + "ciut_post_box_prev_over.png";
	var img4=new Image();
	img4.src=location_of_over_imgs + "donate_today_over.png";
	var img5=new Image();
	img5.src=location_of_over_imgs + "listen_live_over.png";	
}





/////////////////////////////////////////////////////////////////////////////////
////////// Legacy Javascript to do with donation form that seems to work, so I'm leaving it in ///////////

if (document.location.toString().indexOf("membership-form") != -1 ) {

$(document).ready(function(){turnOffGifts(); });//this call was previously in a body onLoad statement in the old CIUT site

function otherAmount(){
	document.orderForm.txtAmount.disabled=false;
	turnOnGifts();
}

function turnOffGifts(){
	for (i=0, n=document.orderForm.gift.length; i<n; i++) {
		document.orderForm.gift[i].disabled=true;
	}
	for (j=0, m=document.orderForm.news.length; j<m; j++) {
		document.orderForm.news[j].checked=false;
	}
}
function turnOnGifts(){
	for (i=0, n=document.orderForm.gift.length; i<n; i++) {
		document.orderForm.gift[i].disabled=false;
	}
	for (j=0, m=document.orderForm.news.length; j<m; j++) {
		document.orderForm.news[j].checked=false;
	}
}

function turnOnOff2(o){
	for (i=0, n=document.orderForm.news.length; i<n; i++) {
		if (document.orderForm.news[0].checked=true){
			for (j=0, m=document.orderForm.gift.length; j<m; j++) {
				document.orderForm.gift[j].disabled=true;
				document.orderForm.gift[j].checked=false;
			}
			break;
		}
		else
		{
			document.orderForm.news[i].disabled=false;
		}
	}
}

function fillCom() {
	var msg = "This donor is ";
	var student = "";
	var gift = "";
	
	for (i=0, n=document.orderForm.student.length; i<n; i++) {
		if (document.orderForm.student[i].checked) {
			student = document.orderForm.student[i].value;
			break;
		}
	}
	
	if( student == ""){
		msg = msg + "not selected. | The gifts selected are: ";
	}
	else{
		msg = msg + student + " | The gifts selected are: ";
	}
	
	for (i=0, n=document.orderForm.gift.length; i<n; i++) {
		if (document.orderForm.gift[i].checked) {
			gift = gift + document.orderForm.gift[i].value + ",  ";
		}
	}
	
	for (i=0, n=document.orderForm.news.length; i<n; i++) {
		if (document.orderForm.news[i].checked) {
			gift = gift + document.orderForm.news[i].value + "  ";
		}
	}
	
	msg = msg + gift + "| Their favourite show is: ";
	
	if( document.getElementById('favShow').value == "")
	{
		msg = msg + " not selected.";
	}
	else{
		msg = msg + document.getElementById('favShow').value;
	}
	
	if (document.orderForm.Price1[14].selected){
		document.orderForm.Price1[14].value = document.orderForm.Price2.value.trim("$");
	}
	
	document.orderForm.Comments.value = msg;
	document.orderForm.Price1[14].value = document.orderForm.txtAmount.value;

	return true;
}
	

function newWindow(bookgif) {
	bookWindow = window.open(bookgif, "bookWin", "width=420,height=400")
	bookWindow.focus()
}
	//This creates the popup browser embedded with windows media player
 function streamWindow(bookgif) {
	bookWindow = window.open(bookgif, "streamWin", "width=432,height=290")
	bookWindow.focus()
}




}//end if