var ShoppingCart = new Class({
	
    options: {
		active: false
    },
	
    initialize: function(eElement) {
        this.eElement = eElement;
		
		this.eExtension = this.eElement.getElement('DIV.shoppingcart_extension');
		
		this.eOpenCloseShoppingCart = this.eElement.getElementById('shoppingcart_openshoppingcart');
		this.eOpenCloseShoppingCart.addEvent('click', this.eventOpenCloseShoppingCart.bind(this));
		
		//this.eElement.addEvent('mouseover', this.eventMouseOver.bind(this));
		//this.eElement.addEvent('mouseout', this.eventMouseOut.bind(this));
		
		this.eNumOfItems = this.eElement.getElementById('shoppingcart_numofitems');
		this.eAmauntsTotal = this.eElement.getElementById('shoppingcart_amauntstotal');
		this.eNoItems = this.eElement.getElementById('shoppingcart_noitems');
		
		this.eCurrency = this.eElement.getElementById('selected_currency');
		this.eCurrency.addEvent('change', this.eventCurrencyChange.bind(this));
		this.eventCurrencyChange();
		
		this.countNumOfItems();
		this.countAmauntsTotal();
    },
	
	countNumOfItems: function() {
		var elements = this.eElement.getElements('DIV.shopping_cart_advanced UL.items LI.item');
		
		if(elements.length==0) {
			this.eNumOfItems.set('text', '');
			this.eNumOfItems.setStyle('display', 'none');
			this.eAmauntsTotal.setStyle('display', 'none');
			this.eNoItems.setStyle('display', 'block');
		} else {
			this.eNumOfItems.set('text', shoppingcart_numofitems + ' ' + elements.length);
			this.eNumOfItems.setStyle('display', 'block');
			this.eAmauntsTotal.setStyle('display', 'block');
			this.eNoItems.setStyle('display', 'none');
		}
	},
	
	countAmauntsTotal: function() {
		var value = 0;
		var tCurrencyValue = this.eCurrency.getSelected().get('value');
		var tCurrencyText = this.eCurrency.getSelected().get('text');
		
		var elements = this.eElement.getElements('DIV.shopping_cart_advanced UL.items LI.item DIV.product_price DIV.product_price_'+tCurrencyValue+' SPAN.price');
		if(elements.length>0) {
			elements.each(function(element, index) {
				value += parseFloat(element.get('text'));
			});
		
			this.eAmauntsTotal.set('text', round(value) + ' ' + tCurrencyText);
		} else {
			this.eAmauntsTotal.set('text', '');
		}
	},
	
	eventOpenCloseShoppingCart: function() {
		if(this.options.active) {
			this.eElement.removeClass('shoppingcart_extended');
			this.eExtension.setStyle('display', 'none');
			
			$('shoppingcart_openshoppingcart_openlabel').setStyle('display', 'block');
			$('shoppingcart_openshoppingcart_closelabel').setStyle('display', 'none');
			
			this.options.active = false;
		} else {
			var elements = this.eElement.getElements('DIV.shopping_cart_advanced UL.items LI.item');
			if(elements.length>0) {
				this.eElement.addClass('shoppingcart_extended');
				this.eExtension.setStyle('display', 'block');
				
				$('shoppingcart_openshoppingcart_openlabel').setStyle('display', 'none');
			$('shoppingcart_openshoppingcart_closelabel').setStyle('display', 'block');
				
				this.options.active = true;
			}
		}
    },
	
	eventOpenShoppingCart: function() {
		var elements = this.eElement.getElements('DIV.shopping_cart_advanced UL.items LI.item');
		
		if(elements.length>0) {
			this.eElement.addClass('shoppingcart_extended');
			this.eExtension.setStyle('display', 'block');
		}
    },
	
	eventCloseShoppingCart: function() {
        this.eElement.removeClass('shoppingcart_extended');
		this.eExtension.setStyle('display', 'none');
    },
	
    eventMouseOver: function() {
        this.eventOpenShoppingCart();
    },
	
    eventMouseOut: function() {
        this.eventCloseShoppingCart();
	},
	
	eventCurrencyChange: function() {
		var tCurrencyValue = this.eCurrency.value;
		
		var elements = this.eElement.getElements('DIV.shopping_cart_advanced UL.items LI.item DIV.product_price DIV');
		elements.each(function(element, index) {
			if(element.hasClass('product_price_' + tCurrencyValue)) {
				element.setStyle('display', 'block');
			} else {
				element.setStyle('display', 'none');
			}
		});
		
		this.countAmauntsTotal();
	}
	
});

var Tabs = new Class({
	
	options: {
		active: 0
	},
	
	initialize: function(eElement) {
		this.eTabsElement = eElement;
		this.aTabs = new Array();
		
        var tTabs = this.eTabsElement.getElements('LI.tab');
        for(var i=0; i<tTabs.length; i++)
            this.aTabs[i] = new Tab(tTabs[i], i, this);
		
		this.eventShowTab(0);
    },
	
	eventShowTab: function(index) {
		this.eventHideTabs();
		
		this.options.active = index;
		this.aTabs[this.options.active].eventShowTab();
	},
	
	eventHideTabs: function() {
		for(i=0; i<this.aTabs.length; i++) 
			this.aTabs[i].eventHideTab();
	}
	
});

var Tab = new Class({
	
    options: {
		active: 0,
		index: 0
    },
	
    initialize: function(eElement, index, parent) {
		this.parent = parent;
        this.options.index = index;
		
		this.eTabElement = eElement;
		this.eTabCaptionElement = eElement.getElement('DIV.tab_caption');
		this.eTabContentElement = eElement.getElement('DIV.tab_content');
		
		this.eTabCaptionElement.addEvent('click', this.eventChangeTab.bind(this));
		
		if(this.eTabCaptionElement.get('id')=='tab_caption_item_comments')
			var tTabComments = new TabComments(this.eTabCaptionElement, this.eTabContentElement, this);
		this.eTabCaptionElement.setStyle('width', this.eTabCaptionElement.get('text').length*7+'px');
		
		this.eTabCaptionSize = this.eTabCaptionElement.getSize();
		this.eTabContentSize = this.eTabContentElement.getSize();
		
		var tTabCaptionLeftPosition = 0;
		for(i=0; i<this.options.index; i++) 
			tTabCaptionLeftPosition += this.parent.aTabs[i].eTabCaptionSize.x;
		this.eTabCaptionElement.setStyle('left', tTabCaptionLeftPosition+index + 'px');
    },
	
	eventChangeTab: function() {
		this.parent.eventShowTab(this.options.index);
	},
	
	eventShowTab: function() {
		this.options.active = 1;
		this.eTabCaptionElement.addClass('active_tab_caption');
		this.eTabContentElement.addClass('active_tab_content');
	},
	
	eventHideTab: function() {
		this.options.active = 0;
		this.eTabCaptionElement.removeClass('active_tab_caption');
		this.eTabContentElement.removeClass('active_tab_content');
	}
	
});

var TabComments = new Class({
	
	initialize: function(eCaption, eContent, parent) {
		if(eContent.getElement('DIV#comments_link_div A')) {
			var tText = eContent.getElement('DIV#comments_link_div A').get('text');
			var numOfComments = tText.substring(tText.indexOf('(')+1, tText.lastIndexOf(')'));
			var tText = eContent.getElement('DIV#mark_div').get('text');
			var averageCommentMark = tText.substring(tText.indexOf('(')+1, tText.lastIndexOf(')'));
			
			eCaption.set('text', eCaption.get('text') + ' - ' + numOfComments + ' ' + '(' + $('label_mark').get('value') + ' ' + averageCommentMark + ')');
		} else {
			eCaption.set('text', $('label_postyourcomment').get('value'));
			eCaption.removeEvents('click');
			eCaption.addEvent('click', function() {
				var url = window.location.href;
				var baseURL = url.substring(0, url.indexOf('/', 14));

				
				window.location = baseURL + '/user/' + eContent.getElement('DIV#post_div A').get('href');
			});
		}
	}
	
});

var Images = new Class({
	
	options: {
		active: 0
	},
	
	initialize: function(eImages, eThumbs) {
		this.eImagesElement = eImages;
		this.eThumbsElement = eThumbs;
		this.aImages = new Array();
		
        var tImages = this.eImagesElement.getElements('DIV.product_image');
		var tThumbs = this.eThumbsElement.getElements('DIV.product_thumb');
        for(var i=0; i<tImages.length; i++)
            this.aImages[i] = new ImagesImage(tImages[i], tThumbs[i], i, this);
		
		this.eventShowImage(0);
    },
	
	eventShowImage: function(index) {
		this.eventHideImages();
		
		this.options.active = index;
		this.aImages[this.options.active].eventShowImage();
	},
	
	eventHideImages: function() {
		for(i=0; i<this.aImages.length; i++) 
			this.aImages[i].eventHideImage();
	}
	
});

var ImagesImage = new Class({
	
	options: {
		active: 0,
		index: 0
    },
	
    initialize: function(eImage, eThumb, index, parent) {
		this.parent = parent;
        this.options.index = index;
		
		if(eImage) {
			this.eImageElement = eImage;
			this.eImageElementSize = this.eImageElement.getSize();
		}

		if(eThumb) {
			this.eThumbElement = eThumb;
			this.eThumbElementSize = this.eThumbElement.getSize();
			
			this.eThumbElement.addEvent('click', this.eventChangeImage.bind(this));
		}
    },
	
	eventChangeImage: function() {
		this.parent.eventShowImage(this.options.index);
	},
	
	eventShowImage: function() {
		this.options.active = 1;
		this.eThumbElement.addClass('active_product_thumb');
		this.eImageElement.addClass('active_product_image');
	},
	
	eventHideImage: function() {
		this.options.active = 0;
		this.eThumbElement.removeClass('active_product_thumb');
		this.eImageElement.removeClass('active_product_image');
	}
	
});

var Rotation = new Class({
	
	options: {
		active: 0,
		speed: 3000
    },
	
	initialize: function(sItemsElement, speed) {
		this.options.speed = speed;
		this.aItems = new Array();
		
		var tItems = $$(sItemsElement);
		for(var i=0; i<tItems.length; i++) {
			this.aItems[i] = tItems[i];
			if(i>0) this.aItems[i].set('opacity',0);
		}
		
		window.addEvent('load', this.eventLoad.bind(this));
    },
	
	showItem: function() {
		this.aItems[this.options.active].fade('out');
		this.aItems[this.options.active = this.options.active < this.aItems.length - 1 ? this.options.active+1 : 0].fade('in');
	},
	
	eventLoad: function() {
		this.interval = this.showItem.bind(this).periodical(this.options.speed);
	}
	
});

var RotationSteps = new Class({
	
	options: {},
	
	initialize: function(oRotation, eParent) {
		this.oRotation = oRotation;
		this.eParent = eParent;
		
		this.oRotation.showItem = this.showItem.bind(this);
		this.aSteps = new Array();
		
		var tStepsElement = new Element('div', {id: eParent.get('id') + '_' + 'steps'});
		tStepsElement.addClass('rotation_steps');
		this.eParent.adopt(tStepsElement);
		for(var i=0; i<this.oRotation.aItems.length; i++) {
			var tStepElement = new Element('div', {id: eParent.get('id') + '_' + 'steps' + '_' + 'step' + i});
			tStepElement.addClass('rotation_step');
			tStepElement.addEvent('click', this.eventClick.bind(this, i));
			tStepsElement.adopt(tStepElement);
			
			this.aSteps[i] = tStepElement;
		}
		
		this.aSteps[0].addClass('active');
    },
	
	activateStep: function(index) {
		for(var i=0; i<this.aSteps.length; i++) 
			this.aSteps[i].removeClass('active');
		
		this.aSteps[index].addClass('active');
	},
	
	showItem: function() {
		this.oRotation.aItems[this.oRotation.options.active].fade('out');
		this.oRotation.aItems[this.oRotation.options.active = this.oRotation.options.active < this.oRotation.aItems.length - 1 ? this.oRotation.options.active+1 : 0].fade('in');		
		
		this.activateStep(this.oRotation.options.active);
	},
	
	eventClick: function(index) {
		clearInterval(this.oRotation.interval);
		this.oRotation.interval = this.oRotation.showItem.bind(this.oRotation).periodical(this.oRotation.options.speed);
		
		this.oRotation.aItems[this.oRotation.options.active].fade('out');
		this.oRotation.aItems[this.oRotation.options.active = index].fade('in');
		
		this.activateStep(this.oRotation.options.active);
	}
	
});

window.addEvent('domready', function() {
	
	var elements = $$('DIV.categories_menu UL.items LI.item');
	elements.each(function(element, index) {
		element.addEvent('click', function() {
			if(element.hasClass('active')) {
				element.removeClass('active');
			} else {
				element.addClass('active');
			}
		});
    });
	
	if($('category-id').value!=0) {
		if($('category' + $('category-id').value)) {
			$('category' + $('category-id').value).addClass('active');
		
			var tCategoryParent = $('category' + $('category-id').value).getParent('LI.item');
			if(tCategoryParent) tCategoryParent.addClass('active');
		}
	}
	
	if($('product_rotation')) {
		var tProductRotation = new Rotation('DIV#product_rotation DIV.products DIV.product', 2000);
	}
	
	if($('f_sorting_id')) {
		$('f_sorting_id').addEvent('change', function() {
			$('form_item_sorting').action = $('friendlyurlgenerator').value;
			$('form_item_sorting').submit();
		});
	}
	
	var elements = $$('DIV.shoppingcart');
	elements.each(function(element, index) {
		var tShoppingCart = new ShoppingCart(element);
    });
	
	if($('product')) {
		var size = $('product').getFirst('DIV.product_info').getSize();
		$$('DIV.layout_middle_column_second').setStyle('margin-top', size.y + 'px');
		
		var elements = $$('DIV#product UL.tabs');
		elements.each(function(element, index) {
			var tProductTabs = new Tabs(element);
		});
		
		var images = $('product').getFirst('DIV.product_images');
		var thumbs = $('product').getFirst('DIV.product_thumbs');
		var tProductImages = new Images(images, thumbs);
	}
	
	if($('front')) {
		var tFrontRotationSize = $('front_rotation').getSize();
		var tSliderSize = $('slider').getSize();
		$$('DIV.layout_middle_column_second').setStyle('margin-top', tFrontRotationSize.y+20+tSliderSize.y+20 + 'px');
		
		var tFrontRotation = new Rotation('DIV#front_rotation DIV.items DIV.item', 5000);
		var tRotationSteps = new RotationSteps(tFrontRotation, $('front_rotation'));
		
		var numOfProductsInSlider = $$('DIV#slider_items DIV.products DIV.product').length;
		$('slider_items').getElement('DIV.products').setStyle('width', numOfProductsInSlider*(380+20) + 'px');
		var tProductsSlider = new ScrollBar(
			$('slider_items'), 
			$('bar'), 
			$('knob'), 
			{	
				steps: numOfProductsInSlider*(380+20)-782,
				offset: -14
			}
		);
		tProductsSlider.set(0);
	}
	
});

window.addEvent('load', function() {
});

/* CATEGORIES_MENU */
function categoriesMenuItemShow(item) {
}

function categoriesMenuItemHide(item) {
}

function categoriesMenuItemClick(item) {
}

function categoriesMenuItemsShow(items) {
}

function categoriesMenuItemsHide(item) {
}

function categoriesMenuItemsClick(item) {
}
/* CATEGORIES_MENU */

function round(value) {
	var val = parseFloat(value);
	
	if (!isNaN(val)) {
		var n = Math.round(val*100+((val*1000)%10>4?1:0))/100+"";
		if (n.indexOf(".")==-1) {
			n += ".00";
		}
		while(n.length-n.indexOf(".")<3) {
			n += "0";
		}
		return n;
	}
}
