var Gallery = (function(){
    var DATA = {
            basePath    : null,
            isLogged    : false,
            chList      : [],
            chRef       : {},
            chathost    : null,
            picIdx      : 0
        };
        
    var ORIG = {
            footer : null,
            footerData : null
        };

    function _init( basePath, isLogged, chathosts, fromProfile, fromList ) {
        DATA.basePath = basePath;
        DATA.isLogged = isLogged;
        DATA.chList = chathosts;
        DATA.fromProfile = fromProfile ? 1 : 0;
        DATA.fromList = fromList ? 1 : 0;
        
        for ( var i = 0; i < DATA.chList.length; i++ ) {
            DATA.chRef[ DATA.chList[ i ].getID() ] = i;
        }
        
        $('#galleryBody .prev a')
            .unbind('click.gallery')
            .bind('click.gallery', displayPrev);

        $('#galleryBody .next a')
            .unbind('click.gallery')
            .bind('click.gallery', displayNext);
    };
    
    function _show( chathostID, idx ) {
        if ( !( chathostID in DATA.chRef ) ) {
            _hide();
            return;
        }

		DATA.chathost = DATA.chList[ DATA.chRef[ chathostID ] ];
		DATA.chathost.setLogged( DATA.isLogged );
        DATA.picIdx = idx || 0;

        ORIG.footer = $('[rel=galleryData] >[rel=galleryFooter]');
        ORIG.footerData = ORIG.footer.contents();

        // reset title
        $('#galleryHeader').hide().empty();

        // reset initial size
        $('#galleryBody .photo').empty().width( 300 ).height( 300 );
        //$('#galleryBody').width( 300 ).height( 300 );

        // assign actions to buttons
        $('#galleryFooter .profile a').attr('href', '/webcam.html?ci='+ chathostID);
        $('#galleryFooter .chatnow a').attr('href', '/accessvid.html?ci='+ chathostID);
        $('#galleryFooter .alert a').attr('href', '/alerts.html?c='+ chathostID);

        resetButtons();

        $.easyBox({
            target  : '[rel=galleryData] >[rel=galleryBody]',
            type    : 'inline',
            onClose : galleryOnClose,
            onShow  : resetButtons,
            loader  : '/img/events/32/loading.gif'
        });

        loadImage( DATA.picIdx );
        
        return false;
    };
    
    function _hide() {
        DATA.chathost = null;
        DATA.picIdx = 0;

        $.easyBox().hide();
    };
    
    function resetButtons() {
        // hide all buttons to show later
        $('#galleryFooter .profile').hide();
        $('#galleryFooter .chatnow').hide();
        $('#galleryFooter .alert').hide();
        $('#galleryFooter .login').hide();
        $('#galleryFooter .register').hide();
    };
    
    function galleryOnClose() {
        if ( ORIG.footer != null ) {
            ORIG.footer.html( ORIG.footerData );
            ORIG.footer = null;
            ORIG.footerData = null;
        }
    };

    function displayPrev(e) {
        e.preventDefault();
        
        loadImage( DATA.chathost.getPrevPicID() );
    };
    
    function displayNext(e) {
        e.preventDefault();
		loadImage( DATA.chathost.getNextPicID() );
    };
    
	function loadImage( idx ) {
        if ( idx == null ) {
            return;
        }
        
        if ( DATA.fromList ) {
            var picPath = DATA.chathost.getArchivePicPath( idx );
        }
        else {
            var picPath = DATA.chathost.getPicPath( idx );
        }
        
		var picURL = DATA.basePath +'/'+ picPath;
		
		$('#galleryBody .photo > img').css({ opacity:0.7 });
        $('#galleryBody .prev').hide();
        $('#galleryBody .next').hide();

		var tmpImg = new Image();
		
		tmpImg.onload = function() {
            displayPhoto( tmpImg, idx + 1 );
            tmpImg.onload = {};
            delete tmpImg;
        };
        
		tmpImg.src = picURL;
	};
	
    function displayPhoto( img, picID ) {
        var screenName = DATA.chathost.getScreenName();
        var picCount = DATA.chathost.getPicsCount( DATA.fromList );

        resetButtons();

        $('#galleryBody .photo').width( img.width ).height( img.height );
        $('#galleryBody').width( img.width ).height( img.height );
    
        if ( !DATA.isLogged && picID == 4 ) {
            $('#galleryFooter .login').show();
            $('#galleryFooter .register').show();
        }
        else if ( !DATA.fromProfile ) {
            $('#galleryFooter .profile').show();

            if ( DATA.isLogged ) {
                if ( DATA.chathost.isOnLine() ) {
                    $('#galleryFooter .chatnow').show();
                }
                else {
                    $('#galleryFooter .alert').show();
                }
            }
            else {
                $('#galleryFooter .chatnow').show();
            }
        }

        if ( DATA.chathost.hasPrevPic( DATA.fromList ) ) {
            $('#galleryBody .prev').show();
        }

        if ( DATA.chathost.hasNextPic( DATA.fromList ) ) {
            $('#galleryBody .next').show();
        }
        
        $('<img>')
            .attr({
                    src     : img.src,
                    width   : img.width,
                    height  : img.height,
                    alt     : ''
                })
            .appendTo( $('#galleryBody .photo').empty() );

        $.easyBox({
                target  : '[rel=galleryData] >[rel=galleryBody]',
                type    : 'inline',
                title   : '<div><span class="sname">'+ screenName +'</span> (Image '+ picID +' of '+ picCount +')</div>',
                onClose : galleryOnClose,
                onOpen  : function () {
                    ORIG.footerData && this.setFooter( ORIG.footerData );
                }
            });
    }

    return {
        init : _init,
        show : _show,
        hide : _hide
    };
})();

/*
 * @id
 * @screenName
 * @pics : number of pictures for logged in clients
 * @folderId : folder Id when clients are logged in
 */
function Chathost(id, screenName, pics, isOnline){
	this.id = id || 0;
	this.screenName = screenName || "";
	
	if ( pics.constructor == Array ) {
        this.pics = pics.length;
        this.picList = pics;
    }
    else {
	   this.pics = pics || 0;
	   this.picList = [];
    }
    
	this.isOnline = isOnline || false;
	this.isLogged = false;
	this.picIdx = null;
}

Chathost.prototype = {
	getID : function() {
		return this.id;
	},
	setLogged : function( isLogged ) {
		this.isLogged = isLogged ? true : false;
	},
	getScreenName : function() {
		return this.screenName;
	},
	getPicsCount : function( isFromList ) {
		return ( this.isLogged || isFromList ) ? this.pics : 4;
	},
	getPicID : function() {
		return this.picIdx;
	},
    getPicPath : function( idx ) {
        var cnt = this.getPicsCount();
        this.picIdx = idx;
	
        if ( this.picIdx > cnt ) {
            this.picIdx = cnt;
        }
        else if ( this.picIdx < 0 ) {
            this.picIdx = 0;
        }

        var picID = this.picIdx + 1;

		return this.id +'/'+ ( this.isLogged ? '' : 'pre' ) +'/girl'+ ( picID < 10 ? '0' : '' ) + picID + '.jpg';
	},
	getArchivePicPath : function( idx ) {
        var cnt = this.getPicsCount( true );
        this.picIdx = idx;

        if ( this.picIdx > cnt ) {
            this.picIdx = cnt;
        }
        else if ( this.picIdx < 0 ) {
            this.picIdx = 0;
        }
        
		return  this.picList[ this.picIdx ] + '.jpg';
	},
	hasPrevPic : function( isFromList ) {
        return ( this.picIdx > 0 );
    },
	hasNextPic : function( isFromList ) {
        return ( this.picIdx < ( this.getPicsCount( isFromList ) - 1 ) );
    },
	getPrevPicID : function() {
        return this.hasPrevPic() ? this.picIdx - 1 : null;
    },
	getNextPicID : function() {
        return this.hasNextPic() ? this.picIdx + 1 : null;
    },
	isOnLine : function() {
        return this.isOnline;
    }
};

