//<!{CDATA[
/*
1. skrypt będzie tworzył cało&#156;ć htmla sam - dla niego ważne będzie jedynie kontener z id pgAdverContainer
2. zostan&#353; utworzone linki na obrazkacch i na tytułach - je&#156;li będ&#353; istnieć - tzn jakie&#156; w xml będ&#353;
3. jeżeli sciezka do obrazka będzie pusta i okaze się ze nie ma obrazkow caly kontener zostanie usuniety ze strony
4. po najechaniu na linki skrypt się zatrzymuje a po wyjsciu znad linka startuje od nowa

parametry przychodz&#353;ce do skryptu:
	containerId = html id dla div kontenera - domyslnie pgAdvertContaiener
	className = nazwa klasy z cssa - domyslnie 'demo1'
	animationType = fullpart (domyslna) - animuje texty i zdjęcia jednoczesnie - jak http://www.artkulinaria.pl/
					partial - animuje najpierw zdjęcie potem teksty  - najlepiej laczyc z klas&#353; css demo2
	delay = opoznienie miedzy zmianami slajdow - domyslnie 3500 - 3 i pół sekundy
	
*/
var pgAdverts = new Class({
	Implements: [Chain, Events, Options],
  	
  	options: {
		'containerId': 'pgAdvertContainer',
		'className': 'demo2',
		'animationType': 'partial',
		'delay': 12800
  	},
	container: null,
	scriptname: 'hAdvertiser.js',
	scriptpath: '',
	adverts: [],
	buttons: [],
	images: [],
	pages: [],
	currentpage: 0,
	timer: 0,
	textCoordinates: {},
	valid: false,
	
  	initialize: function(options) {
  		this.setOptions(options);
		if (this.options.containerId.length > 0) {
			if (Browser.ie) {
				this.container = document.getElementById(this.options.containerId);
			} else {
				this.container = $(this.options.containerId);
			}
		}
		if (!this.container) return(false);
		if (this.options.className.length > 0) this.container.className = this.options.className;
		var scripts = document.getElement('head').getElements('script');
		scripts.each(function(scr) {
				if (scr.src.indexOf(this.scriptname) >= 0) this.scriptpath = scr.src;
			}, this);
		if (this.scriptpath.length == 0) return(false);
		var xmlpath = this.scriptpath.substr(0, this.scriptpath.length - 2) + 'xml';

		if (xmlpath.length > 0) this.getData(xmlpath);
		if (this.adverts.length == 0) {
			this.container.dispose(); //usuwa istniej&#353;cy kontener o id = pgAdvertContainer - nie ma go już dluzej na stronie
			return(false);
		}
		this.valid = this.create();

		if (this.valid) {
			this.container.start();
		}
  	}
});

pgAdverts.prototype.create = function(src) {
	var servername = '';
	var scrpath = this.scriptpath.split('/');
	if (scrpath.length > 2) servername = scrpath[2];

	var oneimg = null, anchor = null, linkserver = '', imgcont = null, onepage = null;
	var txtc = null, txttt = null, txtdsc = null, ttspan = null;
	for (var i=0; i < this.adverts.length; i++) {
		if (this.adverts[i].targetlink.length > 0) {
			linkserver = this.adverts[i].targetlink.split('/')[2];
		}
		onepage = new Element('div', {
				'class': 'onePage',
				'styles': {
						'display': (i > 0) ? 'none' : 'block'
					}
			});	
		
		imgcont = new Element('div', {
				'class': 'imgContainer'
			});
		onepage.imgContainer = imgcont;
		
		if (this.adverts[i].targetlink.length > 0) {
			anchor = new Element('a', {
					'href': this.adverts[i].targetlink,
					'target': (linkserver != servername) ? '_blank' : '',
					'title': this.adverts[i].targetlink
				});
			linkserver = '';
		}
		oneimg = new Element('img', {
				'src': this.adverts[i].imagepath,
				'title': this.adverts[i].imgtitle
			});
		if (this.adverts[i].targetlink.length > 0) {
			oneimg.inject(anchor);
			this.images.include(anchor);
			anchor.inject(imgcont);
		} else {
			oneimg.inject(imgcont);
			this.images.include(oneimg);
		}

		imgcont.inject(onepage);
		
		/*text container*/
		txtc = new Element('div', {
					'class': 'textContainer'
				});

		txttt = new Element('div', {
				'class': 'titleContainer'
			});
		txttt.inject(txtc);
		if (this.adverts[i].targetlink.length > 0) {
			anchor = new Element('a', {
					'href': this.adverts[i].targetlink,
					'target': (linkserver != servername) ? '' : '',
					'title': this.adverts[i].targetlink
				});
		}
		ttspan = new Element('span', {
				'html': (this.adverts[i].imgtitle.length > 0) ? this.adverts[i].imgtitle : ''
			});
		if (this.adverts[i].targetlink.length > 0) {
			ttspan.inject(anchor);
			anchor.inject(txttt);
		} else {
			ttspan.inject(txttt);
		}

		txtdsc = new Element('div', {
				'class': 'descriptionContainer',
				'html': (this.adverts[i].description.length > 0) ? this.adverts[i].description : ''
			});
		txtdsc.inject(txtc);
				
		txtc.inject(onepage);
		onepage.textContainer = txtc;
		
		onepage.inject(this.container);
		this.pages.include(onepage);
		
	} // for
	
	imgcont = new Element('div', {
			'class': 'buttonsContainer'
		});
	imgcont.inject(this.container);
	
	var btn = null;
	if (this.adverts.length > 1) {
		for (i = 0; i < this.adverts.length; i++) {
			btn = new Element('div', {
					'class': 'advertNumber',
					'text': (i + 1)
				});
			btn.inject(imgcont);
			this.buttons.include(btn);
		}
	}
	if (this.buttons.length > 0) this.buttons[0].addClass('selected');
	
	this.textCoordinates.height = this.pages[0].textContainer.getCoordinates().height.toInt();
	this.textCoordinates.paddingTop = this.pages[0].textContainer.getStyle('padding-top').toInt();
	this.textCoordinates.paddingBottom = this.pages[0].textContainer.getStyle('padding-bottom').toInt();
	this.textCoordinates.height -= (this.textCoordinates.paddingTop + this.textCoordinates.paddingBottom);
	
	this.container.timer = 0;
	
	if (this.adverts.length > 1) this.valid = this.createEvents();
	this.valid = false;
	return(true);
}

pgAdverts.prototype.createEvents = function() {
	/*main move function*/
	this.container.forward = function(event) {
			var nextpage = this.currentpage + 1;
			var idx = 1;
			if (event) {
				idx = this.buttons.indexOf(event.target);
				if (idx >= 0) {
					nextpage = idx;
				}
			}
			if (nextpage == this.pages.length) nextpage = 0;
			switch (this.options.animationType) {
				case 'partial':
					//partially
					clearTimeout(this.container.timer);
					clearTimeout(this.timer);
					this.pages[nextpage].textContainer.setStyles({
							'height': '0px',
							'padding-top': '0px',
							'padding-bottom': '0px'
						});
					var sFx = new Fx.Morph(this.pages[this.currentpage], {
							'onStart': function() {
									this.pages[nextpage].setStyles({
											'opacity': 0,
											'visibility': 'hidden'
										});
								}.bind(this),
							'onComplete': function() {
									this.pages[this.currentpage].setStyles({'display': 'none'});
									var nxtFx = new Fx.Morph(this.pages[nextpage], {
											'onStart': function() {
													this.pages[nextpage].setStyles({'display': 'block'});
													this.buttons.each(function(btn) {
															btn.removeClass('selected');
														});
													this.buttons[nextpage].addClass('selected');
												}.bind(this),
											'onComplete': function() {
													this.currentpage = nextpage;
													var txtMorph = new Fx.Morph(this.pages[this.currentpage].textContainer, {
															'onComplete': function() {
																	this.timer = this.container.forward.delay(this.options.delay);
																}.bind(this)
														});
													var dfn = function() {
														txtMorph.start({
																'height': [0, this.textCoordinates.height],
																'padding-top': [0, this.textCoordinates.paddingTop],
																'padding-bottom': [0, this.textCoordinates.paddingBottom]
															});															
														}.bind(this);
													this.container.timer = dfn.delay(500);
												}.bind(this)
										});
									nxtFx.start({
											'opacity': [0,1]
										});
								}.bind(this)
						});
					break;
				default:
					//fullpart
					var sFx = new Fx.Morph(this.pages[this.currentpage], {
							'onStart': function() {
									this.pages[nextpage].setStyles({
											'opacity': 0,
											'visibility': 'hidden'
										});
								}.bind(this),
							'onComplete': function() {
									this.pages[this.currentpage].setStyles({'display': 'none'});
									var nxtFx = new Fx.Morph(this.pages[nextpage], {
											'onStart': function() {
													this.pages[nextpage].setStyles({'display': 'block'});
													this.buttons.each(function(btn) {
															btn.removeClass('selected');
														});
													this.buttons[nextpage].addClass('selected');
												}.bind(this),
											'onComplete': function() {
													this.currentpage = nextpage;
													this.timer = this.container.forward.delay(this.options.delay);
												}.bind(this)
										});
									nxtFx.start({
											'opacity': [0,1]
										});
								}.bind(this)
						});
			}//switch between methods
			sFx.start({'opacity': [1,0]});
		}.bind(this);
	this.container.start = function() {
			this.timer = this.container.forward.delay(this.options.delay);
		}.bind(this);
	this.container.stop = function() {
			if (this.container.timer > 0) {
				clearTimeout(this.container.timer);
				this.container.timer = 0;
			}
			clearTimeout(this.timer);
			this.timer = 0;
		}.bind(this);
	/*actions for buttons*/
	this.buttons.each(function(btn) {
			btn.addEvent('click', function(event) {
					if (this.container.timer > 0) {
						clearTimeout(this.container.timer);
						this.container.timer = 0;
					}
					clearTimeout(this.timer);
					this.timer = 0;
					this.container.forward(event);
				}.bind(this));
		}, this);
	if (Browser.ie) {
		var anchors = this.container.getElementsByTagName('a');
		for (j=0; j<anchors.length; j++) {
			anchors[j].addEvent('mouseenter', function(event) {
					event.stop();
					this.container.stop();
				}.bind(this));
			anchors[j].addEvent('mouseleave', function(event) {
					this.container.start();
				}.bind(this));
		}
	} else {
		var anchors = this.container.getElements('a');
		anchors.each(function(onea) {
				onea.addEvent('mouseenter', function(event) {
						event.stop();
						this.container.stop();
					}.bind(this));
				onea.addEvent('mouseleave', function(event) {
						this.container.start();
					}.bind(this));
			}, this);		
	}
	
	return(true);
}

pgAdverts.prototype.getData = function(src) {
	var rq = new Request.HTML({'url': src,
			'async': false,
			'onSuccess': function(tree, elements, html, js) {
					if (tree.length > 0) {
						if (Browser.ie) {
							if (Browser.version > 8) tree = elements;
							var newObj = new Object({
									'imagepath': '',
									'imgtitle': '',
									'description': '',
									'targetlink': ''
								});
							var txtval = '', item = null;
							for (var i=0; i < tree.length; i++) {
								if (Browser.version > 8) {
									item = tree[i];
								} else {
									item = tree.item(i);
								}
								if (item.nodeType == 3) {
									txtval = item.nodeValue;
								} else {
									if (typeOf(item) == 'element') {
										/*
										if (item.tagName.toLowerCase() == 'headeradverts') {
											tree = item.getElementsByTagName('hAdvert');
											if (tree.length > 0) i=-1;
										}
										*/
										if (Browser.version > 8) txtval = item.textContent;
										switch(item.tagName.toLowerCase()) {
											case 'imagepath':
											case '/imagepath':
												newObj.imagepath = txtval;
												txtval = '';
												break;
											case 'imgtitle':
											case '/imgtitle':
												newObj.imgtitle = txtval;
												txtval = '';
												break;
											case 'description':
											case '/description':
												newObj.description = txtval;
												txtval = '';
												break;
											case 'targetlink':
											case '/targetlink':
												newObj.targetlink = txtval;
												txtval = '';
												break;
											case 'hadvert':
											case '/hadvert':
												if (newObj.imagepath.length > 0) this.adverts.include(newObj);
												newObj = new Object({
														'imagepath': '',
														'imgtitle': '',
														'description': '',
														'targetlink': ''
													});
												txtval = '';
												break;
											case 'headeradverts':
											default:
										}//switch
									} //element
								}//else nodetype == 3
							}//for
						} else {
							//other than IE
							var newObj = new Object({});
							elements.each(function(elem) {
									if (elem.nodeName.toLowerCase() == 'hadvert') {
										if (newObj) {
											if (newObj.imagepath) {
												if (newObj.imagepath.length>0) this.adverts.include(newObj);
											}
										}
										newObj = new Object({});
									}
									if (!['headeradverts','hadvert'].contains(elem.nodeName.toLowerCase())) newObj[elem.nodeName.toLowerCase()] = elem.innerHTML;
								}, this);
							if (newObj) {
								if (newObj.imagepath) {
									if (newObj.imagepath.length>0) this.adverts.include(newObj);
								}
							}
						}//IE

					}
				}.bind(this)//success
		});
	rq.setHeader('Content-type','application/xml');
	rq.setHeader('Content-charset','UTF-8');
	rq.get({'data': null});	
} //end of get xml data


window.addEvent('domready', function() {
	var pgDemo = new pgAdverts({
			'containerId': 'pgAdvertContainer', //id html div do zaimplementowania zmieniaczki
			'className': 'demo2',	//css klasa, której wybór decyduje o wygl&#353;dzie - patrz hAdvertiser.css
			'animationType': 'partial', //typ animacji - uzywany z klas&#353; demo2
			'delay': 12800 // opoznienie uruchamiania się kolejnego slajdu
		});
});
/**/
//]}> 
