function PhotoGallery() {

    var pgcontainer = null;
    var pgWindow    = 0;

    var pgpictures  = [];
    var currpicture = 1;
    var picturesize = {
        width       : 0,
        height      : 0
    }

    var nextLink, prevLink = null;
    var list        = null;
    var pgMutex     = true;
    var amIntv      = null;
    var pg          = null;
    var DOMgallery  = new _H.DOM();


    this.setLabelClick = function() {
        var _self = this;

        totlink     = pgpictures.length;
        while (totlink--) {

            var label = pgpictures[totlink];

            label.onclick = (function(l) {

                return function() {
                    var input = l.getElementsByTagName('input')[0];
                    input.checked = 'checked';

                    //alert(_self.previousChecked);
                    if (_self.previousChecked) {
                        //_self.previousChecked.parentNode.className = '';
                        //_self.previousChecked.checked = '';
                        _H.$(_self.previousChecked).parentNode.className = '';
                        _H.$(_self.previousChecked).checked = '';
                    };

                    if (!l.className || l.className != 'checked') {
                        _H.addClass(l, 'checked')
                    };
                    _self.previousChecked = input.id;
                    return false;

                };
            })(label);
        };
    };


    this.initGallery = function() {

        //alert(this.pg)

        var _self = this;
        pgpictures      = pg.getElementsByTagName('label');
        pgcontainer     = pg.getElementsByTagName('div')[0];
        list            = pg.getElementsByTagName('ul')[0];


        currlink    = 1;
        this.setLabelClick();
    };


    this.setClasses = function() {
        _H.addClass(pg, "js");
        pgcontainer         = pg.getElementsByTagName('div')[0];
        var w = [((pgpictures.length * picturesize.width)+2), 'px'].join('');
        pgcontainer.style.width = w;
    };

    this.createLinks = function() {

        if (pgWindow >= pgpictures.length) return false;
        var _self = this;

        DOMgallery.create({
			"a" : {
				"class"     : "prevpic",
				"href"      : "#",
				"text"      : "Previous game"
			}
		}).append('after', pg);
		prevLink = DOMgallery.getRootNode();

        DOMgallery.create({
            "a" : {
                "class"     : "nextpic",
                "href"      : "#",
                "text"      : "Next game"
            }
        }).append('after', pg);
        nextLink = DOMgallery.getRootNode();

		
				
        prevLink.onclick = function() {
            return _self.prevClick();
        };

        nextLink.onclick = function() {
            return _self.nextClick(false);
        }
    }


    this.nextClick = function(automove) {
        var _self = this;
        if (!automove) {
            clearInterval(amIntv);
        };

        if (pgMutex) {
            jQuery('.photogallery li').each(function(li) {
                jQuery(this).css("right", 0);
            });


            pgMutex = false;

            firstItem = pg.getElementsByTagName('li')[0];
            firstItemCloned =  firstItem.cloneNode(true);

            jQuery(firstItem).animate({width: 0}, 300, function() {
                firstItem.parentNode.removeChild(firstItem);
                list.appendChild(firstItemCloned);
                _self.setLabelClick();
                pgMutex = true;
            });

        }
        return false;
    };


    this.prevClick = function() {
        var _self = this;
        clearInterval(amIntv);

        if (pgMutex) {
            jQuery('.photogallery li').each(function(i) {
                jQuery(this).css("left", 0);
            });

            pgMutex = false;

            firstItem = pg.getElementsByTagName('li')[0];
            sizedItem = pg.getElementsByTagName('li')[pgWindow];

            lastItem = pg.getElementsByTagName('li')[pgpictures.length - 1];
            lastItemCloned = lastItem.cloneNode(true);
            lastItemCloned.style.width = '0';
            list.insertBefore(lastItemCloned, firstItem);

            lastItem.parentNode.removeChild(lastItem);

            jQuery(lastItemCloned).animate({'width': (picturesize.width-1) }, 300, function() {
                _self.setLabelClick();
                pgMutex = true;
            });

        };

        return false;
    };


    this.autoMove = function(mv) {
        if (mv === 0) return false;
        var _self = this;

        amIntv = setInterval(function() {
            _self.nextClick(true);
        }, mv);
    };


    this.init = function(idpg, width, height, size, mv) {
        if (_H.$(idpg)) {
            pg = _H.$(idpg);
            picturesize.width   = width;
            picturesize.height  = height;
            pgWindow            = size

            this.initGallery();
            this.setClasses();
            this.createLinks();
            this.autoMove(mv || 0);
        }
    };
};


/********************************************************************************/

if (_H.$('choosefriend')) {
    var origvalue = _H.$('choosefriend').value;
    _H.$('choosefriend').onfocus = function() {
        if (this.value == origvalue) this.value = '';
    }
    _H.$('choosefriend').onblur = function() {
        if (this.value == '') this.value = origvalue;
    }
}
