
/* Core functions to support CDF Software website
 * (c)2007-2008 CDF Software Inc.
 * CDF Software is a trademark of Core Digital Frameworks (Pvt) Ltd
 */

 var config = {
	animDuration: 400 //Duration of UI Animations in milliseconds
};

 
// ---------------------------------------------------------------------------------
// Animation engine
// ---------------------------------------------------------------------------------

function Animator()
{
	this.running = 0; // Incremented at start of each animation, decremented afterwards. If zero, the interval timer is disabled
	this.timerID; // ID of the timer used for animating
	this.animations = []; // List of animations in progress
	return this;
}

// Start animation engine
Animator.prototype.startAnimating = function() // Variable number of arguments
{
	for(var t=0; t<arguments.length; t++)
		this.animations.push(arguments[t]);
	if(this.running == 0) {
		var me = this;
		this.timerID = window.setInterval(function() {me.doAnimate(me);},10);
	}
	this.running += arguments.length;
};

// Perform an animation engine tick, calling each of the known animation modules
Animator.prototype.doAnimate = function(me)
{
	var a = 0;
	while(a < me.animations.length) {
		var animation = me.animations[a];
		if(animation.tick()) {
			a++;
		} else {
			me.animations.splice(a,1);
			if(--me.running == 0)
				window.clearInterval(me.timerID);
		}
	}
};

// Map a 0..1 value to 0..1, but slow down at the start and end
Animator.slowInSlowOut = function(progress)
{
	return(1-((Math.cos(progress * Math.PI)+1)/2));
};

//--
//-- Morpher animation
//--

// Animate a set of properties of an element
function Morpher(element,duration,properties,callback)
{
	this.element = element;
	this.duration = duration;
	this.properties = properties;
	this.startTime = new Date();
	this.endTime = Number(this.startTime) + duration;
	this.callback = callback;
	this.tick();
	return this;
}

Morpher.prototype.assignStyle = function(element,style,value)
{
	switch(style) {
		case "-tw-vertScroll":
			window.scrollTo(findScrollX(),value);
			break;
		case "-tw-horizScroll":
			window.scrollTo(value,findScrollY());
			break;
		default:
			element.style[style] = value;
			break;
	}
};

Morpher.prototype.stop = function()
{
	for(var t=0; t<this.properties.length; t++) {
		var p = this.properties[t];
		if(p.atEnd !== undefined) {
			this.assignStyle(this.element,p.style,p.atEnd);
		}
	}
	if(this.callback)
		this.callback(this.element,this.properties);
};

Morpher.prototype.tick = function()
{
	var currTime = Number(new Date());
	progress = Animator.slowInSlowOut(Math.min(1,(currTime-this.startTime)/this.duration));
	for(var t=0; t<this.properties.length; t++) {
		var p = this.properties[t];
		if(p.start !== undefined && p.end !== undefined) {
			var template = p.template ? p.template : "%0";
			switch(p.format) {
				case undefined:
				case "style":
					var v = p.start + (p.end-p.start) * progress;
					this.assignStyle(this.element,p.style,template.format([v]));
					break;
				case "color":
					break;
			}
		}
	}
	if(currTime >= this.endTime) {
		this.stop();
		return false;
	}
	return true;
};

//-- CDFSNIP V0.2.11
//-- Fader animation
//-- endValue == how much should the element fade or unfade? (default is "fully"). For lightbox it is 0.5 (50%).
//--
function Fader(targetElement,opening,endValue,deleteMode,cback)
{
	var p = [];
	var c = null;
	if(opening) {
		if (endValue == null) endValue = 1;
		p.push({style: 'opacity', start: 0, end: endValue, template: '%0'});
		p.push({style: 'filter', start: 0, end: (endValue * 100), template: 'alpha(opacity:%0)'});
		c = function(element, properties) { if (cback) cback(element,properties); }
	} else {
		if (endValue == null) { endValue = 0; }
		p.push({style: 'opacity', start: 1, end: endValue, template: '%0'});
		p.push({style: 'filter', start: 100, end: (endValue * 100), template: 'alpha(opacity:%0)'});

				switch(deleteMode) {
			case "all":
				c = function(element,properties) {removeNode(element); if(cback) cback(element,properties);};
				break;
			case "children":
				c = function(element,properties) {removeChildren(element); if(cback) cback(element,properties);};
				break;
			case "none":
				c = function(element,properties) {if(cback) cback(element,properties);};
				break;
		}
	}
	return new Morpher(targetElement,config.animDuration,p,c);
}


//--
//-- Slider animation
//--

// deleteMode - "none", "all" [delete target element and it's children], [only] "children" [but not the target element]
function Slider(element,opening,unused,deleteMode, cback)
{

	element.style.overflow = 'hidden';
	if(opening) {
		element.style.height = '0px'; // Resolves a Firefox flashing bug
	}
	element.style.display = 'block';
	var left = findPosX(element);
	var width = element.scrollWidth;
	var height = element.scrollHeight;
	var winWidth = findWindowWidth();
	var p = [];
	var c = null;
	if(opening) {
		p.push({style: 'height', start: 0, end: height, template: '%0px', atEnd: 'auto'});
		p.push({style: 'opacity', start: 0, end: 1, template: '%0'});
		p.push({style: 'overflow', atEnd: ''});
		p.push({style: 'filter', start: 0, end: 100, template: 'alpha(opacity:%0)'});
		c = function(element, properties) { if (cback) cback(element,properties); }
	} else {
		p.push({style: 'height', start: height, end: 0, template: '%0px'});
		p.push({style: 'display', atEnd: 'none'});
		p.push({style: 'overflow', atEnd: ''});		//remove the "overflow hidden" added at the start of animation
		p.push({style: 'opacity', start: 1, end: 0, template: '%0'});
		p.push({style: 'filter', start: 100, end: 0, template: 'alpha(opacity:%0)'});
		switch(deleteMode) {
			case "all":
				c = function(element,properties) {removeNode(element);if(cback) cback(element,properties);};
				break;
			case "children":
				c = function(element,properties) {removeChildren(element);if(cback) cback(element,properties);};
				break;
			case "none":
				c = function(element,properties) {if(cback) cback(element,properties);};
				break;
		}
	}
	return new Morpher(element,config.animDuration,p,c);
}

// Get the current width of the display window
function findWindowWidth()
{
	//17px = scrollbar which is always "on"
	return(window.innerWidth ? (window.innerWidth - 17) : (document.documentElement.clientWidth - 17));
}

// Get the current height of the display window
function findWindowHeight()
{
	return(window.innerHeight ? window.innerHeight : document.documentElement.clientHeight);
}

// Get the current horizontal page scroll position
function findScrollX()
{
	return(window.scrollX ? window.scrollX : document.documentElement.scrollLeft);
}

// Get the current vertical page scroll position
function findScrollY()
{
	return(window.scrollY ? window.scrollY : document.documentElement.scrollTop);
}

function findPosX(obj)
{
	var curleft = 0;
	while (obj.offsetParent)
		{
		curleft += obj.offsetLeft;
		obj = obj.offsetParent;
		}
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	while (obj.offsetParent)
		{
		curtop += obj.offsetTop;
		obj = obj.offsetParent;
		}
	return curtop;
}

//utils
// Substitute substrings from an array into a format string that includes '%1'-type specifiers
String.prototype.format = function(substrings)
{
	var subRegExp = new RegExp("(?:%(\\d+))","mg");
	var currPos = 0;
	var r = [];
	do {
		var match = subRegExp.exec(this);
		if(match && match[1])
			{
			if(match.index > currPos)
				r.push(this.substring(currPos,match.index));
			r.push(substrings[parseInt(match[1])]);
			currPos = subRegExp.lastIndex;
			}
	} while(match);
	if(currPos < this.length)
		r.push(this.substring(currPos,this.length));
	return r.join("");
}


//******** INIT ****************
var initialize = function(whichPage) {

		switch (whichPage) {
		
		///////////////////////////////
			case "index":
		//////////////////////////////
				var source = null;
				var imgs = document.getElementsByTagName("IMG");
				for (var i = 0; i < imgs.length; ++i){
				
					source = imgs[i].getAttribute("imgid");
					if (source && (source != "")) {
						imgs[i].style.cursor = "pointer";
						imgs[i].src = imageLoader.prefix + imageLoader.list[source];
						if(indexactions[source])
						{
							imgs[i].onclick = indexactions[source];
						}
					}
				}
				
				////Init the News Scroll
				Ticker.elem = document.getElementById("indexnewsbox");
				window.setTimeout(Ticker.tick,Ticker.tickdur);
				
			break;
		////////////////////////
			default:
		////////////////////////
		
			break;
		}
	
}

/* Simple ticker class.. works on a series of child DIVs of the given elem */
var Ticker = {
	curridx: 0,
	tickdur: 7000,
	elem: null,
	tick: function() {
		if(Ticker.elem)
		{
			var items = Ticker.elem.getElementsByTagName("DIV");
		
			anim.startAnimating(new Fader(items[Ticker.curridx],false,0,"none",function() {

				items[Ticker.curridx].style.display = "none";
				if ((Ticker.curridx + 1) == items.length) { 	//last itme
					Ticker.curridx = -1;	//wrap to start
				}
				Ticker.curridx++;
				items[Ticker.curridx].style.display = "block";
				anim.startAnimating(new Fader(items[Ticker.curridx],true,1,"none"));
				window.setTimeout(Ticker.tick,Ticker.tickdur);
			}));
		}
	}
}

var imageLoader = {
	prefix: "images/",
	list: {
		cdflogo: "cdflogo.jpg",
		sniplogo: "cdfsniplogo.jpg",
		gnwlogo: "gnwlogo.jpg",
		dfulogo: "dfu1.png",
		silogo: "silogo.png",
		snipshot: "snipshot.jpg",
		gnwshot: "gnwshot.png",
		dfushot: "dfu-shot.jpg",
		pashalogo: "pashalogo.png",
		apictalogo: "apicta.png",
		recfly: "recfly.jpg"
	}
}

//global
var whichopen = "";
var anim = new Animator();

//on click handlers for various images etc.
var indexactions = {
	cdflogo: function(e) {  window.href="index.php"; },
	
   sniplogo: function(e) {
		var details = document.getElementById("leadercontent"); //productdetails");
		var snipdetails = document.getElementById("snipdesc");
		anim.startAnimating(new Fader(details,false,0,"none",function() {
			details.innerHTML = snipdetails.innerHTML;
			anim.startAnimating(new Fader(details,true,1,"none",function() { whichopen = "sniplogo"; }));			
		}));
   },
   
    gnwlogo: function(e) {
		var details = document.getElementById("leadercontent");
		var snipdetails = document.getElementById("gnwdesc");

		anim.startAnimating(new Fader(details,false,0,"none",function() {
			details.innerHTML = snipdetails.innerHTML;
			anim.startAnimating(new Fader(details,true,1,"none",function() { whichopen = "gnwlogo"; }));			
		}));
   },
   
   dfulogo: function(e) {
		var details = document.getElementById("leadercontent");
		var snipdetails = document.getElementById("dfudesc");
		anim.startAnimating(new Fader(details,false,0,"none",function() {
			details.innerHTML = snipdetails.innerHTML;
			anim.startAnimating(new Fader(details,true,1,"none",function() { whichopen = "dfulogo"; }));			
		}));
   },
   silogo: function(e) {
		var details = document.getElementById("leadercontent");
		var snipdetails = document.getElementById("sidesc");
		anim.startAnimating(new Fader(details,false,0,"none",function() {
			details.innerHTML = snipdetails.innerHTML;
			anim.startAnimating(new Fader(details,true,1,"none",function() { whichopen = "silogo"; }));			
		}));
   },
   snipshot: function() { },
   
   gnwshot: function() { }


}

function showAllNews() {
	var elem = document.getElementById("extranews")
	elem.style.display = "block";
	anim.startAnimating(new Slider(elem,true,null,"none"));
}
