var clShare = false;
var ConnectorShare = new Class({

    shareButtons:false,
    debug:true,
    modalbox:false,
    type:false,
    title:false,

    initialize: function(name, type) {
        this.type = type;
        // get all share buttons
        this.shareButtons = $$( 'a.' + name );
        for( var i = 0; i < this.shareButtons.length; i++ ){
            this.shareButtons[i].addEvent( 'click', this.buttonClick.bind(this) );
        }
    },

    buttonClick:function( event ){

        new Event( event ).stop();
        var target = event.target || event.srcElement;

        var targetUrl = target.href;
        if( targetUrl.test('/option/share') ){
            targetUrl = targetUrl.substring(0, ( Number( target.href.length ) - 13 ) );
        }

        var targetTitle = target.title;
        if( targetTitle.test('Share ') ){
            targetTitle = targetTitle.substring(6, Number( target.href.length ) );
        }
        this.title = targetTitle;

        new Ajax("/js/functions/shareFunction.php",{
            method:'post',
            data:'target=' + targetUrl + '&title=' + targetTitle + '&type=' + this.type ,
            onComplete:this.shareResponse.bind(this),
            onFailure:function( response ){ this.hideLoader.bind(this); }
        }).request();

    },

    tab:function( tabId ){
        var tabs = $$('div.navtab');
        for( var i= 0; i < tabs.length; i++ ){
            tabs[i].className = 'navtab';
        }

        if( $( tabId + 'NavTab' ) ){
            $( tabId + 'NavTab' ).className = 'navtab on';
        }
        // get all the share boxes and make them display non
        var boxes = $$('div.shareBox');
        for( var i= 0; i < boxes.length; i++ ){
            boxes[i].style.display = 'none';
        }
        if( $( tabId) ){
            $( tabId).style.display = '';
        }
    },

    shareResponse:function( response ){
        this.openWindow( response );
    },

    shareEmail:function( event ){
        new Event(event).stop();
        
        new Ajax("/js/functions/shareEmail.php",{
            method:'post',
            data: $( 'cl_shareByEmailForm' ).toQueryString() ,
            onComplete:this.shareEmailResponse.bind(this),
            onFailure:function( response ){ this.hideLoader.bind(this); }
        }).request();

    },

    hideStatus:function(){
        var jsStatus = $('jsStatus');
        if( jsStatus ){
            jsStatus.className = 'empty';
            jsStatus.empty();
            return true;
        }
        return false;
    },

    showStatus:function( text, type ){
        var jsStatus = $('jsStatus');
        var closeLink = '<a href="javascript:;" onclick="clShare.hideStatus($(\'jsStatus\'));" class="closeButton"><span></span>x</a>';
        if( jsStatus ){
            jsStatus.className = type;
            jsStatus.innerHTML= '<p>' + closeLink+text + '</p>';
            return true;
        }
        return false;
    },

    shareEmailResponse:function( response ){

        var results;
        eval( response );

        if( results.error ){

            if( $('shareByEmailStatusErrors') ){
                $('shareByEmailStatusErrors').innerHTML = results.html;
            }

        } else {
            this.showStatus( 'You have sent ' + this.title + ' to ' + results.number + ' of your friends!', 'success');
            this.closeWindow();
        }

    },

    openWindow:function( str ){

        if( !$('cl_share_window') ){
            var element = new Element('div',{'id': 'cl_share_window', 'styles': {'display': 'none'}}).setHTML( str );
            element.injectInside(document.body);
        } else {
            $('cl_share_window').setHTML( str );
        }

        this.modalbox = ModalBox.init( 'cl_share_window', {initialWidth:560,initialHeight:860} );

        this.modalbox.show();

        if( $( 'cl_shareByEmailForm' ) ){
            $( 'cl_shareByEmailForm' ).addEvent( 'submit', this.shareEmail.bind(this) );
        }

    },

    closeWindow:function(){
        this.modalbox.close();
        this.modalbox = false;
    },

    showLoader:function(){
        if( this.ajaxLocationPicker ){
            this.ajaxLocationPicker.style.display = '';
        }
    },

    hideLoader:function(){
        if( this.ajaxLocationPicker ){
            this.ajaxLocationPicker.style.display = 'none';
        }
    },

    log:function( msg ){
        if( typeof JSLog != 'undefined' ){
            if( JSLog && this.debug ){
                JSLog.write( msg );
            }
        }
    }

});