/*
Copyright (c) 2008, Xavier CAMBAR.
Code licensed under the BSD License:

* Copyright (c) 1998, Regents of the University of California
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the University of California, Berkeley nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

version: 1.0
*/
YAHOO.namespace('Cazaux');
var Dom = YAHOO.util.Dom;
var Anim = YAHOO.util.Anim;
var Selector = YAHOO.util.Selector;
var Event = YAHOO.util.Event;
var UA = YAHOO.env.ua;
var Ylog = YAHOO.log;

//////////////////////////////
if(typeof mYUI=="undefined"||!mYUI){var mYUI={};}

mYUI.HoverObject = function (root, options) {};
mYUI.HoverObject.prototype = {
	root : '',
	is1stTimeOver : function(evt, custom_root) {
		var element = Event.getTarget(evt);
		var related = Event.getRelatedTarget(evt);
		var root = custom_root || this.root;
		return this.isOver(evt, root) && !(related==root || Dom.isAncestor(root, related));
	},
	isOver : function(evt, custom_root) {
		var element = Event.getTarget(evt);
		var related = Event.getRelatedTarget(evt);
		var root = custom_root || this.root;
		return (element==root || this.isOverChild(evt, custom_root));
	},
	isOverChild : function(evt, custom_root) {
		var element = Event.getTarget(evt);
		var root = custom_root || this.root;
		return Dom.isAncestor(root, element);
	},
	isOut : function(evt, custom_root) {
		var element = Event.getRelatedTarget(evt);
		var root = custom_root || this.root;
		return !(element==root || Dom.isAncestor(root, element));
	}
};
///////////////////////////////////////

Event.onDOMReady(function(){
	var logger = new YAHOO.widget.LogReader();
	logger.hide();
	
	Event.on('credits', 'click', YAHOO.Cazaux.Credits);
});

YAHOO.namespace('Cazaux.History');
YAHOO.Cazaux.History.init = function() {
	var intro = Dom.get('intro_container')
	var intro_bg = Dom.getElementsByClassName('bg', null, intro)[0];
	var intro_width =	YAHOO.env.ua.ie ? intro_bg.offsetWidth : Dom.getStyle(intro_bg, "width").replace('px','');
	Dom.setStyle(intro_bg, "width", "0");
	
	var years = Selector.query('.year').reverse();
	for (var i in years) {
		Dom.setStyle(years[i], "visibility", "visible");
		Dom.setStyle(years[i], "opacity", "0");
	};
	var bg_url = YAHOO.env.ua.ie ? 
		Dom.getStyle('history', 'background-image').replace('url("', '').replace('")', '')
		: Dom.getStyle('history', 'background-image').replace('url(', '').replace(')', '');
	var pL = new Ueye.ImgPreloader([bg_url]);
	pL.afterLoading.subscribe(function(){
		// Animations
		var introContainerAnim = new Anim(intro_bg, {
			width: {to: intro_width}
		}, UA.ie ? 0.3 : 1);
		var introTxtAnim = new Anim(Dom.getElementsByClassName('txt', null, intro)[0], {
			opacity: {from: 0, to: 1}
		}, 0.5);
		var Fade = function(el) {
			return new Anim(el, {opacity: {to: 1}}, 0.5);
		}
		var yearAnims = [];
		for (var i=0;  i < years.length; i++) {
			yearAnims[i] = Fade(years[i]);
			if(i<years.length-1) {
				yearAnims[i].onComplete.subscribe(function(evt, effect, index){
					yearAnims[index+1].animate();
			}, i);
			}
		};

		//Callbacks
		introTxtAnim.onComplete.subscribe(function(){
			yearAnims[0].animate();
		})
		introContainerAnim.onComplete.subscribe(function(){
			Dom.setStyle(Dom.getElementsByClassName('txt', null, intro)[0], 'opacity', "0");
			Dom.setStyle(Dom.getElementsByClassName('txt', null, intro)[0], 'display', "");
			introTxtAnim.animate();
		});

		introContainerAnim.animate();
	});
	pL.run();
}

/*************** Tradition / Model1 ******************/

YAHOO.namespace('Cazaux.Model1.Render');
YAHOO.Cazaux.Model1.Render.text = function(eType, aColl, objs) {
	var descContainer = Selector.query('.text_c')[0];
	for (var i in objs) {
		//Insert text
		var text = document.createElement('div');
		Dom.addClass(text, 'yui-b');
		Dom.addClass(text, 'text');
		text.innerHTML = objs[i].description;
		Dom.setStyle(text, 'position', 'absolute');
		descContainer.appendChild(text);
		if(!UA.ie)
			Dom.setStyle(text, 'opacity', '0');
		if(i==0) {
			if(!UA.ie) new Anim(text, {opacity: {from: 0, to: 1}}, 0.5).animate();	
		}
		else
			Dom.setStyle(text, 'display', 'none');
		Dom.addClass(text, 'elt_'+i)
		delete text;
	}
}
YAHOO.Cazaux.Model1.Render.snapshot = function (eTypeeType, aColl, objs) {
	var snapsContainer = Selector.query('#topbox .snaps')[0];
	var picsContainer = Dom.get('pic');
	//insert snapshots
	var allPics = []
	for (var i in objs) {
		var pics = objs[i].pics;
		if(pics.length) {
			var cont = document.createElement('div');
			Dom.addClass(cont, 'yui-g');
			for (var j in pics) {
				var snap = document.createElement('div');
				Dom.addClass(snap, 'yui-u');
				if(j==0) {
					if(i==0) {
						Dom.addClass(snap, 'current');
						var CurrentImg = new Image();
						CurrentImg.src = pics[j];
						picsContainer.appendChild(CurrentImg);
						Dom.setStyle(CurrentImg, 'opacity', '0');
						new Anim(CurrentImg, {opacity: {from: 0, to: 1}}, 0.5).animate()
					}
					Dom.addClass(snap, 'first');
				}
				Dom.setStyle(snap, 'position', 'relative');
				var img = new Image();
				img.src = pics[j];
				allPics.push(img);
				snap.appendChild(img);
				cont.appendChild(snap);
				delete snap;
				delete img;
			};
			Dom.addClass(cont, 'elt_'+i);
			Dom.addClass(cont, 'snaps_c');
			snapsContainer.appendChild(cont);
			if(i!=0) {
				Dom.setStyle(cont, 'display', 'none')
				if(!UA.ie)
					Dom.setStyle(cont, 'opacity', '0')
			}
			delete cont;
		}
		delete pics;
	};
	Event.on(allPics, 'click', YAHOO.Cazaux.Model1.ChgPic);
	Event.on(allPics, 'mouseover', function(){
		Dom.addClass(this, 'hover')
	});
	Event.on(allPics, 'mouseout', function(){
		Dom.removeClass(this, 'hover')
	});
}
YAHOO.Cazaux.Model1.ChgPic = function(){
	var imgHolder = Dom.get('pic');
	var children = Dom.getChildren(imgHolder);
	var chgPicAnim = new Anim(children, {opacity: {to: 0}}, 0.5);
	Dom.setStyle(children, 'position', 'absolute');
	chgPicAnim.onStart.subscribe(function(){
		var newImg = this.cloneNode(false);
		Dom.setStyle(newImg, 'opacity', '0')
		imgHolder.appendChild(newImg);
		new Anim(newImg, {opacity: {from: 0, to: 1}}, 0.5).animate();
	}, null, this);
	chgPicAnim.onStart.subscribe(function(){
		Dom.getElementsByClassName('current', '', Selector.query('#topbox .snaps')[0], function(node){Dom.removeClass(node, 'current')});
		Dom.addClass(this.parentNode, 'current')
	}, null, this)
	chgPicAnim.onComplete.subscribe(function(){
		for (var i in children) {
			children[i].parentNode.removeChild(children[i]);
		};
	});
	chgPicAnim.animate()
};

YAHOO.Cazaux.Model1.Render.tabs = function (eType, aColl, objs) {
	var tabContainer = Dom.get('topbox');
	for (var i in objs) {
		//Build Tabs
		var cont = document.createElement('div');
		Dom.addClass(cont, 'yui-u');
		Dom.addClass(cont, 'top_c');
		var bg = document.createElement('div');
		Dom.addClass(bg, 'bg');
		Dom.addClass(bg, 'yui-b');
		cont.appendChild(bg);
		delete bg;
		var title = document.createElement('div');
		Dom.addClass(title, 'top');
		Dom.addClass(title, 'yui-b');
		title.innerHTML = objs[i].name;
		cont.appendChild(title);
		delete title;
		tabContainer.appendChild(cont)
		if(i==0) { Dom.addClass(cont, 'active'); }
		cont.id = 'elt_'+i;
		Event.on(cont.id, 'click', function(){
			if(Dom.hasClass(this, 'active')) return;
			var active = Dom.getChildrenBy(this.parentNode, function(n){ return Dom.hasClass(n, 'active')});
			for (var k in active) {
				Dom.removeClass(active[k], 'active');
			};
			Dom.addClass(this, 'active');
			var elts = Dom.getElementsBy(function(n){
				var classes = n.className.split(' ');
				for (var i in classes) {
					if(/^elt_\d+/i.test(classes[i])) return true;
				};
				return false;
			}, 'div', Dom.getAncestorByClassName(this, 'model1'));
			for (var j in elts) {
				if(Dom.hasClass(elts[j], this.id)) {
					Dom.setStyle(elts[j], 'display', '');
					if(Dom.hasClass(elts[j], 'snaps_c')) {
						YAHOO.Cazaux.Model1.ChgPic.call(Dom.getFirstChild(Dom.getFirstChild(elts[j])));
						if(!UA.ie)
							new Anim(elts[j], {opacity: {fom: 0, to: 1}}, 0.5).animate();
						else
							Dom.setStyle(elts[j], 'display', '');
					}
					else {
						if(!UA.ie)
							new Anim(elts[j], {opacity: {fom: 0, to: 1}}, 0.5).animate();
					}
				}
				else {
					if(Dom.hasClass(elts[j], 'snaps_c')) {
						var a = new Anim(elts[j], {opacity: {to: 0}}, 0.5);
						a.onComplete.subscribe(function(){ Dom.setStyle(this, 'display', 'none')}, null, elts[j]);
						if(!UA.ie)
							a.animate();
						else
							Dom.setStyle(elts[j], 'display', 'none')
					}
					else {
						if(!UA.ie)
							new Anim(elts[j], {opacity: {from: 1, to: 0}}, 0.5).animate();
						else
							Dom.setStyle(elts[j], 'display', 'none');
					}
				}
			};
		})
		delete cont;
	};
}


YAHOO.Cazaux.Credits = function() {
	Dom.setStyle('credits_c', 'display', (Dom.getStyle('credits_c', 'display')=='none' ? '' : 'none'));
};
