var ss_image;
var ss_container;
var ss_Header;
var ss_title;
var ss_detail;
var ss_images = null;
var ss_fade_time = 100;
var ss_fade_speed = 50;
var ss_index;
var ss_t;
var ss_tfo;
var ss_tfi;
var ss_status = 'pause';
var ss_loaded_image;
var ss_displayTime = 1000;
var ss_imageHasLink = true;
var ss_change_news = false;

window.onload = ss_start;

function ss_start() {
    ss_index = 0;
    if (document.getElementById('ss_image')) {
        if (navigator.appName != 'Microsoft Internet Explorer') {
            document.getElementById('ss_image_container').style.background = 'transparent';
        }
        ss_image = document.getElementById('ss_image');
        ss_image.style.display = 'inline';
        if (ss_images != null) {
            if (ss_index + 1 == ss_images.length) { ss_index = -1; }
            ss_status = 'play';
            ss_continue();
        }
    }
}
function ss_continue() {
    if (ss_images != null) {
        if (ss_index + 1 == ss_images.length) ss_index = -1;
        ss_load_image(ss_index + 1);
        ss_t = setTimeout("ss_fade_out()", ss_displayTime);
        ss_status = 'play';
    }
}
function ss_pause() {
    clearTimeout(ss_t);
    ss_status = 'pause';
}
function ss_change(_index) {
    set_ss_story_clear();
    ss_index = _index - 1;
    ss_load_image(_index);
    ss_status = 'pause';
    ss_change_news = true;
    ss_fade_out();
    ss_set_story(_index, 'on');
    //ss_update_info();
}
function ss_fade_out(value) {
    if (ss_status == 'play' || ss_change_news == true) {
        //document.getElementById('ss_image_container').style.background = 'transparent';
        if (value == null) value = 1;
        value = Math.round(value * 100) / 100;
        var increment = Math.round((1 / (ss_fade_time / ss_fade_speed)) * 100) / 100;
        if (value >= 0) {
            document.getElementById('ss_detail').style.opacity = value;
            document.getElementById('ss_detail').style.filter = 'alpha(opacity=' + (value * 100) + ')';
            document.getElementById('ss_title').style.opacity = value;
            document.getElementById('ss_title').style.filter = 'alpha(opacity=' + (value * 100) + ')';
            document.getElementById('ss_Header').style.opacity = value;
            document.getElementById('ss_Header').style.filter = 'alpha(opacity=' + (value * 100) + ')';
            document.getElementById('ss_container').style.opacity = value;
            document.getElementById('ss_container').style.filter = 'alpha(opacity=' + (value * 100) + ')';

            ss_image.style.opacity = value;
            ss_image.style.filter = 'alpha(opacity=' + (value * 100) + ')';
            value -= increment;
            ss_tfo = setTimeout("ss_fade_out(" + value + ")", ss_fade_speed);
        } else {
            ss_image.style.opacity = 0;
            ss_try_fade_in();
        }
    } else {
        ss_image.style.opacity = 1;
        ss_image.style.filter = 'alpha(opacity=100)';
    }
}
function ss_try_fade_in() {
    clearTimeout(ss_t);
    ss_index++;
    ss_swap_loaded_image();
    ss_update_info();
    ss_fade_in();
}
function ss_fade_in(value) {
    if (value == null) value = 0;
    value = Math.round(value * 100) / 100;
    var increment = Math.round((1 / (ss_fade_time / ss_fade_speed)) * 100) / 100;
    if (value <= 1) {
        document.getElementById('ss_detail').style.opacity = value;
        document.getElementById('ss_detail').style.filter = 'alpha(opacity=' + (value * 100) + ')';
        document.getElementById('ss_title').style.opacity = value;
        document.getElementById('ss_title').style.filter = 'alpha(opacity=' + (value * 100) + ')';
        document.getElementById('ss_Header').style.opacity = value;
        document.getElementById('ss_Header').style.filter = 'alpha(opacity=' + (value * 100) + ')';
        document.getElementById('ss_container').style.opacity = value;
        document.getElementById('ss_container').style.filter = 'alpha(opacity=' + (value * 100) + ')';

        ss_image.style.opacity = value;
        ss_image.style.filter = 'alpha(opacity=' + (value * 100) + ')';
        value += increment;
        ss_tfi = setTimeout("ss_fade_in(" + value + ")", ss_fade_speed);
    } else {
        ss_image.style.opacity = 1;
        if (ss_status == 'play') ss_continue();
    }
}
function ss_load_image(_index, call_back) {
    if (_index <= ss_images.length) {
        ss_loaded_image = new Image();
        if (ss_loaded_image.setAttribute)
            ss_loaded_image.setAttribute('src', get_image_src(_index));
        else
            ss_loaded_image.src = get_image_src(_index);

        if (ss_loaded_image.complete)
            if (call_back) { call_back(); }
        else
            ss_loaded_image.onload = function() { if (call_back) call_back(); }
    }
    else {
        ss_index = 0
        ss_load_image(ss_index, call_back)
    }
}
function ss_swap_loaded_image() {
    ss_image.style.display = 'none';
//    ss_image.width = ss_loaded_image.width;
    ss_image.height = ss_loaded_image.height;
    ss_image.src = ss_loaded_image.src;
    ss_image.style.display = 'inline';
}
function ss_update_info() {
    set_ss_title(get_image_title());
    if (ss_imageHasLink) set_ss_link(get_image_link());
    set_ss_detail(get_image_detail());
    set_ss_story();
    set_ss_bgcolor();
}
function set_ss_bgcolor() {
    document.getElementById('ss_detail').style.backgroundColor = get_image_color();
    document.getElementById('ss_title').style.backgroundColor = get_image_color();
    document.getElementById('ss_Header').style.backgroundColor = get_image_color();
    document.getElementById('ss_container').style.backgroundColor = get_image_color();
    document.getElementById('ss_archive').style.backgroundColor = get_image_color();

    h = ss_getdcolor(get_image_color());

    document.getElementById('ss_archive').style.backgroundColor = '#' + h;
    document.getElementById('ss_Header').style.backgroundColor = '#' + h;
}
function ss_getdcolor(strcolor) {
    h = strcolor.toString().substr(1, 6);
    hr = h.toString().substr(0, 2)
    hg = h.toString().substr(2, 2)
    hb = h.toString().substr(4, 2)
    ir = parseInt(hr, 16);
    ig = parseInt(hg, 16);
    ib = parseInt(hb, 16);

    if (ir >= ig && ir >= ib) ss = 'r'
    if (ig >= ir && ig >= ib) ss = 'g'
    if (ib >= ig && ib >= ir) ss = 'b'
    switch (ss) {
        case 'r':
            ib -= 10;
            ig -= 10;
            break;
        case 'g':
            ir -= 10;
            ib -= 10;
            break;
        case 'b':
            ig -= 10;
            ir -= 10;
    }
    h = ((ir.toString(16).length == 1) ? '0' + ir.toString(16) : ir.toString(16)) +
        ((ig.toString(16).length == 1) ? '0' + ig.toString(16) : ig.toString(16)) +
        ((ib.toString(16).length == 1) ? '0' + ib.toString(16) : ib.toString(16))

    return h;
}
function set_ss_story(last) {
    if (last == null) {
        last = ss_index - 1
        if (last == -1) last = ss_images.length - 1
    }
    ss_set_story(last, 'off');
    ss_set_story(ss_index, 'on');
}
function set_ss_story_clear() {
    try {
        for (i = 0; i <= ss_images.length; i++)
            ss_set_story(i, 'off');
    }
    catch (e) { }
}
function ss_set_story(_index, onORoff) {
    document.getElementById('ss_i' + _index).setAttribute('class', 'icon ' + onORoff);
    document.getElementById('ss_i' + _index).setAttribute('className', 'icon ' + onORoff);

    //alert(_index + ' ' + onORoff)
}
function set_ss_detail(detail) {
    document.getElementById('ss_detail').innerHTML = detail;
}
function set_ss_title(title) {
    document.getElementById('ss_title').setAttribute('title', title);
    document.getElementById('ss_Header').innerHTML = title;
}
function set_ss_link(url) {
    document.getElementById('ss_title').setAttribute('href', url);
}
function get_image_src(_index) {
    if (_index == null) _index = ss_index
    try { return ss_images[_index][0]; }
    catch (e) { return ss_images[0][0]; }
}
function get_image_title() {
    try { return ss_images[ss_index][1]; }
    catch (e) { return ''; }
}
function get_image_link(_index) {
    try { return ss_images[ss_index][2]; }
    catch (e) { return ''; }
}
function get_image_detail() {
    try { return '<div class=newsb></div>' + ss_images[ss_index][3]; }
    catch (e) { return ''; }
}
function get_image_color() {
    try { return ss_images[ss_index][4]; }
    catch (e) { return ''; }
}
//provided by Ali Arafati
