﻿
Ext.onReady(function() {
    //find first UC on News page (last rowWrapper gets ignored otherwise)
    var contentCollectionUC = Ext.get(document.body).select('.weCollectionItemGroup');
    if (contentCollectionUC !== null) {
        contentCollectionUC.each(function(el) {
            findRowWrappers(el);
        });
    }; 

});

/*------------------------------------
Weleda Social Bookmarking
Pop-out control to show the social bookmarking
tools, print and email form
------------------------------------*/
Ext.namespace('Trakkware', 'Trakkware.SharePage');
// trigerEl
Trakkware.SharePage = function(config) {
    Ext.applyIf(this, config, {
});
this.trigerEl = Ext.get(this.triger);
this.panelEl = Ext.get(this.panel);

if (!this.trigerEl || !this.panelEl) {
    alert("both trigger(" + this.triger + ") and panel(" + this.panel + ") need to be set!");
    return;
}
this.socnetWidget = this.panelEl.child('.socnet-widget');
this.emailForm = this.panelEl.select('.email-form');
this.emailBtn = this.panelEl.select('.email-btn');
this.printBtn = this.panelEl.select('.print-btn');
this.myNameField = this.panelEl.child('.my-name-field');
this.myEmailField = this.panelEl.child('.my-email-field');
this.friendNameField = this.panelEl.child('.friend-name-field');
this.friendEmailField = this.panelEl.child('.friend-email-field');
this.friendMsgField = this.panelEl.child('.friend-msg-field');
this.errorEl = this.panelEl.child('.email-form-error');
this.successfulEl = this.panelEl.child('.email-form-successful');
this.shareContainer = this.panelEl.child('.share-container');
this.sendBtn = this.panelEl.child('.submit-btn');

this.trigerEl.on('click', this.showShareControl, this);
this.panelEl.child('.email-btn').on('click', this.showEmail, this);
this.panelEl.child('.print-btn').on('click', this.print, this);
this.panelEl.child('.hide-btn').on('click', this.hideShareControl, this);
this.panelEl.child('.submit-btn').on('click', this.send, this);

this.proxy = this.trigerEl.createProxy("x-window-proxy");
this.proxy.enableDisplayMode('block');

this.socnetWidgetTemplate = Ext.XTemplate.from('socnet-widget-template');
}
Ext.extend(Trakkware.SharePage, Ext.util.Observable, {
    showShareControl: function() {
        this.hideShareControl();
        this.proxy.show();
        this.proxy.setBox(this.trigerEl.getBox());
        this.proxy.setOpacity(0);
        this.emailForm.setStyle('display', 'none');
        this.errorEl.setStyle('display', 'none');
        this.successfulEl.setStyle('display', 'none');
        this.panelEl.setStyle('display', 'block');
        this.panelEl.alignTo(this.trigerEl, 'b', [-(this.panelEl.getWidth() / 2), 0]);
        var b = this.panelEl.getBox(false);
        b.callback = this.afterShow;
        b.scope = this;
        b.duration = .5;
        b.easing = 'easeNone';
        b.opacity = .5;
        b.block = true;
        this.panelEl.setStyle('display', 'none');
        this.proxy.shift(b);

        //this.myNameField.dom.value = '';
        //this.myEmailField.dom.value = '';
        this.friendNameField.dom.value = '';
        this.friendEmailField.dom.value = '';
        this.friendMsgField.dom.value = '';
    },
    afterShow: function() {
        this.proxy.setStyle('display', 'none');
        this.panelEl.setStyle('display', 'block');
        this.socnetWidgetTemplate.overwrite(this.socnetWidget, {
            url: this.URLEncode(top.location.href),
            title: this.URLEncode(document.title)
        });
        this.panelEl.setStyle('display', 'block');
    },
    URLEncode: function(plaintext) {
        var SAFECHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";
        var HEX = "0123456789ABCDEF";
        var encoded = "";
        for (var i = 0; i < plaintext.length; i++) {
            var ch = plaintext.charAt(i);
            if (ch == " ") {
                encoded += "+";
            } else if (SAFECHARS.indexOf(ch) != -1) {
                encoded += ch;
            } else {
                var charCode = ch.charCodeAt(0);
                if (charCode > 255) {
                    encoded += "+";
                } else {
                    encoded += "%";
                    encoded += HEX.charAt((charCode >> 4) & 0xF);
                    encoded += HEX.charAt(charCode & 0xF);
                }
            }
        }
        return encoded;
    },
    hideShareControl: function() {
        this.panelEl.setStyle('display', 'none');
        this.panelEl.setStyle('width', '8.5em');
        this.panelEl.setStyle('height', '20.8em');
    },
    showEmail: function() {
        this.sendBtn.dom.disabled = false;
        this.friendMsgField.dom.value = 'I think you\'ll like this page.';
        this.panelEl.shift({
            width: 365,
            height: 250,
            x: 900,
            opacity: 100,
            easing: 'easeOut',
            duration: 0.5
        });
        this.emailForm.slideIn('l', { duration: 1.5 });
    },
    send: function() {
        this.sendBtn.dom.disabled = true;
        var hidden = document.getElementById('hiddenCaptcha');
        var captcha = document.getElementById(hidden.value);
        var img = captcha.getElementsByTagName('img')[0];
        var capurl = img.src;
        var input = captcha.getElementsByTagName('input')[0];
        var captext = input.value;
        var requestInfo = {
            url: top.location.href,
            title: document.title,
            friendEmail: this.friendEmailField.dom.value,
            myEmail: this.myEmailField.dom.value,
            myName: this.myNameField.dom.value,
            friendName: this.friendNameField.dom.value,
            friendMessage: this.friendMsgField.dom.value,
            captchaurl: capurl,
            captchatext: captext
        };
        //this.emailForm.mask();
        Ext.Ajax.request({
            url: '/Code/Services/IscadorWebService.svc/SendShareEmail',
            success: this.formSuccess,
            failure: this.formFailure,
            scope: this,
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            params: Ext.util.JSON.encode(requestInfo)
        });
    },
    formSuccess: function(xhr) {
        response = Ext.util.JSON.decode(xhr.responseText);
        if (response.d == 'successful') {
            this.errorEl.setStyle('display', 'none');
            this.emailForm.setStyle('display', 'none');
            this.successfulEl.setStyle('display', 'block');
        } else {
            this.formFailure(xhr);
        }
        this.emailForm.unmask();
    },
    formFailure: function(xhr) {
        this.sendBtn.dom.disabled = false;
        this.panelEl.shift({
            height: 310
        });
        response = Ext.util.JSON.decode(xhr.responseText);
        var errorText = 'Internal Server Error.';
        if (response.d !== undefined) {
            errorText = response.d;
        }
        this.errorEl.update(errorText);
        //this.errorEl.setStyle('display', 'block');
        this.errorEl.fadeIn({ duration: 2 });
        //this.emailForm.unmask();
    },
    print: function() {
        window.print();
    }
});
