//ASedinkin@gmail.com
//2010

//Highslige
hs.graphicsDir = '/img/highslide/';
hs.outlineType = 'glossy-dark';

document.observe("dom:loaded", function() {
    var images = [];
    $$("#menu li").each(function(item) {
        images.push('/img/'+item.id+'_image.jpg'); 
        
        item.observe("click", function() {
            document.location = item.getElementsBySelector("a");
        });

        item.observe("mouseover", function() {
            if (window.isMainPage)
                getCloud(item.id);
            if (window.can_use_preload_images)
                setBackground(item.id);
        });
        item.observe("mouseout", function() {
            if (window.can_use_preload_images) 
                setBackgroundNone(item.id);
        });
    });
    var ImagePreloaderObject = new ImagePreloader(images,function(){window.can_use_preload_images = true})
});

//Creating tag clouds
function getCloud(item_id) {
    var content = $('content'),
        tags_offsets = [],
        injection = -7;
    content.width = content.getWidth();
    content.height = content.getHeight();

    function checkPos(offset) {
        function checkOffsets(offsetA,offsetB) {
                    if ((((offsetA[0] >= offsetB[0]) && (offsetA[0] <= offsetB[0] + offsetB[2])) &&
                         ((offsetA[1] >= offsetB[1]) && (offsetA[1] <= offsetB[1] + offsetB[3]))) ||
                        (((offsetA[0] + offsetA[2] >= offsetB[0]) && (offsetA[0] + offsetA[2] <= offsetB[0] + offsetB[2])) &&
                         ((offsetA[1] >= offsetB[1]) && (offsetA[1] <= offsetB[1] + offsetB[3]))) ||
                        (((offsetA[0] >= offsetB[0]) && (offsetA[0] <= offsetB[0] + offsetB[2])) &&
                         ((offsetA[1] + offsetA[3] >= offsetB[1]) && (offsetA[1] + offsetA[3] <= offsetB[1] + offsetB[3]))) ||
                        (((offsetA[0] + offsetA[2] >= offsetB[0]) && (offsetA[0] + offsetA[2] <= offsetB[0] + offsetB[2])) &&
                         ((offsetA[1] + offsetA[3] >= offsetB[1]) && (offsetA[1] + offsetA[3] <= offsetB[1] + offsetB[3]))))
                        return true;
                    else
                        return false;
                }
        for (var i = 0; i < tags_offsets.length; i++) {
            if (checkOffsets(offset, tags_offsets[i]))
                return false;
        }
        return true;
    }

    content.getElementsBySelector('.tags').each(function(item) {
        if (!item.classNames().include(item_id)) {
            item.addClassName('hidden');
        } else {
            item.removeClassName('hidden');
            item.setOpacity(Math.random());
            var size = Math.floor(Math.random() * 100) + 100;
            item.style.fontSize = (size/100)+'em';
            item.width = item.getWidth();
            item.height = item.getHeight();
            item.left = Math.floor(Math.random() * (content.width - item.width)),
            item.top = Math.floor(Math.random() * (content.height - item.height));
            
            while (!checkPos([item.left, item.top, item.width, item.height])) {
                item.left = Math.floor(Math.random() * (content.width - item.width)),
                item.top = Math.floor(Math.random() * (content.height - item.height));
            }
            
            tags_offsets.push([item.left + injection, item.top + injection, item.width - (injection * 2), item.height - (injection * 2)]);
            item.setStyle('top: '+item.top+'px');
            item.setStyle('left: '+item.left+'px');
        }
    });
}

//Setting background image
function setBackground(item_id) {
    $('main_container').setStyle('background: url(/img/'+item_id+'_image.jpg) 100% 0% no-repeat;');
}

function setBackgroundNone(item_id) {
    $('main_container').setStyle('background: none;');
}

//Image preloading
function ImagePreloader(images, callback) {

    this.callback = callback;

    this.nLoaded = 0;
    this.nProcessed = 0;
    this.aImages = new Array;
    
    this.nImages = images.length;
    for ( var i = 0; i < images.length; i++ )
       this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image) {
    var oImage = new Image;
    this.aImages.push(oImage);
 
    oImage.onload = ImagePreloader.prototype.onload;
    oImage.onerror = ImagePreloader.prototype.onerror;
    oImage.onabort = ImagePreloader.prototype.onabort;

    oImage.oImagePreloader = this;
    oImage.bLoaded = false;
 
    oImage.src = image;
}

ImagePreloader.prototype.onComplete = function() {
    this.nProcessed++;
    if ( this.nProcessed == this.nImages )
    {
      this.callback(this.aImages, this.nLoaded);
    }
}

ImagePreloader.prototype.onload = function() {
    this.bLoaded = true;
    this.oImagePreloader.nLoaded++;
    this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function() {
    this.bError = true;
    this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function() {
    this.bAbort = true;
    this.oImagePreloader.onComplete();
}
