﻿$(function () {

//    alert('run');

    var cookie = readCookie('__scroll__');

    if (cookie != null) {
        var values = cookie.split(',');

//        alert(values[0] + ', ' + values[1]);

        if (values[0] != 0 || values[1] != 0) {
            window.scrollTo(values[0], values[1]);
            createCookie('__scroll__', '0,0');
        }
    }

    $('.maintain-position').click(function () {

//        alert('click');

        var x;
        var y;

        if (window.pageYOffset) {
            x = window.pageXOffset;
            y = window.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop) {
            x = document.documentElement.scrollLeft;
            y = document.documentElement.scrollTop;
        }
        else if (document.body) {
            x = document.body.scrollLeft;
            y = document.body.scrollTop;
        }

        createCookie('__scroll__', x + ',' + y);

    });
});

function createCookie(name, value, days) {

    var expires = '';

    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = '; expires=' + date.toGMTString();
    }

//    alert('creating cookie ' + name + '=' + value + expires + '; path=/');

    document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, '', -1);
}

