/**
 *  VideoGallery
 *  v0.97 beta
 *  @author Davide Redaelli
 *  required: jwplayer
 */
jQuery.fn.VideoGallery = function(options) {

    var defaults = {
        width:						600,
		videoHeight:				240,
        videoWidth:					370,
		addPlayIcon:				true,
        plyaIconImage:              "",
		caption:					".caption",
		captionTitle:				".title",
		captionDescription:			".description",
        firstVideo:					".first",
		firstImage:					"",
		flashplayer:				"js/jwplayer/player.swf",
        playerSkin:					"js/jwplayer/skin/glow/glow.xml"
    };
    if (this.length > 1) {
        return this.each(function() {
            $(this).VideoGallery(options);
        });
    }

    this.init = function(options) {
        var opts = jQuery.extend(defaults, options),
        playlist = jQuery(this);

        playlist.addClass('jvg_list');
        /* set width and height */
        playlist.width(opts.width-opts.videoWidth);
        playlist.height(opts.videoHeight);
        /* add inner div */
        playlist.children('li').wrapInner('<div class="jvg_inner" />');
        /* get unique id */
        id=$('.jvg_list').length+1;
        /* create container div */
        $container=$('<div class="jvg_container" />').width(opts.width);
        playlist.wrap($container);
        /* create video div */
        $video = $('<div id="jvg_video_'+id+'">Loading the player...</div>');
        $video.insertBefore(playlist);
        $video.wrap('<div class="jvg_video" />');
        /* set video width and height*/
        $video.width(opts.videoWidth);
        $video.height(opts.videoHeight);
        /* add class to plyalist link */
        playlist.children('li').find('a:first').addClass('jvg_link');
        /* add playicon */
        if(opts.addPlayIcon){
            //$(window).bind("load", function(){
                playlist.children('li').find('a:first').append('<span class="jvg_playIcon" />');
                 if(opts.plyaIconImage){
                    $('.jvg_playIcon').css('background-image','url('+opts.plyaIconImage+')');
                }

            //});
        }
        /* add description class */                
        playlist.children('li').find(opts.caption).addClass('jvg_caption').css({'margin-left': playlist.children('li').find('a:first').outerWidth()});
        playlist.children('li').find(opts.captionTitle).addClass('jvg_title');
        playlist.children('li').find(opts.captionDescription).addClass('jvg_description');
        /* get first video */
        if(opts.firstVideo && playlist.children('li').find(opts.firstVideo).size()>0){
            playlist.children('li').find(opts.firstVideo).parent().parent('li').addClass('jvg_active');
            file=playlist.children('li').find(opts.firstVideo).attr("href");
        }else{
            playlist.children('li:first').addClass('jvg_active');
            file=playlist.children('li').find('a:first').attr("href");
        }
        /* show first video */
        jwplayer($video.attr('id')).setup({
            autostart: false,
            skin: opts.playerSkin,
            flashplayer: opts.flashplayer,
            file: file,
            image: opts.firstImage,
            height: opts.videoHeight,
            width: opts.videoWidth
        });
        /* adds event click */
        playlist.children('li').find('.jvg_link').click(function(){
            vid=$(this).parent('.jvg_inner').parent().parent('.jvg_list').prev('.jvg_video').find('object').attr('id');
            playlist.find('li').removeClass('jvg_active');
            $(this).parent().parent().addClass('jvg_active');
            file=$(this).attr("href");
            jwplayer(vid).setup({
                autostart: true,
                skin: opts.playerSkin,
                flashplayer: opts.flashplayer,
                file: file,
                height: opts.videoHeight,
                width: opts.videoWidth
            });
            return false;
        });
        /* adds hover classs */
        playlist.children('li').hover(function() {
            $(this).addClass("jvg_hover");
        }, function() {
            $(this).removeClass("jvg_hover");
        });


    }
     //init
    this.init(options);
}
