$(function () {
    var act;
    function strStartsWith(str, prefix) {
        return str.indexOf(prefix) === 0;
    }
    function all_null() {
        $('#content > article').css({ position: 'absolute', display: 'none' });
        $('#content .box').css({ width: '0', height: '0', marginTop: '342px', marginLeft: '350px' });
        $('#content .box').css({ opacity: '0.9' });
        $('#content .text').css({ opacity: '0', display: 'none' });
    }
    function null_content() {
        $('#content > article').css({ display: 'none' });
        $('#content .box').css({ width: '0', height: '0', marginTop: '342px', marginLeft: '350px' });
        $('#content .text').css({ opacity: '0', display: 'none' });
    }
    all_null();
    function open_page(page) {
        if (page != '#page_projects') {
            $(page).css({ display: 'block' }).find('.box').animate({ width: '50%', height: '50%', marginTop: '171', marginLeft: '175', rotate: '+=720deg' }, 400).animate({ width: '100%', height: '100%', marginTop: '0', marginLeft: '0' }, 400, 'easeOutBack', function () {
                $(page).find('.text').css({ display: 'block' }).animate({ opacity: '1' }, 400, function () {
                    act = page;
                });
            });
        } else {
            $(page).css({ display: 'block' });
            $(page).find('.text').css({ display: 'block' }).animate({ opacity: '1' }, 400, function () {
                act = page;
            });
        }
    }
    page = location.hash.slice(0, -1);
    if ((page != '') && (page != '#')) {
        var nav = page.substr(6);
        nav = '.nav' + nav;
        $(nav).addClass('active');
        $(nav).find('a').unbind('mouseenter, mouseleave').parent().stop().animate({ backgroundPosition: '-100px -266px' }, 400, 'easeInSine');
        if (page == '#page_projects') { $('.ban3 a').unbind('mouseenter, mouseleave').find('img').stop().animate({ rotate: '-45deg' }, 1200, 'easeOutElastic') }
        open_page(page);
    } else { $('.nav1 a').unbind('mouseenter, mouseleave').parent().stop().animate({ backgroundPosition: '-100px -266px' }, 400, 'easeInSine'); }

    $('a').click(function () {
        page = $(this).attr('href');

        var _pageToD = page.toLowerCase().indexOf('default.aspx');
        var _pageToP = page.toLowerCase().indexOf('products.aspx');
        var _pageToN = page.toLowerCase().indexOf('news.aspx');
        var _pageNewsItem = page.toLowerCase().indexOf('#page_news_');

        var _pageCurrentD = document.URL.toLowerCase().indexOf('default.aspx');
        var _pageCurrentP = document.URL.toLowerCase().indexOf('products.aspx');
        var _pageCurrentN = document.URL.toLowerCase().indexOf('news.aspx');
        var _pageCurrentNewsItem = document.URL.toLowerCase().indexOf('#page_news_');

        var needeRedirect = false;
        if (_pageToP >= 0 && _pageCurrentP == -1) {
            needeRedirect = true;
        }
        else if (_pageToD >= 0 && _pageCurrentD == -1) {
            needeRedirect = true;
        }
        else if (_pageToN >= 0 && _pageCurrentN == -1) {
            needeRedirect = true;
        }
        else if (_pageCurrentN >= 0 && _pageNewsItem >= 0 && _pageCurrentNewsItem >= 0) {
            var myval = page.toLowerCase().substr(page.toLowerCase().indexOf('#page_news'), page.toLowerCase().length - page.toLowerCase().indexOf('#page_news'));
            myval = myval.substr(11, myval.length - 11);
            myval = 'News.aspx?page=' + myval + '#page_news_' + myval + '/'
            window.location.href = myval
            return false;
        }
        else {
            if (strStartsWith(page, "#") == false) {
                page = page.substr(page.indexOf('#'), page.length - 5);
            }
        }
        
        if (needeRedirect == true) {
            window.location.href = page + '/';
            return false;
        }
        else {
            if (page.substr(page.indexOf('#'), 6) == '#page_') {
                if (page == '#page_0') {
                    menu_hover();
                    banners_hover();
                    $(this).unbind('mouseenter, mouseleave').parent().stop().animate({ backgroundPosition: '-100px -266px' }, 400, 'easeInSine');
                    $('#menu > li').removeClass('active').find('> a').mouseleave();
                    $('.banners > li').find('> a').mouseleave();
                    $(act).find('.text').animate({ opacity: '0' }, 400, function () {
                        $(act).find('.text').css({ display: 'none' });
                        if (act != '#page_projects') {
                            $(act).find('.box').animate({ width: '50%', height: '50%', marginTop: '171', marginLeft: '175' }, 400, 'easeInBack').animate({ width: '0', height: '0', marginTop: '342px', marginLeft: '350px', rotate: '+=720deg' }, 400, function () {
                                null_content();
                                location.hash = 'page_home/';
                                act = 0
                            })
                        } else {
                            null_content();
                            location.hash = 'page_home/';
                            act = 0
                        }
                    })
                } else {
                    menu_hover();
                    banners_hover();
                    $(this).unbind('mouseenter, mouseleave').parent().stop().animate({ backgroundPosition: '-100px -266px' }, 400, 'easeInSine');
                    $('#menu > li').removeClass('active').find('> a').mouseleave();
                    $('.banners > li').find('> a').mouseleave();
                    if (act) {
                        $(act).find('.text').animate({ opacity: '0' }, 400, function () {
                            $(act).find('.text').css({ display: 'none' })
                            if (act != '#page_projects') {

                                $(act).find('.box').animate({ width: '50%', height: '50%', marginTop: '171', marginLeft: '175' }, 400, 'easeInBack').animate({ width: '0', height: '0', marginTop: '342px', marginLeft: '350px', rotate: '+=720deg' }, 400, function () {
                                    null_content();
                                    open_page(page);
                                    location.hash = page + '/';
                                })
                            } else {
                                null_content();
                                open_page(page);
                                location.hash = page + '/';
                            }
                        })
                    } else {
                        open_page(page);
                        location.hash = page + '/';
                    }
                }
                return false;
            } else { return true }
        }
    })
})
