/*global content, design, template, result, options, StylesBuilder, TemplateBuilder, FileManager */
/*jshint forin:true, noarg:true, noempty:true, globalstrict:true, strict:true, eqeqeq:true, bitwise:true, undef:true, browser:true, devel:true, jquery:true, curly:false */
"use strict";

function createManifest() {
    var newNode = { };
    var doc = $.parseXML(template.read("Common/dnn" + options.Version + ".Manifest.dnn"), "text/xml");

    $(doc).find("package").attr("name", options.ThemeName);
    $(doc).find("package").attr("version", parseVersion(options.TemplateVersion));
    
    $(doc).find("skinFiles > skinName").text(options.ThemeName);
    $(doc).find("containerFiles > containerName").text(options.ThemeName);
    $(doc).find("package").attr("name", options.ThemeName);
    $(doc).find("package > friendlyName").text(options.ThemeName + " Skin");
    $(doc).find("package > description").text(options.TemplateDescription);

    $(doc).find("owner > name").text(options.TemplateAuthor);
    $(doc).find("owner > url").text(options.TemplateAuthorURL);

    var skinFiles = $(doc).find("skinFiles");
    var node = $.parseXML("<?xml version=\"1.0\" encoding=\"UTF-8\"?><skinFile><path/><name/></skinFile>").documentElement;
    
    var rootFiles = result.enumerate("/");
    for (var f = 0; f < rootFiles.length; f++) {

        var fileName = rootFiles[f];
        if (!fileName.match(/\.css$|\.js$/)) {
            continue;
        }
        
        newNode = node.cloneNode(true);

        $(newNode).children("name").text(fileName);

        skinFiles.append(newNode);
    }

    var images = result.enumerate("images");
    for (var i = 0; i < images.length; i++) {
        var img = images[i];

        if (img === "page.jpg") continue;

        newNode = node.cloneNode(true);

        $(newNode).children("path").text("images");
        $(newNode).children("name").text(img);

        skinFiles.append(newNode);
    }
    
    if (options.Version === '6' && options.includeContent) {
        
		if (content.exists("images")) {
			var cFiles = content.enumerate("images");
			for (var j = 0; j < cFiles.length; j++) {
				var file = cFiles[j];

				newNode = node.cloneNode(true);

				$(newNode).children("path").text("content/images");
				$(newNode).children("name").text(file);

				skinFiles.append(newNode);
			}
		}

        var contentXml = node.cloneNode(true);

        $(contentXml).children("path").text("content");
        $(contentXml).children("name").text("content.xml");

        skinFiles.append(contentXml);
    } else if (options.Version === '6' && !options.includeContent) {
        $(doc).find("component[type='SkinObject'], component[type='File']").remove();
    }

    result.write(options.ThemeName + ".dnn", new XMLSerializer().serializeToString(doc));

    function parseVersion(version) {
        if (version.length === 0) {
            return "01.00.00";
        }
        
        var parts = version.split(".");
        var resultVersion = "";
        var k = 0;
        for (; k < Math.min(parts.length, 3); k++) {
            var part = parseInt(parts[k], 10);
            if (isFinite(part)) {
                resultVersion += pad(part, 2) + (k < parts.length - 1 ? "." : "");
            }
        }
        for (; k < 3; k++) {
            resultVersion += ".00";
        }

        if (resultVersion.length === 0)
            resultVersion = "01.00.00";
        
        return resultVersion;
    }
    
    function pad(number, length) {
        var str = '' + number;
        while (str.length < length) {
            str = '0' + str;
        }
   
        return str;
    }
}

