﻿var flashMovie;
var $body;

var $jqModel;
var $applicationContainer;
var $jqModel2;
var $applicationContainer2;

$(document).ready(function () {
    // application content
    $jqModel = $('#jqModel').appendTo('body');
    $applicationContainer = $("#applicationContainer");

    $body = $("html");
    $header = $("#header");
    $flash = $(flashMovie);
    var flashHeight = $body.height();
    //$('#flashContent').height(flashHeight);
    function embedSwf() {
        var flashvars = {
            ga_account: "UA-17547610-1",
            ga_path: "",
            path: "home"
        };

        if ($.cookie('age_check_passed') == 'true') {
            flashvars.ageCheckPassed = 'true';
        }
        var params = {};
        params.base = "/template/rootsite/binaryresource/assets/flash/content/swf";
        params.backgroundColor = "#f0f1f2";
        params.wmode = "transparent";
        params.allowFullScreen = "true";


        var attributes = { id: "gFlashID", name: "gFlashID" };
        swfobject.embedSWF("/template/rootsite/binaryresource/assets/flash/content/swf/GrolschSite.swf", "flashContent", "100%", flashHeight, "9.0.0", "/template/rootsite/binaryresource/swfobject/expressInstall.swf", flashvars, params, attributes, function (e) {
            if (e.success) {
                flashMovie = document.getElementById(attributes.id);
                $('#main-header').hide();
                $('body').css('overflow', 'hidden');
                var hash = location.hash;
                if (hash.indexOf('#/subscribe/active') > -1) {
                    var link = '/c-rootsite/subscribe/active?activecode=' + hash.substr(hash.lastIndexOf('/') + 1);
                    openLink(link);
                }
                swfmacmousewheel.registerObject(attributes.id);
            } else {
                $('#main-header').show();
                $('body>.content').css('opacity', '1');

                if ($('div.ageCheck').length > 0) {
                    $('div.ageCheck').show();
                    $('body>.content').hide();
                }
            }
        });
    }
    embedSwf();

    $('.nonflashLink').click(function (evt) {
        var href = $(this).attr('href');
        $(this).attr('href', href + "?h=1");
    });

    $(window).resize(function () {
        if ($jqModel.is(":visible")) {
            var width = $(window).width();
            var lleft = 0;
            if (width > $jqModel.width()) {
                lleft = (width - $jqModel.width()) / 2 - 175;
            }
            $jqModel.css({ left: lleft });
        }
    });
});

function closeApp() {
    $jqModel.jqmHide().find('#applicationContainer').empty();
}

function openApp(uuid) {
    $.ajax({
        type: "post",
        contentType: "application/json",
        url: "/template/rootsite/LayoutTemplate/GrolschService.asmx/GetApplicationContent",
        data: "{uuid: '" + uuid + "'}",
        dataType: "json",
        cache: false,
        success: function (data) {
            if (data.d) {
                var scrolling = (data.d.Scrolling == "True");
                var width = data.d.Width;
                var height = data.d.Height;
                var isEmbedUrl = data.d.UrlEmbeddable == "true";

                var bodyWidth = $body.width();
                var bodyHeight = $body.height();

                if (width > bodyWidth - 200) {
                    //width = bodyWidth - 200;
                }

                if (height > bodyHeight - 100) {
                    height = bodyHeight - 100;
                }

                $jqModel.jqm({
                    modal: false,
                    overlay:0
                }).attr({ "width": width, "height": height });
                var top = (bodyHeight - parseInt(height) - 32) / 2 + 46;
                var left = (bodyWidth - parseInt(width) - 61) / 2;
                $jqModel.css({ "top": top, "left": left, "margin-left": 0 }).removeClass('footer-page');
                $jqModel.find('a.close').remove();
                var url = data.d.Url;
                if ($.trim(url) != "" && !isEmbedUrl) { // load url
                    var $iframe = $("<iframe/>").attr({ width: width, height: height, frameBorder: "0", scrolling: "auto", src: url });
                    if (!scrolling) {
                        $iframe.attr("scrolling", "yes");
                    }

                    $applicationContainer.html($iframe);
                    $jqModel.jqmShow();
                } else { // load flash
                    var flashUrl = data.d.File;
                    if ($.trim(url) != "")
                        flashUrl = url;
                    if ($.trim(flashUrl) != "") {
                        var params = { allowScriptAccess: "always", swliveconnect: "true", wmode: "transparent", allowFullScreen: "true" };
                        swfobject.embedSWF(flashUrl, "applicationContainer", width, height, "10.0.0", "/template/rootsite/binaryresource/swfobject/expressInstall.swf", null, params, null);
                        $jqModel.jqmShow();
                    }
                }
            }
        }
    });
}

function openLink(link) {
    var bodyWidth = $body.width();
    var bodyHeight = $body.height();
    var width = 552;
    var height = 400;
    var top = (bodyHeight - parseInt(height) - 32) / 2;
    var left = (bodyWidth - parseInt(width) - 61) / 2;
    $jqModel.jqm({
        modal: true,
        overlay: 0
    }).addClass('footer-page').css({ "top": top, "left": left, "margin-left": 0 });
    $jqModel.append('<a class="jqmClose close"/>');
    var $iframe = $("<iframe/>").attr({ width: width, height: height, frameBorder: "0", scrolling: "no", src: link });
    $applicationContainer.html($iframe).width(width);
    $jqModel.jqmShow();
}

function ageCheck(pass) {
    if (pass) {
        $.cookie('age_check_passed', 'true', { expires: 10, path: '/', domain: '.grolsch.nl' });
        $('#footer ul').show();
    }
    else
        $.cookie('age_check_passed', null, {domain:'.grolsch.nl'});
}
