<!DOCTYPE html>

<head>
    <meta charset="utf-8">
    <title>
        <%=file%>
    </title>
    <script type="text/javascript" src="<%=cif%>/static/plugins/ep_syntax/static/js/jquery.js"></script>
    <script type="text/javascript" src="<%=cif%>/static/plugins/ep_syntax/static/js/cookies.js"></script>
    <script src="<%=cif%>/static/plugins/ep_syntax/static/js/highlight.codepad.js"></script>
    <link rel="stylesheet" type="text/css" href="<%=cif%>/static/plugins/ep_syntax/static/css/view.css">
    <script type="text/javascript">
        function getSelectionHtml() {
            var html = "";
            if (typeof window.getSelection != "undefined") {
                var sel = window.getSelection();
                if (sel.rangeCount) {
                    var container = document.createElement("div");
                    for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                        container.appendChild(sel.getRangeAt(i).cloneContents());
                    }
                    html = container.innerHTML;
                }
            } else if (typeof document.selection != "undefined") {
                if (document.selection.type == "Text") {
                    html = document.selection.createRange().htmlText;
                }
            }
            return html;
        }

        if ('<%=uri%>' != '#') {
            if (document.addEventListener) {
                document.addEventListener('contextmenu', function(e) {
                    //console.log(e.target.innerHTML);
                    //console.log(getSelectionHtml());
                    // if there is no selection right click just opens the document for editing
                    if (getSelectionHtml().length === 0) {
                        window.location.assign("<%=uri%>");
                        e.preventDefault();
                    }
                }, false);
            } else {
                document.attachEvent('oncontextmenu', function() {
                    if (getSelectionHtml().length === 0) {
                        window.location.assign("<%=uri%>");
                        window.event.returnValue = false;
                    }
                });
            }
        }

        var theme = '<%=theme%>';
        if (getCookie("codepad_theme") !== '') theme = getCookie("codepad_theme");

        document.write('<link rel="stylesheet" type="text/css" href="<%=cif%>/static/plugins/ep_syntax/static/css/theme/' + theme + '.css">');

        if ('<%=brush%>' != 'plain')
            $(document).ready(function() {

                // turn lines into pre blocks
                document.getElementById('view').innerHTML = '<pre>' + document.getElementById('view').innerHTML.replace(/(?:\r\n|\r|\n)/g, '</pre><pre>') + '</pre>';

                // highlight all lines - line by line
                var hl;
                var hl_stack = '';
                $('pre').each(function(i, block) {
                    if ($(this).text().length === 0) $(this).replaceWith('<br>');
                    else {
                        hl = hljs.highlight('<%=brush%>', $(this).text(), true, hl_stack);
                        if (hl.top) hl_stack = hl.top;
                        $(this).html(hl.value);
                    }
                });

                // bind the click event to each line
                var i = 1;
                var lines = $("#view").contents();
                $.each(lines, function(n, ln) {
                    $(this).bind("click", {
                        line: n
                    }, function(event) {
                        window.location.assign("<%=uri%>?line=" + (event.data.line + 1) + "&char=" + Math.floor((event.pageX - 30) / 7));
                    });

                });
            });
    </script>
</head>

<body>
    <code id='view' class="<%=brush%>" style="'margin': 0, 'padding': 0"><%=code%></code>
</body>

</html>












































































































