function getCook(a) {
    deb = document.cookie.indexOf(a + "=");
    if (deb >= 0) {
        deb += a.length + 1;
        fin = document.cookie.indexOf(";", deb);
        if (fin < 0) {
            fin = document.cookie.length
        }
        return unescape(document.cookie.substring(deb, fin))
    }
    return ""
}
var nbPict = 0,
    firstPict = 0;
var nbMaxView = 3;
$(document).ready(function () {
    var a = 30;
    var c = 0;

	// Use this example, or...
	$('a.lightbox').lightBox(); // Select all links with lightbox class

	$.ajax({
			url: "http:/keynote.macgeneration.com/img/count.php", // url de la page ö charger
			cache:false,
			async:false,
			success:function(xml)
			{
				nbPict = xml;
			},
			error:function (xhr, ajaxOptions, thrownError){
	            console.debug(xhr.status);
	            console.debug(thrownError);
	        }    
		});
	//console.debug(nbPict);
    function b() {
        $("#cpt").html("en cours !");
        $.ajax({
            url: "inc/" + _type + ".html?_=" + new Date().getTime(),
            success: function (f) {
                $("#page_content").html(f);
                c = a;
                $("#cpt").html("dans <b>" + c + "</b> secondes.");
                if ($("#log").size()) {
                    var e = $("#log").html().split("|");
                    e[1] = parseInt(e[1]);
                    if (e[1] > 0) {
                        var d = getCook("keyLastReload");
                        if (d = "" || d != e[1]) {
                            document.cookie = "keyLastReload=" + e[1];
                            location.reload();
                        }
                    }
                    if (_type == "content") {
                        //if (nbPict < e[0]) {
                         //   nbPict = e[0];
                            firstPict = (nbPict < nbMaxView) ? 1 : nbPict - 2;
                            show();
                        //}
                    }
                }
            },
            onFailure: function (d) {
                c = a;
                $("#cpt").html("dans <b>" + c + "</b> secondes.")
            },
            on404: function (d) {
                c = a;
                $("#cpt").html("dans <b>" + c + "</b> secondes.")
            }
        });
        c = -1
    }
    b();
    $.timer({
        name: "cpt",
        interval: 1
    }, function () {
        if (c > -1) {
            if (c == 0) {
                b()
            } else {
                $("#cpt").html("dans <b>" + c + "</b> secondes.");
                c = c - 1
            }
        }
    })
});

function getID(a) {
    return document.getElementById(a)
}
function show() {
    var a = nbMaxView;
    if (nbPict < a) {
        a = nbPict
    }
    for (var c = 0; c < a; c++) {
        var b = (firstPict + c) + ".jpg";
        $("#i" + c).attr("src", "http:/keynote.macgeneration.com/img/thumb/" + b);
        $("#lk" + c).attr("href", "http:/keynote.macgeneration.com/img/" + b);
       
    }
    if (nbPict > nbMaxView) {
        if (firstPict > 1) {
            $("#prevImg").css("visibility", "visible")
        } else {
            $("#prevImg").css("visibility", "hidden")
        }
        if (firstPict == (nbPict - 2)) {
            $("#nextImg").css("visibility", "hidden")
        } else {
            $("#nextImg").css("visibility", "visible")
        }
    } else {
        $("#prevImg").css("visibility", "hidden");
        $("#nextImg").css("visibility", "hidden");
        for (var c = a; c < nbMaxView; c++) {
            $("#i" + c).attr("src", "http://keynote.macgeneration.com/img/thumb/default.jpg");
            $("#lk" + c).attr("href", "javascript:void(0);")
        }
    }
}
function prevImg() {
    if (firstPict - 1 >= 0) {
        firstPict--;
        show()
    }
    return false
}
function nextImg() {
    if (firstPict + 1 <= (nbPict - 2)) {
        firstPict++;
        show()
    }
    return false
};
