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();
            hideBanner();
        }
    });

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


function closeIt(){
		$('#page_container').slideUp(effects_speed);
		downMenu();
		showBanner();
}


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

function upMenu(){
   if($.browser.msie){
      $('.menu').animate({bottom: '465px'}, 500);
   }else{
      $('.menu').animate({bottom: '440px'}, 500);
   }
//    if($(document).height() < 720){ $('.menu').animate({paddingTop: '10px'}, 500);}
}

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

function showBanner(){
	$('#presentation').show()
}

function hideBanner(){
	$('#presentation').hide()
}

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

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

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

function request(params, url, callback) {
    $('#page_container').slideUp(effects_speed, function () {
		
        $('#progressbar').show();
//		alert (params.id); // TristansStuff --------------------------------------------------------------------------------------------------
        current_obj = 'data/' + params.id + '.php';
        if (false || typeof(cached[current_obj]) != 'undefined') {
            up(cached[current_obj], callback);
        } else {
            $.get (
                current_obj,
                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);

    $('.page-next').mouseover(function() {
        $('#page_next_bubble').show();
    }).mousemove(function(kmouse){
        $('#page_next_bubble').css({left:kmouse.pageX+1, top:kmouse.pageY+1});
    }).mouseout(function(){
        $('#page_next_bubble').hide();
    });

    $('.page-prev').mouseover(function() {
        $('#page_prev_bubble').show();
    }).mousemove(function(kmouse){
        $('#page_prev_bubble').css({left:kmouse.pageX+1, top:kmouse.pageY+1});
    }).mouseout(function(){
        $('#page_prev_bubble').hide();
    });
}

function toPage(page) {
    changeCol(page);
    $('.paging li a').blur();
}

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);
    }

    $('.paging li a').removeClass('active');
    $('.paging li.page' + num + ' a').addClass('active');

    if ($('.pagecol').length == 1) {
        $('#pagecol1').fadeIn(effects_speed);
    } else {
        $('.pagecol').stop().each(function () {
            var a = false;
            current_col = num;

            $('#pagecol' + num).css('display', 'none');
            if (this.id != 'pagecol' + num) {
                if (!a) {
                    $(this).fadeOut(effects_speed, function () {
                        $('#pagecol' + num).fadeIn(effects_speed, function () {  });
                    });
                    a = true;
                } else {
                    $(this).fadeOut(effects_speed);
                }
            }
        });
    }
}

function prepareGallery() {

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

    $('#pcarousel').jcarousel({
        size:               portfolio_itemList.length,
        initCallback:       portfolio_initCallback,
        itemLoadCallback:   { onBeforeAnimation: portfolio_itemLoadCallback },
		buttonNextEvent: 'click',
        buttonPrevEvent: 'click'
    });
		
    $('.jcarousel-next').click(function() {
 		// moved here by Tristan so gallery pages outside initial page load work (bug: doesnt catch scroll wheel) -------------------------
		$("a[id^='gallery']").each(function (i) {
			if (this.id.match(/(\d+)/g)) {
				$(this).unbind('click', galleryOnClick).bind('click', galleryOnClick);
			}
		});
		// --------------------------------------------------------------------------------------------------------------------------------
    });

    $('.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);
        }
    });
	

}
