var EasyCms = {
    time:new Date(),
    status:true,
    rw:true,
    sendFormLang: function(currentLang, toLang  ){
        if(currentLang == toLang){
            return false;
        }        
        if(document.location.href.indexOf('/'+currentLang+'/')){ 
            document.location.href = document.location.href.replace("/"+currentLang+"/","/"+toLang+"/");           
        }
        else if(document.location.href.indexOf('lang='+currentLang)){
            document.location.href = document.location.href.replace('lang='+currentLang,'lang='+toLang);          
        }
       
        return true;
    },
    sendForm:function(elForm,div){    
        $.ajax({
            url: elForm.action+this.cacheBuster(),
            async:true,
            beforeSend: function(){
                EasyCms.status = false;
                $(div).addClass("Sendloading");
                $(div+ ' .sender').val('Sending...');
                $('input[type=submit]', elForm).attr('disabled', 'disabled');
            },
            data:$(elForm).serialize(),
            complete: function(object, success){
                EasyCms.status = true;
                $(div).removeClass("Sendloading");
                if(success == "success"){                    
                    $(div).html(object.responseText);                    
                }
               
            },           
            error: function(obcjet, errorMessage , objectAlt){
                
                alert(errorMessage);
            },
            processData:true,                      
            type: 'POST'
        });
        return false;
    },
    cacheBuster:function(){
        if(EasyCms.rw)
            return '?'+parseInt(Math.random() * 99999999)+'_'+this.time.getTime();
        else 
            return '&'+parseInt(Math.random() * 99999999)+'_'+this.time.getTime();
    },
    mouseScroller:function(div,listItemDiv, fix){

        var listItem = $(listItemDiv+' li');
        var container = $(div);

        $(div).mousemove(function(e) {
           
            var s_top = parseInt($(container).offset().top + 10);
            var s_bottom = parseInt($(container).height() + s_top);
            var mheight = (s_bottom - s_top - fix);           
            var top_value = Math.round(((s_top - e.pageY) / 100) * mheight / 5.8)
           
            $(listItemDiv).animate({
                top: top_value
            }, {
                queue: false,
                duration: 100
            });

        });

        $(div).mouseout(function() {
            var resetList = setTimeout("$('"+listItemDiv+"').animate({ top: '0px' }, { queue: false, duration: 200 });", 400);
            $(div).mouseover(function() {
                clearTimeout(resetList);
            });
        });

    },
    refreshExplorer: function(val, page , maxPages){

        $('#Params_refresh').val(val);
        var rpp;
        rpp = parseInt($('#Params_rpp').val());
        if(val == 3 && page == maxPages)
            return false;
        if(val == 2 && page == 1)
            return false;
        if(isNaN(rpp) || isNaN(page) || page > maxPages || page <= 0 || rpp <= 0){
            alert('Query error');
            return false;
        }
        
        $('#projectExplorer').html("<div class='loading'>Sending...</div>");
        if(EasyCms.status){
            return EasyCms.sendFrom(document.getElementById('navBarProjects'),'#projectsFinder');
        }
        return false;
    },
    getProject:function(url){
        $('#overlay2 .contentWrap').html('<div class=\"loading\">Loading....</div>');
        $('#overlay2 .contentWrap').load(url);
    },
    embedVideo :function(div, video , w , h) {
        
        videoEmbedCode = video.replace(/height="(\d+)"/, 'height="'+h+'"');
        videoEmbedCode = videoEmbedCode.replace(/width="(\d+)"/, 'width="'+w+'"');
        $(div).html(unescape(videoEmbedCode));

    },
    checklength : function(e , max)
    {        
        var n = e.value.length;
        if (n>max)
        {
            e.value = e.value.substring(0, max);
            return false;
        }
        return true;
    },
    handleCKEditorPost: function (formId, updater  )
    {
        var elForm = document.getElementById(formId);
        if(EasyCms.status){
            $.ajax({
                url: elForm.getAttribute('actionx') + EasyCms.cacheBuster(),
                async:true,
                beforeSend: function(){
                    EasyCms.status = false;
                },
                data:$(elForm).serialize(),
                complete: function(object, success){
                    EasyCms.status = true;
                    if(success == "success"){
                        //$('#'+updater).html(object.responseText);
                         location.reload();
                    }                  
                },
                error: function(obcjet, errorMessage , objectAlt){
                    alert(errorMessage);
                },
                processData:true,
                type: 'POST'
            });
        }
    },
    ajaxForm: function(e, idform, updatediv )
    {
        $.post($(e).attr('action'), $(idform).serialize(), function(data) {

            $(updatediv).html(data);
        
        });
    }
};
