function preparePage() {
    markCurrent(); // Also hide home link on homepage
    prepareRequiredFields();
    prepareValitatedButtons();
    prepareFlashes();
    prepareAjaxButtons();

    // Async postbacklerin sonuna eklenecek işler
    addAsyncPostBackEventHandler();
}

/*  bu fonksyon prepareDeleteLinks fonksyonunu async 
postbackin sonunda yapılacaklar listesine ekliyor */
function addAsyncPostBackEventHandler() {
    //Sys.WebForms.PageRequestManager.getInstance().add_endRequest(PrepareMiniImages);
}

// Also hide home link on homepage
function markCurrent() {
    var path = window.location.pathname.split('/');
    var name = path[path.length - 1];
    var qString = window.location.search.substring(1);
    if (qString.length > 0)
        name = name + "?" + qString;

    $('a[href="' + name + '"]').addClass('current');

    // Hide the home link on homepage
    if (name == "Default.aspx" || name == "")
        $('.lnkHome').hide();
}

function prepareAjaxButtons() {
    $('.btAjax').click(function (e) {
        var mX = e.pageX; // get the mouse cursor position - mX, mY
        var mY = e.pageY;
        var image = $('.progress').first(); // get the first progress image
        var iWidth = image.width(); // get the width and height of the progress image, iWidth, iHeight
        var iHeight = image.height();
        image.css("top", mY - (iHeight/2)); // calculate and assign the position of the progress image 
        image.css("left", mX - (iWidth/2));        
    });
}

var cWidth = 220; //target image width
var cHeight = 220; //target image height
var crop = 0; // crop choice

function prepareFlashes() {
    // size the cropping divs
    $('.divCrop').each(function () {
        $(this).width(cWidth);
        $(this).height(cHeight);
    });


    $('#contentRotator img').each(function () {
        var image = $(this);
        var iWidth = image.width();
        var iHeight = image.height();
        var isLandscape = iWidth >= iHeight ? true : false;
       
        var sFactor;
        if (isLandscape) {
            sFactor = cHeight / iHeight;
            if (iWidth * sFactor < cWidth)
                sFactor = cWidth / iWidth;
        }
        else {
            sFactor = cWidth / iWidth;
            if (iHeight * sFactor < cHeight)
                sFactor = cHeight / iHeight;
        }

        image.height(iHeight * sFactor);
        image.width(iWidth * sFactor);

        switch (crop) {
            case '0':
                $(this).css({ top: '0', left: '0' });
                break;
            case '1':
                $(this).css({ top: '0', left: '0' });
                var marginLeft = Math.abs((thWidth - $(this).width()) / 2) * -1;
                var marginTop = Math.abs((thHeight - $(this).height()) / 2) * -1;
                $(this).css("margin-left", marginLeft);
                $(this).css("margin-top", marginTop);
                break;
            case '2':
                $(this).css({ bottom: '0', right: '0' });
                break;
        }
    });
}


/*  ******************************************************
VALIDATION
****************************************************** */

function prepareRequiredFields() {
    $('.requiredField').blur(function () { fieldNotNull(this.id); })
        .keyup(function () { fieldNotNull(this.id); })
        .change(function () { fieldNotNull(this.id); });
}

function fieldNotNull(id) {
    var control = $('#' + id);
    if (control.val() == "" || control.val() == "-1")
    { control.addClass("attention"); return false; }
    else { control.removeClass("attention"); return true; }
}

function prepareTextAreas() {
    $('[class*="txtLimited"]').unbind();
    $('[class*="txtLimited"]').each(function () {
        var maxLength;
        maxLength = $(this).attr("data-limit");
        $('label[for=' + this.id + ']').html(maxLength);
        $(this).keyup(function () {
            checkLength(this.id, maxLength);
        });
    })
}

function checkLength(id, maxLength) {
    var textArea = $('#' + id)[0];
    var label = $('label[for=' + textArea.id + ']')[0];

    if ($(textArea).val().length > maxLength) {
        // Reached the Maximum length so trim the textarea
        $(textArea).val($(textArea).val().substring(0, maxLength));
        $(label).html('0');
    }
    else {
        // Maximum length not reached so update the value of my_text counter
        $(label).html(maxLength - $(textArea).val().length);
    }
}

function prepareValitatedButtons() {
    $('.validatedButton').click(function () {
        var idArray = this.id.split('_');
        var id = idArray[idArray.length - 1];
        var allIsOk = true;

        $('.' + id).each(function () {
            if (validate(this) == false) { allIsOk = false; }
        });

        if (allIsOk)
            return true;
        else {
            alert("Lütfen tüm işaretli alanları doldurun.");
            return false;
        }
    });
}

function validate(control) {
    if (fieldNotNull(control.id)) { return true; }
    else { return false; }
}
