﻿var NO_RECORDS = 'Не бяха открити статии!';
var Obj1;
var secs;
var timerID = null;
var timerRunning = false;
var delay = 1000;

$.ajax({
    type: "POST",
    url: "WebService.asmx/GetFrontArticles",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        Obj1 = msg.d;
        if (!(Obj1.length > 0)) {
            //alert(NO_RECORDS);
        }        
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
    }
});

function InitializeTimer(id) {
    // Set the length of the timer, in seconds
    secs = 10;
    StopTheClock();
    StartTheTimer(id);
}

function StopTheClock() {
    if (timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer(id) {
    if (secs == 0) {
        StopTheClock()        
        switch (id) {
            case 1: Change_Artical(2); InitializeTimer(2); break;
            case 2: Change_Artical(3); InitializeTimer(3); break;
            case 3: Change_Artical(4); InitializeTimer(4); break;
            case 4: Change_Artical(1); InitializeTimer(1); break;
        }
    }
    else {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer(" + id + ")", delay)
    }
}
function Change_Artical(num) {
    InitializeTimer(num);

   document.getElementById('Image1').src = Obj1[num - 1].Pic1;
   document.getElementById('Image1_link').href = Obj1[num - 1].Href;
   document.getElementById('Image1_link').alt = Obj1[num - 1].Title;
   document.getElementById('HyperLink1').innerHTML = Obj1[num - 1].Title;
   document.getElementById('HyperLink1').href = Obj1[num - 1].Href;
   document.getElementById('TextInfo').innerHTML = Obj1[num - 1].Text;
    var i=0;
    for (i = 1; i < 5; i++) {
        if (i == num)
            document.getElementById('divart' + i).className = 'article over';
        else
            document.getElementById('divart' + i).className = 'article out';
    }    
}