var photos = new Array();
var currentPhoto = 0;
integrio.js.include("integrio.element");
integrio.js.include("integrio.utils.events");
integrio.js.include("integrio.effects.MoveEffect");
integrio.js.include("integrio.effects.MoveEffect");
integrio.js.include("integrio.effects.FadeEffect");
integrio.js.include("integrio.effects.AppearanceEffect");
var photoSort = "pos";

function getPhoto(photoPos) {
    for (var i = 0; i < photos.length; i++) {
        if (photos[i].position == photoPos) {
            return photos[i];
        }
    }
    return null;
}

function getPhotoNumber(photoPos) {
    for (var i = 0; i < photos.length; i++) {
        if (photos[i].position == photoPos) {
            return i;
        }
    }
    return 0;
}

var FunArticlePhoto = function(src, tsrc, rating, countVoting, title, isRated, id, position) {
    this.src = src;
    this.tsrc = tsrc;
    this.rating = rating;
    this.countVoting = countVoting;
    this.title = title;
    this.isRated = isRated;
    this.element = null;
    this.moveEffect = null;
    this.position = position;
    this.relPos = {x:0,y:0};
    this.id = id;
};

function initPhotoList() {
    if (!loadPageComplete) {
        setTimeout(initPhotoList, 100);
        return;
    }
    var place = integrio.Element.getElementById("photosListPlace");
    var d = place.getPosition();
    var leftStart = d.x + 5;
    var topStart = d.y + 5;
    var top = 0;
    var left = 0;
    place.innerHTML = "";
    for (var i = 0; i < photos.length; i++) {
        var pd = integrio.Element.createElement("TABLE");
        pd.className = photos[i].position == currentPhoto ? "smallPhotoActiv" : "smallPhoto";
        pd.style.position = "absolute";
        pd.style.width = "75px";
        pd.style.height = "90px";
        pd.style.top = (topStart + top) + "px";
        pd.style.left = (leftStart + left) + "px";
        photos[i].relPos.x = left;
        photos[i].relPos.y = top;
        left += 81;
        if ((i + 1) % 6 == 0) {
            left = 0;
            top += 95;
        }
        document.body.appendChild(pd);
        photos[i].element = pd;
        var img = integrio.Element.createElement("IMG");
        img.src = photos[i].tsrc;
        img.title = photos[i].title;
        pd.insertRow(0).insertCell(0).appendChild(img);
        var starCell = pd.insertRow(1).insertCell(0);
        var starTable = integrio.Element.createElement("TABLE");
        starTable.cellPadding = 0;
        starTable.cellSpacing = 0;
        var tr = starTable.insertRow(0);

        var r = photos[i].rating / photos[i].countVoting;
        var j = 1;
        while (r >= 0.7) {
            var td = tr.insertCell(tr.cells.length);
            td.innerHTML = "<img src='/templates/fun/images/star_fill_small.png'>";
            r--;
            j++;
        }
        if (r > 0.3 && r < 0.7) {
            var td = tr.insertCell(tr.cells.length);
            td.innerHTML = "<img src='/templates/fun/images/star_fill05_small.png'>";
            j++;
        }
        while (j <= 5) {
            var td = tr.insertCell(tr.cells.length);
            td.innerHTML = "<img src='/templates/fun/images/star_empty_small.png'>";
            j++;
        }


        starCell.appendChild(starTable);
        pd.setAttribute("photoNumber", photos[i].position);
        pd.onclick = showPhoto;
    }
    if (integrio.Ext.Browser.isIE) {
        integrio.Element.initElement(document.body).setEventListener("resize", resizePhotosList, true, document.body);
    } else {
        integrio.Object.extend(window, integrio.js.utils.eventsMethods);
        window.setEventListener("resize", resizePhotosList, true, window);
    }
}

function showPhoto() {
    showPhotoProcess(parseInt(this.getAttribute("photoNumber")));
}

function showPhotoProcess(newCP) {
    var p = getPhoto(currentPhoto);
    if (currentPhoto == newCP) {
        return;
    }
    currentPhoto = newCP;
     var photoNumner = getPhotoNumber(currentPhoto)+1;
    document.getElementById("photoNumberSpan").innerHTML = photoNumner;

    p.element.className = "smallPhoto";
    var t = integrio.Element.getElementById("mainPhotoTable");
    if (t.fadeEffect == null) {
        t.fadeEffect = integrio.effects.FadeEffect.create();
        t.fadeEffect.afterEnd = function() {
            var t = integrio.Element.getElementById("mainPhotoTable");
            var p = getPhoto(currentPhoto);
            var rowItem = 0;
            if (t.rows.length > 1) {
                t.rows.item(0).cells.item(0).innerHTML = p.title;
                rowItem = 1;
            }
            t.rows.item(rowItem).cells.item(0).getElementsByTagName("IMG")[0].onload = checkLoadPhoto;
            t.rows.item(rowItem).cells.item(0).getElementsByTagName("IMG")[0].src = p.src;
        };
    }
    new Image().src = p.src;
    t.fadeEffect.init(t, 500);
}

function checkLoadPhoto(){
    var t = integrio.Element.getElementById("mainPhotoTable");
    if (t.appearanceEffect==null){
        t.appearanceEffect = integrio.effects.AppearanceEffect.create();
    }
    t.appearanceEffect.init(t,500);
    showPhoto2();
}

function showPhoto2() {
    var p = getPhoto(currentPhoto);
    isRated = p.isRated;
    videoRating = p.rating;
    videoCRating = p.countVoting;
    overRating(p.rating, p.countVoting);
    p.element.className = "smallPhotoActiv";
}

initPhotoList();

function resizePhotosList() {
    var place = integrio.Element.getElementById("photosListPlace");
    var d = place.getPosition();
    var leftStart = d.x + 5;
    var topStart = d.y + 5;
    for (var i = 0; i < photos.length; i++) {
        photos[i].element.style.left = (leftStart + photos[i].relPos.x) + "px";
        photos[i].element.style.top = (topStart + photos[i].relPos.y) + "px";
    }
}

function sortByRating() {
    if (!loadPageComplete) {
        return;
    }
    photos.sort(function(a, b) {
        return (b.rating / b.countVoting) - (a.rating / a.countVoting);
    });
    photoSort = "rating";
    var tabsTable = document.getElementById("photoListTabs");
    tabsTable.rows.item(0).cells.item(0).className = "tab";
    tabsTable.rows.item(0).cells.item(2).className = "activTab";
    movedAfterSort();
}

function sortByPosition() {
    if (photoSort == "pos") {
        return;
    }
    photos.sort(function(a, b) {
        return (a.position) - (b.position);
    });
    photoSort = "pos";
    var tabsTable = document.getElementById("photoListTabs");
    tabsTable.rows.item(0).cells.item(2).className = "tab";
    tabsTable.rows.item(0).cells.item(0).className = "activTab";
    movedAfterSort();
}

function movedAfterSort() {
    var place = integrio.Element.getElementById("photosListPlace");
    var d = place.getPosition();
    var leftStart = d.x + 5;
    var topStart = d.y + 5;
    var top = 0;
    var left = 0;
    for (var i = 0; i < photos.length; i++) {
        photos[i].relPos.x = left;
        photos[i].relPos.y = top;
        if (photos[i].moveEffect == null) {
            photos[i].moveEffect = integrio.effects.MoveEffect.create();
        }
        photos[i].moveEffect.init(photos[i].element, 500, {toX:leftStart + left,toY:topStart + top});
        left += 81;
        if ((i + 1) % 6 == 0) {
            left = 0;
            top += 95;
        }
    }
}

function redrawSmallStars(p) {
    var r = p.rating / p.countVoting;
    var j = 1;
    var tr = p.element.rows.item(1).cells.item(0).getElementsByTagName("TABLE")[0].rows.item(0);
    while (r >= 0.7) {
        var td = tr.cells.item(j - 1);
        td.innerHTML = "<img src='/templates/fun/images/star_fill_small.png'>";
        r--;
        j++;
    }
    if (r > 0.3 && r < 0.7) {
        var td = tr.cells.item(j - 1);
        td.innerHTML = "<img src='/templates/fun/images/star_fill05_small.png'>";
        j++;
    }
    while (j <= 5) {
        var td = tr.cells.item(j - 1);
        td.innerHTML = "<img src='/templates/fun/images/star_empty_small.png'>";
        j++;
    }
}

function nextPhoto() {
    var t = getPhotoNumber(currentPhoto) + 1;
    if (t >= photos.length) {
        t = 0;
    }
    showPhotoProcess(photos[t].position);
}
function prevPhoto() {
    var t = getPhotoNumber(currentPhoto) - 1;
    if (t < 0) {
        t = photos.length-1;
    }
    showPhotoProcess(photos[t].position);
}

