﻿var Cookie = {
    create: function (name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    },
    
    read: function (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 ($) {
var MobilePack = {
    init: function () {
        MobilePack.deviceResolution();

        $(document).ready(function() {
            // This event gets called after a page has finished loading (same as $(document).ready() in jQuery)
            $(document).bind("pagecreate", MobilePack.pageCreate);

            $('.mobilePage').live('swipeleft swiperight', function (event) {
                if (event.type == "swipeleft") {
                    MobilePack.previousPage();
                }
                
                if (event.type == "swiperight") {
                    MobilePack.nextPage();
                }
                
                event.preventDefault();
            });
            
            $('.toggle-desktop-mobile').live('click', function (e) {
                e.preventDefault();

                MobilePack.checkToggleModeCookie();

                location.href = this.href;
            });
        });
    },
    
    previousPage: function () {
        var prev = $(".prevlink", $.mobile.activePage);
        if (prev.length) {
            var prevurl = $(prev).attr("href");
            $.mobile.changePage(prevurl, "slideright");
        }
    },
    
    nextPage: function () {
        var next = $(".nextlink", $.mobile.activePage);
        if (next.length) {
            var nexturl = $(next).attr("href");
            $.mobile.changePage(nexturl, "slideleft");
        }
    },
    
    pageCreate: function (event) {
        // Look for external links and mark them external (jQuery Mobile won't use Ajax to load these links)
        $('.bodyContent a:not(.mobileLink)', this).attr('rel', 'external');

        MobilePack.clientSideImageResizing(this);
    },
    
    checkToggleModeCookie: function () {
        var mobileToggleValue = Cookie.read('mobileToggle');

        if (mobileToggleValue == undefined || mobileToggleValue === 'mobile') {
            mobileToggleValue = 'normal';
        }
        else {
            mobileToggleValue = 'mobile';
        }

        Cookie.create('mobileToggle', mobileToggleValue, 365);
    },
    deviceResolution: function () {
        /* 
        Source: http://adaptive-images.com/
        we want to get the image for the largest window size the device can support, not just the current window size */
        var device_width = window.innerWidth; // get screen width
        var device_height = window.innerHeight; // get the screen height

        /* if this is a device that can rotate, we need the longest edge, not just the current width */
        if (device_width > device_height) {
            ri_width = device_width;
        } else {
            ri_width = device_height;
        }

        /* set the client width in a cookie for the server to read */
        Cookie.create('resolution', ri_width, 365);
    },
    
    clientSideImageResizing: function (context) {
        $('.bodyContent img[width]', context).each(function (i) {
            if (parseInt($(this).attr('width')) > window.innerWidth) {
                var w = parseInt($(this).attr('width'));
                var h = parseInt($(this).attr('height'));
                var mw = window.innerWidth-15;
                var nh =  ((mw*h) / w);
                $(this).attr('width', mw);
                $(this).attr('height', nh);
            }
        });
    }
};

MobilePack.init();
} (jQuery));
