function ZoolooGraffitiShare(){
    this.postType = '';
    this.urlDetails = {};
    this.queryParams = ZFramework.getQueryParams();
    this.setup = function(postType){
        this.postType = postType;
        $('.zgraffitiType').unbind('click').click(function(e){
            $('.zgraffitiType').removeClass('zgraffitiTypeHover');
            $(this).addClass('zgraffitiTypeHover');
            var type = $(this).attr('name');
            ZGraffitiShare.postType = type;
            ZGraffitiShare.setupEditor(type);
            $('#z_graffiti_post_' + type).css('display', '');
            $('.zgraffitiForm').not('#z_graffiti_post_' + type).css('display', 'none');
            $('#z_graffiti_post_' + type).find('.zgraffitiInputField a:first').select().focus();
        });
        
        $('#z_graffiti_link_url').change(function() {
            ZGraffitiShare.getURLDetails($(this).val());
        });
        
        $('#z_graffiti_cancel').click(function(e){
            ZGraffitiShare.closeModal();
        });
        
        $('#z_graffiti_post').click(function(e){
            var post_type = $('#z_graffiti_type .zgraffitiTypeHover').attr('name');
            var post_data = {};
            var services = {};
            var edit = $('#z_graffiti_edit').val();
            var repost = $('#z_graffiti_repost').val();
            var post_id = $('#z_graffiti_post_id').val();
            
            $('#z_graffiti_post_' + post_type).find('.zgraffitiInputField').each(function(){
                var id = $(this).attr('id');
                var field = id.split('z_graffiti_' + post_type + '_')[1]
                if ($(this).hasClass('zgraffitiHTML')) {
                    post_data[field] = FCKeditorAPI.GetInstance(id).GetHTML();
                }
                else {
                    post_data[field] = $(this).val();
                }
            });
            
            var services = [];
            $('#z_graffiti_services input:checked').each(function(){
                services.push($(this).val());
            });
            if (services.length === 0) {
                ZFramework.alert(ZFramework.i18n(ZMsg.INVALID_SERVICE_NONE));
                return;
            }
            
            ZGraffitiShare.post(post_type, post_data, edit, repost, post_id, services, function(){
                $('.zgraffitiFrame').css('display', 'none');
                $('#z_graffiti_done_overlay').show();
                setTimeout(ZGraffitiShare.closeModal, 1000);
            }, function(result){
                ZFramework.alert(result.error);
                return;
            });
            e.preventDefault();
        });
        
        if (this.queryParams.u) {
            this.getURLDetails(this.queryParams.u);
        }
        
        $('').keydown(function(e){
            if (e.keyCode == 27) {
                e.preventDefault();
                ZGraffitiShare.closeModal();
            }
        });
        $('#z_graffiti_type a[name=' + postType + ']').click();
        window.focus();
    };
    
    this.setupThumbListener = function(){
        $('.zgraffitiImages img').click(function(e){
            if ($(this).hasClass('zgraffitiImagesSelected')) {
                $(this).removeClass('zgraffitiImagesSelected');
                $('#z_graffiti_' + ZGraffitiShare.postType + '_img_url').val('');
            }
            else {
                $('#z_graffiti_' + ZGraffitiShare.postType + '_img_url').val($(this).attr('src'));
                $(this).addClass('zgraffitiImagesSelected');
            }
            $(this).siblings().removeClass('zgraffitiImagesSelected');
        });
    };
    
    this.setupEditor = function(type){
        $('#z_graffiti_post_' + type).find('.zgraffitiHTML').each(function(){
            if (ZGraffitiShare.urlDetails && ZGraffitiShare.urlDetails.meta && ZGraffitiShare.urlDetails.meta.description) {
                if ($('#z_graffiti_' + type + '_body').attr('id')) {
                    $('#z_graffiti_' + type + '_body').val(ZGraffitiShare.urlDetails.meta.description);
                }
                if ($('#z_graffiti_' + type + '_caption').attr('id')) {
                    $('#z_graffiti_' + type + '_caption').val(ZGraffitiShare.urlDetails.meta.description);
                }
            }
            
            ZGraffitiShare.oFCKeditor = new FCKeditor($(this).attr('id'));
            ZGraffitiShare.oFCKeditor.Height = $('#zgraffiti_share').height() - 330;
            ZGraffitiShare.oFCKeditor.ToolbarSet = 'Graffiti';
            ZGraffitiShare.oFCKeditor.BasePath = ZFramework.getEditorBasePath();
            ZGraffitiShare.oFCKeditor.ReplaceTextarea();
        });
    };
    
    this.closeModal = function(){
        $('.zgraffitiFrame').css('display', '');
        $('#z_graffiti_done_overlay').hide();
        window.close();
    };
    
    this.getURLDetails = function(url) {
        ZFramework.zfAjaxPost('/zGraffiti/getURLDetails', {
            url: url,
            all_images: true
        }, true, function(result){
            if (result.status) {
                var imageTestTarget = $('#zgraffiti_imagetest');
                if (typeof result.data == 'object') {
                    ZGraffitiShare.urlDetails = result.data;
                    if (typeof result.data.images == 'object' && typeof result.data.oembed != 'object') {
                        var imghtml = '';
                        for (img in result.data.images) {
                            imageTestTarget.append('<img src="' + result.data.images[img].src + '" />');
                        }
                        var first = true;
                        setTimeout(function(){
                            var images = ZGraffitiShare.getImages();
                            var count = 0;
                            for (img in images) {
                                if (count >= 9) break;
                                if (first) {
                                    $('.zgraffitiImgURL').val(images[img]);
                                }
                                imghtml += '<img src="' + images[img] + '" width="50" height="50"';
                                imghtml += (first) ? ' class="zgraffitiImagesSelected" />' : '/>';
                                first = false;
                                count++;
                            }
                            if (imghtml) {
                                $('.zgraffitiImages').html(imghtml);
                                $('.zgraffitiPhoto').css('display', '');
                            }
                            ZGraffitiShare.setupThumbListener();
                            if (first) {
                                $('.zgraffitiImages').css('display', 'none');
                                $('.zgraffitiPhoto').css('display', 'none');
                            }
                            else {
                                $('.zgraffitiImages').css('display', '');
                                $('.zgraffitiPhoto').css('display', '');
                            }
                        }, 400);
                    }
                    
                    if (result.data.oembed && result.data.oembed.thumbnail_url) {
                        $('#z_graffiti_video_thumbnail').html('<img class="zgraffitiImagesSelected" src="' + result.data.oembed.thumbnail_url + '" height="50" />');
                        $('.zgraffitiVideoThumbnail').css('display', '');
                        $('.zgraffiti_video').css('display', '');
                    }
                    else {
                        $('.zgraffiti_video').css('display', 'none');
                    }
                    
                    if (result.data.title) {
                        $('#z_graffiti_' + ZGraffitiShare.postType + '_title').val(result.data.title);
                    }
                    if (result.data.meta && result.data.meta.description && !ZGraffitiShare.queryParams.s) {
                        if ($('#z_graffiti_' + ZGraffitiShare.postType + '_body').attr('id')) {
                            $('#z_graffiti_' + ZGraffitiShare.postType + '_body').val(result.data.meta.description);
                            FCKeditorAPI.GetInstance('z_graffiti_' + ZGraffitiShare.postType + '_body').SetHTML(result.data.meta.description);
                        }
                        if ($('#z_graffiti_' + ZGraffitiShare.postType + '_caption').attr('id')) {
                            $('#z_graffiti_' + ZGraffitiShare.postType + '_caption').val(result.data.meta.description);
                            FCKeditorAPI.GetInstance('z_graffiti_' + ZGraffitiShare.postType + '_caption').SetHTML(result.data.meta.description);
                        }
                    }
                }
            }
        });
    };
    
    this.post = function(post_type, post_data, edit, repost, post_id, services, successCallback, failureCallback){
        ZFramework.zfAjaxPost('/zGraffiti/post', {
            post_type: post_type,
            post_data: JSON.stringify(post_data),
            edit: edit,
            repost: repost,
            post_id: post_id,
            services: JSON.stringify(services)
        }, true, function(result){
            if (result.status == 1) {
                successCallback(result);
            }
            else {
                failureCallback(result);
            }
        });
    };
    
    this.getImages = function(){
        var imgList = {};
        $('#zgraffiti_imagetest img').each(function(){
            if (this.offsetWidth >= 75 && this.src !== '' && $.inArray(this.src, imgList) == -1) {
                var ratio = this.offsetWidth / this.offsetHeight;
                if (ratio < 0) {
                    ratio = this.offsetHeight / this.offsetWidth;
                }
                if (ratio < 3) {
                    imgList[this.src] = this.offsetHeight * this.offsetWidth;
                }
            }
        });
        var sortedList = ZGraffitiShare.sortAssoc(imgList);
        $('#zgraffiti_imagetest').css('display', 'none');
        return sortedList;
    };
    
    this.sortAssoc = function(aInput){
        var aTemp = [];
        for (var sKey in aInput) {
            aTemp.push([sKey, aInput[sKey]]);
        }
        
        aTemp.sort(function(){
            return arguments[0][1] < arguments[1][1];
        });
        
        var aOutput = [];
        var idx = 0;
        for (var nIndex = 0; nIndex < aTemp.length; nIndex++) {
            aOutput[idx] = aTemp[nIndex][0];
            idx++;
        }
        return aOutput;
    }
    
    
}

ZGraffitiShare = new ZoolooGraffitiShare();
