var Doc = function() {
    this.clip = null;
    this.h_c = 0;
    
    function is_safary() {
        var sAgent = navigator.userAgent.toLowerCase() ;
        if ( sAgent.indexOf( ' applewebkit/' ) != -1 ) {
            return ( sAgent.match( / applewebkit\/(\d+)/ )[1] >= 522 );  // Build must be at least 522 (v3)
        }
        return false;
    }
    function is_mac() {
        if (navigator.platform) {
            if (navigator.platform.indexOf('Mac') != -1) {
                return true;
            }
        }
        return false;
    }
    this.safary_mac = (is_safary() && is_mac()) ? true : false;
    
    this.replaceUrls = function() {
        $('a').each(function(){
            if (window.location.hostname == this.hostname) {
                if (this.href.search('#') != -1) return;
                if (this.target == '_blank') return;
                
                $(this).click(function() {
                    this.blur();
                    //var link = $(this).attr('href');
                    var link = this.pathname;
                    
                    $.address.value(link);
                    return false;  
                });  
            }
        });
    }
    
    this.changeUrl = function(url) {
        //if (window.location.hash == '') return;
        var scrollTop  = $(document).scrollTop();
        var scrollLeft = $(document).scrollLeft();
        this.prepare();
        
        JsHttpRequest.query(
            url+'?ajax&scrollTop='+scrollTop+'&scrollLeft='+scrollLeft+'&cache='+Utils.getCacheValue(),
            { },
            function(result, error) {
                if (error) alert(error);
                if (result['error'].length) {
                    for(var i in result['error']) {
                        alert(result['error'][i]);
                    }
                }
                Doc.update(result['doc']);
            }
        );
    }
    
    this.prepare = function() {
        $('#ajax').empty().append('<div class="loader"></div>');
    }
    
    this.update = function(doc) {
        if ((doc.redirect!=null) && (doc.redirect.length>0)) {
            $.address.value(doc.redirect);
            return false;
        }
        
        if (doc.module == '404') {
            window.location = doc.fullpath;
            return;
        }
        
        if (typeof(pageTracker) != 'undefined') {
            pageTracker._trackPageview(doc.fullpath);
        }
        
        if (doc.menu.length > 0) {
            for (var i in doc.menu) {
                var m = doc.menu[i];
                var m_item = document.getElementById('menu_item_'+m.id);
                if (m_item != null) {
                    if (m.active == 1) {
                        Utils.addClassName(m_item, 'active');
                    } else {
                        Utils.removeClassName(m_item, 'active');
                    }
                }
            }
        }
        
        if (doc.index != null) {
            $('#mainlogo').css('cursor', 'auto').click(function(){
                return false;
            });
        } else {
            $('#mainlogo').css('cursor', 'pointer').click(function(){
                $.address.value('/');
            });
        }
        
        $('#ajax').empty().append(doc['body']);
        document.title = doc['title'];
        
        if (doc.module == 'contact') {
            $('#contacts_div').css('display', 'none');
            var link = document.getElementById('icq_link');
            if (link != null) {
                this.clip = new ZeroClipboard.Client();
                this.clip.setHandCursor(true);
                this.clip.setCSSEffects(true);
                this.clip.setText(link.innerHTML);
                
                this.clip.addEventListener( 'complete', function(client) {
                    alert("Номер ICQ скопирован в буфер обмена.");
                });
                this.clip.glue('icq_link');
            }
        } else {
            $('#contacts_div').css('display', 'block');
        }
        
        if (doc.ajax_hidden == 1) {
            document.getElementById('ajax').style.overflow = '';
            setSlider();
            fixSliderWidth();
            Utils.addClassName(document.getElementById('footer'), 'works-f');
        } else {
            document.getElementById('ajax').style.overflow = 'hidden';
            Utils.removeClassName(document.getElementById('footer'), 'works-f');
            document.getElementById('footer').style.width = 'auto';
        }
        
        var h_c = doc.height_correct;
        if (this.safary_mac && (doc.index != null)) {
            h_c = 20;
        }
        this.h_c = h_c;
        
        Doc.replaceUrls();
        Doc.animateHeight();
        
        if (doc.scrollTop != null) {
            $(document).scrollTop(doc.scrollTop);
        }
        if (doc.scrollLeft != null) {
            $(document).scrollLeft(doc.scrollLeft);
        }
    }
    
    this.animateHeight = function() {
        //$('#ajax').animate({'height': 'show'}, 1000);
    }
    
    return this;
} ();

$.address.change(function(event){
    Doc.changeUrl(event.path);
});
$(document).ready(function(){
    Doc.replaceUrls();
});
