var
    effects_speed = 500,
    current_col,
    current_obj,
    cols_number,
    cached = {},
    portfolio_timer,
    portfolio_time = 500,
    portfolio_itemList_array,
    portfolio_itemList,
    portfolio_timer,
    portfolio_time = 500,
    current_language;

$(document).ready(function () {
    $('.menu a').click(function (e) {
        $('.menu a').removeClass('selected');
        $(this).addClass('selected').blur();
    });

    $("a[id^='gallery']").bind('click', galleryOnClick);

    $("a[id^='page']").click(function (e) {
        if (match = this.id.match(/(\d+)/g)) {
            setCurrentLanguage(this);
            getPage(match);
            upMenu();
        }
    });

    // if URL conrains anchor, open corresponding url
    if (location.hash != '') {
        $(".menu a[href='" + location.hash + "']").click();
    }
});

function galleryOnClick(e) {
    if (match = this.id.match(/(\d+)/g)) {
        setCurrentLanguage(this);
        getGallery(match);
        upMenu();
    }
}

function upMenu(){
	if($(document).height() < 720){ $('.menu').animate({paddingTop: '10px'}, 500);}
}

function downMenu(){
	$('.menu').animate({paddingTop: '120px'}, 500)
}

function setCurrentLanguage(obj) {
    if ($(obj).attr('lang')) {
        current_language = $(obj).attr('lang');
    } else {
        current_language = 0;
    }
}

function getPage(id) {
    request({ id : id, language_id : current_language }, 'redactor.php', 'preparePage');
}

function getGallery(id) {
    request({ id : id }, 'portfolio.php', 'prepareGallery');
}

function request(params, url, callback) {
//    down();

    $('#page_container').slideUp(effects_speed, function () {

        $('#progressbar').show();

        current_obj = url + '_' + params.id + '_' + current_language;
        if (typeof(cached[current_obj]) != 'undefined') {
            up(cached[current_obj], callback);
        } else {
            $.get (
                BASE_URL + '/ajax/' + url,
                params,
                function (data) {
                    cached[current_obj] = data;
                    up(data, callback);
                },
                'json'
            )
        }
    });
}

function down() {
    $('#page_container').slideUp(effects_speed, function () {
        $('#progressbar').show();
    });
}

function up(data, callback) {
    $('#page_container').html(data.__html);

    if (data.script != '') {
        eval(data.__script);
    }

    $('#page_container').slideDown(effects_speed, function () {
        eval(callback + '()');
        $('#progressbar').hide();
    });
}

function preparePage() {
    $('.page-next').click(function() {
        pagerOnclick(1);
    });
    $('.page-prev').click(function() {
        pagerOnclick(-1);
    });
    cols_number = $('.pagecol').length;
    current_col = 1;

    $("#page a[id^='page']").click(function () {
        if (match = this.id.match(/(\d+)/g)) {
            setCurrentLanguage(this);
            getPage(match);
        }
    });

    changeCol(current_col);
}

function pagerOnclick(val) {
    changeCol((current_col + val));
}

function changeCol(num) {
    if (num < 1 || num > cols_number) {
        return false;
    }

    if (cols_number == 1) {
        if ($('.page-prev').css('display') != 'none') $('.page-next').fadeOut(effects_speed);
        if ($('.page-prev').css('display') != 'none') $('.page-prev').fadeOut(effects_speed);
    } else if (num == cols_number) {
        $('.page-next').fadeOut(effects_speed);
        if ($('.page-prev').css('display') == 'none') $('.page-prev').fadeIn(effects_speed);
    } else if (num == 1) {
        $('.page-prev').fadeOut(effects_speed);
        if ($('.page-next').css('display') == 'none') $('.page-next').fadeIn(effects_speed);
    } else {
        if ($('.page-next').css('display') == 'none') $('.page-next').fadeIn(effects_speed);
        if ($('.page-prev').css('display') == 'none') $('.page-prev').fadeIn(effects_speed);
    }

    if ($('#pagecol' +  current_col).css('display') == 'none') {
        $('#pagecol' + num).fadeIn(effects_speed, function () { current_col = num; });
    } else {
        $('#pagecol' +  current_col).fadeOut(effects_speed, function () {
            $('#pagecol' + num).fadeIn(effects_speed, function () { current_col = num; });

        });
    }
}

function prepareGallery() {

    if (typeof(carousel) != 'undefined') {
        carousel.reset();
    }

    $('#pcarousel').jcarousel({
        size:               portfolio_itemList.length,
        initCallback:       portfolio_initCallback,
        itemLoadCallback:   { onBeforeAnimation: portfolio_itemLoadCallback },
        buttonNextCallback: portfolio_buttonnext,
        buttonPrevCallback: portfolio_buttonprev
    });

    $('.jcarousel-next').mouseover(function() {
        portfolio_timer = setTimeout('portfolio_startnext()', portfolio_time);
    });

    $('.jcarousel-next').mouseout(function() {
        portfolio_stop();
    });

    $('.jcarousel-prev').mouseover(function() {
        portfolio_timer = setTimeout('portfolio_startprev()', portfolio_time);
    });

    $('.jcarousel-prev').mouseout(function() {
        portfolio_stop();
    });

    $('.jcarousel-container-horizontal').css('width', parseInt($('.wrapper').width()-160));
    $('.jcarousel-clip-horizontal').css('width', parseInt($('.wrapper').width()-160));

    $("a[id^='gallery']").each(function (i) {
        if (this.id.match(/(\d+)/g)) {
            $(this).unbind('click', galleryOnClick).bind('click', galleryOnClick);
        }
    });


}