cghub.forum = {};

cghub.forum.lazyLoadedImages = [];
cghub.forum.lazyloadForumImages = function() {
    var images = $('div[id^=\"post_message_\"] img');

	images.each(function(){
		var img = this;
    //images.lazyload({
    //   'threshold' : 200, //how close to the edge image should come before it is loaded
    //   'placeholder' : '/img/1x1.gif',
    //   'callback'    : function(img){
            cghub.forum.lazyLoadedImages.push(img);
            cghub.forum.imageSize(img);
    //   }
    });
};

cghub.forum.imageSize = function(img) {
    img = $(img);
    //$('div[id^=\"post_message_\"] img').length

    //var img = $('img[src=\"http://cghub.localhost/images/download/757/\"]');
    var table = img.parents('table[id^=\"post\"]');
    var leftColumn = $('tr:first td:first', table);
    var rightColumn = leftColumn.next();
    //var postMessage = $('div[id^=\"post_message_\"]', table);
    var rightColumnPadding = rightColumn.outerWidth() - rightColumn.width();

    //'10' is a magic number, but it should not make any harm
    var maxImgWidth = table.parent().outerWidth() - leftColumn.outerWidth() - rightColumnPadding - 10;

    var imgParent = img.parent();

    if (!img.data('originalWidth')) {
        img.data('originalWidth', img.width());
    }

    if (img.data('originalWidth') <= maxImgWidth) {
        if (imgParent.hasClass('highslide')) {
            cghub.forum.removeHighslide(img);
        }
        return true;
    }

    img.width(maxImgWidth);

    if (imgParent.is('a') && !imgParent.hasClass('highslide') && !imgParent.hasClass('highslideDisabled')) {
        //image wrapped with bbcode link
        return true; //do not add lightbox
    }

    if (!imgParent.hasClass('highslide')) {
        cghub.forum.addHighslide(img, imgParent);
    }

    return true;
};

cghub.forum.highslideImages = [];

cghub.forum.addHighslide = function (img, imgParent) {
	 if ($.browser.msie) {
		var onclickAttr = "return true;";
	 } else {
		var onclickAttr = "return hs.expand(this, {'allowHeightReduction': false});";
	 }

    //, { slideshowGroup: "first-group" }

    if (imgParent.hasClass('highslideDisabled')) {
        imgParent.removeClass('highslideDisabled')
            .addClass('highslide')
            .unbind()
            .bind('click', function(){
	            if ($.browser.msie) {
		            return true;
	            } else {
		            return hs.expand(this, {allowHeightReduction: false});
	            }
            })
            .css({'cursor':''});

	    if ($.browser.msie) {
			imgParent.attr('target', '_blank');
		} else {
			imgParent.removeAttr('target');
		}
    } else {
        img.wrap('<a href="'+img.attr('src')+'" class="highslide" onclick="'+onclickAttr+'" '+($.browser.msie ? 'target="_blank"' : '')+'  />');
    }

    cghub.forum.highslideImages.push(img);
};

cghub.forum.removeHighslide = function (img) {
    //var imgHtml = img.parent.html();
	//img.parent().replaceWith(imgHtml);
    img.parent().removeClass('highslide')
            .addClass('highslideDisabled')
            .css({'cursor':'default'})
            .removeAttr('onclick')
            .unbind()
            .bind('click', function(){return false;});

    delete cghub.forum.highslideImages[cghub.forum.highslideImages.indexOf(img)];
};


if (document.location.href.indexOf('/forum/') != -1) {


//$.requireScript(['/js/highslide/highslide.js'], function(){
$(function(){

	$('div.subforums').each(function(){
		var subforums = $(this).find('h3.subforum a');

		subforums.each(function(key){
			if (key != subforums.length - 1) {
				var link = $(this);
				link.html(link.html()+', ');
			}
		});
	});

	cghub.forum.lazyloadForumImages();

	cghub.forum.windowResizeTimeout = null;
	$(window).resize(function(){
		if (cghub.forum.windowResizeTimeout != null) {
			return false;
		}

		cghub.forum.windowResizeTimeout = setTimeout(function(){
			for (var i = 0; i < cghub.forum.lazyLoadedImages.length; i++) {
				cghub.forum.imageSize(cghub.forum.lazyLoadedImages[i]);
			}
		  cghub.forum.windowResizeTimeout = null;
		}, 150);
	});


//}, {'parrallel': false});
});



if (typeof hs == 'undefined') {
	var hs = {};
}
hs.graphicsDir = '/img/highslide/';
hs.align = 'center';
hs.transitions = ['expand', 'crossfade'];
hs.outlineType = 'glossy-dark';
hs.dimmingOpacity = 0.75;
hs.wrapperClassName = 'dark';
hs.showCredits = false;




} //if (document.location.href.indexOf('/forum/') != -1)




/*
hs.numberPosition = 'caption'; //bad

hs.fadeInOut = true;

if (hs.addSlideshow) hs.addSlideshow({
	//slideshowGroup: 'group1',
	interval: 5000,
	repeat: false,
	useControls: true,
	fixedControls: 'fit',
	overlayOptions: {
		opacity: .75,
		position: 'bottom center',
		hideOnMouseOut: true
	}
});

hs.addSlideshow({
	// slideshowGroup: 'group1',
	//interval: 5000,
	repeat: false,
	useControls: true,
	fixedControls: true,
	overlayOptions: {
		opacity: .6,
		position: 'top center',
		hideOnMouseOut: true
	}
});

class='highslide' onclick='return hs.expand(this)'

*/
