function getContent(content, beginMark, endMark) {
    content = content.replace(eval('/^[\\s\\S]*' + beginMark + '/m'), "");
    content = content.replace(eval('/' + endMark + '[\\s\\S]*$/m'), "");
    return content;

}

function loadPage(urlFull) {

    $.ajax({ async: false, url: urlFull, cache: false,
        dataType: "text",
        success: function (data) {
            level = getContent(urlFull, 'level=', 'xxxxxx');

            if (level.length > 1 || level < 4) {
                background = getContent(data, 'bgImage BEGIN -->', '<!-- bgImage END -->');

                $('div.bgImageHigher').hide();
                $('div.bgImageHigher').html(background);


                setTimeout(function () {

                    $('div.bgImageHigher').fadeIn('slow', function () {
                        $('div.bgImageLower').hide();
                        $('div.bgImageLower').html('');
                        $('div.bgImage').toggleClass('bgImageLower');
                        $('div.bgImage').toggleClass('bgImageHigher');


                    });
                }, 200);


                subnav = getContent(data, 'ul.subnav BEGIN -->', '<!-- ul.subnav END -->');

                if (subnav == '<ul class="subnav"></ul>') {
                    $('.header div.subnav').fadeOut('fast');
                    $('.header div.subnav').html('');
                }
                else {
                    $('.header div.subnav').fadeIn('fast');
                    $('.header div.subnav').html(subnav);
                }

            }

            content = getContent(data, 'Content BEGIN -->', '<!-- Content END -->');
            
            $('#ajaxstorage').html(content);
            heightcontent = $('#ajaxstorage').height();
            if (heightcontent < 425) heightcontent = 425;

            $('#ajaxstorage').html('');
            $('div.content-box div.content').html('');
            document.pagecontent = content;
            $('div.content-box div.content').animate({ 'min-height': heightcontent }, 2000, "swing", function () {
                $('div.content-box div.content').html(document.pagecontent);
                initLinks();

                loadjscssfile("/_resx/v1/sifr3-r436/js/sifr-config.js", "js")

            });

        }
    });

}

function initLinks() {

	$('a').each(function (i, e) {

	    if ($(this).attr('href') != "#" && $(this).attr('rel') == "" && $(this).attr('target') == "") {
			$(this).attr('rel', $(this).attr('href'));
			$(this).attr('href', 'javascript:loadPage(\'' + $(this).attr('rel') + '\');');
		}
	});
}

function loadjscssfile(filename, filetype) {
    if (filetype == "js") { //if filename is a external JavaScript file
        var fileref = document.createElement('script')
        fileref.setAttribute("type", "text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype == "css") { //if filename is an external CSS file
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}

