//  m5_gallery 0.5b 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.next_img = next_img;
    this.prev_img = prev_img;
    this.close_img = close_img;
    this.bgcolor = bgcolor;
    this.border = border;
    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.loadAjaxPopup = m5_gallery_loadAjaxPopup;
    this.openAjaxPopup = m5_gallery_openAjaxPopup;
    this.setAjaxContent = m5_gallery_setAjaxContent;
    this.openPopup = m5_gallery_openPopup;
    this.create_html = m5_gallery_create_html;
    this.setPanelOpacity = m5_gallery_setPanelOpacity;
    this.hide_select = m5_gallery_hide_select;
    this.current_img = 0;
    this.hide_time;
    this.hide_time_daley = 10;
    this.imgs = new Array();
    this.imgs_title = new Array();
    this.imgs_dsc = new Array();
    
    this.create_html();
    
    window.onresize = function (){ eval(name+'.onresize()') };
    
    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;}');
        }
    }
    
    
    
}

function m5_gallery_hide_select(){
    if(this.browser.ie && this.browser.ie_ver < 7){
        var objs = document.body.getElementsByTagName('SELECT');
        for(var i = 0; i <= objs.length -1; i++){
            with (objs[i]){
                style.visibility = 'hidden';
            }
        }
    }
}

function m5_gallery_create_html(){

    if(!this.o('m5_grey_panel')){
        this.d = document.createElement('DIV');
        document.body.appendChild(this.d);
    }else  this.d = this.o('m5_grey_panel');
    
    if(!this.o('m5_grey_panel_offset')){
        this.d2 = document.createElement('DIV');
        document.body.appendChild(this.d2);
    }else  this.d2 = this.o('m5_grey_panel_offset'); 
    
    if(!this.o('m5_grey_panel_container')){
        this.d3 = document.createElement('DIV');
        document.body.appendChild(this.d3); 
    }else  this.d3 = this.o('m5_grey_panel_container');
    
    
    
    with (this.d){
        style.display = 'none';
        
        style.width = '100%';
        style.height = '300px';
        style.top = '0px';
        style.left = '0px';
        style.filter = 'alpha(opacity=0)';
        style.opacity = '0';
        style.backgroundColor = '#000';
        id = 'm5_grey_panel';
        style.position = 'absolute';  
    }
    
    f = this.name+'.close()';
    
    this.d.onclick = function (){ eval(f) };   
    
    with (this.d2){
        id = 'm5_grey_panel_offset';
        style.position = 'absolute'; 
    }
    
    with (this.d3){
        innerHTML = '<div id="m5_gallery_img_div"></div><table width="100%" cellpadding="0" cellspacing="5" id="m5_gallery_table"><tr><td colspan="5" id="m5_gallery_title" align="left" style="font-weight:bold"></td></tr><tr><td colspan="5" id="m5_gallery_dsc" align="left" style="font-size:10px"></td></tr><tr><td id="m5_gallery_count" style="font-size:10px;color:#777" width="1" nowrap="nowrap" align="left"></td><td align="left" width="1"><img src="'+this.prev_img+'" id="m5_gallery_prev" onclick="'+this.name+'.prev()" title="Предыдущая" style="cursor:pointer"></td><td width="1"><img src="'+this.next_img+'" id="m5_gallery_next" title="Следующая" onclick="'+this.name+'.next()" style="cursor:pointer"></td><td width="99%" style="text-align:right"><img src="'+this.close_img+'" id="m5_gallery_next" title="Закрыть окно" onclick="'+this.name+'.close()" style="cursor:pointer"></td></tr></table>'; 
        style.display = 'none';
        style.visibility = 'hidden';
        style.backgroundColor = this.bgcolor;
        style.padding = this.border+'px';
        id = 'm5_grey_panel_container'; 
    }
    
    if(this.browser.ie && this.browser.ie_ver < 7){
       this.d3.style.position = 'absolute';
       f2 = this.name+'.onscroll()'; 
       window.onscroll = function (){ eval(f2) };   
    }else{
       this.d3.style.position = 'fixed'; 
    }
    
    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');
    
}



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(1, -1, 0, \''+this.name+'.exit()\')',200);
}
function m5_gallery_exit(){
    this.d.style.display = 'none'; 
    document.body.removeChild(this.d); 
    document.body.removeChild(this.d2);
    document.body.removeChild(this.d3);
    var objs = document.body.getElementsByTagName('SELECT');
    for(var i = 0; i <= objs.length -1; i++){
        with (objs[i]){
            style.visibility = '';
        }
    }
    
}

function m5_gallery_resize(){
    if(this.d.style.display == 'none') return false;
    var scr =  alertSize();
    if(this.imgs.length > 0){
        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';
    }else{
       this.d3.style.top = (scr.height - (this.height+15))/2 + 'px';
       this.d3.style.left = (scr.width - this.width)/2 + 'px';
    }
}

function m5_gallery_scroll(){
    if(this.d.style.display == 'none') return false;
    var scr =  alertSize();
    if(this.imgs.length > 0){
        this.d3.style.top = (scr.height - (this.imgs[this.current_img].height+this.o('m5_gallery_table').clientHeight))/2 + document.documentElement.scrollTop + 'px';
    }else{
        this.d3.style.top = (scr.height - (this.height+15))/2 + document.documentElement.scrollTop + '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.create_html();
    this.hide_select();
    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(2, 2, 6, \''+this.name+'.show()\')',this.hide_time_daley);
}

function m5_gallery_setPanelOpacity(opacity, step, exit_on, callBack){
    if(opacity != exit_on){
        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 = '<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.create_html();
    this.hide_select();
    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.style.display = 'none'; 
    if (this.hide_time) clearTimeout(this.hide_time);
    this.hide_time = setTimeout(this.name+'.setPanelOpacity(2, 2, 6, \''+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_loadAjaxPopup(url, title, width, height){
    this.create_html();
    this.hide_select();
    this.url = url; 
    this.width = width; 
    this.height = height; 
    this.title = title; 
    this.d3.innerHTML = '<table width="100%" border="0" style="padding:0px" cellpadding="0" cellspacing="0" id="m5_gallery_table"><tr><td width="99%" id="m5_gallery_title" style="text-align:left;font-size:16px;color:#000;font-weight:bold"></td><td width="1" style="text-align:right"><img src="'+this.close_img+'" id="m5_gallery_close" title="Закрыть окно" onclick="'+this.name+'.close()" style="cursor:pointer"></td></tr></table><div id="m5_gallery_img_div" style="color:#000;padding:15px"><img src="/images/m5_gallery/loading5.gif" style="margin-top:240px;margin-left:300px"></div>'; 
    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(7, 1, 8, \''+this.name+'.openAjaxPopup()\')',this.hide_time_daley);
}

function  m5_gallery_openAjaxPopup(){
    this.d3.style.zIndex = '999999';
    this.o('m5_gallery_title').innerHTML  = ''+this.title+'';
    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';
    if(this.browser.ie && this.browser.ie_ver < 7)this.onscroll(); 
    this.d3.style.visibility = 'visible';
    this.request = new m5_request(this.params, this.name+'.setAjaxContent', '');
    this.request.get(this.url); 
}

function  m5_gallery_setAjaxContent(data){
//    alert(   data.responseText);
     this.o('m5_gallery_img_div').innerHTML = data.responseText;
}

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};
}