jQuery.extend({
    slide: function($container, child_element_no, callback) {
        var $slider = $container.children(":first");
        var new_offset = $slider.children(':eq('+child_element_no+')').offset().left - $container.offset().left - ($slider.offset().left - $container.offset().left);
        $slider.animate( {left: -1 * new_offset}, 600, 'easeOutQuad', callback);
        return this;
    },
    slide_page: function($container, page_no, size, callback) {
        if (typeof(size) == 'undefined') {
            size = parseInt($container.parent().css('width').match(/\d+/));
        }
        var new_offset = size * page_no;
        $container.animate( {left: -1 * new_offset}, 600, 'easeOutQuad', callback);
        return this;
    },
    imgLoad: function(src, loaded_callback) {
        var img = new Image();
        $(img).load( function () {
            loaded_callback(img);
        });
        img.src = src;
        return this;
    },
    dimIn: function (user_css, callback, on_click) {
        $dim = $("#dim");
        if ($dim.length == 0) {
            $dim = $("<div />").attr('id', 'dim').css(
                {position: 'absolute',
                  top: 0,
                  left: 0,
                  width: '100%',
                  height: $(document).height(),
                  zIndex: 99999998,
                  background: '#333',
                  opacity: .4});
            $('body').prepend($dim);
        }
        if (on_click != undefined) {
            $dim.click(on_click);
        }
        if ($dim.data('act') == true) return false;
        if (user_css != undefined) {
            $dim.css(user_css);
        }
        return $dim.data('act', true).fadeIn(400, callback);
    },
    dimOut: function (callback) {
        $dim = $("#dim").unbind('click');
        if ($dim.data('act') == true) {
            $dim.fadeOut(400, callback).data('act', false);
        }
    }
});


function getNext(count, num)
{
    num++;
    if(num >= count)
    {
        num = 0; 
    }    
    return num;
}

function getPrev(count, num)
{
    num--;
    if(num <= 0)
    {
        num = count;
        num--;
    }    
    return num;
}

function updatelinks(count,number)
{    
    nextnumber = getNext(count,number)
    prevnumber = getPrev(count,number)

    $('.nextbut').attr('rel', nextnumber);	
    $('.prevbut').attr('rel', prevnumber);
    $('.nextbut').attr('href', '#'+nextnumber);	
    $('.prevbut').attr('href', '#'+prevnumber);    
}


//- required
function popup(url,number) {
    var images = $('#myImageFlow_images .popuplist');
    var count = images.length;
    
    index = number--;
        
    var nextimage = getNext(count,index);
    var previmage = getPrev(count,index);

    if (typeof(window.popuped) == 'undefined') {
        window.popuped = $('<div style="z-index:99999999; display: none; cursor: pointer; position: absolute; padding: 7px; background: #ffffff; box-shadow: 0 0 5px #666; -moz-box-shadow: 0 0 5px #666; -webkit-box-shadow: 0 0 5px #666;" />').appendTo('body').click( function () {
            $(this).find('img').fadeOut(400).end().fadeOut(400);$.dimOut();
        } );
        $('<img class="popupwindow" style="display: none;" />').appendTo(window.popuped);
        $('<div class="prevnext" style="display: block; background-color: white;"><a class="prevbut" rel="'+ previmage +'" href="#'+ previmage +'">&laquo; Vorige</a> - <a class="nextbut" rel="'+ nextimage +'" href="#'+ nextimage +'">Volgende &raquo;</a></div>').appendTo(window.popuped);
    }


    $('.prevbut').click(function(){
        var p_number = $(this).attr('rel');
        $('.popupwindow').attr('src',$('#myImageFlow_images .popuplist:eq('+p_number+')').attr('longdesc'));
        updatelinks(count,p_number);
        return false;
    })


    $('.nextbut').click(function(){
        var p_number = $(this).attr('rel');
        $('.popupwindow').attr('src',$('#myImageFlow_images .popuplist:eq('+p_number+')').attr('longdesc'));
        updatelinks(count,p_number);
        return false;
    })

    $popuped = window.popuped;

    $.dimIn({}, null, function () {
        window.popuped.click();
    });

    var props = {width: 100,
                  height: 100,
                  left: Math.ceil($(window).width() / 2) - 50,
                  top: $(document).scrollTop() + Math.ceil(($(window).height() - 25) / 2) - 20};

    if ($popuped.css('display') == 'none') {
        $popuped.css(props);
    } else {
        $popuped.find('img').fadeOut(400);
    }

    var img = new Image();

    $popuped.fadeIn(400);

    $(img).load( function () {

        $popuped.find('img').attr('src', img.src).end().animate(
            {
                left: Math.ceil(($(window).width() - img.width) / 2),
                top: (($(window).height() - img.height) / 2) + $(document).scrollTop(),
                height: img.height,
                width: img.width
            }, 400, function () {
                $(this).find('img').fadeIn('fast');
            }
        );


    });

    img.src = url;

}

//function resetnumber(newnumber) {
//	var prevnumber = newnumber; 	
//	var nextnumber = newnumber; 		
//	
//	prevnumber--;
//	nextnumber++;
//	
//	$('.prevbut').attr('rel', prevnumber);
//	$('.nextbut').attr('rel', nextnumber);
//	
//	console.log(prevnumber);
//	console.log(nextnumber);	
//}
