var ie6 = false;

function ge(id) {
	var el = null;
	if (el = document.getElementById(id)) {
		return el;
	}
	else {
		return null;
	}
}

function gt(s, t) {
	var els = [], el = null;
	if (el = ge(s)) {
		if (els = el.getElementsByTagName(t)) {
			return els;
		}
		else {
			return null;
		}
	}
	else {
		return null;	
	}
}

function gup(el, srch) {
	while (el.nodeName.toLowerCase() != srch.toLowerCase()) {
		el = el.parentNode;
	}
	return el;
}

function cpih(s, d) {
	var dest = null, el = null;
	if (s && d && s.innerHTML && d.innerHTML) {
		d.innerHTML = s.innerHTML;
		if (el = gup(s, 'ul')) {
			el.style.display = 'none';
		}
		if (d.id) {
			var el = null;
			if (el = ge(d.id + '_input')) {
				el.value = d.innerHTML;
			}
		}
	}
	return false; 
}

function drawCircles(add, colors) {
	
	ge(add + '_canvas').style.width = 25*colors.length + 'px';
	for (var i = 0; i < colors.length; i++) {
		var div = null;
		if (div = ge(i + '_circle' + add)) { 
			var jg = new jsGraphics(div);
			if (colors[i].colorCode) {
				jg.setColor(colors[i].colorCode);
			}
			else {
				jg.setColor('#FFFFFF');
			}
			jg.fillEllipse(i*30, 0, 23, 23);
			jg.setColor('#9E9E9E');
			jg.setStroke(1);
			jg.drawEllipse(i*30, 0, 23, 23);
			jg.paint();
		}
	}

}

function drawCirclesWithLinks(insd, colors) {
	var newDiv = null, jg = null;
	
	for (var i = 0; i < colors.length; i++) {
		var div = null;
		if (div = ge(i + '_circle')) { 
		  ic = new ImageChanger(div, colors[i], imgObject.PreviewImages[i], imgObject.LightBoxImages[i], imgObject.LightBoxBacks[i] ? imgObject.LightBoxBacks[i] : null, imgObject.LightBoxSides[i] ? imgObject.LightBoxSides[i] : null);
			ic.init();
			var jg = new jsGraphics(div);
			if (colors[i].colorCode) {
				jg.setColor(colors[i].colorCode);
			}
			else {
				jg.setColor('#FFFFFF');
			}
			jg.fillEllipse(i*30, 0, 23, 23);
			jg.setColor('#9E9E9E');
			jg.setStroke(1);
			jg.drawEllipse(i*30, 0, 23, 23);
			jg.paint();
		}
	}
}

var userOrder = {

	color : null,
	size : null,
	count : null,
	dealer : false,
	
	init : function() {
		if (typeof(ColorCodes) != 'undefined') {
			if (ColorCodes.length == 1 && ColorCodes[0]) {
				this.setColor(ColorCodes[0].colorName);
			}
		} 
	},	
	setCount : function (val) {
		if (!isNaN(val) && (val > 0)) { 
			this.count = val;
		}
	},	
	setSize : function (val) {
		this.size = val;
	},
	setColor : function (val) {
		this.color = val;
	},	
	check : function () {
		if (!this.color) {
			alert('Вы не выбрали цвет!');
			return false;
		}
		if (!this.size) {
			alert('Вы не выбрали размер!');
			return false;
		}
		if (!this.count) {
			alert('Вы не выбрали количество!');
			return false;
		}
	
		return true;
	},
	serialize : function () {
		return "{ color: '" + this.color + "', size : '" + this.size + "' }";
	}
}

function submitBuyForm() {
	
	userOrder.setCount(ge('clothes-number').value);
	ge('additional').value = userOrder.serialize();
	
	if (userOrder.check()) {
		ge('buy-form').submit();
	}
	return false;
}

function spanClick(span) {
	var els = $$('.bold-span');
	for (var i = 0; i < els.length; i++) {
		if (els[i].className) {
			els[i].className = 'norm-span';
		}
	}
	span.className = 'bold-span';

}

var ImageChanger = function (clickDiv, colors, imageUrl, lightBoxImageUrl, lightBoxBackUrl, lightBoxSideUrl) {

	this.clickDiv = clickDiv;
	imageUrl = imageUrl;
	lightBoxImageUrl = lightBoxImageUrl;
	lightBoxBackUrl = lightBoxBackUrl;
	lightBoxSideUrl = lightBoxSideUrl;
	color = colors;
      
	this.init = function () {
		  
        var cname = colors.colorName;

		var resA = null;
		
		this.clickDiv.onclick = function() {
			if (resA = ge('lba')) {
				resA.href = lightBoxImageUrl;
				resA.style.background = '';
				userOrder.setColor(cname);
				/*ie6 addicted*/
				resA.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imageUrl + "');";
				if (!ie6) {					
					resA.style.background = 'url(' + imageUrl + ')';
				}
				var lbaBack = ge('lba_back');
				if (lbaBack) {
					if (lightBoxBackUrl && lightBoxBackUrl != '/cat/images/') {
						if (lbaBack.rel != 'lightbox[soda]') {
							lbaBack.rel = 'lightbox[soda]';
						}
						lbaBack.href = lightBoxBackUrl;
					}
					else {
						lbaBack.rel = '';
						lbaBack.href = '';
					}
				}
				var lbaSide = ge('lba_side');
				if (lbaSide) {
					if (lightBoxSideUrl && lightBoxSideUrl != '/cat/images/') {
						if (lbaSide.rel != 'lightbox[soda]') {
							lbaSide.rel = 'lightbox[soda]';
						}
						lbaSide.href = lightBoxSideUrl;
					}
					else {
						lbaSide.rel = '';
						lbaSide.href = '';
					}
				}
			}
		}

		this.clickDiv.onmouseout = function() {
			this.style.cursor = 'default';
		}	
		this.clickDiv.onmouseover = function() {
			this.style.cursor = 'pointer';
		}		
	}
}

function geti (id, doc) {
	if((id)&&((typeof id == "string")||(id instanceof String))) {
		if (!doc) { 
			doc = document; 
		}
		var ele = doc.getElementById(id);
		// workaround bug in IE and Opera 8.2 where getElementById returns wrong element
		if (ele && (ele.id != id) && doc.all) {
			ele = null;
			// get all matching elements with this id
			eles = doc.all[id];
			if (eles) {
				// if more than 1, choose first with the correct id
				if (eles.length) {
					for (var i=0; i < eles.length; i++) {
						if (eles[i].id == id) {
							ele = eles[i];
							break;
						}
					}
					// return 1 and only element
				} 
				else { 
					ele = eles; 
				}
			}
		}
		return ele;
	}
	return id; // assume it's a node
}

var anketaDriver = function(data) {

	this.inputsClassName = data.inputs,
	this.selectsClassName = 'sl';
	this.layersClasses = [
		'educ',
		'lang',
		'work'
	];
	this.spans = null;
	this.selects = null;
	this.selectedVars = { 
		educ : { 
			curr : 1, 
			max : 2
			},  
		lang : { 
			curr : 1, 
			max : 3
			},
		work : { 
			curr : 1, 
			max : 3
			} 
		};
	
	this.init = function() {
		this.spans = $$('#work_form .right_sel');
		this.selects = $$('.sl');
		this.selareas = $$('#work_form .case');
		var self = this;				
		var el = null;		
		
		for (var i = 0; i < this.spans.length; i++) {
			el = null;
			this.spans[i].onclick = function() {
				var elId = this.id.substring(5);
				el = null;
				if (el = geti(elId)) {
					el.style.display = (el.style.display == 'none') ? '' : 'none';
					/*var lis = el.select(['li']);
					for (var k = 0; k < lis.length; k++) { 
						lis[k].onmouseout = function() {
							var elId = this.parentNode.id;				
							if (el = geti(elId)) {					
								el.style.display = 'none';
							}						
						}
					}*/
					self.hideSelects(el.id);
				}		
			}
		}

		for (var i = 0; i < this.selareas.length; i++) {
			el = null;
			this.selareas[i].onclick = function() {
				var elId = this.id.substring(0,this.id.indexOf('_label'));
				el = null;
				if (el = geti(elId)) {
					el.style.display = (el.style.display == 'none') ? '' : 'none';
					self.hideSelects(el.id);
				}		
			}
		}
		
		for (var i = 0; i < this.layersClasses.length; i++) {
			lclass = this.layersClasses[i];
			el = null;
			if (el = geti(lclass + 'plus')) {			
				el.onclick = function() {
					var id = this.id.substr(0,4);
					var prop = self.selectedVars[id];
					if ((prop.curr + 1) <= prop.max) {
						var els = $$('.' + id + (prop.curr + 1));
						for (var j = 0; j < els.length; j++) {
							els[j].style.display = '';
						}
						self.setProp({ 
							name : id,
							value : (prop.curr + 1)
						})
						if (prop.curr == prop.max) {
							this.style.cursor = 'default';
							this.style.border = 'none';
						}
					}
					return false;
				}
			}
			if (el = geti(lclass + 'minus')) {			
				el.onclick = function() {					
					var id = this.id.substr(0,4);
					var prop = self.selectedVars[id];
					if (prop.curr != 1) {					
						var els = $$('.' + id + prop.curr);
						for (var j = 0; j < els.length; j++) {
							els[j].style.display = 'none';
						}
						if ((prop.curr - 1) >= 1) {
							self.setProp({ 
								name : id,
								value : (prop.curr - 1)
							})
						}						
					}
					return false;
				}
			}
		}
	}
	
	this.hideSelects = function(id) {
		for (var i = 0; i < this.selects.length; i++) {
			if (this.selects[i].id != id) {
				this.selects[i].style.display = 'none';
			}
		}
	}
	
	this.setProp = function(prop) {
		//this.selectedVars[prop.name].curr = (prop.value + 1 > this.selectedVars[prop.name].max) ? prop.value : (prop.value + 1);
		this.selectedVars[prop.name].curr = prop.value;
	}
}




