var fadeSpeed = 1000;
var showOverImageOnActiveLink = false;
var colCount = 3;
var rowCount = 9;
var pagePrefix = 'page/';
var imgPrefix = 'i/';
var activeThumbnail = null;
var faded = true;
var loaded = false;

var next = [0, 0, 0];

$(function(){
    for (var i = 0; i < rowCount; i++){
        showNextRow();
    }
    $('#forLoadIframe').load(onIframeLoad);
    activeThumbnail = $('#thumbnails')[0].firstChild;
    $('#presentation')[0].src = activeThumbnail.href;
    $('#forLoadIframe')[0].src = activeThumbnail.href;
    var img = activeThumbnail.firstChild;
    img.src = imgPrefix + img.thumnailID + '_on.gif';
    window.onload = function(){$('#thumbnails').css('visibility', 'visible');};
    $('#scrollButton').click(scrollThumnails);
});

function showNextRow(){
    for (var i = 0; i < colCount; i++){
        $('#thumbnails').append(getNextImgLink(i));
    }
}

function getNextImgLink(col){
    if (next[col] >= columns[col].length){
        next[col] = 0;
    }
    var link = $('<a href="' + pagePrefix + columns[col][next[col]] + '.html" style="padding:2px;display:block;float:left;width:22px;height:30px;outline:0;"></a>');
    var img = $('<img src="' + imgPrefix + columns[col][next[col]] + '_off.gif" style="border:0;" />');
    var largeImg = $('<img src="' + imgPrefix + columns[col][next[col]] + '_over.gif" style="border:0;position:relative;top:-39px;left:-6px;visibility:hidden;z-index:20;" />');
    link.append(img);
    link.append(largeImg);
    img[0].thumnailID = columns[col][next[col]];
    link.click(onThumbnailClick);
    link.mouseover(onThumbnailMouseOver);
    link.mouseout(onThumbnailMouseOut);
    next[col]++;
    return link;
}

function hideFirstRow(){
    $('#thumbnails').animate({'top':'-35px'}, 500, 0, removeFirstRow);
}

function removeFirstRow(){
    for (var i = 0; i < colCount; i++){
        $($('#thumbnails')[0].firstChild).remove();
    }
    $('#thumbnails').css({'top':0});
}

function scrollThumnails(){
    showNextRow();
    hideFirstRow();
}

function onThumbnailMouseOver(){
    if(showOverImageOnActiveLink || this != activeThumbnail){
        var img = this.lastChild;
        img.style.visibility = 'visible';
    }
}

function onThumbnailMouseOut(){
    if(this != activeThumbnail){
        var img = this.firstChild;
        img.src = imgPrefix + img.thumnailID + '_off.gif';
    }
    this.lastChild.style.visibility = 'hidden';
}

function onThumbnailClick(){
    faded = false;
    loaded = false;
    $('#presentation').fadeOut(fadeSpeed, afterFade);
    $('#forLoadIframe')[0].src = this.href;
    var oldActive=activeThumbnail.firstChild;
    activeThumbnail = this;
    var img = activeThumbnail.firstChild;
    img.src = imgPrefix + img.thumnailID + '_on.gif';
    $(oldActive).mouseout();
    $(this).mouseout();
    return false;
}

function onIframeLoad(){
    loaded = true;
    if (faded){
        showIframe();
    }
}

function onIframeLinkClick(){
    faded = false;
    loaded = false;
    $('#presentation').fadeOut(fadeSpeed, afterFade);
    $('#forLoadIframe')[0].src = this.href;
    return false;
}

function afterFade(){
    faded = true;
    if (loaded){
        showIframe();
    }
}

function showIframe(){
    $('#presentation')[0].contentWindow.document.body.innerHTML = $('#forLoadIframe')[0].contentWindow.document.body.innerHTML;
    $('#presentation')[0].contentWindow.document.body.style.backgroundImage = 'url(../images/iframe-background.jpg)';
    $('a', $('#presentation')[0].contentWindow.document.body).each(function(){if(this.target!='_blank')this.onclick = onIframeLinkClick});
    $('#presentation').fadeIn(fadeSpeed);
}
function opennew(url,x,y){
  var z = screen.width/2 - 275;
  var msgWindow = window.open(url,'window2','width=' + x + ',height=' + y + ',left=' + z + ',xpos=' + z + ',top=120,ypos=120,dependent=no,location=no,titlebar=no,resizable=no,status=no');
  msgWindow.document.write('.');
  msgWindow.document.location = url;
}