// Cases class for collection of case studies 
// ==========================================
Vormo.Cases = function() {
    this.Container;
    this.Url;
    this.Cases = new Array();
    this.Ajax;
    this.MainDiv = document.createElement('div');
        this.MainDiv.className = "cases_main";
	this.ZoomDiv = document.createElement("div");
		this.ZoomDiv.style.display="none";
		this.ZoomDiv.className="case_shot_zoom";
		this.ZoomImg = document.createElement('img');
		this.ZoomDiv.appendChild(this.ZoomImg);
		var closeSpan = document.createElement("span");
		var closeA = document.createElement("a");
		closeA.appendChild(document.createTextNode("Close"));
		closeA.href="";
		closeSpan.appendChild(closeA);
		this.ZoomDiv.appendChild(closeSpan);
		
	var cases = this;
	closeA.onclick = function() {
		cases.ZoomDiv.style.display="none";
		return false;
	}
	this.ZoomDiv.onclick = function() {
		cases.ZoomDiv.style.display="none";
	}
}
Vormo.Cases.prototype.Render = function() {
    this.GetData();
}
Vormo.Cases.prototype.GetData = function() {
    this.Ajax = new Vormo.Ajax();
    this.Ajax.URL = this.Url;
    var cases = this;
    this.Ajax.ExecuteAfter = function() {
        cases.ProcessData();
    }
    this.Ajax.GetXML();
}
Vormo.Cases.prototype.ProcessData = function() {
    var xml = this.Ajax.ResponseXML;
    var root = xml.firstChild;
    if (root) {
        var cases = root.getElementsByTagName('case');
        for (var i=0; i<cases.length; i++) {
            var c = new Vormo.Cases.Case(cases[i], this);
            this.Cases[this.Cases.length] = c;
        }
        if (this.Container) {
			this.Container.appendChild(this.MainDiv);
			this.Container.appendChild(this.ZoomDiv);
		}
    }
}
Vormo.Cases.prototype.CreateTitle = function(owner, xml, div) {
    if (typeof(div)=='undefined') var div = owner.MainDiv;
    var titleNode = xml.getElementsByTagName('title')[0];
    if (!titleNode) titleNode = xml;
        owner.Title = titleNode.firstChild.nodeValue;
    owner.TitleP = document.createElement('p');
		var titlespan = document.createElement('span');
		titlespan.className="name";
        titlespan.appendChild(document.createTextNode(owner.Title));
		owner.TitleP.appendChild(titlespan);
    div.appendChild(owner.TitleP);
}
Vormo.Cases.prototype.CreateDesc = function(owner, xml, div) {
    if (typeof(div)=='undefined') var div = owner.MainDiv;
    var descNode = xml.getElementsByTagName('description')[0];
        owner.Description = descNode.firstChild.nodeValue;
        owner.Description = owner.Description.replace(/\\n\\n/g, '<br/><br/>');
    owner.DescriptionP = document.createElement('p');
        //owner.DescriptionP.appendChild(document.createTextNode(owner.Description));
        owner.DescriptionP.innerHTML = owner.Description;
    div.appendChild(owner.DescriptionP);
}

// Case class for individual case study 
// ==========================================
Vormo.Cases.Case = function(source, parent) {
    this.Parent = parent;
    this.SourceXML = source;
    this.Title;
    this.Description;
    this.Deliv = new Array();
    this.Technologies = new Array();
    this.Processes = new Array();
    this.Shots = new Array();
    
    this.MainDiv = document.createElement('div');
        this.MainDiv.className = "case_main";
    this.ContentDiv = document.createElement('div');
        this.ContentDiv.className = "case_content";
    this.DelivDiv = document.createElement('div');
        this.DelivDiv.className = "case_deliverables";
    this.TechsDiv = document.createElement('div');
        this.TechsDiv.className = "case_techs";
    this.ProcessesDiv = document.createElement('div');
        this.ProcessesDiv.className = "case_processes";
    this.ShotsDiv = document.createElement('div');
        this.ShotsDiv.className = "case_shots";
    
    // 	   this.Arrow = document.createElement('img');
    //     this.Arrow.src='images/expand.gif';
    //     this.Arrow.style.position='absolute';
    //     this.Arrow.style.top = '1px';
    //     this.Arrow.style.right = '2px';
    //     this.Arrow.style.cursor = 'pointer';
    //     this.MainDiv.appendChild(this.Arrow);
    
    this.descDiv = document.createElement('div');
    	this.descDiv.className="case_desc"
	this.ContentDiv.appendChild(this.descDiv);
    this.ContentDiv.appendChild(this.ShotsDiv);
    
	var clearDiv = document.createElement('div');
    	clearDiv.style.clear="left";
		this.ContentDiv.appendChild(clearDiv);
		
    this.Parent.CreateTitle(this,this.SourceXML);
        this.TitleP.className = 'case_title';
    this.Parent.CreateDesc(this,this.SourceXML, this.descDiv);
    
    // Add the category info to the title
    var cats = this.SourceXML.getElementsByTagName('category');
    for (var i=0; i<cats.length; i++) {
        var cat = cats[i].firstChild.nodeValue;
        var catSpan = document.createElement('span');
			catSpan.className = "attribute_"+i;
            //catSpan.style.position = 'absolute';
            //catSpan.style.top='2px';
            //catSpan.style.left = (100+i*130) + 'px';
        catSpan.appendChild(document.createTextNode(cat));
        this.TitleP.appendChild(catSpan);
    }
        
    var Deliv = this.SourceXML.getElementsByTagName('role');
    var techs = this.SourceXML.getElementsByTagName('technology');
    var processes = this.SourceXML.getElementsByTagName('process');
    var s = this.SourceXML.getElementsByTagName('shots')[0];
    var shots = s.getElementsByTagName('shot');


    var rHead = document.createElement('p');
        rHead.className = 'emph';
        rHead.appendChild(document.createTextNode('Deliverables'));
        rHead.className = 'emph';
    var tHead = document.createElement('p');
        tHead.className = 'emph';
        tHead.appendChild(document.createTextNode('Technologies'));
    var pHead = document.createElement('p');
        pHead.className = 'emph';
        pHead.appendChild(document.createTextNode('Processes'));
    var sHead = document.createElement('p');
        sHead.className = 'emph';
        sHead.appendChild(document.createTextNode('Shots'));
    this.DelivDiv.appendChild(rHead);
    this.TechsDiv.appendChild(tHead);
    this.ProcessesDiv.appendChild(pHead);
    //this.ShotsDiv.appendChild(sHead);
    
    
    this.ProcessObjects('role', Deliv);
    this.ProcessObjects('tech', techs);
    this.ProcessObjects('process', processes);
    this.ProcessObjects('shot', shots);
    
    this.descDiv.appendChild(this.DelivDiv);
    this.descDiv.appendChild(this.TechsDiv);
    this.descDiv.appendChild(this.ProcessesDiv);
    
    this.MainDiv.appendChild(this.ContentDiv);
    this.Parent.MainDiv.appendChild(this.MainDiv);
    
    var caseN = this;
    // this.MainDiv.onclick = function() {
    //         caseN.ToggleContent();
    //     }
    //     this.ContentDiv.onclick = function(){
    //         return false;
    //     };
    
    // this.ToggleContent();
}
Vormo.Cases.Case.prototype.ProcessObjects = function(type, xml) {
    for (var i=0; i<xml.length; i++) {
        switch (type){
            case 'role':
                var x = new Vormo.Cases.Case.Deliv(xml[i],this);
                this.Deliv[this.Deliv.length] = x;
                break;
            case 'tech':
                var x = new Vormo.Cases.Case.Technology(xml[i],this);
                this.Technologies[this.Technologies.length] = x;
                break;
            case 'process':
                var x = new Vormo.Cases.Case.Process(xml[i],this);
                this.Processes[this.Processes.length] = x;
                break;
            case 'shot':
                var x = new Vormo.Cases.Case.Caseshot(xml[i],this);
                this.Shots[this.Shots.length] = x;
                break;
            default : alert('Case Process Error 101: ' + type);
        }
    }
}
Vormo.Cases.Case.prototype.ToggleContent = function() {
    if (this.ContentDiv.style.display=='none') this.ContentDiv.style.display='block';
    else this.ContentDiv.style.display='none';
}

Vormo.Cases.Case.Deliv = function(source, parent) {
    this.Parent = parent;
    this.SourceXML = source;
    this.Title;
    this.Title = this.SourceXML.firstChild.nodeValue;
    this.MainDiv = document.createElement('div');
    this.Parent.Parent.CreateTitle(this,this.SourceXML);
    
    this.Parent.DelivDiv.appendChild(this.MainDiv);
}

Vormo.Cases.Case.Technology = function(source, parent) {
    this.Parent = parent;
    this.SourceXML = source;
    this.Title;
    this.Title = this.SourceXML.firstChild.nodeValue;
    this.MainDiv = document.createElement('div');
    this.Parent.Parent.CreateTitle(this,this.SourceXML);
    
    this.Parent.TechsDiv.appendChild(this.MainDiv);
}

Vormo.Cases.Case.Process = function(source, parent) {
    this.Parent = parent;
    this.SourceXML = source;
    this.Title;
    this.Title = this.SourceXML.firstChild.nodeValue;
    this.MainDiv = document.createElement('div');
    this.Parent.Parent.CreateTitle(this,this.SourceXML);
    
    this.Parent.ProcessesDiv.appendChild(this.MainDiv);
}

Vormo.Cases.Case.Caseshot = function(source, parent) {
    this.Parent = parent;
    this.SourceXML = source;
    this.File;
    this.Title;
    this.Description;
    this.MainDiv = document.createElement('div');
    
    var fileNode = this.SourceXML.getElementsByTagName('src')[0];
        this.File = fileNode.firstChild.nodeValue;
    
    this.Thumb = document.createElement('img');
		var thumSrc = this.File.replace(/.jpg/, '_th.jpg');
        this.Thumb.src = 'images/'+thumSrc;
        //this.Thumb.style.width = '120px';
        //this.Thumb.style.height = '90px';
    this.MainDiv.appendChild(this.Thumb);
    
    this.Parent.Parent.CreateTitle(this,this.SourceXML);
    this.Parent.Parent.CreateDesc(this,this.SourceXML);
    this.TitleP.className = 'emph';
    
    this.Parent.ShotsDiv.appendChild(this.MainDiv);
    
    var shot = this;
    this.Thumb.onclick = function(e) {
		shot.Parent.Parent.ZoomImg.src = 'images/'+shot.File;
		shot.Parent.Parent.ZoomDiv.style.display="block";
		var scrollTop = document.documentElement.scrollTop;
		var offset = shot.Parent.MainDiv.offsetTop;
		shot.Parent.Parent.ZoomDiv.style.top = offset + "px";
    }
}

