
var Gallery = function(el)
{
    this.el = $(el);

    this.current_image = 0;
    var to_load = 1;
    var m = window.location.pathname.match(/image-(\d+)/);
    if(m)
        to_load = parseInt(m[1]);
    this.num_images = $('.image img', this.el).length;

    this.bindEvents();
    this.dontrefresh = true;
    this.go(0, to_load);
};
Gallery.prototype.referrer = function()
{
    var ret = window.g.cur_loc();
    ret = window.g.tracking_prefix(this.prev_img)+ret;
    return ret;
};
Gallery.prototype.tracking_prefix = function(n)
{
    if(!n)
        n = this.current_image;
    return 'slide_photo-image'+n+'_';
};
Gallery.prototype.cur_loc = function()
{
    var ret = window.location.href.replace(/\/image-(\d+)/,'');
    return ret;
};
Gallery.prototype.go = function(offs, num)
{
    this.prev_img = this.current_image;

    var cur = this.current_image;
    var next;

    if (num)
        next = parseInt(num);
    else
        next = cur + offs;

    if (next > this.num_images)
        next = 1;
    if (next < 1)
        next = this.num_images;

    this.show('image');

    if (cur == next)
        return;

    // TODO: transitions

    // hide current image
    var to_hide = cur;
    if(to_hide == 0)
        to_hide = 1;
    $('.image img[name=image_'+to_hide+']', this.el).hide();
    // hide current caption
    $('.caption[name=caption_'+to_hide+']', this.el).hide();
    // deselect current thumb
    $('.thumb[name=thumb_'+to_hide+']', this.el).removeClass('selected');

    // show next image
    $('.image img[name=image_'+next+']', this.el).show();
    // show next caption
    $('.caption[name=caption_'+next+']', this.el).show();
    // select next thumb
    $('.thumb[name=thumb_'+next+']', this.el).addClass('selected');

    this.current_image = next;

    var counter = this.current_image + '/' + this.num_images;
    $('.counter', this.el).text(counter);

    if(!this.dontrefresh)
        this.refreshAd();
    this.dontrefresh = false;
};
Gallery.prototype.refreshAd = function()
{
    $('.horizadvert iframe,.advert iframe').each(function(i,el){
        el.contentWindow.location.reload();
    });
    try {
        var pageTracker = _gat._getTracker("UA-9853785-1");
        pageTracker._trackPageview();
    } catch(err) { } 

    //refresh aba tracking:
    if(window.g) {
        var src = 'http://secure-au.imrworldwide.com/cgi-bin/m?'
            + 'ci=auditbc-au'
            + '&cg=0'
            + '&rnd='+new Date().getTime()
            + '&si='+window.g.tracking_prefix()+escape(window.g.cur_loc())
            + '&rp='+escape(window.g.referrer());
        var img = $('<img src="'+src+'"/>');
    }
};
Gallery.prototype.show = function(view)
{
    var $img = $('.image, .captions, .viewalbum, .counter', this.el)
    var $thb = $('.thumbnails', this.el)
    if (view == 'image')
    {
        $thb.hide();
        $img.show();
    }
    if (view == 'thumbnails')
    {
        $img.hide();
        $thb.show();
    }
};
Gallery.prototype.bindEvents = function()
{
    var self = this;
    
    $('.controls a', this.el).click(function()
    {
        self.go( $(this).is('.next')?1:-1 );
    });

    $('.thumb', this.el).click(function()
    {
        var num = $(this).attr('name').substr('thumb_'.length);
        self.go(null, num);
    });

    $('.viewalbum', this.el).click(function()
    {
        self.show('thumbnails');
    });

};


