$( function() {
    $.fn.scrap = function(options) {
        defaults = {
            containerId : 'scrapContainer',
            buttonId: 'scrapContainer',
            buttonClass: 'scrapButton',
            objectType : null,
            objectUkey : null,
            memberUkey : null,
            url : '/site/scrap/scrap.json',
            success: function() {}
        };
        options = $.extend(defaults, options || {});

        /* 입력폼 */
        var scrapForm = $('<form id="scrapForm"></form>');
        scrapForm.append(
                        $('<input type="hidden" name="objectType" value="' + options.objectType + '" />'));
        scrapForm.append(
                        $('<input type="hidden" name="objectUkey" value="' + options.objectUkey + '" />'));
        
        var scrapFormContainer = $('#'+options.containerId);
        scrapFormContainer.append(scrapForm);
        $('#scrapForm').ajaxForm( {
            url : options.url,
            type : 'POST',
            dataType : 'json',
            beforeSubmit : function(a, f, o) {
            },
            success : function(data) {
                if (data.result == null) {
                    alert("이미 스크랩 하신 컨텐츠입니다.");
                } else {
                    options.success(data);
                    alert("스크랩되었습니다.");
                }
                return false;
            }
        });

        $('#'+options.containerId).click( function() {
            if (options.memberUkey != null && options.memberUkey != '') {
                $("#scrapForm").submit();
            } else {
            	$("#signInForm").submit();
                //$("#signInDialog").dialog("open");
            }
            return false;
        });
        
        $('.'+options.buttonClass).click( function() {
            if (options.memberUkey != null && options.memberUkey != '') {
                $("#scrapForm").submit();
            } else {
            	$("#signInForm").submit();
                //$("#signInDialog").dialog("open");
            }
            return false;
        });
        
    }
    
});

