if ( typeof( IPG ) == 'undefined' ) IPG = {};
IPG.UserPropertyManager = function(){
    this.version = "0.1";
    return this;
};

IPG.UserPropertyManager.prototype.setUserProperty = function( url , user_property ){
    var ajaxOpt = {
        'method': 'get',
        'onFailure': this.onUserPropertyFailure
    };
    if( url && url.charAt( url.length - 1 ) != '?' ) url += '?';
    url = this.setQueryString( url , user_property );
    new Ajax.Request( url , ajaxOpt );
    //IPG.Utility.debugOut( '[setUserProperty['+url+']' , "debug" , "black" );
};
IPG.UserPropertyManager.prototype.setQueryString = function( url , user_property ){
    var flg = false;
    if( user_property.area != undefined ){
        url += "area="+user_property.area;
        flg = true;
    }
    if( user_property.sorttype != undefined ){
        if( flg ) url += "&";
        url += "sorttype="+user_property.sorttype;
        flg = true;
    }
    if( user_property.bctype != undefined ){
        if( flg ) url += "&";
        url += "bctype="+user_property.bctype;
        flg = true;
    }
    if( user_property.genrecolor != undefined && user_property.genrecolor.length ){
        if( flg ) url += "&";
        url += "color=";
        for( var i = 0 , len = user_property.genrecolor.length; i < len; i++ ){
            if( i ) url += "_";
            url += user_property.genrecolor[i];
        }
    }
    return url;
};

IPG.UserPropertyManager.prototype.getUserProperty = function( url ){
    var ajaxOpt = {
        'method': 'get',
        'asynchronous': false
    };
    var xhrObj = new Ajax.Request( url , ajaxOpt );
    if( xhrObj && xhrObj._complete ){
        this.xml_data = xhrObj.transport.responseXML.documentElement;
    }
    return this.parseData();
};
IPG.UserPropertyManager.prototype.parseData = function(){
    if( !this.xml_data ) return undefined;
    
    var data = {};
    
    var tmp = this.xml_data.getElementsByTagName("area")[0];
    if( tmp.firstChild.nodeType == Node.TEXT_NODE ){
        data.area = tmp.firstChild.nodeValue;
    }
    tmp = this.xml_data.getElementsByTagName("sorttype")[0];
    if( tmp.firstChild.nodeType == Node.TEXT_NODE ){
        data.sorttype = tmp.firstChild.nodeValue;
    }
    tmp = this.xml_data.getElementsByTagName("bctype")[0];
    if( tmp.firstChild.nodeType == Node.TEXT_NODE ){
        data.bctype = tmp.firstChild.nodeValue;
    }
    var genre_color = this.xml_data.getElementsByTagName( "genrecolor" )[0];
    if( !genre_color || !genre_color.childNodes || !genre_color.childNodes.length ) return data;
    var genre_list = [];
    for( var i = 0 , len = genre_color.childNodes.length; i < len; i++ ){
        genre_list[i] = genre_color.childNodes[i].firstChild.nodeValue;
    }
    data.genrecolor = genre_list;
    return data;
};