/**************************************************
 * selectur.js
 * 28.12.2006
 * www.mubility.com
 **************************************************/

// ======= SELECTUR =========================================================================================================================
var Selectur = {
    folders : {},
    current: -1,
    proxyPath: '/proxy.php',

    color: {
        body: '#FFFFFF',
        border: '#CCCCCC',
        button: '#CCCCCC',
        player_text: '#666666',
        playlist_text: '#666666'
    },

    init: function() {
        var last_folder_video = $.cookie('last_folder');
        if(this.folders[last_folder_video] == undefined) last_folder_video = 1;
        this.showFolder(last_folder_video);
    },

    setSelecturCookie: function() {
        var expire = new Date('01-01-2018');
        setCookie('selectur', $.toJSON(this.folders), expire, '/');
    },
    
    showFolder: function(id) {
        
        if (id == Selectur.current) {
            return;
        }
        
    	// EXIT FOLDER
        for(key in Selectur.folders) {
            $('#tab_'+key).css('top','0px');
        }
        
    	// INIT FOLDER
    	var folder = Selectur.folders[id];
        $('#selector_tabs > ul > li').css('top',4);
        $('#selector_tabs > ul > li[name="tab_'+id+'"]').css('top',6);

        // DISPLAY RADIO.BLOG
        RadioBlog.cover = 1;
      	RadioBlog.playlist = folder.playlist;
      	RadioBlog.width = '100%';
      	RadioBlog.height = '100%';
        RadioBlog.debug = 0;
        RadioBlog.color.body = Selectur.color.body;
        RadioBlog.color.border = Selectur.color.border;
        RadioBlog.color.button = Selectur.color.button;
        RadioBlog.color.player_text = Selectur.color.player_text;
        RadioBlog.color.playlist_text = Selectur.color.playlist_text;

        // PARAMS
        if (folder.shuffle) {
            RadioBlog.shuffle = folder.shuffle;
        }
        else {
            delete(RadioBlog.shuffle);
        }

        if (folder.autoplay) {
            RadioBlog.autoplay.tracknum = folder.autoplay;
        }
        else {
            delete(RadioBlog.autoplay.tracknum);
        }
        
        RadioBlog.where = document.getElementById('selector_content');
    	RadioBlog.start();

        if (folder.uacct) {
            RadioBlog.stats.uacct = folder.uacct;
            RadioBlog.stats.start();
        }
        else {
            delete(RadioBlog.stats.uacct);
        }
        
    	// SET COOKIE
        var expires = new Date();
        expires.setYear(2999);
        $.cookie('last_folder', id, {expires:expires});
        
        Selectur.current = id;
    },

    showPlaylist: function(id, playlist_id) {

    	// INIT FOLDER
        var folder = Selectur.folders[playlist_id];
        $('#selector_tabs > ul > li').css('top',4);
        $('#selector_tabs > ul > li[name="tab_'+id+'"]').css('top',6);
        $('#selector').css('height','auto');
        $('#selector_content').css('height','auto');

        $.ajax({
            type: 'GET',
            url: Selectur.proxyPath,
            data: {type:'xml', url:folder.playlist},
            dataType: 'xml',
            success: function(xmlObj){
                var html = 'ho';
                $('track',xmlObj).each(function(){
                    html += $(this).attr('title');
                });
    
                $('#selector_content').html(html);
            }
        });
    },
        
    share: function() {
        window.location = ('/share/?playlist='+encodeURIComponent(RadioBlog.playlist));
    },
    
    switchButton: function(id) {
        button = document.getElementById("add_"+id);
        button.src = 'http://stat.radioblogclub.com/images/add_to_selector_on.gif';
    }
}

// ======= UTILS =========================================================================================================================
 /**
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};