Object.extend(Prototype.Browser, {
    IE6:     Prototype.Browser.IE && (typeof window.XMLHttpRequest == "undefined"),
    IE7:     Prototype.Browser.IE && (typeof window.XMLHttpRequest == "object")
});


var ImageSlide = Class.create({
	
	//currently active item, count from 0
	items: [],
	currentItem: 0,
	navigation: [],
	pe: false,
	inProgress: false,
	
	
	/**
	 * Initialize class
	 *
	 * @container: html-parent-container, usually with class fce-imageslide
	 */
	initialize: function(container) {
		this.container  = container;
		this.height = 0;
		this.items = this.container.select('ul.images>li');
		this.itemCount = this.items.size();
		
		this.items.each(function (item, index) {
			imgTag = item.select('img').first();
			//preload all images
			preloadImage = new Image();
			preloadImage.src = imgTag.readAttribute('src');
			
			//search for image with greatest height
			if (Prototype.Browser.IE) {
				regExp = new RegExp(/\<img.*height[^\d]+(\d+)/i);
				match = regExp.exec(item.innerHTML);
				tempHeight = match[1];
			} else {
				tempHeight = imgTag.readAttribute('height');
			}
			this.height = (tempHeight > this.height) ? tempHeight : this.height;
			
			//display first image
			if (index == this.currentItem) {
				item.show();
			}
		}.bind(this));
		//FG, this is a hotfix, please change 
		if (this.container.hasClassName('imageslider')) {
			this.container.setStyle({height: this.height + 'px'});
		}

		if (this.itemCount > 1) {
			this.navigation = this.container.select('ul.navigation>li');
			this.navigation.each(function (item, index) {
				item.observe('click', function(event) {
					elementClicked = event.element().tagName.toLowerCase() == 'li' ? event.element() : event.element().up('li');
					this.currentItem = this.navigation.indexOf(elementClicked);
					this.pe.stop();
					this.showItem(this.currentItem);
				}.bindAsEventListener(this));
			}.bind(this));
			
			//startPeriodicalExecuter
			this.pe = new PeriodicalExecuter(function(pe) {
				this.nextItem();
			}.bind(this), 5);
		}
	},
	
	nextItem: function () {
		this.items[this.currentItem].fade();
		this.currentItem = (this.currentItem + 1) % this.itemCount;
		this.items[this.currentItem].appear();
		
		if (this.navigation.size() > 0) {
			this.navigation.invoke('removeClassName', 'act');
			this.navigation[this.currentItem].addClassName('act');
		}
	},
	
	showItem: function (itemPos) {
		inProgress = false; 
		this.items.each(function (item) {
			if (item.inProgress) {
				inProgress = true;
			}
		});
		
		if (! inProgress) {
			this.items.each(function (item, index) {
				item.inProgress = true;
				if (index == itemPos) {
					item.appear({
						afterFinish: function() {
							item.inProgress = false;
						}.bind(item)
					});
				} else {
					item.fade({
						afterFinish: function() {
							item.inProgress = false;
						}.bind(item)
					});
				}
			}.bind(this));
			
			if (this.navigation.size() > 0) {
				this.navigation.invoke('removeClassName', 'act');
				this.navigation[itemPos].addClassName('act');
			}
		}
	}
});


var HdProductsDetails = Class.create({

	container: null,
	productlist: null,
	
	/**
	 * Initialize class
	 *
	 * @container: html-parent-container, usually with class fce-imageslide
	 */
	initialize: function(container) {
		this.container = container;
		this.items = container.select('ul.fifty-fifty');
		this.productlist = container.up('div').select('div.products table>tbody>tr');
		
		this.height = 402;
		this.productdetails = container.down('div.product-details');
		this.checkHeight(0);
		this.showFblink(0);
		
		this.productlist.each( function (item) {
			item.observe('click', function (event) {
				//remove all other acts
				elementClicked = event.element().tagName.toLowerCase() == 'tr' ? event.element() : event.element().up('tr');
				this.productlist.invoke('removeClassName', 'act');
				elementClicked.addClassName('act');
				this.currentItem = this.productlist.indexOf(elementClicked);
				
				this.showItem(this.currentItem);
				
			}.bindAsEventListener(this));			
			
			if (Prototype.Browser.IE6) {
				item.observe('mouseover', function(event) {
					this.addClassName('over');
				}.bindAsEventListener(item));
				item.observe('mouseout', function(event) {
					this.removeClassName('over');
				}.bindAsEventListener(item));
			}
		}.bind(this));
	},
	
	checkHeight: function (itemPos) {
		var item = this.items[itemPos];
		var itemHeight = item.getHeight();
		if (itemHeight > this.height) {
			this.container.setStyle({
				height: itemHeight+36+'px'
			});
		} else {
			this.container.setStyle({
				height: this.height+'px' //+35+'px'
			});
		}
	},
	
	showItem: function (itemPos) {
		this.checkHeight(itemPos);
		this.showFblink(itemPos);
		inProgress = false; 
		this.items.each(function (item) {
			if (item.inProgress) {
				inProgress = true;
			}
		});
		
		if (! inProgress) {
			this.items.each(function (item, index) {
				item.inProgress = true;
				if (index == itemPos) {
					item.appear({
						duration: 0.4,
						afterFinish: function() {
							item.inProgress = false;
						}.bind(item)
					});
				} else {
					item.fade({
						duration: 0.4,
						afterFinish: function() {
							item.inProgress = false;
						}.bind(item)
					});
				}
			}.bind(this));
			
			if (this.productlist.size() > 0) {
				this.productlist.invoke('removeClassName', 'act');
				this.productlist[itemPos].addClassName('act');
			}
		}
	},
	
	showFblink: function (itemPos) {
		this.items.each(function (item, index) {
			fbLinkItem = item.select('span.fblikelink').first();
			fbHTML = fbLinkItem.innerHTML;
			if (index == itemPos) {
				if(fbHTML.substr(0,4) == '<!--') {
					newHTML = fbHTML.substring(4, fbHTML.length-3);
					fbLinkItem.update(newHTML);
					fbLinkItem.show();
				}
			} else {
				if(fbHTML.substr(0,4) != '<!--') {
					newHTML = '<!--'+fbHTML+'-->';
					fbLinkItem.update(newHTML);
					fbLinkItem.hide();
				}
			}
		});
	}
});


function huenersdorff_showyear(id) {
	if($('fce-event-'+id)) {
		$$('.fce-event').each(function (item) {
			item.hide();
		});
		$('fce-event-'+id).show();
	} else {
		return false;
	}
}


var HdLocations = Class.create({
	container: null,
	imageContainer: null,
	statedivs: null,
	stateselect: null,
	
	/**
	 * Initialize class
	 *
	 * @container: html-parent-container, usually with class fce-imageslide
	 */
	initialize: function(container) {
		this.container = container;
		this.imageContainer = container.select('li.fce-locations-image').first();
		this.statedivsContainer = container.select('div.fce-location-states').first();
		this.statedivs = container.select('div.fce-location-states .fce-location-state');
			
		this.stateselect = container.select('.fce-locations-stateselect select').first();
		this.stateselect.observe('change', function (event) {
			this.stateselectEvent(event);
		}.bindAsEventListener(this));
		this.stateselectTrigger = container.select('.fce-locations-stateselect a').first();
		this.stateselectTrigger.observe('click', function (event) {
			this.stateselectEvent(event);
		}.bindAsEventListener(this));
	},
	
	stateselectEvent: function(event) {
		selectedState = $F(this.stateselect);
		this.showState(selectedState);
	},
	
	showState: function(i) {
		this.statedivs.invoke('hide');
		this.statedivsContainer.select('.fce-location-state-'+i).first().show();
		this.imageContainer.setStyle({
			backgroundPosition: '0px -'+(i*440)+'px'
		});
	}
});

function onXHRSuccess() {
	var ibox = $('ibox');
	if(!ibox) {
		return;
	}
	
	var tablinks = $$('.tx-hdproducts-languagetabs li a.tx-hdproducts-languagetab');
	if(!tablinks) {
		return;
	}
	
	var shoplinkContainers = $$('.tx-hdproducts-shoplinks-tabbed');
	if(!shoplinkContainers){
		return;
	}

	tablinks.each(function(tablink) {
		tablink.observe('click', function(event) {
			var index = tablinks.indexOf(tablink);
			var containerId = tablink.readAttribute('rel');
			var container = $(containerId);
			tablinks.invoke('removeClassName', 'active');
			tablink.addClassName('active');
			shoplinkContainers.invoke('hide');
			container.show();
		});
	});
}


document.observe('dom:loaded', function(event){
	$$('.imageslider').each(function (item) {
		new ImageSlide(item);
	});
	
	$$('a.branch-menu').each(function(item) {
    	item.observe('click', function() {
    		$('branches-dropdown').toggle();
    	});
    });
	
	$$('#product-details').each( function(item) {
		new HdProductsDetails(item);
	});
	
	$$('#fce-locations').each( function(item) {
		new HdLocations(item);
	});
});

