/**
 * $Id$
 */
(function($){
 $.fn.mediagallery = function(options) {
    var gallery = this;
    this.find(".imageThumbs").css("display","block");
    this.find(".videoThumbs").css("display","block");
        
    var images = this.find(".image .thumbLink");
    var videos = this.find(".video a");
    
    var placeholder = $('#media_placeholder');
    if (!(placeholder && (images.length || videos.length))) return this;
    
    var image_hold = false;
    var video_hold = false;    
    var img_target = '';
    var player = '';

    var ch_loaded = 0;
    var ch_all = 0;
    var popup_url = null;

        if (images.length) {
            image_hold = $('<div class="main_photo"><div class="displayfield image" id="id_main_photo">' +
            		'<a href="#" id="show_big_photo_popup"><img src="___" alt="" /></a></div></div>');
            placeholder.append(image_hold);
            image_target = image_hold.find('img');
        };

        if (videos.length) {
            video_hold = $('<div id="flashbanner"><div id="player_placeholder"></div></div>');
            placeholder.append(video_hold);
        };

    // connect image events instantly:
    images.click(function() {
        a = $(this);
        popup_url = a.next('a')[0].href;
        ch_loaded = a;

        var href = a.attr('href');
        if (href == "#") return false;
        
        if (video_hold) {
        	video_hold.css({
        		'visibility': 'hidden',
        		'height': '0px'
        	});
        }
        image_target.attr('src', href);
        var src_image = a.children('img')[0];
        image_target.attr('alt', src_image.alt);
        image_hold.show();
        return false;
    });

    var init_changers = function () {
        if (!options['changers']) return;
        
        ch_all = videos.add(images);
		if ( ch_all.length > 1 ) {
	        gallery.append($('<div id="thumbsChanger"></div>').append(
	        $('<a class="preview" href="#">'+options['changers']['prev']+'</a>').click(function(){
	            i = ch_all.index(ch_loaded.get(0));
	            if (i==0) { i = ch_all.length-1; }
	            else { i = i-1;}
	            ch_all.eq(i).click();
	            return false;
	            })).append(
	        $('<a class="next" href="#">'+options['changers']['next']+'</a>').click(function(){
	            i = ch_all.index(ch_loaded.get(0));
	            if (i==ch_all.length-1) { i = 0; }
	            else { i = i+1;}
	            ch_all.eq(i).click();
	            return false;
	            })
	            ));
		};
    };

    if (videos.length) {        
        // first video
        ch_loaded = videos.eq(0);
        iv_urls = ch_loaded.attr('href').split("#");
        
        var flashvars = $.extend(options['playervars'], {image:iv_urls[0], file:iv_urls[1]});
        // insert video gallery and connect when it's ready
        create_player("player_placeholder", flashvars, options['width'], options['height'], "player_0",
            function(p) { /* callback after player is ready:*/
               if (image_hold) image_hold.hide();
               videos.click(function() {
                    a = $(this); ch_loaded = a;
                    iv_urls = a.attr('href').split("#");
                    if (image_hold) image_hold.hide();                    
                    p.sendEvent("LOAD", {image:iv_urls[0], file:iv_urls[1]}); 
                    /* p.sendEvent("PLAY", true); */
                    video_hold.css({
						'visibility': 'visible',
                        'height': 'auto'
						});                    
                    return false;
                });               
            });
			init_changers();
    } else { // no videos load first image
        images.eq(0).click();
        init_changers();
    };          

    $('#show_big_photo_popup').click(function() {
		var $popup_box = $("<div id='image_popup' class='popup boxLayer' style='display:none;width:auto;height:auto;'></div>");
		var $close = $('<div class="close"><span>close</span></div>');
        var $big_image = $('<img src="'+popup_url+'" />');
        $big_image.load(function() {
            $('body .mainColumn').append($popup_box)
            $popup_box.append($close).append($big_image);
            
            $close.click(function() {
                popup.hidePopup('image_popup');
                $popup_box.remove();
            });
            popup.showPopup('image_popup');
        });
        return false;
		
    })
    
    return gallery;
};
})(jQuery);




