var product;
var Product;
var Product = new Class({

	current_view: null,
	element_list: null,
	
	//initialization
	initialize: function() {
		var _this = this;
		
		//	Hidde All elements
		var big_elements = $(document.body).getElements('*[rel=product_info]');
		_this.element_list = new Array(big_elements.length);
		var i =0;
		big_elements.each(function(element) {
/* 			element.setStyle('display', 'none'); */
			_this.element_list[i++] = element.get('id');
		});

		//	Get Element to display
		var anchor = _this.getAnchor();
		if(anchor != null){
			if(_this.element_list.indexOf(anchor) != -1)
				var element_to_display = $(anchor);
			else
				var element_to_display = big_elements[0];
		}
		else
		var element_to_display = big_elements[0];

		//	Display element
		var element_to_display = (anchor != null) ? $(anchor) : big_elements[0];
		_this.current_view = element_to_display.get('id');

		element_to_display.setStyle('display', 'block');
		
		//	Bind links
		var elements = $(document.body).getElements('*[rel=product_more_info]');
		elements.each(function(element) {
			element.addEvent('click', function(e) {
				e.stop();
				//	Start the process
				_this.element_click(element);
			});
		});

	},
	
	element_click: function(element){
		_this = this;
		
		//	Hide current item
		if(_this.current_view !== null)
			$(_this.current_view).setStyle('display', 'none');
		
		
		//	Get new item
		var anchor = null;
		var link = element.get('href').toString();
		if (link.match('#')) {
			anchor = link.split('#')[1];
		}
		
		if(anchor != null){
			//	Set as current item
			_this.current_view = anchor;
			
			//	Show current item
			$(anchor).setStyle('display', 'block');
			
			//	Update url
			_this.changeAnchor(anchor);
		}
	},

	changeAnchor: function(anchor){
		// Si l'url possde dj une ancre
		if (window.location.href.match(new RegExp('#.*$'))){
			window.location = window.location.href.replace(
				new RegExp('#.*$'),
				'#' + anchor
			);
		}
		// Sinon, il suffit de l'ajouter  la fin
		else{
			window.location = window.location.href + "#" + anchor
		}
		
		return window.location;
	},
	
	
	getAnchor: function(){
		var myFile = document.location.toString();
		var myAnchor = null;
		if (myFile.match('#')) {
			myAnchor = myFile.split('#')[1];
		}
		
		return myAnchor;
	},

	end: function(){
	
	}	
});


var product_ddl;
var Product_ddl;
Product_ddl = new Class({
	initialize: function(){
		var _this = this;
	
		// Collect elements 
		var elements = $(document.body).getElements('*[rel=paypal_buy_now]');

		elements.each(function(element) {

		var target = element.getElements('select');
			target.addEvent('change', function(e) {
				e.stop();
				
				//	Submit the form
				if(target.get('value') != 0)
				element.submit(); 
			});

		});
	},
	end: function(){}
});