var THEMEMAKERS_USER_PROFILE = function() {

    return {
        init: function() {
            var self = this;

            if (jQuery('#games_popup').length) {
              
                jQuery('#games_popup').fancybox({
                    'titlePosition': 'inside',
                    'transitionIn': 'none',
                    'transitionOut': 'none'
                });
            }

            jQuery("#add_youtube_item").live('click', function() {
                self.add_youtube();
                return false;
            });

            jQuery(".remove_youtube_item").live('click', function() {
                self.remove_youtube_item(this);
                return false;
            });

            jQuery("#games_search_button").live('click', function() {
                self.games_search();
                return false;
            });


            //*****
            jQuery(".button_save_user_profile").live('click', function() {
                self.save_user_profile();
                return false;
            });

            jQuery(".add_to_my_games").live('click', function() {
                var game_id = jQuery(this).attr('game_id');
                jQuery(this).remove();
                self.add_to_my_games_list(game_id);
                return false;
            });

            jQuery(".remove_from_my_games").live('click', function() {
                var game_id = jQuery(this).attr('game_id');
                self.remove_from_my_games(game_id);
                return false;
            });

            jQuery(".my_games_console_checkbox").live('change', function() {
                var value = jQuery(this).val();
                var is_checked = jQuery(this).is(':checked');

                if (is_checked) {
                    jQuery(this).attr("checked", 'checked');
                    jQuery("[value=" + value + "]").prop("checked", true);
                    jQuery(".hidden_console_value_" + value).val(1);//for data post
                } else {
                    jQuery(this).attr("checked", 'checked');
                    jQuery("[value=" + value + "]").prop("checked", false);
                    jQuery(".hidden_console_value_" + value).val(0);//for data post
                }


                return false;
            });

            jQuery(".user_subscribe_theme").live('change', function() {
                var is_checked = jQuery(this).is(':checked');

                if (is_checked) {
                    jQuery(this).attr("checked", 'checked');
                    jQuery(this).prev('input').val(1);//for data post
                } else {
                    jQuery(this).removeAttr('checked');
                    jQuery(this).prev('input').val(0);//for data post
                }


                return false;
            });


            jQuery(".js_add_to_favorites").live('click', function() {
                var self_link = this;
                var data = {
                    action: "add_user_to_favourites",
                    user_id: jQuery(this).attr('user-id')
                };
                //send data to server
                jQuery.post(ajaxurl, data, function(response) {

                    if (response === 'already_exists') {
                        alert('Already exists!');
                    }

                    if (response === 'succsess') {
                        jQuery(self_link).html('User added to favourites succsessfully!');
                    }


                });

                return false;
            });



            jQuery(".js_delete_favourite_user").live('click', function() {
                if (confirm("Are you sure")) {
                    var self_link = this;

                    var data = {
                        action: "delete_favourite_user",
                        user_id: jQuery(self_link).attr('user-id')
                    };
                    //send data to server
                    jQuery.post(ajaxurl, data, function(response) {
                        jQuery(self_link).parent().remove();
                    });
                }

                return false;
            });




        },
        add_youtube: function() {
            var html = jQuery("#youtube_item_template").html();
            jQuery("#youtube_list_bk").append(html);
        },
        remove_youtube_item: function(self) {
            jQuery(self).parents('li').eq(0).remove();
        },
        save_user_profile: function() {
            var data = {
                action: "save_user_profile",
                values: jQuery("#profile_form").serialize()
            };
            //send data to server
            jQuery.post(ajaxurl, data, function(response) {
                //TODO
            });
			
				jQuery(".info_popup").text("Options have been saved");
				jQuery(".info_popup").fadeTo(400, 0.9);
				window.setTimeout(function() {
					jQuery(".info_popup").fadeOut(400);
				}, 1000);
				
        },
        games_search: function() {
            var data = {
                action: "search_games",
                game_name: jQuery('#games_search_name').val(),
                search_mode: 'user_profile'
            };
            //send data to server
            jQuery.post(ajaxurl, data, function(html) {
                jQuery("#games_search_results").html(html);
            });
        },
        add_to_my_games_list: function(game_id) {
            var html = jQuery(".game_id_" + game_id).html();
            jQuery(".game_id_" + game_id).remove();
            jQuery(".gameList").prepend('<li class="game_id_' + game_id + '">' + html + '</li>');
        },
        remove_from_my_games: function(game_id) {
            jQuery(".game_id_" + game_id).remove();
        }
    };
};

var user_profile_object = new THEMEMAKERS_USER_PROFILE();

jQuery(function(){
    user_profile_object.init();
});




