/*--- IE6 hover function ---*/
function ieHover(h_list) {
    $(h_list).hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });
}
/*--- faid gallery function ---*/
function faidGall() {
    var _hold = $('#visual');
    if (_hold.length) {
        var _list = _hold.find('.visual').children();
        var _btn = _hold.find('.menu a');
        var _a = Math.floor(Math.random() * _list.length);
        if ($.browser.msie && $.browser.version < 7) _list.removeClass('active').hide().eq(_a).addClass('active').show();
        else _list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
        _btn.removeClass('active').eq(_a).addClass('active');
        var _ind;
        _btn.mouseenter(function() {
            _ind = _btn.index(this);
            if (_ind != _a) {
                if ($.browser.msie && $.browser.version < 7) {
                    _list.eq(_a).removeClass('active').hide();
                    _list.eq(_ind).addClass('active').show();
                }
                else {
                    _list.eq(_a).removeClass('active').animate({ opacity: 0 }, { queue: false, duration: 1000 });
                    _list.eq(_ind).addClass('active').animate({ opacity: 1 }, { queue: false, duration: 1000 });
                }
            }
        }).mouseleave(function() {
            if (_ind != _a) {
                if ($.browser.msie && $.browser.version < 7) {
                    _list.eq(_ind).removeClass('active').hide();
                    _list.eq(_a).addClass('active').show();
                }
                else {
                    _list.eq(_ind).removeClass('active').animate({ opacity: 0 }, { queue: false, duration: 1000 });
                    _list.eq(_a).addClass('active').animate({ opacity: 1 }, { queue: false, duration: 1000 });
                }
            }
        }).click(function() {
            _a = _ind;
            return false;
        });
    }
}
/*--- slide gallery function ---*/
function slideGall() {
    var stay_time = 10000; //in ms;
    $('div.gallery').each(function() {
        var _hold = $(this);
        var btn_prev = _hold.find('a.link-prev');
        var btn_next = _hold.find('a.link-next');
        var list_h = _hold.find('div.inner > ul');
        var list_w = list_h.children().length * list_h.children().outerWidth();
        var _step = list_h.parent().width();

        var _m = 0;
        var _f1 = false;
        var _t;
        if (list_w > _step) {
            _f1 = true;
            btn_prev.click(function() {
                _f1 = false;
                moveList(false);
                return false;
            });
            btn_next.click(function() {
                _f1 = false;
                moveList(true);
                return false;
            });
        }
        else {
            btn_prev.click(function() {
                return false;
            });
            btn_next.click(function() {
                return false;
            });
        }
        function moveList(_f) {
            if (_f) {
                if (_m < list_w - _step) _m += _step;
                else _m = 0;
            }
            else {
                if (_m > 0) _m -= _step;
                else _m = list_w - _step;
                if (_m < 0) _m = 0;
            }
            list_h.animate({ marginLeft: -_m }, { queue: false, duration: 500 });
            if (_f1) {
                if (_t) clearTimeout(_t);
                _t = setTimeout(function() {
                    moveList(true);
                }, stay_time);
            }
        }
        if (_t) clearTimeout(_t);
        if (_f1) {
            _t = setTimeout(function() {
                moveList(true);
            }, stay_time);
        }
    });
}

function getPageName() {
    var pageName = document.location.href.split('/');
    // Get last url value 
    pageName = pageName[pageName.length - 1];
    // Get page and extenstion
    var values = pageName.split(".");
    if (values.length == 1 || values[0] == 'index')
        return 'home'
    else
        return values[0];

    return pageName;
}

function updateMenu() {
    $("#menu li").removeClass("active");
    $('a.' + activePage).parent().addClass("active");
}

function changeLanguage() {
    // Mapping page key => translated page name
    // Two special keys: url to set url for each language and home just to have same behavior on home page (na page name)
    var translationMap = {
        'italiano': {
            'url': 'http://www.ormetis.it',
            'home': '',
            'solution': 'solutioneo',
            'product': '',
            'customers': 'clientalino',
            'support': 'supporto',
            'news': '',
            'partners': '',
            'company': '',
            'press': '',
            'jobs': '',
            'legal': '',
            'privacy': '',
            'contact': ''
        },
        'english': { 'url': 'http://www.ormetis.com',
            'home': '',
            'solution': 'solution',
            'product': 'product',
            'customers': 'customers1',
            'support': 'support',
            'news': 'news',
            'partners': 'partners',
            'company': 'company',
            'press': 'press',
            'jobs': 'careers',
            'legal': 'legal-information',
            'privacy': 'privacy',
            'contact': 'contact'
        },
        'français': { 'url': 'http://www.ormetis.fr',
            'home': '',
            'solution': 'solution',
            'product': 'produit',
            'customers': 'clients1',
            'support': 'support',
            'news': 'actualite',
            'partners': 'partenaires',
            'company': 'societe',
            'press': 'presse',
            'jobs': 'emplois',
            'legal': 'informations-legales',
            'privacy': 'confidentialite',
            'contact': 'contact'
        }
    
}

    $('#lang ul li').click(function() {
        var lang = $(this).find("a").text().toLowerCase(),
        data = translationMap[lang],
        url = data['url'] + (data[activePage].length > 0 ? '/' + data[activePage] + '.shtml' : '');
        document.location.href = url
    })
}

$(document).ready(function() {
    ieHover('ul.tabs > li, #lang, #menu > li');
    faidGall();
    slideGall();
    updateMenu();
    changeLanguage();
});
