/**
  * This is The Fancy-Tools Libary proving some useful js and AJAX-Tools
  * @version 0.1 - 11.12.2006
  * @author Jan D.S. Wischweh written for www.martens-cm.de
  * requirements prototype.js - The Prototype Ajax Framework
  * 
  * Tools include:
  * - an Ajax-Pager
*/  



/**
  * This Class is an implementation of an abstract Pager.
  * The most simple way to use it is to implemet getItemTemplage(itemnr) make Yourself an Instance of your new Pager-Class and 
  * put <script type="text/javascript>YourPager.renderPager(0)</script> in your HTML where you want the pager to appear.
  *
  * There are also some other Template-metods which you can override if you want to for customizing your Pager. 
  * Methods that may be overriden for customization: getPageTemplate(pagenr), getNaviTemplate(pagenr) and even getWholePager(pagenr)
  * Methods that _MUST_ be implemented by you: getItemTemplate(itemnr);
  */


/*
 * The Constructor
 * @param itemsperpage - How many Items shall ther be per page?
 * @param totalitems - Number of Items being Paged in total
 * @param domid - The Id-Name the Pager will have in the DOM of your XHTML-Page
 */
var FANCYTOOLS_pagers=new Array();
function abstractPager(itemsperpage,totalitems,domid){
  this.itemsperpage=itemsperpage;
  this.totalitems=totalitems;
  this.DOM_Id=domid;
  this.lastpagenr=-1;
  FANCYTOOLS_pagers[this.DOM_Id]=this;
  
}

  /** 
    * Template-Method  
	*
	*  This method will be called by the Pagerframework to get the (X)HTML-Code for a certain Pager Items.
    *  This method is abstract and _NEEDS_ to be implemented by you if you want to use a pager.
	* 
	*  @param itemnr - The Number of the item for wich the Template is for. This is passed to you from the Pager framework to use in your Templating script
	*  @return - Returns a String with the XHTML-Code representing the Pager-Item
	*/
abstractPager.prototype.getItemTemplate = function(itemnr) {
    var template='';
    return template;
  }
  
  /** 
    * Template-Method  
    * 
    *  This method will be called by the Pagerframework to get the (X)HTML-Code for a certain Pager Page.
	*  It may be overriden by you for customization. Hower it should have a loop to call getItemTemplate(itemnr) somewhere.
	*
	*  @param pagenr - The Number of the Page currently displayed. This is passed to you from the Pager framework to use in your Templating script
	*  @return - Returns a String with the XHTML-Code representing the current Pager's Page content.
	*  @see getItemTemplate
	*/
abstractPager.prototype.getPageTemplate = function(pagenr) {
    var template='<div class="pager_page"><div id="pageforeground"';
   //if (this.lastpagenr>=0) template+=' style="display: none"'
   template+='>';
    
    
    template+=this.getItemTemplate(pagenr)+'</div>';
    
	
	/*template+='<div id="pagebackground">';
	
	if (this.lastpagenr>=0)
		template+=this.getItemTemplate(this.lastpagenr)+'</div>'; */
	this.lastpagenr=pagenr;
	template+='</div>'; 
    return template;
  }
  
  /** 
    * Template-Method  
    * 
    *  This method may be overriden by you for customization. Hower it should render some javascriptCode that calls updatePager(pagenr) 
	*  so that there is something for the User to click on and use this pager.
	* 
	*  @param pagenr - The Number of the Page currently displayed. This is passed to you from the Pager framework to use in your Templating script
	*  @return - Returns a String with the XHTML-Code representing the current Pager's Page content.
	*/
abstractPager.prototype.getNaviTemplate = function(pagenr) {
    var template='';
	//if (this.totalitems>this.itemsperpage) {
		/* Oldschool
		template+='<div class="pager_navi">';
		// Previous button
		template+='<a href="javascript:abstractPager.updatePager(\''+this.DOM_Id+'\','+(pagenr-1)+');"';
		if (pagenr<=0) template+=' class="hidden"';
		template+='>&lt;</a> ';
		
		// List of Pages
		//template+='<div class="pagelist">';
		for (var curPage=0; curPage<Math.ceil(this.totalitems/this.itemsperpage); curPage++) {
		 template+='<a href="javascript:abstractPager.updatePager(\''+this.DOM_Id+'\','+curPage+');"';
		 if (curPage==pagenr) template+=' class="current"';
		 template+='>'+(curPage+1)+'</a>';
		 if (curPage<Math.ceil(this.totalitems/this.itemsperpage)-1) template+=' | ';
		}
		//template+='</div>';
		
		// Next Button
		template+=' <a href="javascript:abstractPager.updatePager(\''+this.DOM_Id+'\','+(pagenr+1)+');"';
		if (pagenr+1>=Math.ceil(this.totalitems/this.itemsperpage)) template+=' class="hidden"';
		template+='>&gt;</a>';
		
		template+='</div>';
		*/
		
		
		template+='<div id="picnavi_series">';				
		template+='<div id="untertitel">'+this.items[pagenr].title[this.lang]+'</div>';
		
		// Previous button
		template+='<a href="javascript:abstractPager.updatePager(\''+this.DOM_Id+'\','+(pagenr-1+this.totalitems)%this.totalitems+');"';
		if (pagenr<=0) template+=' class="hidden"';
		template+='><img src="img/pic_navi_back.jpg" alt="zurueck" width="8px" height="9px" style="margin: 8px 14px 0px 14px; padding-top: 0px;"/></a>';
		template+=(pagenr+1)+"/"+this.totalitems;
		// Next Button
		template+=' <a href="javascript:abstractPager.updatePager(\''+this.DOM_Id+'\','+(pagenr+1)%this.totalitems+');"';
		if (pagenr+1>=Math.ceil(this.totalitems/this.itemsperpage)) template+=' class="hidden"';
		template+="><img src='img/pic_navi_next.jpg' alt='vor' width='8px' height='9px' style='margin: 8px 14px 0px 14px; padding-top: 0px;'/></a>";			
		template+="</div>";
		
	//}
	return template;
  }
  
  /** 
    * Template-Method  
    * 
    *  This method may be overriden by you for customization. Hower it should call getPageTemplate(pagenr) and getNaviTemplate(pagenr) somewhere.
	*  @param pagenr - The Number of the Page currently displayed. This is passed to you from the Pager framework to use in your Templating script
	*  @return - Returns a String with the XHTML-Code representing the current Pager's Page content.
	*  @see getItemTemplate
	*/
abstractPager.prototype.getWholePager = function(pagenr) {
    var template='';
	if (this.totalitems>0) {
		if (pagenr<0) pagenr=0;
		if (pagenr>=Math.ceil(this.totalitems/this.itemsperpage)) pagenr=Math.ceil(this.totalitems/this.itemsperpage)-1;
    
		template+=this.getPageTemplate(pagenr);
		template+=this.getNaviTemplate(pagenr);
		template+="";
	}
	return template;
  }

  /**
   * This Function renders the inital Pager Page. You can put it somwhere in your (X)HTML to playe the pager there.
   * @pagenr - The Page the Pager initialy starts with. Usualy "0". 
   */
abstractPager.prototype.renderPager = function(pagenr) {
    document.write('<div id="'+this.DOM_Id+'">'+this.getWholePager(pagenr)+'</div>');
  }
  /**
   * This Function is called to switch the Pager to another Page. It updates the Pagers (X)HTML within the DOM.
   * @pagenr - The Page you want to have displayed. 
   */
abstractPager.updatePager = function(domid, pagenr) {
    var thepager=FANCYTOOLS_pagers[domid];
	if (thepager.lastpagenr<0) {
	  // No blending Effects on first pic
	  Element.replace(domid,'<div id="'+thepager.DOM_Id+'">'+thepager.getWholePager(pagenr)+'</div>');
	} else {
	  Element.replace(domid,'<div id="'+thepager.DOM_Id+'">'+thepager.getWholePager(pagenr)+'</div>');
	  //Effect.Appear('pageforeground', {delay: 0.5, duration: 1.0});
	  //Effect.Fade('pagebackground', {duration: 1.5});
        }
    thepager.preload(pagenr+1);
    thepager.preload(pagenr-1);	
  }




abstractGalleryItem = function (type) {
	this.type=type;
	this.title = $H({
		de: '',
		en:''
	});
	this.text = $H({
		de: '',
		en: ''
	});
}
abstractGalleryItem.prototype.getHTML = function () {
	return '';
}



pictureItem = function(img) {
	this.type='picture';
	this.title = $H({
		de: '',
		en:''
	});
	this.text = $H({
		de: '',
		en: ''
	});
	this.img = img;
}

pictureItem.prototype = new abstractGalleryItem('picture');

pictureItem.prototype.getHTML = function (lang) {
	/* var html='<div class="item picture">';
	html+='<img src="'+ this.img +'" />';
	html+='<h3 class="title">Titel: '+this.title[lang]+'</h3>';
	html+='<span class="description">'+this.text[lang]+'</span>';
	html+='</div>';
	return html; */
	return '<div id="pic_series"><div class="wraptocenter"><span></span><img src="'+this.img+'" alt=""/></div></div>';
	
}
pictureItem.prototype.preload = function () {
	//lastPreloadImg = preloadImg;
	preloadImg = new Image();
	preloadImg.src=this.img;
}



textslideItem = function() {
	this.type='textslide';
	this.title = $H({
		de: '',
		en:''
	});
	this.text = $H({
		de: '',
		en: ''
	});
}

textslideItem.prototype = new abstractGalleryItem();

textslideItem.prototype.getHTML = function (lang) {
	var html='<div class="item textslide">';
	//html+='<h3>'+this.title[lang]+'</h3>';
	html+='<p>'+this.text[lang]+'</p>';
	
	html+='</div>';
	return html;
}
textslideItem.prototype.preload = function () {
}

/**
 * This implements the GalleryPgaer
 * @param domid - The domid which will contain this gal
 * @param lang - lang-code to use
 */
function xmlgalleryPager(domid, lang){
  this.itemsperpage=1;
  this.totalitems=1;
  this.DOM_Id=domid;
  FANCYTOOLS_pagers[this.DOM_Id]=this;
  this.lastpagenr=-1;
  this.lang=lang;
  //this.gallerysources=gallerysources;
  //this.gallerydescriptions=gallerydescriptions;
}

xmlgalleryPager.prototype=new abstractPager();

xmlgalleryPager.prototype.loadXML = function (path, file) {
	this.path=path;
	this.imgpath=this.path+file.split('.')[0]+'/images/';
	this.file=file;
	var param = {
	method: 'get',
  	onSuccess: function (response) {this.processXMLResponse(response);}.bind(this)
  	}
  	new Ajax.Request(path+file,param);
}
xmlgalleryPager.prototype.processXMLResponse = function (response) {
	myitems = response.responseXML.getElementsByTagName("item");
	this.totalitems=myitems.length;
	this.items = new Array(this.totalitems);
	for (i=0; i < myitems.length; i++) {
	    switch (myitems[i].getAttribute('type')) {
	   	case "picture":
	   		myitem = new pictureItem(this.imgpath+myitems[i].getElementsByTagName('img')[0].firstChild.data);
	   		/* $A(items[i].getElementsByTagName('title')).each( function (t) {
	   			item.title[t.getAttribute('lang')]=t.firstChild.data; 
	   		}); */
	   		$A(myitems[i].getElementsByTagName('description')).each( function (t) {
	   			if (t.firstChild!=null && t.firstChild!=undefined)
	   			myitem.title[t.getAttribute('lang')]=t.firstChild.data; 
	   			
	   		});
	   		this.items[i]=myitem;
	   		break;

	   	case "textslide":
	   		myitem = new textslideItem(i);
	   		$A(myitems[i].getElementsByTagName('title')).each( function (t) {
	   		    if (t.firstChild!=null && t.firstChild!=undefined)
				myitem.title[t.getAttribute('lang')]=t.firstChild.data; 
	   		});
	   		$A(myitems[i].getElementsByTagName('text')).each( function (t) {
	   		    if (t.firstChild!=null && t.firstChild!=undefined)
	   			myitem.text[t.getAttribute('lang')]=t.firstChild.data; 
	   		});
	   		this.items[i]=myitem;
	   		break;
	   }
	  
	   		
	}
	
	
	abstractPager.updatePager(this.DOM_Id, 0);
	
}



xmlgalleryPager.prototype.getItemTemplate= function (itemnr) {
  return this.items[itemnr].getHTML(this.lang);
}
xmlgalleryPager.prototype.preload = function (itemnr) {
  this.items[(itemnr+this.items.length) % this.items.length].preload();
}



gallerySwitcher = function(divid) {
	this.divid=divid;
}

gallerySwitcher.prototype.loadConf = function (xmlurl){
	this.xmlurl=xmlurl;
	var param = {
  	onSuccess: function (response) {this.processXMLResponse(response);}.bind(this),
  	method: 'get'
	}
  	new Ajax.Request(xmlurl,param);
}

gallerySwitcher.prototype.processXMLResponse = function (response) {
	var serien = response.responseXML.getElementsByTagName("serie");
	this.serien = new Array(serien.length);
	
	for (var i=0; i < serien.length; i++) {
		this.serien[i]={
			title: $H(),
			xml: ''
		}
		$A(serien[i].getElementsByTagName('title')).each(function (title) {
			this.serien[i].title[title.getAttribute('lang')]=title.firstChild.data;
		}.bind(this));
	
	    this.serien[i].xml=serien[i].getElementsByTagName('xmlfile')[0].firstChild.data;
	   
	}

	this.updateMenu();
	
}

gallerySwitcher.prototype.updateMenu = function () {
	
	var html='';//'<ul class="galswitcher">';
	for (var i=0; i < this.serien.length; i++) {
		html+='<a href="index.php?action=showserie&xml='+this.serien[i].xml+'">'+serien[i].titel['de']+'</a><br/>';
		//html+='<li><a href="javascript:mygal.loadXML(\'/fancygal/galleries/\',\''+this.serien[i].xml+'\');">'+this.serien[i].title['de']+'</a></li>';
	}
	//html+='</ul>';
	
	$(this.divid).innerHTML=html;
}

			

