jQuery(function(){

    setCallbacks();

	jQuery(document).click(function(e) {
		if (!jQuery(e.target).parents('div.myaccount_section').length) {
			jQuery('li a.my_ac').removeClass('active');
			jQuery('div.drop').hide();
		}
		return true;
	});

    // 'play now' section functionality
    jQuery('#now_play_expand').bind('click', function () {
        var PLAY_NOW_ROW_HEIGTH = 37;
        var newHeight = 0;
        var nowPlayLink = jQuery('#now_play_expand');
        if (nowPlayLink.hasClass('now_play_expand')) {
            newHeight = PLAY_NOW_ROW_HEIGTH * parseInt(jQuery('#now_play_ul_expand li').length);
        } else {
            newHeight = PLAY_NOW_ROW_HEIGTH;
        }
        jQuery("#now_play_ul_expand").animate({height:newHeight}, 300, "linear");

        nowPlayLink.toggleClass('now_play_expand');
        nowPlayLink.toggleClass('now_play_contract');
    });
    // update play_now section every X minutes
    setInterval(getActualSongs, 60000);
    // cycle through stations
    setInterval(cycleStations, 5000);
});


function setCallbacks() {
    jQuery('li a.my_ac').click(function() {
		//jQuery('div.two').hide();
		if (jQuery('div.drop').is(':visible')) {
			jQuery(this).removeClass('active');
			jQuery('div.drop').hide();
		}
		else {
			jQuery(this).addClass('active');
			jQuery('div.drop').show();
		}
		return false;
	});

    // add _blank to all links
    jQuery('a:not([target="_blank"])').each(function(e,m){
        if (typeof jQuery(m).attr('onclick') == 'undefined') {
            jQuery(m).attr('target', '_blank');
        }
    });
    jQuery('#sign-out').attr('target', '');
}

function addToPlaylist(song_id) {
    jQuery.post(
        'index.php?p=ajax/add_to_playlist',
        {
            song_id: song_id
        },
        function(resp) {
            switch(resp.status) {
                case 'exists':
                    alert('The song is already in your Playlist.');
                    break;
                case 'ok':
                    alert('The song has been added to your Playlist.');
                    break;
                case 'error':
                    alert('You are not logged in.')
                    break;
            }
        },
        'json'
    );
}


function get_slide_playlist(user_id, song_id) {
    jQuery('#banner_section').load('index.php?p=get_player&give_empty=1', function() {
        jQuery('#banner_section').html('<div class="inner_banner1"><p>Loading playlist...</p><img src="images11/loading-bar.gif" alt=""></div>');
        N77music.prevSongIndex = -1; //fix immediate radio play
        if (jQuery('#banner_section').css('display') == 'none') {
            jQuery('#banner_section').show('blind', {to:{height:500}}, 1500, function() {
                jQuery('#banner_section').load("index.php?p=get_player&all=1&user_id=" + user_id + (song_id ? '&song_id=' + song_id : ''));
            })
        } else {
            jQuery('#banner_section').load("index.php?p=get_player&all=1&user_id=" + user_id + (song_id ? '&song_id=' + song_id : ''));
        }
    })
}

function get_playlist(user_id) {
    jQuery(".home_banner", jQuery("#banner_section")).css("display", "none");

    jQuery(".home_banner", jQuery("#banner_section")).after("<div id=\"to_delete\" class=\"inner_banner1\"><p>Loading playlist...</p><img src=\"images11/loading-bar.gif\" alt=\"\"></div>");

    jQuery(".player_section", jQuery("#banner_section")).fadeOut("slow");
    jQuery('.player_section').load('index.php?p=get_player&give_empty=1', function() {
        jQuery(".player_section", jQuery("#banner_section")).load("index.php?p=get_player&user_id=" + user_id,
            function() {
                jQuery(".home_banner", jQuery("#banner_section")).css("display", "block");
                jQuery("#to_delete").remove();
                jQuery(".player_section", jQuery("#banner_section")).fadeIn("slow");
                if (jQuery("#hidden_status").length) {
                    jQuery(".player_status", jQuery("#banner_section")).html(jQuery("#hidden_status").val());
                }
                if (jQuery("#hidden_openlink").length) {
                    jQuery(".left_side p:first a", jQuery("#banner_section")).attr("href", jQuery("#hidden_openlink").val());
                }
            }
        );
    })

    return false;
}

function get_slide_radio(radio_name) {
    jQuery('#banner_section').load('index.php?p=get_player&give_empty=1');
        jQuery('#banner_section').html('<div class="inner_banner1"><p>Loading station...</p><img src="images11/loading-bar.gif" alt=""></div>');
        N77music.prevSongIndex = -1; //fix immediate radio play
        if (jQuery('#banner_section').css('display') == 'none') {
            jQuery('#banner_section').show('blind', {to:{height:500}}, 1500, function() {
                jQuery('#banner_section').load("index.php?p=get_player&all=1&radio_name=" + radio_name);
            })
        } else {
            jQuery('#banner_section').load("index.php?p=get_player&all=1&radio_name=" + radio_name);
        }
}

function getActualSongs() {
    jQuery.post('index.php?p=ajax/now_playing_songs',
        {},
        function(resp) {
            // update all stations with new songs/artists
            for (var key in resp) {
                if (resp[key].id != -1) {
                    jQuery('#play_now_station_' + resp[key].id + ' span.song').text(resp[key].play_song);
                    var artist_link = jQuery('#play_now_station_' + resp[key].id + ' div.now_play_song_info a');
                    artist_link.text(resp[key].play_artist);
                    artist_link.attr('href', resp[key].artist_link);
                } else {
                    var kk = resp[key].member_id == 15160 ? 'latin' : 'christ';
                    jQuery('#play_now_station_' + kk + ' span.song').text(resp[key].play_song);
                    var artist_link = jQuery('#play_now_station_' + resp[key].id + ' div.now_play_song_info a');
                    artist_link.text(resp[key].play_artist);
                    artist_link.attr('href', resp[key].artist_link);
                    jQuery('#play_now_station_' + kk + ' a.now_play_button').attr('rel', resp[key]['song_id'])
                }
            }
        },
        'json'
    );
}

/**
 * Perfom sliding down and obtain artist player
 */
function getArtistSlidePlayer(artist, domain) {
    jQuery('#banner_section').html('<div class="inner_banner1"><p>Loading Station for "<strong>' + artist + '</strong>"</p><img src="images11/loading-bar.gif" alt=""></div>');
    if (jQuery('#banner_section').css('display') == 'none') {
        jQuery('#banner_section').show('blind', {to:{height:500}}, 1500, function() {
            jQuery('#banner_section').load(domain + '/index.php?p=get_player&search_text=' + encodeURIComponent(artist));
        })
    } else {
        jQuery('#banner_section').load(domain + '/index.php?p=get_player&search_text=' + encodeURIComponent(artist))
    }
}

/**
 * Cycle through stations on play now section
 */
function cycleStations() {
    var PLAY_NOW_ROW_HEIGTH = 37;
    // cycle only for contracted section
    if (parseInt(jQuery('#now_play_ul_expand').css('height')) > PLAY_NOW_ROW_HEIGTH) {
        return false;
    }
    // cut first station and push it to the end of the list
    var firstStation  = jQuery('#now_play_ul_expand li:first').clone();
    jQuery("#now_play_ul_expand li:first").animate({height:'0px'}, function(){
        jQuery('#now_play_ul_expand li:first').remove();
        firstStation.appendTo('#now_play_ul_expand');
    });
}
