﻿
var currentPage = 0;
var websiteRoot = '';

$(document).ready(function() {
    websiteRoot = $('#hidApplicationUrl').val();
	
    if ($('#loopedSliderSeri').length > 0) {
        $('#loopedSliderSeri').loopedSlider({
            restart: 5000,
            slidespeed: 1500,
            containerClick: false
        });
    }
    $('.Eng a').each(function() {
        if ($(this).css('background-image') != "none") {
            $(this).css('background-image', $(this).css('background-image').replace('.png', '-eng.png'));

        }
    });
    $('.css-scrollbar').scrollbar();
    
    var emailGirinText = $('#hidEnterYourEmail').val();
    var alreadySubscribed = $('#hidAlreadySubscribed').val();
    var notAnEmailText = $('#hidInvalidEmail').val();
	var successfullySubscribed = $('#hidSuccessfullySubscribed').val();
	
	var searchFd = $('#hidSearchFd').val();

    $(".KaydolData input").val(emailGirinText);

    $(".KaydolData input").focus(function() {
        if ($(this).val() == emailGirinText) {
            $(this).val("")
        }
    });
    $(".KaydolData input").click(function() {
        if ($(this).val() == emailGirinText) {
            $(this).val("")
        }
    });
    $(".UrunKoduArama").val(searchFd);
    $(".UrunKoduArama").click(function() {
        if ($(this).val() == searchFd) {
            $(this).val("");
        }
    });
    $('body').click(function() {
        if ($(".KaydolData input").val() == "") {
            $(".KaydolData input").val(emailGirinText);
        }
        if ($(".UrunKoduArama").val() == "") {
            $(".UrunKoduArama").val(searchFd);
        }
    });
    $('.KaydolData input').click(function(event) { event.stopPropagation(); });
    $(".UrunKoduArama").click(function(event) { event.stopPropagation(); });
    $("#NewsletterEmail").blur(function() {
        if ($(this).val() == "") {
            $(this).val(emailGirinText)
        }
    });
    $(".KaydolBtn img").click(function() {
        // Disable input
        $(".KaydolData input").attr("disabled", "disabled");
        $(".KaydolBtn img").attr("disabled", "disabled");

        $.ajax({
            type: "POST",
            url: websiteRoot + "/WebMethods/SaveEmailForNewsletter.aspx/SaveEmailForNewsletter",
            data: "{ 'email' : '" + $(".KaydolData input").val() + "' }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {

                if (msg.d.IsSuccessful) {
                    alert(successfullySubscribed);
                    $(".KaydolData input").val(emailGirinText);

                }
                else {
                    if (msg.d.ErrorMessage == "NotAnEmail") {
                        alert(notAnEmailText);
                        //$("#NewsletterEmail").val(notAnEmailText);
                    }
                    else if (msg.d.ErrorMessage == "AlreadySubscibed") {
                        //$("#NewsletterEmail").val();
                        alert(alreadySubscribed);
                    }
                    //alert(msg.d.ErrorMessage);
                }
            },
            error: function(xmlHttpRequest, textStatus, errorThrown) {
                //alert(textStatus + ", " + errorThrown + ", " + xmlHttpRequest);
            }
        });

        $(".KaydolData input").removeAttr("disabled");
        $(".KaydolBtn img").removeAttr("disabled");
    });
});

// Seri/Fonksiyon sayfasındaki ürünler slider'ının kodu
// Set UrunContent width after images have been loaded
$(window).load(function()
{
    var totalWidth = 0;

    $(".UrunContent > div").each(function()
    {
        totalWidth += $(this).width();
        totalWidth += $(this).css('padding-right').replace("px", "") * 1;
    });

    $(".UrunContent").width(totalWidth);

    if (totalWidth < $(".UrunContainer").width() - 100)
    {
        $(".UrunContainer .next").hide();
        $(".UrunContainer .previous").hide();
    }

    if ($('.TextArea').parent().parent().hasClass('RightContentContainer'))
    {
        $('.TextArea').parent().parent().attr('class', 'RightContentContainerInner');
    }

    var pageSize = 300;
    var animationDuration = 700;

    $(".UrunContainer .next").click(function()
    {
        var containerWidth = $(".UrunContent").width();
        var visibleWidth = $(".UrunContainer").width();
        var maxPage = Math.ceil((containerWidth - visibleWidth + 20) / pageSize);

        if (currentPage < maxPage)
        {
            currentPage++;

            if (currentPage == maxPage)
                $(".UrunContent").animate({ left: -(containerWidth - visibleWidth + 120) }, animationDuration);
            else
                $(".UrunContent").animate({ left: -currentPage * pageSize }, animationDuration);
        }

        return false;
    });

    $(".UrunContainer .previous").click(function()
    {
        if (currentPage > 0)
        {
            currentPage--;
            $(".UrunContent").animate({ left: -currentPage * pageSize }, animationDuration);
        }

        return false;
    });

    // Hide loading gif
    $('#UrunLoadingGif').hide();

    // fadeIn() will not work with visibility:hidden. so first we hide it (so that fadeIn works),
    // then make contents visible and finally fadeIn(). visibility:hidden is used instead of
    // display:none because in case of the latter, child elements' widths are not calculated
    // correctly
    $('.UrunContent').hide();
    $('.UrunContent').css('visibility', 'visible');
    $('.UrunContainer > .next').css('visibility', 'visible');
    $('.UrunContainer > .previous').css('visibility', 'visible');
    $('.UrunContent').fadeIn();

    // Shadowbox was initiated in document.ready, so we need to initialize it again.
    Shadowbox.init();
});

// Called in window load to get height correctly
$(window).load(function()
{
    SetBottomBarPosition();

    $(window).resize(function()
    {
        SetBottomBarPosition();
    });
});

function SetBottomBarPosition()
{
    var $bottomBar = $('#InnerPage #Bottom');

    if ($bottomBar.length == 0)
    {
        return;
    }

    var mainContainerHeight = $('#InnerPage').height();
    var windowHeight = $(window).height();

    if (windowHeight > mainContainerHeight)
    {
        $bottomBar.css('position', 'absolute');
        $bottomBar.css('bottom', '0px');
    }
    else
    {
        $bottomBar.css('position', 'static');
        $bottomBar.css('bottom', '0px');
    }
}

// DropDown transformations
// Transform dropdowns (jqtransform)
$(document).ready(function()
{
    $('.jqtransform').jqTransform();
});

// İletişim sayfalarındaki dropdown'lar
$(document).ready(function()
{
    $('.jqTransformSelectWrapper ul li a', $('#SehirlerDropDownContainer')).click(function()
    {
        var cityUrl = $('select', $('#SehirlerDropDownContainer')).val();

        if (cityUrl == '')
        {
            return;
        }

        //alert('redirecting to: ' + cityUrl);
        document.location = cityUrl;
    });

    $('.jqTransformSelectWrapper ul li a', $('#MagazalarDropDownContainer')).click(function()
    {
        var magazaUrl = $('select', $('#MagazalarDropDownContainer')).val();

        if (magazaUrl == '')
        {
            return;
        }

        //alert('redirecting to: ' + magazaUrl);
        document.location = magazaUrl;
    });

    $('.jqTransformSelectWrapper ul li a', $('#Yurtici.BolgelerDropDownContainer')).click(function()
    {
        var bolgeUrl = $('select', $('#Yurtici.BolgelerDropDownContainer')).val();

        if (bolgeUrl == '')
        {
            return;
        }

        //alert('redirecting to: ' + bolgeUrl);
        document.location = bolgeUrl;
    });

    $('.jqTransformSelectWrapper ul li a', $('#Yurtdisi.BolgelerDropDownContainer')).click(function()
    {
        var bolgeUrl = $('select', $('#Yurtdisi.BolgelerDropDownContainer')).val();

        if (bolgeUrl == '')
        {
            return;
        }

        //alert('redirecting to: ' + bolgeUrl);
        document.location = bolgeUrl;
    });
});

// Initialize Shadowbox
$(document).ready(function()
{
    Shadowbox.init({
        continuous: 'true',
        handleOversize: 'resize',
        onClose: back_to_resize,
        onChange: back_to_resize,
        onOpen: back_to_resize
    });
});

