﻿var ArnicaBG;
var BGCount = 3;
var BGNumberRnd = Math.floor(Math.random() * BGCount) + 1;
var MenuArray = ["anasayfa", "kurumsal", "urun-ailesi", "musteri-hizmetleri", "guncel", "arnicadan", "iletisim"];
var Menu1 = ["tarihce", "tanitim-filmi", "bayi-girisi"];
var Menu2 = ["ev-temizligi", "mutfak-gerecleri", "kisisel-bakim", "ev-gerecleri"];
var Menu3 = ["turkiye-genel-dagiticilari", "yetkili-servisler", "garanti-sartlari", "oneri-ve-dilekleriniz", "sikca-sorulan-sorular"];
var Menu4 = ["haberler", "basinda-arnica", "tv-reklamlari", "basin-ilanlari"];
var Menu5 = ["tarifler", "kisisel-bakim", "pratik-bilgiler"];
var Menu6 = ["iletisim-bilgileri", "is-basvuru-formu"]
var MainPage = '';
var SubPage = '';
var SubMenuMaxTop = 0;
var ProductMenuMaxTop = 0;
var SubMenuAktif = 0;
var SPNumber;
var LastSPNumber = 0;
var LeftMargin = 0;
var ProductListAktif = 0;
var ListWidth = 0;
var AktifList = '';
var LastAktifList = '';
var ContentShow = 0;
var NewsListAktif = 0;

function CookieCheck() {
    if ($.cookie("ArnicaBGNumber") != null) {
        ArnicaBG = parseInt($.cookie("ArnicaBGNumber")) + 1;
        if (ArnicaBG > BGCount) {
            ArnicaBG = 1
        }
    } else {
        ArnicaBG = 1
    }
    $.cookie("ArnicaBGNumber", ArnicaBG, { expires: 7 });
}
function Ortala() {
    var w = $(window).width();
    var h = $(window).height();
    if (h > 681) {
        f = (h - 680) / 2
        $('.line td').css("height", f + "px");
    } else {
        $('.line td').css("height", "0px");
    }
    var y = $('#content').offset();
    SubMenuMaxTop = y.top + 539;
    ProductMenuMaxTop = y.top + 501;
    $('#categories').css("top", 579 + $('.line td').height());
    $('#products').css("top", SubMenuMaxTop);
    $('#haberler-menu').css("top", SubMenuMaxTop);
}
function CalcListWidth(id) {
    if (LastAktifList != id) {
        $("#list-" + id).show();
        ListWidth = 0;
        $("#list-" + id + " li").each(function() {
            ListWidth += $(this).outerWidth(true);
        });
        $("#list-" + id).width(ListWidth).css("margin-left", "0px");
        LeftMargin = 0;
        LastAktifList = id;
        $('#product-list ul li').removeClass('current');
    }
}
function CalcListPosition(sb, pa) {
    var ulWidth = $('#product-list #list-' + sb).width();
    var ulOffset = $('#product-list #list-' + sb).position();
    var ulLeft = ulOffset.left;
    var li = $('#product-list #list-' + sb + ' #' + pa);
    var liOffset = li.position();
    var liLeft = liOffset.left - ulLeft;
    var liWidth = li.width();
    if (liLeft + liWidth > 1142) {
        var mLeft = liLeft - 1142 + liWidth
        $("#list-" + sb).css("margin-left", (mLeft) * -1);
    }
}
function ShowNewsList() {
    $('#haberler-menu').animate({ top: ProductMenuMaxTop }, 500).dequeue().fadeIn(500, function() {
        $('#haberler-menu-content ul').fadeIn();
    });
    NewsListAktif = 1;
}
function HideNewsList() {
    if (NewsListAktif == 1) {
        $('#haberler-menu-content ul').fadeOut();
        $('#haberler-menu').animate({ top: SubMenuMaxTop }, 500).dequeue().fadeOut(500);
        NewsListAktif = 0;
    }
}
function ShowProductList(id) {
    $('#products').animate({ top: ProductMenuMaxTop }, 500).dequeue().fadeIn(500, function() {
        $('#list2').fadeIn();
    });
    ProductListAktif = 1;
    ToolsChange(id)
    LoadProductList(id);
}
function HideProductList() {
    if (ProductListAktif == 1) {
        $('#list2').fadeOut();
        $('#products').animate({ top: SubMenuMaxTop }, 500).dequeue().fadeOut(500);
        ProductListAktif = 0;
    }
}
function LoadProductList(id) {
    if (AktifList != id) {
        $('.product-list-sub').hide();
        $("#list-" + id).fadeIn();
        AktifList = id;
    }
}
function ToolsChange(id) {
    var newid = id.replace("-", "");
    $('#tools ul li a').removeClass("current");
    //$('#tools ul li.' + newid + ' a').addClass("current");
}
function CloseText(hash) {
    $('#text-content #text').empty();
    $('#text-content').fadeOut();
    if (hash != 'a') {
        window.location = '#!/' + hash;
    }
    ContentShow = 0;
}
function ShowSubMenu(id, n) {
    if (LastSPNumber != n) {
        $('.submenu').hide();
    }
    $('#mainmenu ul li').removeClass("current").eq(n - 1).addClass("current");
    $('#categories').animate({ top: SubMenuMaxTop }, 500).dequeue().fadeIn(500, function() {
        $('#categories #' + id).fadeIn();
    });
    SubMenuAktif = 1;
    LastSPNumber = n;
}
function HideSubMenu() {
    if (SubMenuAktif == 1) {
        $('.submenu').fadeOut();
        $('#categories').animate({ top: 579 + $('.line td').height() }, 500).dequeue().fadeOut(500);
        SubMenuAktif = 0;
        $('#mainmenu ul li').removeClass("current");
    }
}
function LoadContent(page, ContentType) {
    $('#text-content #text').html('');
    if (ContentType == 1) {
        $.ajax({
            type: "GET",
            cache: false,
            dataType: "html",
            url: 'content/' + page + '.html',
            success: function(data) {
                $('#text-content #text').empty().delay(500).html(data);
                if (page == 'tanitim-filmi' || page == 'bayi-girisi' || page == 'basin-ilanlari' || page == 'oneri-ve-dilekleriniz' || page == 'tv-reklamlari' || page == 'yeni-urun' || page == 'basinda-arnica' || page == 'yetkili-servisler') {
                    $('#text-content').width(529).css("left", "335px");
                    $('#text-content #text').addClass("text2");
                    /*} else if (page == 'oneri-ve-dilekleriniz') {
                    $('#text-content').width(578).css("left", "311px");
                    $('#text-content #text').width(538)*/
                    /*} else if (page == 'basin-ilanlari') {
                    $('#text-content').width(456).css("left", "372px");
                    $('#text-content #text').width(416)*/
                } else {
                    $('#text-content').width(984).css("left", "108px");
                    $('#text-content #text').removeClass("text2").width(944);
                }
                $('#text-content').fadeIn();
            }
        });
    } else if (ContentType == 2) {
        PageMethods.UrunGetirMethod(page, UrunBilgisiYaz);
        $('#product-list ul li').removeClass('current');
        $('#product-list ul #' + page).addClass('current');
    } else if (ContentType == 3) {
        $.ajax({
            type: "GET",
            cache: false,
            dataType: "html",
            url: 'content/haber-' + page + '.html',
            success: function(data) {
                $('#text-content #text').empty().delay(500).html(data);
                $('#text-content').width(529).css("left", "335px");
                $('#text-content #text').addClass("text2");
                $('#text-content').fadeIn();
            }
        });
    }
    ContentShow = 1;
}
function UrunBilgisiYaz(bilgi) {
    $('#text-content').width(984).css("left", "108px");
    $('#text-content #text').removeClass("text2").width(944).empty().delay(500).html(bilgi);
    $('#text-content').fadeIn();
    if ($('.twitter-share-button').length) {
        twttr.widgets.load();
    }
}
function ImageChange(src) {
    $('.images #big').attr("src", src);
}

$(function() {
    $("#mainmenu ul li a").click(function() {
        $(this).parent().addClass("current").siblings().removeClass("current");
    });
    /*
    $("#tools ul li a").click(function() {
    $("#tools ul li a").removeClass("current");
    $(this).addClass("current");
    });
    */
    $("#products-right").hover(function() {
        if (ListWidth > 1142) {
            LeftMargin = (parseInt($("#list-" + AktifList).css("margin-left")) * -1)
            var MaxLeftMargin = $("#list-" + AktifList).width() - 1142;
            for (LeftMargin; LeftMargin <= MaxLeftMargin; LeftMargin++) {
                $("#list-" + AktifList).animate({ marginLeft: -1 * LeftMargin }, 1);
            }
        }
    }, function() {
        $("#list-" + AktifList).stop(true, true);
    });
    $("#products-left").hover(function() {
        if (ListWidth > 1142) {
            LeftMargin = (parseInt($("#list-" + AktifList).css("margin-left")) * -1);
            if (LeftMargin > 0) {
                for (LeftMargin; LeftMargin >= 0; LeftMargin--) {
                    $("#list-" + AktifList).animate({ marginLeft: -1 * LeftMargin }, 1);
                }
            }
        }
    }, function() {
        $("#list-" + AktifList).stop(true, true);
    });
    $("#products-left").click(function() {

    });
});
$.address.change(function(event) {
    var PageArray = event.value.split("/")
    var AcilacakSayfa = PageArray[1];
    if (event.value == '/') { AcilacakSayfa = "anasayfa" }
    if (PageArray.length == 2) {
        if (jQuery.inArray(AcilacakSayfa, MenuArray) > -1) {
            SPNumber = 0;
            if (AcilacakSayfa != "anasayfa") {
                ShowSubMenu(AcilacakSayfa, jQuery.inArray(AcilacakSayfa, MenuArray));
                $("#categories .submenu a").removeClass("current");
                HideProductList();
                HideNewsList();
            } else {
                HideNewsList();
                HideSubMenu();
                HideProductList();
                $("#categories .submenu a").removeClass("current");
            }
            $("#tools ul li a").removeClass("current");
            $('#text-content #text').empty();
            $('#text-content').fadeOut();
            ContentShow = 0;
        }
        if (AcilacakSayfa == 'yeni-urun') {
            HideNewsList();
            HideSubMenu();
            HideProductList();
            $("#categories .submenu a").removeClass("current");
            LoadContent('yeni-urun', 1);
        }
    }
    if (PageArray.length == 3) {
        SubPage = PageArray[2];
        if (jQuery.inArray(AcilacakSayfa, MenuArray) > -1) {
            SPNumber = jQuery.inArray(AcilacakSayfa, MenuArray)
            ShowSubMenu(AcilacakSayfa, SPNumber);
            if (jQuery.inArray(SubPage, eval("Menu" + SPNumber)) > -1) {
                if (AcilacakSayfa == "urun-ailesi") {
                    HideNewsList();
                    ShowProductList(SubPage);
                    CalcListWidth(SubPage);
                    if (ContentShow == 1) {
                        $('#text-content #text').empty();
                        $('#text-content').fadeOut();
                        ContentShow = 0;
                    }
                    $('#bg').removeClass();
                    $('#bg').addClass('bg-' + SubPage);
                } else {
                    if (SubPage == "haberler") {
                        HideProductList();
                        $('#text-content #text').empty();
                        $('#text-content').fadeOut();
                        ContentShow = 0;
                        ShowNewsList();
                        $('#haberler-menu-content ul li').removeClass("current");
                    } else {
                        HideProductList();
                        HideNewsList();
                        LoadContent(SubPage, 1);
                    }
                }
                $('#categories #' + AcilacakSayfa + ' a').eq(jQuery.inArray(SubPage, eval("Menu" + SPNumber))).addClass("current").siblings().removeClass("current");
            }
        }
    }
    if (PageArray.length == 4) {
        SubPage = PageArray[2];
        if (AcilacakSayfa == "urun-ailesi") {
            ShowSubMenu(AcilacakSayfa, 2);
            $('#bg').removeClass();
            $('#bg').addClass('bg-' + SubPage);
            if (jQuery.inArray(SubPage, Menu2) > -1) {
                ShowProductList(SubPage);
                CalcListWidth(SubPage);
                LoadContent(PageArray[3], 2);
                //CalcListPosition(SubPage, PageArray[3])
            }
        }
        if (SubPage == "haberler") {
            ShowSubMenu('guncel', 4);
            $('#categories #guncel a').removeClass("current").eq(0).addClass("current");
            ShowNewsList();
            $('#haberler-menu-content ul li').removeClass("current");
            $('#haber-' + PageArray[3]).addClass("current");
            LoadContent(PageArray[3], 3);
        }
    }
});
