/**
 *  JMR - gallery.js
 *
 *  @author  Patrick Gerdsmeier
 *  @version 20090316
 */

$ = function(id) {

    return document.getElementById(id);
}

/**
 *  $$ gets elements by classname
 *  @param string class
 *  @param element
 *  @return Array
 */
$$ = function(class_name) {

    var all_obj;
    var ret_obj = new Array();
    var j = 0;
    var teststr;

    if ($$.arguments.length == 1) {
        if (document.all) {
            all_obj = document.all;
        }
        else
        if (document.getElementsByTagName && !document.all) {
            all_obj = document.getElementsByTagName("*");
        }
    }

    else {
        all_obj = $$.arguments[1].getElementsByTagName("*");
    }

    for (i=0; i < all_obj.length; i++) {

        if (all_obj[i].className.indexOf(class_name) != -1) {
            teststr = "," + all_obj[i].className.split(" ").join(",") + ",";

            if (teststr.indexOf("," + class_name + ",") != -1) {
                ret_obj[j] = all_obj[i];
                j++;
            }
        }
    }

    return ret_obj;
}

/**
 *  Gallery including preloader
 *  @param element el
 *
 *  Needs div.progress_bar, a.previewlinks (href to image-src)
 */
var Gallery = function(el) {

    var initialized = false;

    var fading_counter = 0;
    var next_images = Array();

    var progress_max_width = 450;
    var loaded_images = 0;
    var current_fullview;

    var progress_bar = $$('progress_bar', el)[0];
    var fullview = $$('fullview', el)[0];
    var gallery_info = $$('gallery_info', el)[0];
    var preview = $$('preview', el)[0];
    var Previews = $$('preview_link', el);

    var Images = Array(Previews.length);

    var debug = function(text) {

        //$('debug').innerHTML += text + '<br />';
    }

    /**
     *  Updates the progress div
     */
    var update_progress_bar = function() {

        var width = Math.round(progress_max_width * loaded_images / (Images.length * 2));

        if (width == 450) {
            initialized = true;
        }

        progress_bar.style.width = width + 'px';
    };

    var load_onload = function() {

        this.onload = null;
        this.onerror = null;
        loaded_images++;
        update_progress_bar();
        debug('loaded image ');
    };

    var load_onerror = function() {

        this.onload = null;
        this.onerror = null;
        progress_bar.style.background = "#f00";
        debug('ERROR loading image ');
    };

    var load_image = function(index, src) {

        Images[index] = new Image();
        Images[index].onload = load_onload;
        Images[index].onerror = load_onerror;
        Images[index].src = src;
    };

    var load_thumb = function(el, src) {

        el.onload = load_onload;
        el.onerror = load_onerror;
        el.src = src;
    };

    var load_all_images = function() {

        for (var i = 0; i < Previews.length; i++) {
            load_image(i, Previews[i].getAttribute('href'));
            var preview = Previews[i].getElementsByTagName('img')[0];
            load_thumb(preview, preview.getAttribute('alt'));
        }
    }

    var get_abs_y_pos = function(el) {

        return (el.offsetParent)
            ? el.offsetTop + get_abs_y_pos(el.offsetParent)
            : el.offsetTop;
    }

    var set_opacity = function(el, value) {

        try {
            el.style.filter = 'alpha(opacity=' + value + ')';
            el.style.opacity = ( value / 100 ) ;
        }
        catch(e) {}
    }

    this.animate = function() {

        var Imagetarget = fullview.getElementsByTagName('img');

        if (fading_counter > 100) {
            set_opacity(Imagetarget[0], fading_counter - 100);
            set_opacity(Imagetarget[1], fading_counter - 100);
        }

        else
        if (fading_counter == 100) {
            Imagetarget[0].src = next_images[0];
            Imagetarget[1].src = next_images[1];
        }

        else
        if (fading_counter >= 0) {
            set_opacity(Imagetarget[0], 100 - fading_counter);
            set_opacity(Imagetarget[1], 100 - fading_counter);
        }

        if (fading_counter > 0) {
            fading_counter -= 10;
        }
    }

    this.display_fullview = function(mode) {

        fullview.style.display = mode
            ? 'block'
            : 'none';
        gallery_info.style.display = fullview.style.display;
        preview.style.marginBottom = mode
            ? '48px'
            : '24px';

        fading_counter = mode ? 200 : 0;
    }

    this.display_preview = function(mode) {

        if (!mode) {
            this.display_fullview(false);
        }

        preview.style.display = mode
            ? 'block'
            : 'none';

        if (!initialized && preview.style.display == 'block') {
            load_all_images();
        }
    }

    this.toggle_preview = function() {

        this.display_preview(preview.style.display == 'none' ? true : false);
    }

    this.previous = function() {

        try {
            var prev_div = current_fullview.parentNode.previousSibling.previousSibling;

            if (prev_div.nodeName == 'BR') {
                prev_div = prev_div.previousSibling.previousSibling;
            }

            var prev_link = prev_div.getElementsByTagName('a')[2];

            this.fullview(prev_link);
        }
        catch (e) {}
    }

    this.next = function() {

        try {
            var next_div = current_fullview.parentNode.nextSibling.nextSibling;

            if (next_div.nodeName == 'BR') {
                next_div = next_div.nextSibling.nextSibling;
            }

            var next_link = next_div.getElementsByTagName('a')[2];

            this.fullview(next_link);
        }
        catch (e) {}
    }

    this.fullview = function(el) {

        if (!fading_counter && el && (el.getAttribute('class') == 'pair_click' || el.className == 'pair_click')) {

            current_fullview = el;
            var Imagesrc = el.parentNode.getElementsByTagName('a');

            next_images[0] = Imagesrc[0].getAttribute('href');
            next_images[1] = Imagesrc[1].getAttribute('href');

            this.display_fullview(true);
        }
    };

    // Initialize
    //load_all_images();
    update_progress_bar();
    this.display_fullview(false);
    this.display_preview(false);
};
