//fish.js .2

function Fish(dataArray, panelType){
	this.fishDiv = document.createElement('div');
	this.dataArray = dataArray;
	this.fishDiv.className = 'fish';
	var displayTitle = dataArray.title.length < 30 ? dataArray.title : dataArray.title.substring(0,27)+'...';
	this.fishDiv.innerHTML = displayTitle;
	this.fishDiv.fish = this;
	this.fish = this;
	
	var fishCallback = function(ajaxData){
		var artifactData = eval('('+ajaxData+')');
		var displayTitle = artifactData.project.title.length < 60 ? artifactData.project.title : artifactData.project.title.substring(0,57)+'...';
		var p = new ArtifactPanel(displayTitle);
		p.loadData(artifactData);	
	}
		
	this.launchPanel = function(){
		//intercept the launch if the user isn't clickwrapped
		if(!topMenu.agreed){
			new WrapPanel(this.fish);
			return;	
		}
		
		Loader.require(panelType+'Panel',ajax.get,[siteBaseDir+'js/jsCache.js.php', {id: this.fish.dataArray.id, type:'project'}, fishCallback]);
		//ajax.get(siteBaseDir+'js/jsCache.js.php', {id: this.fish.dataArray.id, type:'project'}, fishCallback);
	}
	this.showPreview = function(){
		var previewID = parseInt(this.fish.fishDiv.style.left) < 40 || this.fish.fishDiv.style.left == '' ? 'rightPreview' : 'leftPreview';
		var preview = document.getElementById(previewID);
		preview.innerHTML = this.fish.dataArray.description;
		preview.style.display = 'block';
		var previewBG = document.getElementById(previewID + 'BG');
		previewBG.style.display = 'block';
	}
	this.hidePreview = function(){
		var previewID = parseInt(this.fish.fishDiv.style.left) < 40 || this.fish.fishDiv.style.left == '' ? 'rightPreview' : 'leftPreview';
		document.getElementById(previewID).style.display = 'none';
		document.getElementById(previewID + 'BG').style.display = 'none';
	}
	this.fishDiv.onmouseup = this.launchPanel;
	this.fishDiv.onmouseover = this.showPreview;
	this.fishDiv.onmouseout = this.hidePreview;
}

Fish.prototype.makeActive = function(){
//Set up the array for color cycling
	if(!defined(this.baseColor)) {alert('no basecolor in '+this.dataArray.title); return false;}
	//Recycling the math.line function...x1 is red, y1 is green, x2 is blue
	var colors1 = Math.line({begin: {x: this.baseColor[0], y: this.baseColor[1]}, end: {x: 255, y:255}, steps: 10});
	var colors2 = Math.line({begin: {x: this.baseColor[2], y: 0}, end: {x: 255, y: 0}, steps: 10});
	
	this.cycleArray = {red: [], green: [], blue: []};
	
	for(var i=0; i<colors1.length; i++){
		this.cycleArray.red.push(colors1[i].x);
		this.cycleArray.green.push(colors1[i].y);
		this.cycleArray.blue.push(colors2[i].x);
	}
	for(var i=colors1.length - 1; i>=0; i--){
		this.cycleArray.red.push(colors1[i].x);
		this.cycleArray.green.push(colors1[i].y);
		this.cycleArray.blue.push(colors2[i].x);
	}
}

function ArtFish(dataArray){
	var workingFish = new Fish(dataArray, 'art');
	workingFish.poolType = 'Art';
	return workingFish; 
}

function CodeFish(dataArray){
	var workingFish = new Fish(dataArray, 'code');
	workingFish.poolType = 'Code';
	return workingFish;	
}

function ReferenceFish(referenceId, written, poolType, dataArray){
	//dataArray is optional, and turns the referenceFish into a full-blown pool-ready fish
	if(arguments.length == 3){
		this.fishSpan = document.createElement('span');
		this.fishSpan.className = 'referenceFish';
		this.dataArray = {written: written, id: referenceId};
		this.fishSpan.innerHTML = written;
		this.fishSpan.fish = this;
		this.fish = this;
		
		var fishCallback = function(ajaxData){
			var referenceData = eval('('+ajaxData+')');
			var p = new ReferencePanel(referenceData.reference.title);
			p.loadData(referenceData);	
		}	
		switch(poolType){
			case 'artreference':
				this.launchPanel = function(){
					Loader.require('artReferencePanel',ajax.get,[siteBaseDir+'js/jsCache.js.php', {id: this.dataArray.id, type: 'reference'}, fishCallback]);			
				}
			break;
			case 'codereference':
				this.launchPanel = function(){
					Loader.require('codeReferencePanel',ajax.get,[siteBaseDir+'js/jsCache.js.php', {id: this.dataArray.id, type: 'reference'}, fishCallback]);
				}
			break;	
		}
		this.fishSpan.onmouseup = this.launchPanel;
	}	
	if(arguments.length == 4){
		this.fishDiv = document.createElement('div');
		this.dataArray = dataArray;
		this.fishDiv.dataArray = dataArray;
		this.fishDiv.className = 'fish';
		var displayTitle = written.length < 30 ? written : written.substring(0,27)+'...';
		this.fishDiv.innerHTML = displayTitle;
		this.fishDiv.fish = this;
		this.fish = this;
		
		var fishCallback = function(ajaxData){
			var artifactData = eval('('+ajaxData+')');
			var displayTitle = artifactData.reference.title.length < 60 ? artifactData.reference.title : artifactData.project.title.substring(0,57)+'...';
			var p = new ReferencePanel(displayTitle);
			p.loadData(artifactData);	
		}
			
		switch(poolType){
			case 'artreference':
				this.launchPanel = function(){
					Loader.require('artReferencePanel',ajax.get,[siteBaseDir+'js/jsCache.js.php', {id: this.dataArray.id, type: 'reference'}, fishCallback]);			
				}
			break;
			case 'codereference':
				this.launchPanel = function(){
					Loader.require('codeReferencePanel',ajax.get,[siteBaseDir+'js/jsCache.js.php', {id: this.dataArray.id, type: 'reference'}, fishCallback]);		
				}
			break;	
		}
		
		this.showPreview = function(){
			var previewID = parseInt(this.fish.fishDiv.style.left) < 40 || this.fish.fishDiv.style.left == '' ? 'rightPreview' : 'leftPreview';
			var preview = document.getElementById(previewID);
			preview.innerHTML = this.fish.dataArray.description;
			preview.style.display = 'block';
			var previewBG = document.getElementById(previewID + 'BG');
			previewBG.style.display = 'block';
		}
		
		this.hidePreview = function(){
			var previewID = parseInt(this.fish.fishDiv.style.left) < 40 || this.fish.fishDiv.style.left == '' ? 'rightPreview' : 'leftPreview';
			document.getElementById(previewID).style.display = 'none';
			document.getElementById(previewID + 'BG').style.display = 'none';
		}
		this.fishDiv.onmouseup = this.launchPanel;
		this.fishDiv.onmouseover = this.showPreview;
		this.fishDiv.onmouseout = this.hidePreview;
	}
}

ReferenceFish.prototype.makeActive = function(){
//Set up the array for color cycling
	if(!defined(this.baseColor)) {alert('no basecolor in '+this.dataArray.title); return false;}
	//Recycling the math.line function...x1 is red, y1 is green, x2 is blue
	var colors1 = Math.line({begin: {x: this.baseColor[0], y: this.baseColor[1]}, end: {x: 255, y:255}, steps: 10});
	var colors2 = Math.line({begin: {x: this.baseColor[2], y: 0}, end: {x: 255, y: 0}, steps: 10});
	
	this.cycleArray = {red: [], green: [], blue: []};
	
	for(var i=0; i<colors1.length; i++){
		this.cycleArray.red.push(colors1[i].x);
		this.cycleArray.green.push(colors1[i].y);
		this.cycleArray.blue.push(colors2[i].x);
	}
	for(var i=colors1.length - 1; i>=0; i--){
		this.cycleArray.red.push(colors1[i].x);
		this.cycleArray.green.push(colors1[i].y);
		this.cycleArray.blue.push(colors2[i].x);
	}
}

function PersonFish(personId, written){
	this.fishSpan = document.createElement('span');
	this.fishSpan.className = 'personFish';
	this.dataArray = {written: written, id: personId};
	this.fishSpan.innerHTML = written;
	this.fishSpan.fish = this;
	this.fish = this;
	
	var fishCallback = function(ajaxData){
		var personData = eval('('+ajaxData+')');
		var p = new PersonPanel(personData.person.written);
		p.loadData(personData);	
	}
		
	this.launchPanel = function(){
		Loader.require('personPanel',ajax.get,[siteBaseDir+'js/jsCache.js.php', {id: this.fish.dataArray.id, type:'person'}, fishCallback]);
		//ajax.get(siteBaseDir+'js/jsCache.js.php', {id: this.fish.dataArray.id, type:'person'}, fishCallback);
	}
	this.fishSpan.onmouseup = this.launchPanel;
}