﻿var oImg = document.createElement('img');

var sURL = document.location.toString();
if(sURL.lastIndexOf('from=bannerad') > 0) {
	document.location.href = '/aperture/profiles/';
}


function selectQuotes() {
	var iRandNum = Math.ceil(Math.random()*2);
	if(iRandNum == 1) {
		Intro.aQuotesHash = new Array(
		{src:'http://images.apple.com/aperture/images/quote1_20060713.gif',alt:'That Aerture is as comprehensive as it is and functions as fast and efficiently to produce high-quality results with relative ease is pretty miraculous.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote2_20060713.gif',alt:'Wheather you are a professional or passionate amateur, Aperture offers more productivity for your photos than any application I\'ve seen to date.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote3_20060713.gif',alt:'Aperture offers a terrific solution for importing, organizing and developing RAW files.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote4_20060713.gif',alt:'Aperture\'s user interface is intuitive, flexible, and beautiful.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote5_20060713.gif',alt:'Nothing handles a RAW workflow like Aperture.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote6_20060713.gif',alt:'If you\'re a Windows user who has been thinking about making the switch, Aperture is all the incentive you need.',plimg:''});	
	} else {
		Intro.aQuotesHash = new Array(
		{src:'http://images.apple.com/aperture/images/quote1_20060713.gif',alt:'That Aerture is as comprehensive as it is and functions as fast and efficiently to produce high-quality results with relative ease is pretty miraculous.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote2_20060713.gif',alt:'Wheather you are a professional or passionate amateur, Aperture offers more productivity for your photos than any application I\'ve seen to date.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote3_20060713.gif',alt:'Aperture offers a terrific solution for importing, organizing and developing RAW files.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote7_20060713.gif',alt:'Aperture\'s features are wrapped up in a truly beautiful experience.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote8_20060713.gif',alt:'Apple has provided a tool that promises to give us more time behind our cameras.',plimg:''},
		{src:'http://images.apple.com/aperture/images/quote9_20060713.gif',alt:'Aperture provides exceptional, unique tools for comparing and sorting images.',plimg:''});
	}
}


var Intro = {
	iQuoteWidth: 479,
	iQuoteHeight: 220,
	aQuotesHash: new Array(),
	oLens: false,
	oTour: false,
	oQuote: false,
	oMore: false,
		
	init: function() {
		this.oLens = new HTMLObject($('lens'));
		this.oTour = new HTMLObject($('tour'));
		this.oQuote = new HTMLObject($('quote'));
		Object.extend(this.oLens,WhizBang.prototype);
		Object.extend(this.oTour,WhizBang.prototype);
		Object.extend(this.oQuote,WhizBang.prototype);
		this.preloadQuotesHash();
		this.initQuotes();
		var self = this;
		setTimeout(function(){self.oLens.Fade(500,0,100,20,20,20,function(){
			self.oQuote.Fade(500,0,100,30,30,30,function(){
				self.crossfadeQuotes(self);
			});
		});},1000);
		setTimeout(function(){self.oTour.Fade(500,0,100,20,20,20);},1000);
	},
	initQuotes: function() {
		this.oQuote.innerHTML = '';
		var oQuoteImg = oImg.cloneNode(true);
		oQuoteImg.src = this.aQuotesHash[0].src;
		oQuoteImg.alt = this.aQuotesHash[0].alt;
		oQuoteImg.width = this.iQuoteWidth;
		oQuoteImg.height = this.iQuoteHeight;
		this.oQuote.appendChild(oQuoteImg);
		oQuoteImg.style.zIndex = '100';
	},
	preloadQuotesHash: function() {
		if(document.images) {
			for(var i=0, oQuoteImg; oQuoteImg = this.aQuotesHash[i]; i++) {
				var preloadedImg = new Image();
				preloadedImg.src = oQuoteImg.src;
				oQuoteImg.plimg = preloadedImg;
			}
		}
	},
	crossfadeQuotes: function(obj) {
		for(var i=1; i<obj.aQuotesHash.length; i++) {
			var oQuoteImg = oImg.cloneNode(true);
			oQuoteImg.src = obj.aQuotesHash[i].src;
			oQuoteImg.alt = obj.aQuotesHash[i].alt;
			oQuoteImg.width = obj.iQuoteWidth;
			oQuoteImg.height = obj.iQuoteHeight;
			obj.oQuote.appendChild(oQuoteImg);
		}
		var fadeimgs = new HTMLObject(obj.oQuote.getElementsByTagName('img'));
		Object.extend(fadeimgs,WhizBang.prototype);
		fadeimgs.CrossFade();
	}
};


WhizBang.prototype.CrossFade = function() {
	this.interval = 60; // initial interval
	this.duration = 1000;
	this.step = 10;
	this.setOpacity = function (obj, o) {
	    obj.style.opacity = (o / 101);
	    obj.style.MozOpacity = (o / 100);
	    obj.style.KhtmlOpacity = (o / 100);
	    obj.style.filter = "alpha(opacity=" + o + ")";
	}
	
	for(j=0;j<this.length;j++) {
		this[j].style.position='absolute';
		this[j].style.top=0;
		this[j].style.zIndex=0;
		if(j>0) this.setOpacity(this[j],0);
		else this.setOpacity(this[j],100);
	}
	
	this.opacity = 100;
	//this.setOpacity(this[0].parentNode,this.opacity);
	this.currentDiv = 0;
	this.loadingCF = false;
	this.previousDiv = this.length-1;
	this.changeOpacity = function (prevopac,currentopac) {
		var stepDuration = Math.round(this.duration/this.step);
		this.duration -= stepDuration;
		this.step--;
		if(prevopac > 0 && currentopac == 0) {
			this.setOpacity(this[this.previousDiv],prevopac);
			this[this.previousDiv].style.zIndex = 100;
			this[this.currentDiv].style.zIndex = 0;			
			prevopac -= 10;
			window.setTimeout(this.changeOpacity.bindWithArguments(this,prevopac,currentopac),stepDuration);
		}
		else if(currentopac < 100 && prevopac == 0) {
			this.setOpacity(this[this.previousDiv],prevopac);
			this.setOpacity(this[this.currentDiv],currentopac);
			currentopac += 10;
			this[this.previousDiv].style.zIndex = 0;
			this[this.currentDiv].style.zIndex = 100;
			if(currentopac == 10) {
				this.duration = 1000;
				this.step = 10;
				stepDuration = Math.round(this.duration/this.step);
			}
			window.setTimeout(this.changeOpacity.bindWithArguments(this,prevopac,currentopac),stepDuration);
		} else {
			this.previousDiv = this.currentDiv;
			this.currentDiv = (this.currentDiv>=this.length-1) ? 0 : this.currentDiv + 1;
			prevopac = 100;
			currentopac = 0;
			this.duration = 1000;
			this.step = 10;
			window.setTimeout(this.changeOpacity.bindWithArguments(this,prevopac,currentopac),(this.interval*100));
		}
	}
	this.changeOpacity(0,this.opacity);
}

selectQuotes();



/*

function fadeIntroInit() {
	var myIntro = new FadeIntro();
	myIntro.init();
}

FadeIntro = function() {
	this.aPhotosHash = new Array(
		{src: 'http://images.apple.com/aperture/images/index01.jpg', image: ''},
		{src: 'http://images.apple.com/aperture/images/index02.jpg', image: ''},
		{src: 'http://images.apple.com/aperture/images/index03.jpg', image: ''}
	);
}

FadeIntro.prototype = {
	init: function() {
		var oIntroContainer = $('introimg');
		oIntroContainer.style.visibility = 'hidden';
		for(var i=0, oPhotoObj = aPhotosHash[i]; i++) {
			var oImg = new Image();
			oImg.src = oPhotoObj.src;
			oImg.alt = '';
			oImg.className = 'fadeimg';
			oImg.height = '430';
			oImg.width = '900';
			oPhotoObj.image = oImg;
		}
	},
	setOpacity: function(iOpacity) {
		
	}
}
*/


function CrossFade() {
	this.interval = 50; // initial interval
	this.setOpacity = function (obj, o) {
	    obj.style.opacity = (o / 101);
	    obj.style.MozOpacity = (o / 100);
	    obj.style.KhtmlOpacity = (o / 100);
	    obj.style.filter = "alpha(opacity=" + o + ")";
	}
	for(j=0;j<this.length;j++) {
		this[j].style.position='absolute';
		//this[j].style.top=0;
		//this[j].style.zIndex=0;
		this.setOpacity(this[j],0);
	}
	this.opacity = 100;
	this.setOpacity(this[0].parentNode,this.opacity);
	this.currentDiv = 0;
	this.loadingCF = true;
	this.zIndexing = false;
	this.previousDiv = this.length-1;
	//this[0].parentNode.style.display = 'block';
	this[0].parentNode.style.visibility = 'visible';
	this.initOpacity = function (div, startOpacity, endOpacity) {
		if (startOpacity <= endOpacity) {
			this.setOpacity(div, startOpacity);
			startOpacity += 10;
				window.setTimeout(this.initOpacity.bindWithArguments(this,div,startOpacity,endOpacity),this.interval);
		}
	}
	this.initOpacity(this[this.currentDiv],100,100);
	var iNewImageStart = this.iNewImageNum = 1;
	this.changeOpacity = function (opac) {
		if(opac < 100) {
			if(opac == 0) this[this.currentDiv].style.zIndex = 1;
			this.setOpacity(this[this.currentDiv],opac);
			//this.setOpacity(this[this.previousDiv],100-opac);
			opac += 10;
			var intervalMult = 1;
			if(this.iNewImageNum == 0) {
				intervalMult = 0.15;
			}
			window.setTimeout(this.changeOpacity.bindWithArguments(this,opac),this.interval*intervalMult);
		} else {
			var intervalMult = 10;
			if(this.loadingCF != true) {
				//alert(this.iNewImageNum);
				this.setOpacity(this[this.currentDiv],100);
				if(this.iNewImageNum == 0) {
					for(var i=0; i<this.length; i++) {
						if(i != this.currentDiv) {
							this[i].style.zIndex = -1;
							this.setOpacity(this[i],0);
						}
					}
					this.iNewImageNum = 1;
				} else {
					this.iNewImageNum--;
					intervalMult = 150;
				}
				this.previousDiv = this.currentDiv;
				this.currentDiv = (this.currentDiv>=this.length-1) ? 0 : this.currentDiv + 1;
				
				if(this.zIndexing == true) {
					this[this.previousDiv].style.zIndex = 0;
				} else {
					this.zIndexing = true;
				}
			} else {
				this.loadingCF = false;
				intervalMult = 10;
				this.previousDiv = this.currentDiv;
				this.currentDiv = (this.currentDiv>=this.length-1) ? 0 : this.currentDiv + 1;
				
				this[this.currentDiv].style.zIndex = 2;
				this[this.previousDiv].style.zIndex = 1;
			}

			opac = 0;
			window.setTimeout(this.changeOpacity.bindWithArguments(this,opac),(this.interval*intervalMult));
		}
	}
	this.olen = this[0].className.length;
	this.onum = this[0].className.charAt(this.olen-1);
	if(!isNaN(this.onum) && this.onum > 1) {window.setTimeout(this.changeOpacity.bindWithArguments(this,this.opacity),(80*this.interval)/this.onum);
	//} else if(this.onum == 3) { 
	//	window.setTimeout(this.changeOpacity.bindWithArguments(this,this.opacity),300*this.interval);
	}else {
		this.changeOpacity(this.opacity);
	}
}
WhizBang.prototype.CrossFade = CrossFade;

var currentImg;
var previousImg;
var fadedivs = [];

function isA(o,klass){ if(!o.className) return false; return new RegExp('\\b'+klass+'\\b').test(o.className) }

// get elements by class name, eg $c('post', document, 'li')
function $c(c,o,t) { o=o||document;
	if (!o.length) o = [o]
	var elements = []
	for(var i = 0, e; e = o[i]; i++) {
		if(e.getElementsByTagName) {
			var children = e.getElementsByTagName(t || '*');
			for (var j = 0, child; child = children[j]; j++) {
				if(isA(child,c)) elements.push(child);
			}
		}
	}
	return elements
}

function InitCrossFade() {
	if(InitCrossFade.arguments) {
		var fadecontainer = document.getElementById(InitCrossFade.arguments[0]);
		for(i=1;i<InitCrossFade.arguments.length;i++) {
			fadecontainer.innerHTML+=InitCrossFade.arguments[i];
		}
		var fade = fadecontainer.lastChild;
		var fadeclass = fade.className;
		fadedivs = new HTMLObject($c(fadeclass,'','*'));
		Object.extend(fadedivs,WhizBang.prototype);
		fadedivs.CrossFade();
	}
}

//addEventToObject(window,'onload',InitCrossFade);

