﻿$(document).ready(function () {
    // set curtain to full page size
    $('#curtain').height($(document).height()); // height, current problem with IE when no vertical scrollbar -> page shifted to the left when cloudBox is shown 
    // iPhone curtain not fullscreen
    //(temp fix: $(window).height()
    // other fix: always show scrollbar in IE
    ////$('#curtain').width($(window).width()); // viewport width

    // set position of cloudClone
    var cloudClone = $('#cloudClone');
    var cloud = $('#cloud');
    cloudClone.width(cloud.width());
    cloudClone.height(cloud.height());
    cloudClone.css('top', cloud.offset().top);

    var currentAnchor;
    var xhr;

    // add click event for cloud anchors
    $('#cloud a').click(showCloudBox);
    $('#cloud a').click(hideNoteAndSetCookie);

    var cloudNote = getCookie('cloudnote');
    if (cloudNote != 'no') {
        setTimeout("showCloudNote();", 2000);
    }

    // add click event for cloudBoxes
    $('#cloudClone .cloudBox').click(function (e) {
        e.stopPropagation();
    });

    function showCloudBox(event) {
        currentAnchor = $(event.target).attr('id');
        showCurtain();
        // if: handle special case: Unternehmenssoftware (2 part overlay)
        if ($(event.target).attr('id').indexOf('unternehmenssoftware') == 0) {
            showUnternehmensSoftwareCloudBox(event);
        }
        else {
            var cloneId = $(event.target).attr('id') + 'Clone';
            var cloneIdSelStr = '#' + cloneId;
            $('#cloudClone').prepend('<div id="' + cloneId + '" class="cloudBg"></div>');
            var curtainAnchor = $(cloneIdSelStr);
            var curtainBox = $('#' + $(event.target).attr('id') + 'Box');
            // ajax request for cloudBox content
            xhr = $.get(
                $(event.target).attr('href'),
                '',
                cloudBoxLoadSuccess(curtainBox),
                'html');

            //register hiding event
            $('#curtain').click(function (e) {
                // abort pending request
                xhr.abort();
                // hide box
                $('#' + currentAnchor + 'Box').hide();
                // remove cloned anchor
                $('#' + currentAnchor + 'Clone').remove();
                hideCurtain();
                // remove all click handlers from curtain
                $('#curtain').unbind('click');
            });
        }
        //cd tracking
        cdAnalytics.trackLink('http://www.puffer.at/' + $(event.target).attr('href'), 'Wolke: ' + currentAnchor);
        return false;
    };

    function showUnternehmensSoftwareCloudBox(event) {
        currentAnchor = 'unternehmenssoftware';
        var cloneIdSelStr = '#unternehmenssoftwareClone';
        var curtainAnchor = $(cloneIdSelStr);
        curtainAnchor.show();
        var curtainBox = $('#unternehmenssoftwareBox');
        // ajax request for cloudBox content
        xhr = $.get(
                $(event.target).attr('href'),
                '',
                cloudBoxLoadSuccess(curtainBox),
                'html');

        // register hiding event
        $('#curtain').click(function (e) {
            // abort pending request
            xhr.abort();
            // hide box
            $('#unternehmenssoftwareBox').hide();
            // remove cloned anchor
            $('#unternehmenssoftwareClone').hide();
            hideCurtain();
            // remove all click handlers from curtain
            $('#curtain').unbind('click');
        }).bind(event);
    };

    function cloudBoxLoadSuccess(box) {
        return function (data, textStatus) {
            // extract only content from loaded page
            var obj = $(data);
            var content = obj.find("#dnn_ContentPane").html();
            box.html(content);
            box.show("fold", 1000);
            //box.show();
        };
    };

    function hideNoteAndSetCookie(event) {
        $('#cloud a').unbind('click', hideNoteAndSetCookie);
        $('#cloudnote').hide();
        setCookie('cloudnote', 'no', 365);
    }

    function showCurtain() {
        $('#curtain').show();
    };

    function hideCurtain() {
        $('#curtain').hide();
    };
});

function showCloudNote() {
    $('#cloudnote').show();
    setTimeout("$('#cloudnote').hide();", 10000);
};

/* Post Clickdimensions forms with javascript + client side validation */

var email_regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;  // email address

// code from http://www.w3schools.com/JS/js_cookies.asp
function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == c_name) {
            return unescape(y);
        }
    }
};

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
};

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
};

function submitCdFormCallback(data) {
    window.location = $('#cd_redirect').val();
};

function submitCdForm() {
    $('#customform_validation').html("");
    //check required fields
    var validation = "";
    $.each($('input.required'), function (i, field) {
        if ($(field).val().length == 0) {
            $(field).addClass('invalid');
            validation = "Bitte tragen Sie einen gültigen Wert in die hervorgehobenen Felder ein.<br/>";
        } else {
            $(field).removeClass('invalid');
        }
    });

    //check e-mail fields
    $.each($('input.email'), function (i, field) {
        if (!email_regex.test($(field).val())) {
            $(field).addClass('invalid');
            validation += "Bitte tragen Sie eine gültige E-Mail-Adresse ein.";
        } else {
            $(field).removeClass('invalid');
        }
    });

    if (validation.length == 0) {
        var values = {};
        $.each($('.cd_field').serializeArray(), function (i, field) {
            values[field.name] = field.value;
        });

        values['cd_visitorkey'] = getCookie('cuvid');

        $.post($('#cd_action').val(), values, submitCdFormCallback);
    }
    else {
        $('#customform_validation').html(validation);
    }

    return false;
};

$(document).ready(function () {

    var cd_redirect = getUrlVars()['cd_redirect'];
    if (cd_redirect != undefined) {
        $('#cd_redirect').val(cd_redirect);
    }
});
