//  m5_gallery 0.1b 30.04.2008
//  Copyright (c) 2008 media5 (http://www.media5.com)



function m5_gallery(name, bgcolor, next_img, prev_img, close_img, border){



    this.name = name;
    this.browser = new m5_gallery_browser_check();
    this.onresize = m5_gallery_resize;
    this.onscroll = m5_gallery_scroll;
    this.show = m5_gallery_show;
    this.show_image = m5_gallery_show_image;
    this.show2 = m5_gallery_show2;
    this.o = m5_gallery_o;
    this.add = m5_gallery_add;
    this.next = m5_gallery_next;
    this.prev = m5_gallery_prev;
    this.close = m5_gallery_close;
    this.exit = m5_gallery_exit;
    this.start = m5_gallery_start;
    this.loadPopup = m5_gallery_loadPopup;
    this.openPopup = m5_gallery_openPopup;
    this.setPanelOpacity = m5_gallery_setPanelOpacity;
    this.current_img = 0;

    if(!this.o('m5_grey_panel'))this.d = document.createElement('DIV');
        else  this.d = this.o('m5_grey_panel');
    if(!this.o('m5_grey_panel_offset'))this.d2 = document.createElement('DIV');
        else  this.d2 = this.o('m5_grey_panel_offset');
    if(!this.o('m5_grey_panel_container'))this.d3 = document.createElement('DIV');
        else  this.d3 = this.o('m5_grey_panel_container');


    document.body.appendChild(this.d);
    document.body.appendChild(this.d2);
	document.body.appendChild(this.d3);


    this.imgs = new Array();
    this.imgs_title = new Array();
    this.imgs_dsc = new Array();
    //<img id="m5_gallery_img" onload="if('+name+'.imgs['+name+'.current_img].src != this.src )'+name+'.show2()">
    this.d3.innerHTML = '<table style="border:none" width="100%" cellpadding="0" cellspacing="5" id="m5_gallery_table"><tr style="border:none"><td colspan="6" id="m5_gallery_title" align="left" style="font-weight:bold; border:none"></td></tr><tr><td id="m5_gallery_count" style="font-size:10px;color:#eee;border:none;vertical-align:top; line-height:16px" width="1" nowrap="nowrap" align="left"></td><td style="border:none" align="left" width="1"><img src="'+prev_img+'" id="m5_gallery_prev" onclick="'+this.name+'.prev()" title="Предыдущая" style="cursor:pointer"></td><td  style="border:none" width="1"><img src="'+next_img+'" id="m5_gallery_next" title="Следующая" onclick="'+this.name+'.next()" style="cursor:pointer"></td><td id="m5_gallery_dsc" align="center" style="font-size:12px; border:none; color:white; vertical-align:top; font-weight:bold"></td><td style="text-align:right; border:none; width:55px" width="30"><img src="'+close_img+'" id="m5_gallery_next" title="Закрыть окно" onclick="'+this.name+'.close()" style="cursor:pointer" width="16" height="16" /></td></tr></table><div id="m5_gallery_img_div"></div>';
    this.m5_gallery_img = this.o('m5_gallery_img');
    this.m5_gallery_img_div = this.o('m5_gallery_img_div');
    this.m5_gallery_dsc = this.o('m5_gallery_dsc');
    this.m5_gallery_title = this.o('m5_gallery_title');
    this.m5_gallery_prev = this.o('m5_gallery_prev');
    this.m5_gallery_next = this.o('m5_gallery_next');
    this.d.style.position = 'absolute';
    this.d2.style.position = 'absolute';
//    alert(this.browser.ie_ver)
    if(this.browser.ie && this.browser.ie_ver < 7){
       this.d3.style.position = 'absolute';
       window.onscroll = function (){ eval(name+'.onscroll()') };
       //this.d3.style.right = 'auto';
//       this.d3.style.bottom = 'auto';
//       this.d3.style.left = 'expression( ( -20 - fixme.offsetWidth + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + \'px\' );';
//       this.d3.style.top = 'expression( ( -10 - fixme.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + \'px\' );';
    }else{
       this.d3.style.position = 'fixed';
    }


    this.d.style.display = 'none';
    this.d3.style.display = 'none';
    this.d.onclick = function (){ eval(name+'.close()') };
    this.d3.style.visibility = 'hidden';
    this.d.style.width = '100%';
    this.d.style.height = '300px';
    this.d.style.top = '0px';
    this.d.style.left = '0px';
    this.d.style.filter = 'alpha(opacity=0)';
    this.d.style.opacity = '0';
    this.d.style.backgroundColor = '#000';
//    this.d3.style.backgroundColor = bgcolor;
    this.d3.style.padding = border+'px';
    this.d.id = 'm5_grey_panel';
    this.d2.id = 'm5_grey_panel_offset';
    this.d3.id = 'm5_grey_panel_container';

    window.onresize = function (){ eval(name+'.onresize()') };

    this.hide_time;
    this.hide_time_daley = 10;



    var objs = document.body.getElementsByTagName('A');

    for(var i = 0; i <= objs.length -1; i++){
        var obj = objs[i];
        if(obj.className == 'm5_gallery'){
            var img_num = this.add(obj.href, '', obj.title);
            eval('obj.onclick = function () {eval(name+\'.show_image('+img_num+');\'); return false;}');
//            alert(obj.innerHTML)
        }
    }

}



function m5_gallery_add(img_url, title, dsc){
    this.imgs[this.imgs.length] = new Image();
    this.imgs[this.imgs.length-1].src = img_url;
    this.imgs_title[this.imgs_title.length] = title;
    this.imgs_dsc[this.imgs_dsc.length] = dsc;
    return this.imgs.length-1;
}

function m5_gallery_close(){
this.d3.style.visibility = 'hidden';
this.d3.style.display = 'none';
    if (this.hide_time) clearTimeout(this.hide_time);
    this.hide_time = setTimeout(this.name+'.setPanelOpacity(6, -2, 0, \''+this.name+'.exit()\')',200);
}
function m5_gallery_exit(){
    this.d.style.display = 'none';

}

function m5_gallery_resize(){
    if(this.d.style.display == 'none') return false;
    var scr =  alertSize();
    this.d3.style.top = (scr.height - (this.imgs[this.current_img].height+this.o('m5_gallery_table').clientHeight))/2 + 'px';
    this.d3.style.left = (scr.width - (this.imgs[this.current_img].width))/2 + 'px';
}

function m5_gallery_scroll(){
    if(this.d.style.display == 'none') return false;
    var scr =  alertSize();
    this.d3.style.top = (scr.height - (this.imgs[this.current_img].height+this.o('m5_gallery_table').clientHeight))/2 + document.documentElement.scrollTop + 'px';
//    alert()
//    this.d3.style.left = (scr.width - (this.imgs[this.current_img].width))/2 + 'px';
}

function m5_gallery_next(){
    if(this.current_img < this.imgs.length-1){
        this.d3.style.visibility = 'hidden';
        this.current_img++;
        this.show();
    }
}

function m5_gallery_prev(){
    if(this.current_img > 0){
    this.d3.style.visibility = 'hidden';
        this.current_img--;
        this.show();
    }
}

function m5_gallery_start(){
    this.d.style.height = (this.d2.offsetTop)+'px';
    this.d.style.display = 'block';
    this.d.style.zIndex = '888888';
    this.d.style.filter = 'alpha(opacity=0)';
    this.d.style.opacity = '0';

    this.d3.style.display = 'none';
    if (this.hide_time) clearTimeout(this.hide_time);
    this.hide_time = setTimeout(this.name+'.setPanelOpacity(1, 2, 9, \''+this.name+'.show()\')',this.hide_time_daley);
}

function m5_gallery_setPanelOpacity(opacity, step, exit_on, callBack){
//    alert(opacity)
    if(opacity != exit_on){
  //  alert(opacity);
        this.d.style.filter = 'alpha(opacity='+(opacity*10)+')';
        this.d.style.opacity = '0.'+opacity;
       if (this.hide_time) clearTimeout(this.hide_time);
       this.hide_time = setTimeout(this.name+'.setPanelOpacity('+(opacity+step)+', '+step+', '+exit_on+', \''+callBack+'\')',this.hide_time_daley);
    }else{
        this.d3.style.display = 'block';
        eval(callBack);
    }
}

function m5_gallery_show_image(image_num){
    this.current_img = image_num;
    this.start();
}

function m5_gallery_show(){
    if(this.current_img < this.imgs.length-1) var onclick = ' onclick="'+this.name+'.next()"';
        else var onclick = '';
    this.m5_gallery_img_div.innerHTML = '<img id="m5_gallery_img"'+onclick+' src="'+this.imgs[this.current_img].src+'" onload="'+this.name+'.show2()">';
}




function m5_gallery_show2(){
    this.d3.style.zIndex = '999999';
    this.o('m5_gallery_table').style.zIndex = '999999';
    this.m5_gallery_dsc.innerHTML = this.imgs_dsc[this.current_img];
    this.m5_gallery_title.innerHTML = this.imgs_title[this.current_img];
    this.o('m5_gallery_count').innerHTML = (this.imgs.length > 1) ? ('<nobr>'+(this.current_img+1)+' из '+this.imgs.length+'</nobr>') : '';

    if(this.current_img == 0) this.m5_gallery_prev.style.display = 'none';
        else this.m5_gallery_prev.style.display = 'block';

    if(this.current_img == this.imgs.length-1) this.m5_gallery_next.style.display = 'none';
        else this.m5_gallery_next.style.display = 'block';

    if(this.imgs[this.current_img].width >= 200)this.d3.style.width = this.imgs[this.current_img].width+'px';
        else  this.d3.style.width = '300px';
    this.d3.style.height = (this.o('m5_gallery_img').height+this.o('m5_gallery_table').clientHeight) + 'px';
        var scr =  alertSize();
        this.d3.style.top = (scr.height - (this.imgs[this.current_img].height+this.o('m5_gallery_table').clientHeight))/2 + 'px';
        this.d3.style.left = (scr.width - (this.imgs[this.current_img].width))/2 + 'px';

     if(this.browser.ie && this.browser.ie_ver < 7)this.onscroll();
     this.d3.style.visibility = 'visible';

}

function m5_gallery_loadPopup(content, title, width, height){
    this.content = content;
    this.width = width;
    this.height = height;
    this.title = title;
    this.d.style.height = (this.d2.offsetTop)+'px';
    this.d.style.display = 'block';
    this.d.style.zIndex = '888888';
    this.d.style.filter = 'alpha(opacity=0)';
    this.d.style.opacity = '0';
//     this.d3.innerHTML = '';
    this.d3.style.display = 'none';
    if (this.hide_time) clearTimeout(this.hide_time);
    this.hide_time = setTimeout(this.name+'.setPanelOpacity(1, 2, 9, \''+this.name+'.openPopup()\')',this.hide_time_daley);
}

function m5_gallery_openPopup(){


    this.d3.style.zIndex = '999999';
    this.o('m5_gallery_count').innerHTML  = '<b>'+this.title+'</b>';
    var scr =  alertSize();
    this.d3.style.top = (scr.height - (this.height+15))/2 + 'px';
    this.d3.style.left = (scr.width - this.width)/2 + 'px';
    this.m5_gallery_prev.style.display = 'none';

    this.m5_gallery_next.style.display = 'none';
    this.d3.style.display = 'block';
    this.d3.style.height =  this.height+'px';
    this.d3.style.width =  this.width+'px';
    this.m5_gallery_img_div.innerHTML = this.o(this.content).innerHTML;
    if(this.browser.ie && this.browser.ie_ver < 7)this.onscroll();
    this.d3.style.visibility = 'visible';
}



function m5_gallery_browser_check(){
    var b = navigator.appName
    if (b=="Netscape") this.b = "ns"
    else if (b=="Microsoft Internet Explorer") this.b = "ie"
    else this.b = b
    this.version = navigator.appVersion
    this.v = parseInt(this.version)
    this.ns = (this.b=="ns" && this.v<5)
    this.ns4 = (this.b=="ns" && this.v==4)
    this.ns5 = (this.b=="ns" && this.v==5)
    this.ie = (this.b=="ie" && this.v>=4)
    this.ie4 = (this.version.indexOf('MSIE 4')>0)
    this.ie5 = (this.version.indexOf('MSIE 5')>0)
    this.ie6 = (this.version.indexOf('MSIE 6')>0)
    this.ie7 = (this.version.indexOf('MSIE 7')>0)
    this.min = (this.ns||this.ie)
    if(this.version.indexOf('MSIE 4')>0)this.ie_ver = 4;
    if(this.version.indexOf('MSIE 5')>0)this.ie_ver = 5;
    if(this.version.indexOf('MSIE 6')>0)this.ie_ver = 6;
    if(this.version.indexOf('MSIE 7')>0)this.ie_ver = 7;
}

function m5_gallery_o(name){
    return document.getElementById(name);
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return {width:myWidth, height:myHeight};
}
