if (ThreeLinesSoftware == null) {
    var ThreeLinesSoftware = {};
}
ThreeLinesSoftware.Page = {};

/**
 * Dynamicly loads .css or .js file. Does not use jQuery or any other library.
 * Files are put at the end of <head> for css or <body> for javascript.
 * @param filename - URL to file, or relative file path to file
 * @param fileytpe - "css" or "js", filename extension is ignored
 */
ThreeLinesSoftware.Page.loadJsOrCssFile = function (filename, filetype) {
    var tagLocation = 'head';

    // if filename is a external JavaScript file
    if (filetype == null) {
    	filetype = filename.substring(filename.lastIndexOf('.') + 1);
    }
    if (filetype === "js") {
        var fileref = document.createElement("script");
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", filename);
        tagLocation = 'body';
    } else
    // if filename is an external CSS file
    if (filetype === "css") {
        var fileref = document.createElement("link");
        fileref.setAttribute("rel", "stylesheet");
        fileref.setAttribute("type", "text/css");
        fileref.setAttribute("href", filename);
        tagLocation = 'head';
    }

    document.getElementsByTagName(tagLocation)[0].appendChild(fileref);
};

// jQuery dependent
ThreeLinesSoftware.Page.scripts = [
    'http://app.3linesfeedback.com/css/3lsfeedback.css',
    'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js',
    'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/start/jquery-ui.css',
    'http://app.3linesfeedback.com/javascripts/jquery.jeditable/jquery.jeditable.mini.js',
    'http://app.3linesfeedback.com/javascripts/jquery.rating/jquery.rating.css',
    'http://app.3linesfeedback.com/javascripts/jquery.rating/jquery.rating.pack.js',
    'http://app.3linesfeedback.com/javascripts/jquery.boxy/boxy.css',
    'http://app.3linesfeedback.com/javascripts/jquery.boxy/jquery.boxy.js',
    'http://app.3linesfeedback.com/javascripts/json2.js',
    // leave our lib last to load coz it will trigger feedback script execution on page with callback
    'http://app.3linesfeedback.com/javascripts/3ls.js'
];

// generate random percentage; if already generated by first feedback script lodaded on page, use that as global page percentage number
if (ThreeLinesSoftware.Page.randomPercentage == null) {
    ThreeLinesSoftware.Page.randomPercentage = Math.floor(Math.random() * 101);
}
ThreeLinesSoftware.Page.displayPercentage = 100;

// abort attaching feedback if page URL is in URL filter list
ThreeLinesSoftware.Page.urlFilters = null;
ThreeLinesSoftware.Page.attachImportsAndFeedback = true;
if (ThreeLinesSoftware.Page.urlFilters !== null) {
    for (var i = 0; i < ThreeLinesSoftware.Page.urlFilters.length; i++) {
        var url = window.location.href.toLowerCase();
        var filter = ThreeLinesSoftware.Page.urlFilters[i].toLowerCase();
        if (url.indexOf(filter) != -1) {
            ThreeLinesSoftware.Page.attachImportsAndFeedback = false;
        }
    }
}

// when we load and execute libs, execute feedback start script
ThreeLinesSoftware.Page.loadUserFeedback = function() {
    $(function () {
        var customization = {"imageLocation":"bottom-left","imageSource":"http:\/\/app.3linesfeedback.com\/images\/SimpleBoxyFeedback\/SimpleBoxyFeedback-corner-bottom-left-blue.png","linkElementId":null,"translation":["Leave Feedback","Rate this site:","Leave a comment: (optional)","Your Email: (optional)","Send Feedback!"]};
        var feedback = new ThreeLinesSoftware.Feedback.SimpleBoxyFeedback(
            'http://app.3linesfeedback.com/index.php/api/poll/ec66b98f-999a-8df4-b5ce-8bc5854b1788/data',
            '52e0759d-8f4a-51f4-61fa-1ca6f931ee5e',
            'e454ff06-a26b-f094-8138-e3b47187680b',
            '9e20a30a-bff9-62f4-8d3f-7b1e8338a212',
            customization);
    });
};

ThreeLinesSoftware.Page.dynamicLoadComplete = function(data, text) {
    if (ThreeLinesSoftware.loaded) {
        ThreeLinesSoftware.Page.loadUserFeedback();
    } else {
        setTimeout("ThreeLinesSoftware.Page.dynamicLoadComplete()", 1);
    }
};

if (ThreeLinesSoftware.Page.attachImportsAndFeedback && ThreeLinesSoftware.Page.randomPercentage <= 100) {
    // load scripts... load jQuery first outside
    for (var i = 0; i < ThreeLinesSoftware.Page.scripts.length; i++) {
        var filetype = ThreeLinesSoftware.Page.scripts[i].substring(ThreeLinesSoftware.Page.scripts[i].lastIndexOf('.') + 1);
        if (filetype == 'css') {
            ThreeLinesSoftware.Page.loadJsOrCssFile(ThreeLinesSoftware.Page.scripts[i]);
        } else {
            // when our lib is loaded we can do callback and start user's feedback script
            if (ThreeLinesSoftware.Page.scripts[i] == 'http://app.3linesfeedback.com/javascripts/3ls.js') {
                $.getScript(ThreeLinesSoftware.Page.scripts[i], ThreeLinesSoftware.Page.dynamicLoadComplete);
            } else {
                $.getScript(ThreeLinesSoftware.Page.scripts[i]);
            }
        }
    }
}
