
var MemberCardCollector = {
    roster : [],
    events : [],
    register : function (_obj){
        MemberCardCollector.roster[_obj.ClientID] = _obj;
    },
    DialogClick : function(res){
        var _id = res.data;
        
        if( typeof(MemberCardCollector.roster[_id]) != "undefined" && MemberCardCollector.roster[_id] != null){
            MemberCardCollector.roster[_id].OnDlgClick(res);
        }
    },
    
    registerEvent : function (_name, _handler){
        this.events[_name] = new CEvent(_name, _handler);
    },
    
    fireEvent : function(_name, _value){
        if (typeof(this.events[_name]) != "undefined" && typeof (this.events[_name].handler) == "function"){
            this.events[_name].handler(_value);
        }
    }
}

var CEvent = function(_name, _handler){
    this.name = _name; this.handler = _handler;
}

var CMemberCard = function(_clientid, _cbClientId,_isInContact,_hiddenCommandID){
    this.ClientID = _clientid;
    this.CBClientID = _cbClientId;
    this.IsInContact = _isInContact;
    this.HiddenCommandID = _hiddenCommandID;
    this.BindEvents();
}

CMemberCard.prototype = {
    IcnGoldUrl : "/Design/Profile/red/icn_profile_fav_gold.png",
    IcnGreyUrl : "/Design/Profile/red/icn_profile_fav_gray.png",
    CommandName : "",
    IsInContact : false,
    Question : "Are you sure?",
    YesButtonText : "Yes",
    NoButtonText : "No",
    
    BindEvents : function(){
        var self = this;
        $J('.member-card-icon_'+this.ClientID).bind("click", function(e){e.preventDefault();self.OnIconClick(e); e.stopPropagation(); return false});
        $J("#"+this.CBClientID).click(function(e){e.preventDefault();e.stopPropagation();});
        $J("#"+this.ClientID+"_photo").click(function(e){e.preventDefault();e.stopPropagation();self.OnCardClick();});
    },

    OnIconClick : function(e){
        var self = this;
        var _commandName = $J(e.target).attr("CommName");
        if (_commandName == "FAVORITE_CLICK" && this.IsInContact) return;
        if (_commandName == "FAVORITE_CLICK"){ this.AddToFav(); return; }
        if (_commandName == "BLOCK_CLICK_ACCESS_DENITED" ){GlobalPaidWarning(); return; }
        if (_commandName == "GOTO_GALLERIES") {this.GotoGalleries();return;}
        if (_commandName == "MESSAGE_CLICK") {this.OnMsgClick();return;}
        var _isConfirmNeed = $J(e.target).attr("IsConfirmNeed") == "true";
        this.CommandName = _commandName;
        if (typeof(MemberCardCollector.iconClickHandler) == "function"){
			if (MemberCardCollector.iconClickHandler(e, _commandName) === false) return;
        }
        if (_isConfirmNeed)
            seek4.confirm(this.Question, 'MemberCardCollector.DialogClick', {buttons: [self.YesButtonText , self.NoButtonText ], data:self.ClientID });
        else
            this.Submit();
        
    },
    
    GotoGalleries : function(){
        window.location = this.GalleryUrl;
    },
    
    AddToFav : function(){
        var self = this;
        $J.ajax({
	            type: "GET",
	            url : self.AjaxFavUrl+'&rnd='+Math.random(),
	            async:false,
	            success: function(_res){ MemberCardCollector.roster[self.ClientID].AddToFavComplete(_res)}
	        });
	    
    },
    
    AddToFavComplete : function(_res){
        eval(_res);
        seek4.alert(ajaxRes.message, ajaxRes.title);
        if (ajaxRes.success){
            if (this.AjaxFavUrl.indexOf("unblock=true") != -1){
                this.AjaxFavUrl = this.AjaxFavUrl.replace("unblock=true", "unblock=false");
                $J("#icnFav_"+this.ClientID)[0].src = this.IcnGreyUrl;
                MemberCardCollector.fireEvent("UNBLOCK_SUCCESS", this.ClientID);
            }else{
                $J("#icnFav_"+this.ClientID)[0].src = this.IcnGoldUrl;
                $J("#icnFav_"+this.ClientID).unbind("click");
                MemberCardCollector.fireEvent("ADD_TO_FAV_SUCCESS", this.ClientID);
            }
            
        }
    },
    
    OnCardClick : function(){
        window.location = this.RedirectURL;
    },
    
    Submit : function(_commandName){
        $J('#'+this.HiddenCommandID).val(this.CommandName);
        $J('form')[0].submit();
    },
    
    OnDlgClick : function(_res){
        if (_res.index == 0){
            this.Submit();
        }
    },
    OnMsgClick : function(_res){
        window.location = this.MesasgeUrl;
    }
}
