
var global_is_ie6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1);
var global_is_ie7 = (navigator.userAgent.toLowerCase().indexOf('msie 7') != -1);

var setting_ie6_use_gif_buttons = true;
var setting_image_preload  = true;
var setting_images_dir     = 'images/50_years/';
var setting_default_width  = '500';
var setting_default_height = '380';
var setting_default_alt    = 'Picture';

var setting_buttons_dir    = 'images/gallery_controls/';
var setting_button_size    = '32';

var global_gallery  = null;
var global_pictures = new Array();
var global_buttons  = new Dh_buttons();

global_pictures[ 0] = new Object();
global_pictures[ 0]['src'] = '1.jpg';
global_pictures[ 0]['width'] = '500';
global_pictures[ 0]['height'] = '357';
global_pictures[ 0]['alt'] = 'Picture 1';
global_pictures[ 0]['text'] = 'The Dalai Lama during his escape from Tibet, third from right, March 1959.';
global_pictures[ 1] = new Object();
global_pictures[ 1]['src'] = '2.jpg';
global_pictures[ 1]['width'] = '500';
global_pictures[ 1]['height'] = '362';
global_pictures[ 1]['alt'] = 'Picture 2';
global_pictures[ 1]['text'] = 'A Buddhist monk protests in Lhasa, 1987. Protests erupted in Lhasa also in 1989 and led to the imposition of martial law by the then party secretary of the TAR, Hu Jintao.';
global_pictures[ 2] = new Object();
global_pictures[ 2]['src'] = '3.jpg';
global_pictures[ 2]['width'] = '500';
global_pictures[ 2]['height'] = '344';
global_pictures[ 2]['alt'] = 'Picture 3';
global_pictures[ 2]['text'] = 'Protests in Lhasa, 1987.';
global_pictures[ 3] = new Object();
global_pictures[ 3]['src'] = '4.jpg';
global_pictures[ 3]['width'] = '500';
global_pictures[ 3]['height'] = '337';
global_pictures[ 3]['alt'] = 'Picture 4';
global_pictures[ 3]['text'] = 'Protests in Lhasa, 1987.';
global_pictures[ 4] = new Object();
global_pictures[ 4]['src'] = '5.jpg';
global_pictures[ 4]['width'] = '500';
global_pictures[ 4]['height'] = '337';
global_pictures[ 4]['alt'] = 'Picture 5';
global_pictures[ 4]['text'] = 'Protests in Lhasa, 1987.';
global_pictures[ 5] = new Object();
global_pictures[ 5]['src'] = '6.jpg';
global_pictures[ 5]['width'] = '500';
global_pictures[ 5]['height'] = '336';
global_pictures[ 5]['alt'] = 'Picture 6';
global_pictures[ 5]['text'] = 'A Tibetan victim of the authorities\' crackdown, 1987.';
global_pictures[ 6] = new Object();
global_pictures[ 6]['src'] = '7.jpg';
global_pictures[ 6]['width'] = '500';
global_pictures[ 6]['height'] = '375';
global_pictures[ 6]['alt'] = 'Picture 7';
global_pictures[ 6]['text'] = 'Chinese armed security forces getting ready to raid Tsendrok monastery in Amdo, March 2008.';
global_pictures[ 7] = new Object();
global_pictures[ 7]['src'] = '8.jpg';
global_pictures[ 7]['width'] = '500';
global_pictures[ 7]['height'] = '375';
global_pictures[ 7]['alt'] = 'Picture 8';
global_pictures[ 7]['text'] = 'A Tibetan victim at Kirti monastery, 2008.';
global_pictures[ 8] = new Object();
global_pictures[ 8]['src'] = '9.jpg';
global_pictures[ 8]['width'] = '500';
global_pictures[ 8]['height'] = '353';
global_pictures[ 8]['alt'] = 'Picture 9';
global_pictures[ 8]['text'] = 'Tibetans mourn for victims at Kirti monastery, 2008.';
global_pictures[ 9] = new Object();
global_pictures[ 9]['src'] = '10.jpg';
global_pictures[ 9]['width'] = '500';
global_pictures[ 9]['height'] = '375';
global_pictures[ 9]['alt'] = 'Picture 10';
global_pictures[ 9]['text'] = 'Protest near Labrang monastery, March 2008.';
global_pictures[10] = new Object();
global_pictures[10]['src'] = '11.jpg';
global_pictures[10]['width'] = '320';
global_pictures[10]['height'] = '240';
global_pictures[10]['alt'] = 'Picture 11';
global_pictures[10]['text'] = 'Kirti monastery: a destroyed image of the Dalai Lama, April 2008.';


function display_gallery(obj) {
    if (global_gallery == null) {
        global_gallery = new Dh_gallery();
    }
    global_gallery.display(obj);
}

function close_dh_gal() {
    global_gallery.close();
}

function next_img() {
    global_gallery.next_img();
}

function prev_img() {
    global_gallery.prev_img();
}

function img_src(ele, src) {
    ele.src = global_buttons.get(src);
}

function Dh_picture(src, width, height, alt, text) {
    this.src_    = src;
    this.width_  = width;
    this.height_ = height;
    this.alt_    = alt;
    this.text_   = text;

    if (!isset(this.width_))  this.width_  = setting_default_width;
    if (!isset(this.height_)) this.height_ = setting_default_height;
    if (!isset(this.alt_))    this.alt_    = setting_default_alt;
    if (!isset(this.text_))   this.text_   = '';

    this.get_src = function() {
        return this.src_;
    };
    this.get_width = function() {
        return this.width_;
    };
    this.get_height = function() {
        return this.height_;
    };
    this.get_alt = function() {
        return this.alt_;
    };
    this.get_text = function() {
        return this.text_;
    };
}

function Dh_gallery() {
    this.pics_     = new Array();
    this.div_      = null;
    this.img_      = null;
    this.small_    = null;
    this.over_div_ = null;
    this.active_   = 0;
    this.add_picture = function(src, width, height, alt, text) {
        return this.pics_[this.pics_.length] =
        new Dh_picture(
                       src,
                       width,
                       height,
                       alt,
                       text
                       );
    };
    this.init = function(pics) {
        var t;
        for (var i = 0; i < pics.length; ++i) {
            t = this.add_picture(
                                 setting_images_dir + pics[i]['src'],
                                 pics[i]['width'],
                                 pics[i]['height'],
                                 pics[i]['alt'],
                                 pics[i]['text']
                                 );
            if (setting_image_preload) {
                pics[i]['obj']     = new Image(t.get_width(),t.get_height());
                pics[i]['obj'].src = t.get_src();
            }
        }
    };
    this.display = function(first_ele) {
        if (this.div_ == null) {
            this.create_over_div();
            this.create_div();
        }
        if (first_ele == null) {
            this.active_ = 0;
        } else {
            for (var i = 0; i < this.pics_.length; ++i) {
                if (first_ele.src.search(this.pics_[i].get_src()) != -1) {
                    this.active_ = i;
                    break;
                }
            }
        }
        this.set_img(this.active_);
    };
    this.create_div = function() {
        this.div_    = document.createElement('DIV');
        this.div_.id = 'dh_gallery';

        this.create_close(this.div_);
        this.create_img(this.div_);
        this.create_small(this.div_);
        document.body.appendChild(this.div_);
        this.create_navi(this.div_);
    };
    this.create_over_div = function() {
        this.over_div_    = document.createElement('DIV');
        this.over_div_.id = 'dh_gall_over';

        document.body.appendChild(this.over_div_);
    };
    this.create_navi = function(ele) {
        var div = document.createElement('DIV');
        div.id  = 'dh_gall_navigation';

        if (this.active_ >= 1) {
            var img = document.createElement('IMG');
            img.src = global_buttons.get('prev_button');
            img.onclick = new Function('prev_img()');
            img.onmouseover = new Function('img_src(this,"prev_button_hover")');
            img.onmouseout  = new Function('img_src(this,"prev_button")');
            img.id = 'dh_gall_left_arrow';
            div.appendChild(img);
        }

        if (this.active_ < this.pics_.length - 1) {
            var img = document.createElement('IMG');
            img.src = global_buttons.get('next_button');
            img.onclick = new Function('next_img()');
            img.onmouseover = new Function('img_src(this,"next_button_hover")');
            img.onmouseout  = new Function('img_src(this,"next_button")');
            img.id = 'dh_gall_right_arrow';
            div.appendChild(img);
        }

        var t = document.getElementById('dh_gall_navigation');
        if (t != null) {
            ele.removeChild(t);
        }
        ele.appendChild(div);
    };
    this.create_close = function(ele) {
        var div = document.createElement('DIV');
        div.id  = 'dh_gall_close';

        img = document.createElement('IMG');
        img.src = global_buttons.get('exit_button');
        img.id  = 'dh_gall_button_exit';
        img.onclick = new Function('close_dh_gal()');
        img.onmouseover = new Function('img_src(this,"exit_button_hover")');
        img.onmouseout  = new Function('img_src(this,"exit_button")');
        div.appendChild(img);

        ele.appendChild(div);
    };
    this.create_img = function(parent) {
        this.img_    = document.createElement('IMG');
        this.img_.id = 'dh_img';

        parent.appendChild(this.img_);
    };
    this.create_small = function(parent) {
        this.small_ = document.createElement('SMALL');
        this.small_.id = 'dh_small';
        
        parent.appendChild(this.small_);
    };
    this.next_img = function() {
        if (this.active_ < this.pics_.length - 1) {
            this.set_img(++this.active_);
        }
    };
    this.prev_img = function() {
        if (this.active_ >= 1) {
            this.set_img(--this.active_);
        }
    };
    this.set_img = function(i) {
        this.img_.src     = this.pics_[i].get_src();
        this.img_.width   = this.pics_[i].get_width();
        this.img_.height  = this.pics_[i].get_height();
        this.img_.alt     = this.pics_[i].get_alt();
        this.img_.onclick = new Function('next_img()');
        this.small_.innerHTML = this.pics_[i].get_text();
        this.small_.style.width = this.pics_[i].get_width() + 'px'; 
        this.repaint();
    };
    this.repaint = function() {
        this.create_navi(this.div_);

        var win = get_win_size();
        var t = this.div_.offsetWidth/2;
        this.div_.style.left = (win.width/2)-(t) + "px";

        var t2 = this.div_.offsetHeight/2;
        this.div_.style.top = (win.height/2)-(t2) + "px";

        if (global_is_ie6) {
            // FIXME: insert an element to get pagesize...
            // there should be a better way
            var hack   = document.createElement('DIV');
            var i_body = get_body(window);
            hack.style.bottom = '0px';
            hack.style.height = '0px';
            hack.style.position = 'absolute';
            hack.id = 'dh_cal_last';
            hack.innerHTML = '&nbsp;';
            i_body.appendChild(hack);

            var last = find_pos(document.getElementById('dh_cal_last'));
            this.over_div_.style.height = last[1] + 20 + 'px';
            i_body.removeChild(hack);
        }
        screen_mover(0,0);

    };
    this.close = function() {
        document.body.removeChild(this.div_);
        document.body.removeChild(this.over_div_);
        this.div_  = null;
        this.img_  = null;
        this.over_div_ = null;
        this.active_ = null;
    };
    if (global_pictures != null) {
        this.init(global_pictures);
    }
}

function Dh_buttons() {
    this.end_ = '.png';

    if (
      setting_ie6_use_gif_buttons &&
      global_is_ie6
    ) {
        this.end_ = '.gif';
    }
    this.buttons_ = new Object();

    this.buttons_['exit_button']     = new Image(setting_button_size,setting_button_size);
    this.buttons_['exit_button'].src = setting_buttons_dir + 'exit_button' + this.end_;

    this.buttons_['exit_button_hover']     = new Image(setting_button_size,setting_button_size);
    this.buttons_['exit_button_hover'].src = setting_buttons_dir + 'exit_button_hover' + this.end_;

    this.buttons_['prev_button']     = new Image(setting_button_size,setting_button_size);
    this.buttons_['prev_button'].src = setting_buttons_dir + 'prev_button' + this.end_;

    this.buttons_['prev_button_hover']     = new Image(setting_button_size,setting_button_size);
    this.buttons_['prev_button_hover'].src = setting_buttons_dir + 'prev_button_hover' + this.end_;

    this.buttons_['next_button']     = new Image(setting_button_size,setting_button_size);
    this.buttons_['next_button'].src = setting_buttons_dir + 'next_button' + this.end_;

    this.buttons_['next_button_hover']     = new Image(setting_button_size,setting_button_size);
    this.buttons_['next_button_hover'].src = setting_buttons_dir + 'next_button_hover' + this.end_;

    this.get = function(img) {
        return this.buttons_[img].src;
    };
    this.change_button = function(ele) {
        
    };
}

function get_win_size() {
    var win = window;
    var t = new Object();
    if (isset(win.innerWidth)) {
        t.width = win.innerWidth;
        t.height = win.innerHeight;
    } else {
        var obj  = get_body(win);
        t.width  = parseInt(obj.clientWidth);
        t.height = parseInt(obj.clientHeight);
    }
    return t;
}

function get_body(w) {
    return (w.document.compatMode && w.document.compatMode == "CSS1Compat") ?
      w.document.documentElement : w.document.body || null;
}

function find_pos(obj) {
    if (obj == null) return null;
    var offsetTrail = obj;
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (
      navigator.userAgent.indexOf('Mac') != -1 &&
      typeof document.body.leftMargin != 'undefined'
    ) {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return [offsetLeft,offsetTop];
}

function screen_mover(array) {
    if (array == null) return null;
    window.scrollTo(array[0],array[1]);
}

function isset(v) {
    if (
      v == null ||
      typeof v == 'undefined'
    ) {
        return false;
    }
    return true;
}

