/*$Id: product.js 3473 2010-03-11 07:26:32Z aliyani $*/
var ProductRank = new Class({
	options: {
		sendUrl: ''
	},
	initialize: function(elm, pid, options) {
		this.setOptions(options);
		this.elm = $(elm);
		this.pid = pid;

		this.status = 'idle';
		
		var cs,star;
		if(cs = Cookie.get('Rank_' + pid)) {
			this.status = 'ranked';
			star = this.getRankNumber(null);
			for(var i=0; i < cs.toInt(); i++) {
				star[i].addClass('rank-float-over');
			}	
		}
		
		var self = this;
		this.elm.getElements('div.rank-float').each(function(d){
			var n = self.getRankNumber(d);
			d.addEvent('mouseover', function(){
				if(self.status == 'ranked') return;
				for(var i=0; i<n.length; i++) {
					n[i].addClass('rank-float-over');
				}
			}).addEvent('mouseout', function(){
				if(self.status == 'ranked') return;
				for(var i=0; i<n.length; i++) {
					n[i].removeClass('rank-float-over');
				}
			}).addEvent('click', function(){
				self.sendRank(d);
			});
		});
	},
	sendRank: function(el) {
		if(this.status == 'ranked') return;
		var star = this.getRankNumber(el);
		TSF.post(this.options.sendUrl, 'pid=' + this.pid +'&rank=' + star.length, function(r, success) {
			if(!success || r == 'Error') return this.sendRank(el);
			for(var i=0; i < star.length; i++) {
				star[i].addClass('rank-float-over');
			}
			this.status = 'ranked';
			Cookie.set('Rank_' + this.pid, star.length);
		}.bind(this));
	},
	getRankNumber: function(el) {
		var r = [], n = this.elm.getElements('div');
		for(var i=0; i < n.length; i++) {
			if(n[i] != el) r.push(n[i]);
			else return [el].merge(r);
		}
		return r;
	}
});
ProductRank.implement(new Options);

var ProductTabs = new Class({
	options: {
		tabElement: '',
		container: ''
	},
	initialize: function(options) {
		this.setOptions(options)
		this.current = null;

		var self = this, fl = null;
		$(this.options.tabElement).getElements('li').each(function(li){
			if(!fl) fl = li;
			with(li) {
				addEvent('mouseover',function(){ li.addClass('current'); })
				addEvent('mouseout'	,function(){ if(li != self.current) li.removeClass('current'); })
				addEvent('click'	,function(){
					self.show(li);
					self.current = li;
				});
			}
		});
		this.show(fl);
	},

	show: function(li) {
		var ln = this.getNumber(li, this.options.tabElement);
		$(this.options.container).getChildren().each(function(div){
			var dn = this.getNumber(div, this.options.container);
			div.setStyle('display', (ln != dn) ? 'none':'');
		}, this);
		$(this.options.tabElement).getElements('li').removeClass('current');
		li.addClass('current');
	},

	getNumber: function(elm, parent) {
		var n = $(parent).getChildren();
		for(var i = 1; i <= n.length; i++) {
			if(n[i] == elm) return i;
		}
		return false;
	}
});
ProductTabs.implement(new Options);

var ProductGallery = new Class({
	initialize: function() {
		$$('.product-gallery a').each(function(a){
			a.addEvent('click', this.showPreview.bind(this, a.href, true));
			try{
				$(a.parentNode);
				a.parentNode.addEvent('click', this.showPreview.bind(this, a.href, true));
			}catch(e){ alert(e)};
			
		}, this);
		$(window).addEvent('keypress', function(ev){
			if (ev.code == 27) this.hidePreview();
		}.bind(this));
		this.shadow = new PageShadow();
		this.status = 'idle';
	},

	showPreview: function(ev, url) {
		ev.stop();
		this.shadow.showShadow();
		var img = new Image(), self = this;
		this.status = 'progress';
		img.onload = function() {
			if(self.status == 'progress') self.shadow.update('<img src="'+url+'" style="border: 1px solid #000" />');
		};
		img.onerror = function() {
			if(self.status == 'progress') self.shadow.update('<div class="msg error">خطا در بارگذاری تصویر</div>');
		}; 
		img.src = url;
	},
	
	hidePreview: function() {
		this.status = 'idle';
		this.shadow.hideShadow();
	}
});

var Comment = new Class({
	options: {
		checkUrl: '',
		action:''
	},

	initialize: function( options) {
		this.setOptions(options);
		this.form = $('product_comment').addEvent('submit', this.check.bind(this, null, true));
	},
	
	
	check: function(ev) {
		ev.stop();
		var FieldNames = new Array();
		
		FieldNames.push('input[name=lastname]');
		FieldNames.push('textarea[name=comment]');
		
		if( !TSF.FormValidate.checkAllFormFields( 'product_comment', FieldNames, this.options.errorElem, fill_required_fields ) ) return false;
		if( !TSF.FormValidate.validateFieldValue( 'product_comment', 'input[name=email]', 'mail', this.options.errorElem, invalid_email ) ) return false;
		
		TSF.post(this.options.checkUrl, this.form.toQueryString(), function(r){
			statusElem = $('product_comment_status');
			if(r == 'Done')
				statusElem.innerHTML = doneMsg;
			else{
				statusElem.innerHTML = errorMsg;
				statusElem.addClass('error');
			}
				
				this.form.style.display = 'none';
				statusElem.style.display = '';

		}.bind(this))
	}
});
Comment.implement(new Events, new Options);



function compareProduct() {
	if(!this.shadow) {
		this.shadow = new PageShadow();
	}
	var pSelect = productSelected().map(function(id) {
		return 'product_id[]=' + id;
	});
	if(pSelect.length < 2) {
		alert(twoItemMustBeSelect);
		return false;
	}
	this.shadow.showShadow();
	TSF.get(compareUrl + '&' + pSelect.join('&'), function(h){
		var tmp, width;
		tmp = new Element('div').setStyles({
			'position': 'absolute',
			'left': '0px',
			'top': '0px',
			'visibility': 'hidden',
			'zIndex': 10000
		}).inject(document.body, 'bottom');
		tmp.innerHTML = h;
		
		var width = tmp.offsetWidth;
		tmp.remove();
		
		with(this.shadow) {
			preview.setStyles({'width': width + 'px'});
			update(h);
		}
	}.bind(this));
}

function buyProduct() {
	var pSelect = productSelected();
	if(!pSelect) {
		alert(NoItemSelected);
		return false;
	}
	for(var i=0; i<pSelect.length; i++) {
		for(var j=0; j<dListProducts.length; j++) if(dListProducts[j].id == pSelect[i])
		TSF.basket.add(
			dListProducts[j].id,
			dListProducts[j].name,
			dListProducts[j].price,
			dListProducts[j].quantity
		);
	}
}

function productSelected() {
	return $$('.check-box-checked input[type=hidden]').map(function(el){
		return el.value;
	});
}


function setProductListHeight( id )
{
	var photo_max = 0;
	var table_max = 0;
	var td_name_max = 0;
	
	$('product-list-' + id).getElements('img.product-list-photo').each(function(r){
		photo_max = photo_max < r.height ? r.height : photo_max;
	});
	
	$('product-list-' + id).getElements('td.product-list-photo').each(function(r){
		r.setStyle('height', photo_max+5);
	});
	
	
	$('product-list-' + id).getElements('td.product-name').each(function(r){
		td_name_max = td_name_max < r.offsetHeight ? r.offsetHeight : td_name_max;
	});
	
	$('product-list-' + id).getElements('td.product-name').each(function(r){
		r.setStyle('height', td_name_max);

	});
	
	$('product-list-' + id).getElements('table#product-item').each(function(r){
		table_max = table_max < r.offsetHeight ? r.offsetHeight : table_max;
	});
	
	$('product-list-' + id).getElements('table#product-item').each(function(r){
		r.setStyle('height', table_max);

	});
}
