// PACKAGE: Ansearch Redesign Core Factory
// @Author: Tim T Wu (30 JUNE 08)
// GLOBALS
var hologram = {x:200, y:70, radius:128, offset:45, speed:6, minOpacity:40, minSizeRatio:54};
//var hologram = {x:225, y:70, radius:128, offset:45, speed:6, minOpacity:40, minSizeRatio:1};

var tickInt = 25;
var PIon180 = Math.PI / 180;
var tick = 360/menuItem.length;
var speed = hologram.speed;
var systemItem = [];
var menuCore = [];
var i, r;
var nextPos;

// IE indexOf METHOD Fix
if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
		return -1;
	}
}

// CORE METHODS
function Pos(x, y){
	this.x = Math.round(x);
	this.y = Math.round(y);
}

function Animation(id, path, steps){
	this.elem = document.getElementById(id);
	this.active = 0;
	this.timer = null;
	this.path = path;		// path obj
	this.pathIdx = 0;		// step counter
	this.numSteps = steps;	// total steps, 0 = go forever
	this.curPos = 0;		// current menu position
	this.oWidth = 0;
	this.oHeight = 0;
	this.pAlpha = 0;
}

Animation.prototype.start = function(angle){
	if(this.active){
    	return false;
	}else{
		var saveThis = this;
		this.step();
		this.active = 1;
		this.timer = setInterval(function(){saveThis.step();}, tickInt);
	}
};

Animation.prototype.stop = function(){
	if(!this.timer){
		return false;
	}
	
	clearInterval(this.timer);
	this.active = 0;
};

Animation.prototype.reset = function(){
	if(!this.timer){
		return false;
	}else{
		clearInterval(this.timer);
		this.active = 0;
		this.numSteps = 0;
		this.pathIdx = 0;
	}
};

Animation.prototype.step = function(){
	var nextPos = this.path.nextStep(this.pathIdx);
	var pathTrack;
	var alphaLevel;
	var adjustmentRatio;
	this.moveTo(nextPos.x, nextPos.y);
	
	if((this.numSteps > 0) && (this.pathIdx >= this.numSteps)){
    	this.stop();
	}else{
		pathTrack = this.pathIdx += speed;
		if(pathTrack > 360){
			//console.log(this.elem.id+'(PathIdx:'+pathTrack+', NumSteps:'+this.numSteps+', CurPos:'+this.curPos+')');
			this.numSteps -= 360;
			this.pathIdx = 0;
		}
		alphaLevel = (0.5 - ( hologram.minOpacity / 100 / 2 )) * Math.cos( (pathTrack * PIon180) + ( -hologram.offset * PIon180) ) + (0.5 + ( hologram.minOpacity / 100 / 2 ));
		
		adjustmentRatio = (0.5 - ( (1 - hologram.minSizeRatio / 100) / 2 )) * -1 * Math.sin( (pathTrack * PIon180) + (hologram.offset * PIon180) ) + (0.5 - ( (1 - hologram.minSizeRatio / 100) / 2 ));
		adjustmentRatio = Math.round(adjustmentRatio * 100);//*/
		
		this.opacity(alphaLevel.toFixed(2));
		this.sizeTo(this.oWidth - adjustmentRatio, this.oHeight - adjustmentRatio);
	}
};

Animation.prototype.moveTo = function(x, y){
	this.elem.style.top = y + 'px';
	this.elem.style.left = x + 'px';
};

Animation.prototype.sizeTo = function(width, height){
	this.elem.style.width = width + 'px';
	this.elem.style.height = height + 'px';
};

Animation.prototype.opacity = function(level){
	//console.log("Opacity Level: ", level);
	var obj = this.elem.style;
	obj.opacity = level;
	obj.MozOpacity = level;
	obj.KhtmlOpacity = level;
	obj.filter = "alpha(opacity="+(level*100)+")";
};

// Circular path object
function Circle(initPos, radius, angle0, stepSize){
	this.rad = radius;
	this.startAngle = angle0;
	this.aStep = stepSize;        // angle per step
	this.cx = initPos.x - this.rad * Math.cos(angle0 * PIon180);   // coords of centre
	this.cy = initPos.y - this.rad * Math.sin(angle0 * PIon180);
	this.currX = initPos.x;
	this.currY = initPos.y;
}

Circle.prototype.nextStep = function(index){
	var angle = this.startAngle - index * this.aStep;  // +ve angles are cw, we want ccw
	//console.log(angle);
	this.currX = this.cx + this.rad * Math.cos(angle * PIon180);
	this.currY = this.cy + this.rad * Math.sin(angle * PIon180);
	
	return new Pos(this.currX, this.currY);
};

function Rotate(rotations){
	if(rotations === undefined){
		rotations = 1;
	}
	//console.log(rotations);
	for(i=0; i<systemItem.length; i++){
		systemItem[i].numSteps += tick * rotations;
		systemItem[i].start();
	}
	for(r=0; r<rotations; r++){
		for(i=0; i<systemItem.length; i++){
			if(systemItem[i].curPos < menuItem.length - 1){
				systemItem[i].curPos++;
			}else{
				systemItem[i].curPos = 0;
			}
			//console.log(systemItem[i].elem, systemItem[i].numSteps); 
		}
	}
}

function Select(label){
	$("#ajax").append("<div class='loading'>Loading..</div>");
	$.ajax({
		url: "/wheel/"+label+".php",
		success: function(response){
			$("#ajax").fadeOut("fast", function(){
				$("#ajax").fadeIn("fast");
				$("#ajax").html(response+"<div style='clear:both'></div>");
			});
		}
	});//*/
	$("#queryBox").focus();
	$("#sa").val(label);
	
	var selLoc = systemItem[menuCore.indexOf(label)].curPos;
	if(selLoc !== 0){
		var numRot = 0;
		for(i=selLoc; i<menuItem.length; i++){
			numRot++;
		}
		this.Rotate(numRot);
	}
}//*/

function Over(label){
	var selItem = systemItem[menuCore.indexOf(label)];
	selItem.pAlpha = selItem.elem.style.opacity;
	//console.log(selItem.pAlpha);
	selItem.opacity(1);
	//$("#"+label).fadeTo("fast", 1);
}

function Out(label){
	var selItem = systemItem[menuCore.indexOf(label)];
	//console.log(selItem.pAlpha);
	selItem.opacity(selItem.pAlpha);
	//$("#"+label).fadeTo("fast", selItem.pAlpha);
}

function LoadTwuWheel(){
	$("#queryBox").focus();
	var selectedIcon = document.location.href.split("?");
	var motionPath = new Circle( new Pos(hologram.x, hologram.y) , hologram.radius, 0, 1);
	var thisItem; nextPos = hologram.offset;
	for(i=0; i<menuItem.length; i++){
		systemItem.push(new Animation(menuItem[i].name + 'Button', motionPath, 0));
		menuCore.push(menuItem[i].name);
		thisItem = systemItem[systemItem.length-1];
		
		thisItem.oWidth = menuItem[i].width;
		thisItem.oHeight = menuItem[i].height;
		thisItem.numSteps = nextPos;
		thisItem.curPos = i;
		thisItem.start();
		
		thisItem.sizeTo(thisItem.oWidth, thisItem.oHeight);
		nextPos += tick;
	}
	if(menuCore.indexOf(selectedIcon[1]) >= 0){
		Select(selectedIcon[1]);
	}else{
		Select(menuCore[0]);
	}
}
//console.log('Next Angle (Finish):', nextPos);
