var title = {
    'gallery':{
        'ru':'Галерея Марины Гисич'
        ,
        'en':'Marina Gisich Gallery'
    }
    ,
    'studio':{
        'ru':'Дизайн-студия Марины Гисич'
        ,
        'en':'Marina Gisich design-studio'
    }
}
var section = 'gallery';
var AjE = {
    redirect : {
        'gallery' : 'gallery/artists/img'
        ,
        'gallery/exhibitions' : 'gallery/exhibitions/current'
        ,
        'gallery/publications' : 'gallery/publications/list'
        ,
        'gallery/events'	: 'gallery/events/list'
        ,
        'studio' : 'studio/projects/img'
        ,
        'studio/projects' : 'studio/projects/img'
        ,
        'studio/art' : 'studio/art/img'
        ,
        'studio/publications': 'studio/publications/list'
    }
    ,
    lang : 'ru'
    ,
    checkLang : function(){
        var hash 	= document.location.hash;
        var langPos = hash.indexOf('-');
        if (langPos >= 0) {
            AjE.lang = hash.substr(langPos-2,langPos-1);
        } else {
            AjE.lang = 'ru';
        }
        $('[name=lang]').val(AjE.lang);//.append('<input type="hidden" name="lang" value="'+AjE.lang+'"/>');
    }
    ,
    urlToAction : function(hash){
        hash = hash.substr(1);
        //		console.log(hash);
        var langPos = hash.indexOf('-');
        //		var lang = 'ru';
        if ( langPos >= 0) {
            AjE.lang = hash.substr(langPos-2,langPos);
            hash 	 = hash.substr(langPos+1);
        }
        hash = AjE.checkRedirect(hash);
        //		var redHash = hash;
        //		if(hash[hash.length-1]=='/'){
        //			redHash = redHash.substr(0, redHash.length-1);
        //		}
        //		if( AjE.redirect[redHash]) {
        //			hash = AjE.checkRedirect(hash);
        ////			hash = AjE.redirect[hash];
        //		}
        if(!$.browser.msie){
            langPos = hash.indexOf('-');
            if ( langPos >= 0) {
                AjE.lang = hash.substr(langPos-2,langPos);
                hash 	 = hash.substr(langPos+1);
            }
            $('title').text(title[section][AjE.lang]);
        }
        //		var lang = 'ru';
        var resHash = AjE.findPath(hash);
        var howmany = 1;
//        if($.browser.msie){
//            howmany = 2;
//        }
        if(AjE.lang != 'ru' && typeof AjE.lang == 'string') {
            if(resHash.charAt(resHash.length-1) == 'y') {
                resHash = resHash.substr(0, resHash.length-howmany)+AjE.lang+'/';
            } else {
                resHash = resHash.substr(0, resHash.length-howmany)+'@/'+AjE.lang+'/';
            }
        } else {
            resHash = resHash.substr(0, resHash.length-howmany);
        }
        return resHash;
    }
    ,
    structure: {
        'gallery' : {
            'artists':{
                'img':true,
                'list':true,
                'cv':true,
                'series':true,
                'works':{
                    'videos':true
                },
                'publications':true,
                'videos':true,
                'video':true
            },
            'exhibitions':{
                'images':true,
                'work':true,
                'info':true,
                'invitation':true
            },
            'publications':{
                'list':true
            },
            'news':{
                'list':true
            },
            'gallery':true,
            'history':true,
            'events':{
                'list' : true
            },
            'press':true,
            'contacts':true,
            'careers':true,
            'internship':true
        }
        ,
        'studio' : {
            'projects':{
                'img': true,
                'list': true,
                'images': true
            },
            'art':{
                'img': true,
                'list': true,
                'images': true
            },
            'studio':true,
            'publications':{
                'list': true,
                'images': true
            },
            'press':{
                'list': true,
                'images': true
            },
            'contacts':true,
            'careers':true,
            'internship':true
        }
    }
    ,
    action : ''
    ,
    resultAction : ''
    ,
    findPath: function(str, struct ) {
        var parts = str.split('/');
        if(typeof action != "string") {
            var action = '';
        }
        if( typeof struct != "object" && typeof struct != "boolean") {
            struct = AjE.structure;
        }
        if(parts[0] == "") {
            parts.shift();
        }
        var part = parts[0];
        if(part == 'gallery' || part == 'studio'){
            section = part;
        }
        if (struct[part]) {
            this.action += '/'+part;
            parts.shift();
            str = parts.join('/');
            AjE.findPath(str, struct[part]);
        } else {
            if( parts.length != 0 ) {
                this.action += '/@/'+parts.join('/')+'/y';
            } else {
                this.action += '/n';
            }
            this.resultAction = this.action;
            this.action = '';
        }
        return this.resultAction;
    }
    ,
    checkImagesLoad : function(images, nextAction, callbackOnLast){
        callbackOnLast = (typeof callbackOnLast == 'boolean')?callbackOnLast:false;
        var loadedImages = {};
        var counter = 0;
        if(callbackOnLast) {
            var resultLength = AjE.count(images);
        }
        for (i in images) {
            loadedImages[i] = new Image();
            var path = 'http://'+document.location.host+'/'+images[i];
            //			var path = 'http://gisich.com/shared/images/'+images[i];
            //			loadedImages[i].src = path;
            if(!$.browser.msie){
                loadedImages[i].onload	= callback(i);
                loadedImages[i].onerror = callback(i);
            } else {
                callback(i);
            }
        //			loadedImages[i].src = path;
        }
        function callback(param){
            counter++;
            //			console.log(counter);
            //			alert(name+' '+message);
            if(callbackOnLast) {
                if(resultLength == counter){
                    nextAction(param);
                }
            } else {
                nextAction(param);
            }
        }
    }
    ,
    count : function(counting) {
        var counter = 0;
        for (i in counting) {
            counter++;
        }
        return counter;
    }
    ,
    checkRedirect : function(checking) {
        if(checking[checking.length-1] == '/') {
            checking = checking.substr(0, checking.length-1);
        }
        if(AjE.redirect[checking]) {
            checking = AjE.redirect[checking];
        }
        return checking;
    }
};
