/*
 * Copyright (c) 2010 by Texas Instruments Incorporated All Rights Reserved
 *
 * @author <a href="mailto:usigli@ti.com">Carlos Gonzalez Usigli</a> 
 */
tiDeployPlayer = {
    // those are the default values represented by keys for point the TI-Nspire(tm) Document Player.
    urls: { 
        "prod_3.0.0" : "http://education.ti.com/html/nspireplayer/go3.0.0",
        "prod" : "http://education.ti.com/html/nspireplayer/go",
        "preprod" : "http://preprod10g.eps.ti.com/html/nspiredocumentplayer/go",
        "dev" : "http://dev10g.eps.ti.com/html/nspiredocumentplayer/go"
    },
    /**
     * Creates an iframe with the given iframeAttributes and append the 
     * urlParameters at the end of the iframe src attribute. Defaults the 
     * src to production URL, width to 640px, height to 480px, scrolling to no 
     * and frameborder to 0.
     * @param iframeAttributes the iframe attributes such as src, width, height, 
     * scrolling and frameborder, represented as a Hash.
     * @param urlParameters the src url parameters represented as a Hash.
     */
    tiCreateIframePlayer: function(iframeAttributes, urlParameters) {
        // validates there is iframe attributes, if not, nothing to do...
        if(iframeAttributes != null && iframeAttributes != 'undefined') {
            var iframe = "<iframe";
            var src = iframeAttributes['src'];
            var url = tiDeployPlayer.urls[src];
            // check for dedicated URL variables. if those are preprod, dev, etc.
            if(url != null && url != ''  && url != 'undefined') {
                src = url;
            }
            // if src undefined then defaults to PROD URL. Defaults to 
            // production URL
            if(src == null || src == '' || src == 'undefined') {
                src = "http://education.ti.com/html/nspireplayer/application" +
            		"/nspire-player.html";
            }
            var first = true;
            if(urlParameters != null && urlParameters != 'undefined') {
                // iterates through the urlParameters and append those at the 
                // end of the IFRAME src attribute. Keep the nspirefile 
                // parameter at the end.
                for(var urlParameter in urlParameters) {
                    var value = urlParameters[urlParameter];
                    if(urlParameter != 'nspirefile') {
                        src += ((first) ? "?" : "&") + urlParameter + "=" + 
                            value;
                        first = false;
                    }
                }
                // append the nspirefile parameter at the end of the src URL.
                var nspirefile = urlParameters['nspirefile'];
                if(nspirefile != null && nspirefile != 'undefined' && 
                    nspirefile != '') {
                    src += ((first) ? "?" : "&") + "nspirefile=" + 
                        tiDeployPlayer.tiParseRelativeAddressing(nspirefile);
                }
            }
            // builds the iframe tag with the given attributes.
            iframe += " src=\"" + src + "\"";
            for(var iframeAttribute in iframeAttributes) {
                var value = iframeAttributes[iframeAttribute];
                if(iframeAttribute != 'src') {
                    iframe += " " + iframeAttribute + "=\"" + value + "\"";
                }
                
            }
            iframe += ">This browser does not support iframes</iframe>";
            // writes the iframe tag.
            document.write(iframe);
        }
    },
    /**
     * Validates if the given url is a relative address then prepend the 
     * window.locationon. Returns the parsed URL if the given url value 
     * is a relative address, otherwise the given url.
     * @param param the URL parameter.
     * @return the parsed URL if the given url value is a relative address, 
     * otherwise the given url.
     */
    tiParseRelativeAddressing: function(param) {
        var urlRegex = /^([A-Za-z]+:\/*).*/ig;
        // checks if it stars with protocol.
        if(!urlRegex.test(param)) { 
            var href = window.location.href.substring(
                0, window.location.href.lastIndexOf('/'));
            return unescape(href + "/" + param);
        } 
        return param;
    }
};
