﻿function MicroBlog(expBoxStr){
    expBoxStr = '#'+expBoxStr;
    var loadModalPopup=$(expBoxStr+' .loadModalPopup').get(0);
    var loadModalContent=$(expBoxStr+' .loadModalContent').get(0);
    var linkAddImage=$(expBoxStr+' .link');
    var self=this;
    
    $(document).ready(function(){
        $(expBoxStr+' .ExprimezVousText').focus(function(){
            if(this.className.match('inputEmpty')){
                this.value='';
                this.className='ExprimezVousText';
            }
        }).blur(function(){
            if(!this.value || this.value==''){
                this.value='Exprimez-vous';
                this.className='ExprimezVousText inputEmpty';
            }
        }).each(function(){
            new jQuery.simpleautogrow(this);
        });
        
        $(expBoxStr+' .close').click(function(){self._showHideDivImportPhoto(false);});
        linkAddImage.click(function(){self._showHideDivImportPhoto(true);});
    });
    
    this.showHideDivModal=function(show){
        if(show){
            loadModalPopup.style.width=loadModalPopup.parentNode.offsetWidth-2;
            loadModalPopup.style.height=loadModalPopup.parentNode.offsetHeight-3;
        }
        loadModalPopup.style.display=show?'block':'none'
        loadModalContent.style.display=show?'block':'none';
    }    
    this._showHideDivImportPhoto=function(show){
        var div=$(expBoxStr+' .importPhoto').get(0);
        if(div){
            div.style.display=show?'block':'none';
            $(expBoxStr+' .divAddImage').get(0).style.display=show?'none':'block';
        }
    }
    this.showWait=function(){
        this.showHideDivModal(true);
    }
    this.btnPartager_click=function(upPanelPartagerBlog,linkPartager){
        //Execute un vrai PostBack s'il y a un fichier à télécharger
        this.showWait();
        
        var file=$(expBoxStr+' :file').get(0);
        if(file && file.value && file.value.length>0)
            this.makePostBack(upPanelPartagerBlog,linkPartager);
        return true;
    }
    this.makePostBack=function(upPanelPartagerBlog,linkPartager){
        Sys.WebForms.PageRequestManager.getInstance()._updateControls([upPanelPartagerBlog], [], [linkPartager], 90);
    }
}

MicroBlogStatic = {
    showTemporarilyMessage: function(id) {
        var obj = $get(id);
        obj.style.display = 'block';
        var timer = setTimeout(function() {
            obj.style.display = 'none';
        }, 3000);
    },
    clearImageBlogSrc: function(obj) {
        $get(obj).src = '';
    },
    showDivRss: function(targetDiv) {
        var div = $('#' + targetDiv);
        if (div.css('display') == 'none') {
            div.slideDown("fast");
        } else {
            div.slideUp("fast");
        }
    },
    saveScreenSize: function(obj) {
        $get(obj).value = MicroBlogStatic.f_clientWidth() + '; ' + MicroBlogStatic.f_clientHeight();
    },
    f_clientWidth: function() {
        return MicroBlogStatic.f_filterResults(
            window.innerWidth ? window.innerWidth : 0,
            document.documentElement ? document.documentElement.clientWidth : 0,
            document.body ? document.body.clientWidth : 0
        );
    },
    f_clientHeight: function() {
        return MicroBlogStatic.f_filterResults(
            window.innerHeight ? window.innerHeight : 0,
            document.documentElement ? document.documentElement.clientHeight : 0,
            document.body ? document.body.clientHeight : 0
        );
    },
    f_scrollLeft: function() {
        return MicroBlogStatic.f_filterResults(
		    window.pageXOffset ? window.pageXOffset : 0,
		    document.documentElement ? document.documentElement.scrollLeft : 0,
		    document.body ? document.body.scrollLeft : 0
	    );
    },
    f_scrollTop: function() {
        return MicroBlogStatic.f_filterResults(
		    window.pageYOffset ? window.pageYOffset : 0,
		    document.documentElement ? document.documentElement.scrollTop : 0,
		    document.body ? document.body.scrollTop : 0
	    );
    },
    f_filterResults: function(n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
            n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
    },
    centerPopup: function(popup, w, h, windowScroll) {
        var modalPopup = $find(popup);
        //        modalPopup.set_X(x);
        //        modalPopup.set_Y(y);
        modalPopup.set_X((MicroBlogStatic.f_clientWidth() - w) / 2 + MicroBlogStatic.f_scrollLeft());
        modalPopup.set_Y((MicroBlogStatic.f_clientHeight() - h) / 2 + MicroBlogStatic.f_scrollTop());
        modalPopup.show();

    },
    afterRefreshListComments: function(listeBlog) {
        $("[id$=imageDeleteBlog]").mouseenter(function() { MicroBlogStatic._imageDeleteMouseEnter(this); }).mouseout(function() { MicroBlogStatic._imageDeleteMouseOut(this); });
        $(document).ready(function() { $('#' + listeBlog + ' textarea').each(function() { new jQuery.simpleautogrow(this); }); });
    },
    _imageDeleteMouseEnter: function(obj) {
        var reg = new RegExp('([a-zA-Z0-9]+_{1})([a-zA-Z0-9]{1})(\.{1}[a-zA-Z0-9]+)$', 'g');
        obj.src = obj.src.replace(reg, '$1$2d$3');
    },
    _imageDeleteMouseOut: function(obj) {
        var reg = new RegExp('([a-zA-Z0-9]+_{1})([a-zA-Z0-9]{1})d{1}(\.{1}[a-zA-Z0-9]+)$', 'g');
        obj.src = obj.src.replace(reg, '$1$2$3');
    },
    showPopup: function(popup) {
        var modalPopup = $find(popup);
        modalPopup.show();
    },
    showHideObj: function(strObj) {
        var obj = $get(strObj);
        if (obj.style.display == 'none')
            obj.style.display = 'block';
        else obj.style.display = 'none';
    }
}