﻿$(document).ready(function () {

    calculateWindowSize();

    $(window).resize(function () {
        calculateWindowSize();
    });

    var windowWidth, windowHeight, contentHeightForSlider, contentHeightForSlideShow, slideShowWidth;
    function calculateWindowSize() {

        windowWidth = $(window).width();
        windowHeight = $(window).height();
        contentHeightForSlider = windowHeight - 137;
        contentHeightForSlideShow = windowHeight - 150;
        slideShowWidth = parseInt(Math.round((contentHeightForSlider * 1100) / 779));
        var totalFooter = parseInt(Math.round(((windowWidth / 2) - ($(".footerLeftText").width()))));

        $('.slider').css("height", contentHeightForSlider).css("width", slideShowWidth);
        $('.container').css("width", slideShowWidth);
        $('.footerNavBox').css("width", slideShowWidth - 130);
        $('.footer').css("width", slideShowWidth - 5);
        $('.controlsToWidth, .controlsToWidthControl').css("width", slideShowWidth);
        $('#slideshow').css("height", contentHeightForSlideShow).css("width", slideShowWidth - 8);
        $('ul.inner-slideshow, ul.inner-slideshow li, ul.inner-slideshow img').css("height", contentHeightForSlideShow).css("width", slideShowWidth);
        $('.footerSocialBox').css("marginLeft", totalFooter - 30);
        $('.keyboard').css("left", parseInt(Math.round(slideShowWidth / 2)) - 111);

        var sum = 0;
        $('.footerMenu .nav-ct').each(function (index) {
            sum += $(this).width() + (parseInt($(this).css("margin-left")) + parseInt($(this).css("margin-right")));
        });
        $(".footerMenu").css("width", sum + 250);
    }

    /*Scrolling Menu*/
    var div = $('div.headerMenuBox'), ul = $('ul.headerMenu'), ulPadding = 15;
    var divWidth = div.width();
    div.css({ overflow: 'hidden' });
    var lastLi = ul.find('li:last-child');
    div.mousemove(function (e) {
        var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
        var left = (e.pageX - div.offset().left) * (ulWidth - divWidth) / divWidth;
        div.scrollLeft(left);
    });

    /*Get Active Class*/
    $('.headerMenu a').each(function (index) {
        if ($(this).attr("class") == "active") {
            var url = $(this).attr("href");
            $(".getAjaxData").load(url, function (response, status, xhr) {
                if (status == "error") {
                    var msg = "Sayfa Yüklenemedi: ";
                    $(".getAjaxData").html(msg + xhr.status + " " + xhr.statusText);
                }
                LoadPageFunction();
                calculateWindowSize();
            });
        }
        $(this).click(function () {
            $('.headerMenu a').removeClass("active");
            $(this).toggleClass("active");
            var url = $(this).attr("href");
            $(".getAjaxData").load(url, function (response, status, xhr) {
                if (status == "error") {
                    var msg = "Sayfa Yüklenemedi: ";
                    $(".getAjaxData").html(msg + xhr.status + " " + xhr.statusText);
                }
                LoadPageFunction();
                calculateWindowSize();
                $('.keyboard').fadeOut(350);
            });
            return false;
        });
    });

    function LoadPageFunction() {


        var FX = "";
        if ($.browser.webkit || $.browser.msie) {
            FX = 'scrollUp';
        }
        else {
            FX = 'scrollVert';
        }

        var inners = $('.inner-slideshow').cycle('stop');
        var slideshow = $('#slideshow').cycle({
            fx: FX,
            speed: 1200,
            timeout: 0,
            before: onBefore,
            after: onAfter,
            rev: 1,
            pager: '#nav',
            pagerAnchorBuilder: function (idx, slide) {
                return '#nav .nav-ct:eq(' + (idx) + ') >div';
            },
            before: function () {
                inners.cycle('stop');
                $(this).cycle({
                    fx: 'scrollHorz',
                    speed: 1000,
                    timeout: 0,
                    //prev: '#prev',
                    //next: '#next',
                    nowrap: 1,
                    after: function (curr, next, opts) {
                        $(".hidden").html('<div id="currSlide">' + (opts.currSlide + 1) + '</div><div id="slideCount">' + opts.slideCount + '</div>');
                    }
                });
            }
        });

        function getListItemNumber(getEqualNumber) {
            var subSliderNumber = $("#slideshow ul").eq(parseInt(getEqualNumber)).find(">li").length;
            return subSliderNumber;
        }

        function prevBtn() {
            if ($("#currSlide").text() == 1 | $("#currSlideMain").text() == 1) {
                slideshow.cycle('prev');
                var getShow = parseInt($("#currSlideMain").text());
                inners.cycle(getListItemNumber(getShow - 2) - 1);
                mouseOver($('.activeSlide'));
                mouseOut($('.activeSlide'));
            }
            else {
                inners.cycle('prev');
            }
            $('.keyboard').fadeOut(350);
        }

        function nextBtn() {
            if ($("#slideCount").text() == $("#currSlide").text() | $("#currSlideMain").text() == 1) {
                slideshow.cycle('next');
                mouseOver($('.activeSlide'));
                mouseOut($('.activeSlide'));
            }
            else {
                inners.cycle('next');
            }
            $('.keyboard').fadeOut(350);
        }

        $("#next, #prev").hover(
          function () {
              $(this).stop(true, true)
                  .animate({
                      width: '35px'
                  }, 250, function () {
                  });
          },
          function () {
              $(this).animate({
                  width: '13px'
              }, 400, function () {
              });
          }
        );

        $("#next").click(function (event) {
            nextBtn();
            slideMenu(1);
        });

        $("#prev").click(function (event) {
            prevBtn();
            slideMenu(0);
        });

        $(document).bind('keydown.scrollable', function (e) {
            if (e.keyCode == 37)
                prevBtn();
            else if (e.keyCode == 39)
                nextBtn();
            else if (e.keyCode == 38) {
                slideshow.cycle('prev');
                slideMenu(0);
                mouseOver($('.activeSlide'));
                mouseOut($('.activeSlide'));
            }
            else if (e.keyCode == 40) {
                slideshow.cycle('next');
                $('.keyboard').fadeOut(350);
                slideMenu(1);
                mouseOver($('.activeSlide'));
                mouseOut($('.activeSlide'));
            }
        });

        /*Slider Anim Function*/
        $('.slider').animate({
            marginTop: '0'
        }, 1250, function () {
            $('.keyboard').fadeIn(1250);
        });

        function onBefore(curr, next, opts, fwd) {
            var $ht = $(this).height();
            $(this).parent().animate({ height: $ht }, 0);
            alert($ht);
        }
        function onAfter(curr, next, opts, fwd) {
            calculateWindowSize();
            callActiveClass($("li.activeSlide"));
            $(".hiddenMain").html('<div id="currSlideMain">' + (opts.currSlide + 1) + '</div><div id="slideCountMain">' + opts.slideCount + '</div>');
        }

        /*var div = $('div.footerNavBox'), ul = $('ul.footerMenu'), ulPadding = 15;
        var divWidth = div.width();
        div.css({ overflow: 'hidden' });
        var lastLi = ul.find('li:last-child');
        div.mousemove(function (e) {
        var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
        var left = (e.pageX - div.offset().left) * (ulWidth - divWidth) / divWidth;
        div.scrollLeft(left);
        });*/

        $("div#makeMeScrollable").smoothDivScroll({});

        $("a.navFacebook, a.navTwitter").click(function (event) {
            event.stopPropagation();
            return false;
        });

        $('.footerMenu .nav-ct, .footerMenuPdf li').each(function (index) {

            callActiveClass($(this));

            $(this).hover(
              function () {
                  mouseOver($(this));
              },
              function () {
                  mouseOut($(this));
              }
            );
        });

        $(".activeSlide h1").css("borderBottom", "1px dotted #e51e26");

        Shadowbox.init();
        Shadowbox.setup("a.gallery", {
            gallery: "kırmızı",
            continuous: false,
            //allowFullScreen: true,
            counterType: "skip"
        });

        /*var browser_info = perform_acrobat_detection();
        if (browser_info.acrobat == "installed") {
        //alert("yüklü");
        }
        else {
        //alert("Makinanızda Adobe Reader Yüklü Değildir");
        }*/

        function slideMenu(whichControl) {
            if (whichControl == 0) {
                $("div#makeMeScrollable").smoothDivScroll("scrollToElement", "number", $('#currSlideMain').text() - 1);
            }
            else {
                $("div#makeMeScrollable").smoothDivScroll("scrollToElement", "number", $('#currSlideMain').text());
            }
        }

        function mouseOver(obj) {
            $(obj).stop(true, true)
                  .animate({
                      marginTop: '2px',
                      marginBottom: '0',
                      height: 140
                  }, 250, function () {
                  });
            $(obj).find('h1').css("borderBottom", "1px dotted #e51e26");
        }

        function mouseOut(obj) {
            $(obj).animate({
                marginTop: '109px',
                marginBottom: '0',
                height: 28
            }, 400, function () {
            });
            $(obj).find('h1').css("border", "none");
        }

        function callActiveClass(obj) {
            if (obj.attr("class") == "activeSlide") {
                $('.navBox em').animate({
                    top: '0'
                }, 100, function () {
                });
                obj.find("em").animate({
                    top: '-4px'
                }, 250, function () {
                });
            }
        }
    }

});
