// JavaScript Document
var startCase = 0;
var casePull = Class.create({
	graftElement:"",
	contentArr: [],
	linkArr: [],
	startCase:0,
	XmlUrl:"/xml/caseStudy.xml?ts=" + new Date().getTime(),
	initialize:function(elementToAddTo){
		this.graftElement = graftElement = elementToAddTo;
		var address = document.location.toString();
		if(address.toQueryParams()['c']){
			var caseStudy = address.toQueryParams()['c'];
		}
		
		new Ajax.Request( this.XmlUrl,
			{
			method:'get',
			onSuccess: function(returned){
				casesNode = returned.responseXML.getElementsByTagName('casestudies');
				$A(casesNode[0].childNodes).each(function(h, p){	
					if(h.nodeType != 3){
						data.contentArr.push(h);
						var curClient = h.getAttribute('client');
						data.linkArr.push(curClient);	
					}
				});
				
				$A(data.linkArr).each(function(s,i){
					if (s == caseStudy ){
						startCase = i;
					}
				});
				
				data.createLinks(startCase);
				data.swapStudy(startCase);	
			},
			onFailure: function(){
				response = "failed to load"
				alert("cannot find document");
			}	
		});
	},
	
	createTable:function (){
		
		try{
			if(!$('scrollContent')){
				var testCell = new Element('td').update("this is a test cell");
				var theRow = new Element('tr').update(testCell);
				var theBody = new Element('tbody').update(theRow);
				var portTable = new Element('table',{cellSpacing:'0',cellPadding:'0', border:'0', id:'scrollContent'});
				portTable.appendChild(theBody); // patch prototype update function malfuntions in IE7 
				$(this.graftElement).appendChild(portTable);
			}
		}
		catch(err){
		}
		
	},
	
	createLinks:function(casesNode, index){
		document.getElementsByClassName('subNav')[0].setAttribute('id','thumbs');
		if($('thumbs').firstChild.childNodes.length<1){
			$A(data.linkArr).each(function(s,i){
					//'javascript:data.swapStudy('+i+');'
					var clientLink = new Element('a',{href:"/caseStudies/?c="+s}).update(s);
					$('thumbs').firstChild.appendChild(clientLink);
			});
		}
		new Effect.Opacity('thumbs',{duration:0.5, from:0, to:1.0});
	},
	
	highlight:function (index){
		if(document.getElementsByClassName('subNav')[0].firstChild.childNodes.length>0){
			$A(document.getElementsByClassName('subNav')[0].firstChild.childNodes).each(function(s,i){
				if(s.nodeType != 3){
					s.blur();
					if(s.firstChild.nodeValue == data.linkArr[index]){
						s.className = "selected";
					}else{
						s.className = "unselected";
					}
				}
			});
		}
	},
	
	swapStudy:function (index){
		var sections = [];
		var contVar =[];
		this.createTable();
		this.highlight(index);
		
		$A($('scrollContent').firstChild.firstChild.childNodes).each(function(s){
			s.remove();
		});
		$A(data.contentArr[index].childNodes).each(function(s,i){
											  
			if(s.nodeType != 3){
				$A(s.childNodes).each(function(j,q){
					if(j.nodeType != 3){
						contVar.push(j.nodeValue);
						sections.push(s.getAttribute('title'));
					}
				});
			}
		});
		contVar.each(function(s, i){
						  
				var newDiv = new Element('div',{className:'caseCell'}).update(s);
				var newCell = new Element('td',{id:sections[i]}).update(newDiv);
				$('scrollContent').firstChild.firstChild.appendChild(newCell);
		});
		slider = new Slider(sections, data.graftElement, 'scrollContent');
		new Effect.Opacity('scrollContent',{duration:0.5, from:0, to:1.0});
		
	}
});