var current_photos=1;
var total_photos=2;
var timeout = 9999999999;
var timerPhotos = null;
photos_is_selected = false;

    function aarq4( src_url, success_handler, error_handler ){

    var req = null;

    if((!src_url) || (!success_handler))
        return false;

    if (window.XMLHttpRequest){
        req = new XMLHttpRequest();
    }else{
        if (window.ActiveXObject){
            ajaxIsIE = true;
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if (req) {
        req.onreadystatechange = function(){
            if (req.readyState == 4) {
                try{
                    if (req.status == 200) {
                        success_handler(req.responseText, req.responseXML);
                    } else {
                        if(error_handler){
                            error_handler(req.status);
                        }
                    }
                }
                finally{
                }
            }
        };
        req.open('GET', src_url, true );
        req.send("");
    }
    return false;
}


function getphotos (news_id, num){
    var src_url="/getphotos.php?num="+num+"&news_id="+news_id;
     var callback = function(response_text, response_xml){
        num=parseInt(response_xml.getElementsByTagName('photos_num').item(0).firstChild.nodeValue);
        total_photos=parseInt(response_xml.getElementsByTagName('photos_cnt').item(0).firstChild.nodeValue);
        html=response_xml.getElementsByTagName('photos_html').item(0).firstChild.nodeValue;
        if(num!=current_photos){
            span=document.getElementById('photos_span');
            $('#photos_span').fadeTo(500, 0, function () {
            span.innerHTML=html;
                                                $('#photos_span').fadeTo(500, 1, function () {
                                                    $('#photos_span').css("opacity","1.0");
             });
             });
        }
        current_photos=num;

        if(current_photos==1){
            o=document.getElementById('l-photos');
            o.src='/i/ajaximages/prev-gray.gif';
            o.onmouseover = function() {}
            o.onmouseout = function() {}
            o=document.getElementById('r-photos');
            o.src='/i/ajaximages/next-red.gif';
            o.onmouseover = function() { this.src='/i/ajaximages/next-red-hover.gif'; }
            o.onmouseout = function() { this.src='/i/ajaximages/next-red.gif'; }
        }else if(current_photos==total_photos){
            o=document.getElementById('r-photos');
            o.src='/i/ajaximages/next-gray.gif';
            o.onmouseover = function() {}
            o.onmouseout = function() {}
            o=document.getElementById('l-photos');
            o.src='/i/ajaximages/prev-red.gif';
            o.onmouseover = function() { this.src='/i/ajaximages/prev-red-hover.gif'; }
            o.onmouseout = function() { this.src='/i/ajaximages/prev-red.gif'; }
        }else{
            o=document.getElementById('r-photos');
            o.src='/i/ajaximages/next-red.gif';
            o.onmouseover = function() { this.src='/i/ajaximages/next-red-hover.gif'; }
            o.onmouseout = function() { this.src='/i/ajaximages/next-red.gif'; }
            o=document.getElementById('l-photos');
            o.src='/i/ajaximages/prev-red.gif';
            o.onmouseover = function() { this.src='/i/ajaximages/prev-red-hover.gif'; }
            o.onmouseout = function() { this.src='/i/ajaximages/prev-red.gif'; }
        }
    o=document.getElementById('counter-photos');
    o.innerHTML = num+' / '+total_photos;


    };
    return aarq4( src_url, callback);
}

function move_photos_left(news_id){
    restartTimerPhotos(news_id);
    if(current_photos>1){
        getphotos(news_id, current_photos-1);
    } else {getphotos(news_id, total_photos);}
}

function move_photos_right(news_id){
    restartTimerPhotos(news_id);
    if(current_photos<total_photos){
        getphotos(news_id, current_photos+1);
    } else {
        getphotos(news_id, 1);
    }
}


function RollTopphotos(news_id)
{
    if (!photos_is_selected) {
        move_photos_right(news_id);
        restartTimerPhotos(news_id);
    }
    else {
        restartTimerPhotos(news_id);
    }
}


function restartTimerPhotos(news_id) {
    clearTimeout(timerPhotos);
    timerPhotos = setTimeout('RollTopphotos('+news_id+')', timeout);
}

    function stars(n) {
        for (i=1; i<=n; i++) {
            document.getElementById("star"+i).src = "/i/star_a.gif"
        }
        if (n == 0) {
            for (i=1; i<=5; i++) {
                document.getElementById("star"+i).src = "/i/star.gif"
            }
        }
    }

    function set_startpage(obj, url) {
        var agent = navigator.userAgent.toLowerCase();
        if (agent.indexOf('msie') >= 0){
            obj.style.behavior='url(#default#homepage)';obj.setHomePage(url);
        }
        else {
            alert('Для того щоб зробити дану сторінку стартової, увійдіть в Інструменти->Настроювання->Основні, натисніть кнопку "Використовувати поточну сторінку"');
        }
    }

    function add_favorite(url,title) {
        var agent = navigator.userAgent.toLowerCase();
        if (agent.indexOf('msie') >= 0){
            window.external.addFavorite(url,title);
        }
        else {
            alert('Для того щоб додати дану сторінку у вибране, закрийте це вікно з повідомленням і натисніть "Ctrl+D"');
        }
    }

window.onload=function(){
Nifty("div.box","normal");
Nifty("div.bottom-box","normal");
Nifty("div.gray","small transparent top");
Nifty("ul#nav a","all");
Nifty("ul#nav div","top");
Nifty("div.taba","top");
Nifty("div.tabb","all");
Nifty("div.gray2","small transparent bottom");
}
