﻿/* On load */
$(document).ready(function () {
    EndRequestHandler(0, 0);
    // This adds the request handler that ensures the functions above are called 
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

    // Initialize static fancybox 
    $('a[rel*=fancybox]').fancybox()

    // Initialize static fancybox 
    $('a[rel*=fancybox75]').fancybox(
        {
            'width': '75%',
            'height':'75%'
        }
    )

    // Initialize static fancybox 
    $('a[rel*=fancybox90]').fancybox(
        {
            'width' :'90%',
            'height':'90%'
        }
    )


    // Initialize treeviews, used in catalogs 
    $("#treeview").treeview({
        control: "#treecontrol",
        collapsed: true,
        unique: false
    });
});



function EndRequestHandler(sender, args) {

    // Update panels are retarded and mess with the postback, this restores the postback url
    Sys.Application.add_load(function() {
        var form = Sys.WebForms.PageRequestManager.getInstance()._form;
        form._initialAction = form.action = window.location.href;
    });
    //Date pickers on our date pick fields
    //$(".datePicker").datepicker();

    // Add a class called "Odd" to all the odd rows
    $("tr:odd").addClass("odd");
    $("th:odd").addClass("odd");
    $("td:odd").addClass("odd");

    // After every asynchronous call we re-parse the images to "fancyboxify"
    $('a[rel*=fancybox]').fancybox()

    // After every asynchronous call we ensure our accordions are initializaed 
    /*
    $("#accordion").accordion({
        header: ".ui-accordion-header",
        active: 0
    });
    */
    // After every asynchronous call we ensure our tables are sortable again 
    $("table[rel*=sortable]").tablesorter({ widgets: ['zebra'] });
    $(".tablesorter").tablesorter({ widgets: ['zebra'] });
};

//onclientclick = "PleaseWaitButton(this,'Please Wait...')";
function PleaseWaitButton(obj, message) {

    if (typeof (Page_ClientValidate) == 'function') {
        if (Page_ClientValidate()) {
            obj.disabled = true;
            obj.value = message;
            return true;
        }
    }
    return false;
};



function getWindowHeight() {
    var windowHeight = 0;
    if (typeof (window.innerHeight) == 'number') {
        windowHeight = window.innerHeight;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight) {
            windowHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
};

function getWindowWidth() {
    var windowWidth = 0;
    if (typeof (window.innerWidth) == 'number') {
        windowWidth = window.innerWidth;
    }
    else {
        if (document.documentElement && document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
        }
        else {
            if (document.body && document.body.clientWidth) {
                windowWidth = document.body.clientWidth;
            }
        }
    }
    return windowWidth;
};


function setFooter() {

    if (document.getElementById) {
        var windowHeight = getWindowHeight();
        if (windowHeight > 0) {

            var headerHeight;
            if (document.getElementById('header')) {
                headerHeight = document.getElementById('header').offsetHeight;
            } else {
                headerHeight = 0;
            }

            var contentHeight;
            if (document.getElementById('contents')) {
                contentHeight = document.getElementById('contents').offsetHeight;
            } else {
                contentHeight = 0;
            }


            var footerHeight;
            var footerElement;
            if (document.getElementById('footer')) {
                footerElement = document.getElementById('footer');
                footerHeight = document.getElementById('footer').offsetHeight;
            } else {
                footerHeight = 0;
            }

            if (document.getElementById('footer_static')) {
                footerElement = document.getElementById('footer_static');
                footerHeight = footerHeight + document.getElementById('footer_static').offsetHeight;
            }
            

            //var footerHeight = footerElement.offsetHeight;
            if (footerElement) {
                if (windowHeight - (headerHeight + contentHeight + footerHeight) >= 0) {
                    footerElement.style.top = (windowHeight - (headerHeight + contentHeight + footerHeight)) + 'px';
                }
                else {
                    footerElement.style.top = '0px';
                }
            }
        }

    }
};

window.onload = function() { setFooter(); };
window.onresize = function() { setFooter(); };



function QuickAdd(ArtikelId, Quantity, statusObject) {
    //alert('a');
    var loadUrl = "/ajax/QuickAdd.aspx?ArtikelId=" + ArtikelId + "&Quantity=" + Quantity

    $.ajax({
        type: "GET",
        url: loadUrl,
        error: function(responseText) {
            alert('Error ' + responseText);
        },
        success: function(responseText) {
            if (responseText == "0") {
                alert('Error');
            } else {
                alert("Uw artikel is toegevoegd:\n" + responseText + " in bestellijst.");
            }
        }
    });
}



/* aqFloater v1.1 - Floats an element that attaches itself to a part of the browser window.
Copyright (C) 2009 paul pham <http://aquaron.com/~jquery/aqFloater>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
(function($) {
    $.fn.aqFloater = function($o) {
        var _opts = $.extend({
            offsetX: 0, offsetY: 0, attach: '', duration: 50, opacity: '.9'
        }, $o);

        var $obj = this;
        $obj.css({ position: 'absolute', opacity: _opts.opacity });

        var _show = function() {
            var _de = document.documentElement;

            var _y = (_opts.attach.match(/n/) ? 0
            : (_opts.attach.match(/s/)
               ? (_de.clientHeight - $obj.outerHeight() - 10)
               : Math.round((_de.clientHeight - $obj.height()) / 2)));

            var _x = (_opts.attach.match(/w/) ? 0
            : (_opts.attach.match(/e/)
               ? (_de.clientWidth - $obj.outerWidth() - 10)
               : Math.round((_de.clientWidth - $obj.width()) / 2)));

            $obj.animate({
                top: (_y + $(document).scrollTop() + _opts.offsetY) + 'px',
                left: (_x + $(document).scrollLeft() + _opts.offsetX) + 'px'
            }, { queue: false, duration: _opts.duration });
        };

        $(window).scroll(_show).resize(_show);

        $(window).trigger('scroll');
    };
})(jQuery);