/**
 * module container for namespace 'helpCenter'
 *
 * @author  Richard Sternagel <richard.sternagel@1und1.de>
 */

 /* JSLint options */
 /*globals helpCenter, YAHOO, stubbles */
 /*jslint browser: true, laxbreak: true */

var helpCenter = {

    // see corresponding file
    callback: {},
    coordinator: {},

    // for namespace purpose
    proxy: {},

    // classes & ids script retrieves/sets dynamically
    // and single point for text configuration
    config: {
        css: {
            get: {
                classes: {
                    expandingScreenshot: 'visual_screenshot',
                    expandingScreenshotThumbnail: 'visual_screenshot_thumbnail',
                    contentElem: 'content'
                },
                ids: {}
            },
            set: {
                classes: {},
                ids: {
                    printVersion: 'link_print'
                }
            }
        },
        text: {
            printVersion: 'Druckversion des Artikels'
        }
    },

    unobtrusive: {
        preparePopupLinks: function() {
                var links = document.getElementsByTagName("a");
                var features = '';

                // cancel function if element to prepare is not availible
                if(links === null) { return false; }

                var doPopUp = function () {
                    if(this.getAttribute("rel").indexOf("@") === 5) {
                        features = this.getAttribute("rel").substring(6);

                        // default: width & height input
                        if(features.match(/^(width|height)=[0-9]{2,4},(height|width)=[0-9]{2,4}$/)) {
                            helpCenter.helper.popUp(this.getAttribute("href"), null, features, true);
                        } else {
                            helpCenter.helper.popUp(this.getAttribute("href"), null, features, false);
                        }

                    }
                    return false;
                };

                for(var i=0; i<links.length; i++) {
                    // avoiding hasAttribute() beacause of lacking IE implementation
                    // @see http://www.quirksmode.org/dom/w3c_core.html
                    if(!links[i].getAttribute("rel")) {
                      continue;
                    }

                    // link with rel="popup"
                    if(links[i].getAttribute("rel").indexOf("popup") > -1) {
                        links[i].onclick = doPopUp;
                    }
                }
        },

        prepareExpandingScreenshots: function(classOfExpandedScreenshot, classOfExpandedScreenshotThumbnail) {
            // expand screenshot
            YAHOO.util.Dom.getElementsByClassName(classOfExpandedScreenshotThumbnail, 'div', null, function() {
                var elem = YAHOO.util.Dom.getPreviousSibling(this);
                this.onclick = function() {
                    elem.style.display='block';
                };
            });

            // close screenshot
            YAHOO.util.Dom.getElementsByClassName(classOfExpandedScreenshot, 'div', null, function() {
                this.onclick = function() {
                    this.style.display='none';
                };
            });
        },

        preparePrintableVersion: function(classOfContainerEl, linkId, linkText) {
            // exist if not an article page
            if(document.getElementById('article') === null) { return false; }
            
            var parentElArr = (document.getElementsByClassName) 
                              ? document.getElementsByClassName(classOfContainerEl)
                              : YAHOO.util.Dom.getElementsByClassName(classOfContainerEl, 'div');
            var aEl = document.createElement('a');
            var textNode = document.createTextNode(linkText);

            // returning false prevents the browser from following the link and jumping to the top of the page after printing
            aEl.onclick = function() {
                window.print();
                return false;
            };

            // make the link focusable for keyboard users
            aEl.href = '#';
            aEl.id   = linkId;

            aEl.appendChild(textNode);
            parentElArr[0].appendChild(aEl);
        }
    },

    helper: {
        popUp: function(url, title, features, useDefaultConf) {
            var defaultConf = (useDefaultConf === true)
                              ? "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenX=100,screenY=100"
                              : '';

            window.open(url, title, features + defaultConf);
        }
    },

    init: function() {
        // append Event Listener
        helpCenter.unobtrusive.preparePopupLinks();
        helpCenter.unobtrusive.prepareExpandingScreenshots(
            helpCenter.config.css.get.classes.expandingScreenshot,
            helpCenter.config.css.get.classes.expandingScreenshotThumbnail
        );
        helpCenter.unobtrusive.preparePrintableVersion(
            helpCenter.config.css.get.classes.contentElem,
            helpCenter.config.css.set.ids.printVersion,
            helpCenter.config.text.printVersion
        );

        // initialize the proxy objects for the json-rpc communication

        // console.debug('init successful');
    }
};

YAHOO.util.Event.onDOMReady(helpCenter.init);