//test
$(document).ready(function() {
	if(!$.browser.msie) {
		$("#show_feed > a").hover(function(){
			$(this).children(".radio_show").children("div").fadeIn("fast");
		},
		function(){
			$(this).children(".radio_show").children("div").fadeOut("slow");
		});
	
		$("#nav>li>a").hover(function(){
			$(this).animate({opacity:0.6});
		},
		function() {
			$(this).animate({opacity:1});
		});
		$("#schedule_nav > h2 > a").hover(function(){
			$(this).animate({opacity:0.6});
		},
		function() {
			$(this).animate({opacity:1});
		});

		$("#nav>li>a").click(function() {
			toggle_content("content", $(this));
		});

		$("#banner>div>a").click(function() {
			var a = document.createElement("a");
			$(a).text("blog");
			toggle_content("content", $(a)); 
		});
	
		$("#schedule_nav > h2 > a").click(function() {
			toggle_content("schedule", $(this));
			$("#schedule_nav").find(".schedule_tab_clicked").attr("class", "schedule_tab");
			$(this).parent().attr("class", "schedule_tab_clicked");
		});
	
		$("#listen > a").click(function() {
			toggle_playback("http://www.rainydawg.org/listen/High");
		});

		$(".stream_show").click(function() {
			toggle_playback("http://www.rainydawg.org/shows/" + $(this).attr("title"), 
			$(this).text() + " of " + $(this).attr("name"),
			false);
		});

		$(".show_link").click(function() {
			toggle_show($(this).attr("title"));
		});
		get_current_song();
		setInterval(get_current_song, 30000);

		var currentDay = $(".schedule_listing[name='current']");
		var topSchedule = $("#schedule > div:first").next();
		$(currentDay).insertBefore(topSchedule);
        djAppToggle();
        djAppSubmit();
	} else {
		$("#content").html("<h2>Rainydawg Radio is best viewed in Firefox, Chrome, Safari, or Opera.</h2>");
	}
});

function get_current_song() {
        $.ajax({
                url:"/data/currentlyplaying.php",
                dataType:"text",
                success: function(data) {
			var info = data.split("\n");
			var time = info[0];
			var title = info[1];                                                                           
			var artist = info[2];
                        var repl = "<h2 class='yellow'>Current Track [" + time + "]:</h2><span style='color:white;margin-left:10px;'>" + title + "</span> - <span style='color:white;'>" + artist + "</span>";
                        $("iframe").contents().find("#current_song").html(repl);
                }
        });
}

function toggle_content(parent, link) {
	if($("#" + parent  + " > div:first").next().attr("id") != $(link).text()) {
        	var id = "#" + $(link).text().replace(" ", "_");
                var ref = $(id); //ty
                var insert_point = $("#" + parent  + " > div:first").next();
                $(ref).insertBefore(insert_point);
        	$(insert_point).fadeOut("fast", function(){
			$(ref).fadeIn("fast");
		});
         }
}

function initialize(page) {
}

function toggle_playback(src, desc, toggle) {
	if(toggle==null) {
		toggle = true;	
	}
	var playerSrc = "";
	var buttonSrc = "";
	var testSrc = $("#listen > a > img").attr("src");
	if(testSrc == "on.gif"||(testSrc == "off.gif" && !toggle)) {
		desc = (!desc)? "" : "&desc=" + desc;	
		playerSrc = "reload_player.php?play=" + src + desc;
		buttonSrc = "off.gif";
		$("#media_player").fadeIn("slow");
	} else {
		playerSrc = "reload_player.php";
		buttonSrc = "on.gif";
		$("#media_player").css("display", "none");
	}
	$("#media_player").attr("src", playerSrc).load(get_current_song);
	$("#listen > a > img").attr("src", buttonSrc);
}

function toggle_show(show) {
	$.ajax({
		url:"getShowService.php?show=" + show,
		dataType:"html",
		success: function(data) {
			$("#show").html(data);
		}
	});
	var a = document.createElement("a");
	$(a).text("show");
	toggle_content("content", $(a));
}

function djAppToggle() {
    $("#form").find("input[name='dj']").attr("checked", false).change(
            function(){
                $("#djApp").slideToggle();
            });
}

function djAppSubmit() {
    var data = {
        action : "Submit",
        fname : $("input[name='fname']").val(),
        lname : $("input[name='lname']").val(),
        email : $("input[name='email']").val(),
        phone : $("input[name='phone']").val(),
        dj : $("input[name='dj']").val(),
        st : $("input[name='st']").val() ,
        audio : $("input[name='audio']").val(),
        writer : $("input[name='writer']").val(),
        photographer : $("input[name='photographer']").val(),
        other : $("#other").val(),
        hours : $("#hours").val(),
        interests : $("#interests").val(),
        skills : $("#skills").val(),
        showformat : $("input[name='showformat']").val(),
        showformatother : $("input[name='showformatother']").val(),
        tracklist : $("textarea#tracklist").val(),
        topics : $("textarea#topics").val(),
        features : $("textarea#features").val(),
        justification : $("textarea#justification").val(),
        comments : $("textarea#comments").val()
    }

    $("#get_involved").find(".button").click(
        function(){
            $.ajax( {
                url:     "/submitapp.php",
                type:    "POST",
                data:    data,
                success: function(){
                    $("#get_involved").find(".text").slideToggle();
                    $("#get_involved").append("<h2>Form Submitted! Thank you for applying!</h2>");
                } 
            });
        }
    );
    return false;
}

