﻿var presentationListIndex = 0;
var newsListIndex = 0;
var contentsListIndex = 0;

$(document).ready(function () {
    $("#calendar-navigator .calendar-prev a").click(function () { tarihHesapla(-1); });
    $("#calendar-navigator .calendar-next a").click(function () { tarihHesapla(1); });
    $("#calendar-top-navigator .calendar-prev").click(function () { etkinlikGoster(-1); });
    $("#calendar-top-navigator .calendar-next").click(function () { etkinlikGoster(1); });
    
    etkinlikTakvimi();

    var flashvars = { Server: sUrl };
    var params = { scale: 'noScale', salign: 't', menu: 'false', allowFullScreen: "false", wmode: "transparent" };
    var attributes = {};
    swfobject.embedSWF("/swf/top_banner.swf", "promo-object", "100%", "430", "9.0.45", "/swf/expressInstall.swf", flashvars, params, attributes);


    $("#presentations-list").width($("#presentations-list div.presentations-list-item").width() * $("#presentations-list div.presentations-list-item").length + "px");
    $("#presentations-nav .prev").click(function () {
        if (presentationListIndex == 0) return;
        presentationListIndex -= 1;
        animatePresentation();
    });
    $("#presentations-nav .next").click(function () {
        if ((presentationListIndex + 1) == $("#presentations-list div.presentations-list-item").length) return;
        presentationListIndex += 1;
        animatePresentation();
    });

    $("#content-middle-content-list").width($("#content-middle-content-list div.container-list").width() * $("#content-middle-content-list div.container-list").length + "px");
    $("#contents-nav .prev").click(function () {
        if (contentsListIndex == 0) return;
        contentsListIndex -= 1;
        animateContents();
    });
    $("#contents-nav .next").click(function () {
        if ((contentsListIndex + 1) == $("#content-middle-content-list div.container-list").length) return;
        contentsListIndex += 1;
        animateContents();
    });

    $("#news-list").width($("#news-list div").width() * $("#news-list div").length + "px");
    $("#news-nav .prev").click(function () {
        if (newsListIndex == 0) return;
        newsListIndex -= 1;
        animateNews();
    });
    $("#news-nav .next").click(function () {
        if ((newsListIndex + 1) == $("#news-list div").length) return;
        newsListIndex += 1;
        animateNews();
    });

    $("#news-tab-buttons a").click(function () {
        $("#news-tab-buttons a").removeClass("selected");
        $(this).addClass("selected");
        newsListIndex = 0;
        animateNews();

        $("#e-gazete-content").hide();
        $("#news-content").hide();
        if ($(this).index() == 0)
            $("#e-gazete-content").fadeIn(250);
        else if ($(this).index() == 1)
            $("#news-content").fadeIn(250);
    });
});

function animatePresentation() {
    $("#presentations-container").animate({ scrollLeft: (presentationListIndex * $("#presentations-list div.presentations-list-item").width()) });
}
function animateContents() {
    $("#content-middle-container").animate({ scrollLeft: (contentsListIndex * $("#content-middle-content-list div.container-list").width()) });
}
function animateNews() {
    $("#news-content-container").animate({ scrollLeft: (newsListIndex * $("#news-list div").width()) });
}

var etkinlikAy = currDate.getMonth() + 1;
var etkinlikYil = currDate.getFullYear();
var bugun = currDate.getDate();
var etkinlikler = null;
var gunIndex = -1;
var etkinlikIndex = 0;
var gunSet = false;
var sonDoluGun = 0;
var sonDoluGunKontrol = false;

function etkinlikTakvimi() {
    $("#calendar").fadeOut(250);
    AjaxService.EtkinlikTakvimi(etkinlikAy, etkinlikYil, function (val) {
        etkinlikIndex = 0;
        gunIndex = -1;
        etkinlikler = val;
        gunSet = false;
        $("#calendar-list ul:eq(1)").empty();
        for (var i = 1; i < etkinlikler.DayOfWeek; i++)
            $("#calendar-list ul:eq(1)").append("<li><a></a></li>");

        var etkinlikAdetMetni = "";
        $(etkinlikler.Gun).each(function () {
            var etkinlikCss = "";
            if (this.Etkinlikler.length > 0) {
                if (this.Gun >= bugun && sonDoluGunKontrol == false) {
                    sonDoluGun = (this.Gun - 1);
                    sonDoluGunKontrol = true;
                }

                etkinlikCss = "etkinlik-var";
                if (gunSet == false) {
                    gunSet = true;
                    gunIndex = (this.Gun - 1);
                }
            }

            etkinlikAdetMetni = "";
            if (this.Etkinlikler.length > 1) {
                etkinlikAdetMetni = "<img src=\"/images/icon/etkinlik/" + this.Etkinlikler.length + ".png\" />";
            }

            $("#calendar-list ul:eq(1)").append("<li><a class=\"" + etkinlikCss + "\" id=\"etkinlik-gun-" + this.Gun + "\" href=\"javascript:void(0)\">" + this.Gun + "</a>" + etkinlikAdetMetni + "</li>");
        });

        if (etkinlikler.TotalRecord == 0) {
            $("#etkinlik-detay").hide();
            $(".record-not-found").show();
        }


        $("#calendar-list ul:eq(1) a").click(function () {
            etkinlikIndex = 0;
            gunIndex = (this.id.substr(13) - 1);
            etkinlikGoster(gunIndex);
        });

        $(".calendar-current").html(aylar[etkinlikAy] + " " + etkinlikYil);
        gunIndex = sonDoluGun;
        etkinlikGoster(sonDoluGun);
        $("#calendar").fadeIn(250);
    }, null);
}

function etkinlikGoster(act) {
    $("#etkinlik-detay").hide();
    if (act == -1) {
        etkinlikIndex -= 1;

        if (etkinlikIndex < 0) {
            etkinlikIndex = 0;
            gunIndex--;
        }

        if (etkinlikler.Gun[gunIndex] != undefined) {
            if (etkinlikler.Gun[gunIndex].Etkinlikler.length == 0)
                etkinlikGoster(-1);
        } 
        else {
            $(etkinlikler.Gun).each(function (_index) {
                etkinlikIndex = 0;
                if (this.Etkinlikler.length > 0) {
                    gunIndex = _index;
                }
            });
        }
    }

    if (act == 1) {
        etkinlikIndex += 1;

        if (etkinlikler.Gun[gunIndex] != undefined) {
            if ((etkinlikIndex + 1) > etkinlikler.Gun[gunIndex].Etkinlikler.length) {
                etkinlikIndex = 0;
                gunIndex++;
            }

            if (etkinlikler.Gun[gunIndex] != undefined) {
                if (etkinlikler.Gun[gunIndex].Etkinlikler.length == 0) {
                    etkinlikIndex = 0;
                    etkinlikGoster(1);
                }
            } else {
                $(etkinlikler.Gun).each(function (_index) {
                    etkinlikIndex = 0;
                    if (this.Etkinlikler.length > 0) {
                        gunIndex = _index;
                        return false;
                    }
                });
            }
        }
    }
    
    if (etkinlikler.Gun[gunIndex] == undefined) {
    } else if (etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex] == undefined) {
    } else {
        $("#etkinlik-detay").fadeIn(500);
        $("#calendar-image img").attr("src", "/bitmap-crop/272/96?src=" + etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].ResimURL);
        $("#calendar-image a").attr("href", "/etkinlik/" + etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].EtkinlikID + "/" + etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].UrlBaslik);
        $("#calendar-image a").attr("title", etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].Baslik);
        $("#calendar-title a").attr("href", "/etkinlik/" + etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].EtkinlikID + "/" + etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].UrlBaslik);
        $("#calendar-title a").html(etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].Baslik);
        $("#calendar-date").html(etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].Zaman.format("dd") + " " + aylar[parseInt(etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].Zaman.format("MM"))] + " " + haftaninGunu(etkinlikler.Gun[gunIndex].Etkinlikler[etkinlikIndex].Zaman.format("ddd")));

        $("#calendar-list ul:eq(1) li a").removeClass("current-day");
        $("#etkinlik-gun-" + (gunIndex + 1)).addClass("current-day");
    }
}

function tarihHesapla(act) {
    $("#etkinlik-detay").show();
    $(".record-not-found").hide();

    if (act == -1) {
        etkinlikAy = etkinlikAy - 1;
        if (etkinlikAy == 0) {
            etkinlikAy = 12;
            etkinlikYil = etkinlikYil - 1;
        }
    }
    else if (act == 1) {
        etkinlikAy = etkinlikAy + 1;
        if (etkinlikAy == 13) {
            etkinlikAy = 1;
            etkinlikYil = etkinlikYil + 1;
        }
    }
    etkinlikTakvimi();
}
