/*****************************   anDeRan   ************************************/

var is_ie  = !!document.uniqueID;
var is_ie6 = is_ie && navigator.appVersion.match(/MSIE (5.5|6)/);

if (is_ie6) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

if (is_ie) {
	var XMLHttpRequest = function () {
		return (new ActiveXObject("Msxml2.XMLHTTP")) || (new ActiveXObject("Microsoft.XMLHTTP")) || false;
	};
}

var preloadImgs = function () {
	var i = arguments.length;
	while (i-- > 0) {
		(new Image()).src = '/images/' + arguments[i];
	}
};

var setOpacity = function (obj, num) {
	if (is_ie6) {
		if (num == 1) {
			obj.style.filter = '';
		} else {
			obj.style.filter = 'alpha(opacity=' + (num * 100) + ')';
		}
	} else {
		obj.style.opacity = num;
	}
};

var request = function () {
	try {
		var link = new XMLHttpRequest();
	} catch (e) {
		alert('Your browser doesn\'t support XMLHttpRequest. Please enable XMLHTTPRequest if disabled or update if your browser is old.');
	}

	var loading = document.getElementById('ajax-loading');
	document.onmousemove = function (e) {
		e = e || event;
		loading.style.display = 'block';
		loading.style.top = (e.clientY + (document.documentElement.scrollTop || window.scrollY || document.body.scrollTop) - 16) + 'px';
		loading.style.left = (e.clientX + (document.documentElement.scrollLeft || window.scrollX || document.body.scrollLeft) - 16) + 'px';
	};

	this.get = function (path, analyze, params, no_loading) {
		if (no_loading) {
			loading.style.display = 'none';
		}
		link.onreadystatechange = function () {
			if (link.readyState == 4 && link.status == 200) {
				loading.style.display = 'none';
				document.onmousemove = null;
				if (analyze) {
					analyze(link, params);
				}				
			}
		};
		link.open('GET', path, true);

		if (is_ie6) {
			link.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
		}
		link.send('');
	};
};

var initDefaults = function () {
	var inputs = document.getElementsByTagName('INPUT');
	var i = inputs.length;
	while (i-- > 0) {
		if (inputs[i].type == 'text' && inputs[i].className.indexOf('def-value') != -1) {
			inputs[i].onfocus = function () {
				if (this.value == this.defaultValue) {
					this.value = '';
				}
			};
			inputs[i].onblur = function () {
				if (!this.value) {
					this.value = this.defaultValue;
				}
			};
		}
	}
};

var initGallery = function () {
	var gallery = document.getElementById('gallery');
	if (gallery) {
		var imgs = gallery.getElementsByTagName('IMG');
		var i = imgs.length;
		while (i-- > 0) {
			(new Image()).src = imgs[i].src.replace('/mini/', '/midi2/');
		}
	}
};

var setPhoto = function (link) {
	link.blur();
	var photo = document.getElementById('photo');
	photo.firstChild.href = link.firstChild.src.replace('/mini/', '/maxi/');
	photo.firstChild.firstChild.src = link.firstChild.src.replace('/mini/', '/midi2/');
	return false;
};

var addToBag = function (input) {
	var form = input.form;
	input.blur();

	var errors = false;
	if (form.elements['size']) {
		if (form.elements['size'].selectedIndex == 0) {
			if (form.elements['size'].className.indexOf('error') == -1) {
				form.elements['size'].className += ' error';
			}
			if (!errors) {
				alert('Please choose product size!');
			}
			errors = true;
		} else {
			form.elements['size'].className = form.elements['size'].className.replace('error', '');
		}
	}
	if (form.elements['color']) {
		if (form.elements['color'].selectedIndex == 0) {
			if (form.elements['color'].className.indexOf('error') == -1) {
				form.elements['color'].className += ' error';
			}
			if (!errors) {
				alert('Please choose product color!');
			}
			errors = true;
		} else {
			form.elements['color'].className = form.elements['color'].className.replace('error', '');
		}
	}

	if (input.type == 'submit') {
		return !errors;
	}

	var url = '/scripts/requests.php?action=bag';
	var i = form.elements.length;
	while (i-- > 0) {
		if (form.elements[i].nodeName.toUpperCase() == 'INPUT') {
			if (form.elements[i].type.toUpperCase() == 'HIDDEN') {
				url += '&' + form.elements[i].name + '=' + form.elements[i].value;
			}
		}
		if (form.elements[i].nodeName.toUpperCase() == 'SELECT') {
			url += '&' + form.elements[i].name + '=' + form.elements[i].value;
		}
	}

	if (!errors) {
		input.disabled = true;
		(new request).get(url, function (req) {
			var bag = document.getElementById('bag');
			var total = parseInt(req.responseText, 10);
			if (total) {
				bag.getElementsByTagName('STRONG')[0].innerHTML = '(' + total + ' item' + (total == 1 ? ')' : 's)');
			}
			var description = document.getElementById('item-description');
			var span = document.createElement('SPAN');
			span.className = 'item-added';
			span.innerHTML = 'Item Added';
			var interval, opacity = 1;
			setTimeout(function () {
					interval = setInterval(function () {
					opacity -= 0.1;
					if (opacity < 0) {
						clearInterval(interval);
						span.parentNode.removeChild(span);
						input.disabled = false;
					} else {
						setOpacity(span, opacity);
					}
				}, 100);
			}, 1000);
			description.appendChild(span);
//			form.parentNode.removeChild(form);
		});
	}

	return false;
};

var makeURL = function (name, val) {
	var parts = location.href.split(/\?/);
	if (parts[1]) {
		parts[1] = '?' + parts[1];
		var pos = parts[1].indexOf(name + '=');
		if (pos != -1) {
			pos = pos + name.length + 1;
			return parts[0] + parts[1].substring(0, pos) + parts[1].substring(pos).replace(/^[a-z0-9]+/, val);
		} else {
			return parts[0] + parts[1] + '&' + name + '=' + val;
		}
	} else {
		return parts[0] + '?' + name + '=' + val;
	}
};

var changePer = function (select) {
	location.href = makeURL('limit', select.options[select.selectedIndex].value);
};

var shopBy = function (select) {
	var shop_by = select.options[select.selectedIndex].value;
	var path = location.href.replace(/.*\/(.+)\/.*$/, '$1');
	(new request).get('/scripts/requests.php?action=nav_sub&shop_by=' + shop_by + '&path=' + path, function (req) {
		var ul = document.getElementById('nav-sub');
		ul.innerHTML = req.responseText;
	});
};

var setURL = function (url) {
	location.href = url;
};

var addHdrLink = function () {
	var header = document.getElementById('header');
	var a = document.createElement('A');
	a.href = '/';
	a.title = 'Addict Fashions';
	a.style.cssText = 'position: absolute; width: 53px; height: 51px; margin: 28px 0 0 12px;';
	header.appendChild(a);
};

var changeAmount = function (input) {
	var parent = input.parentNode;
	if (isNaN(input.value) || input.value < 1) {
		input.value = 1;
	}

	var price = parseFloat(parent.firstChild.value);
	price = price * input.value;
	parent.parentNode.getElementsByTagName('SPAN')[0].innerHTML = '$' + price.toFixed(2);

	var rows = parent.offsetParent.rows;
	var sum = 0, i = rows.length;
	while (i-- > 1) {
		if (rows[i].cells[2]) {
			var num = parseFloat(rows[i].getElementsByTagName('SPAN')[0].innerHTML.substring(1));
			if (!isNaN(num)) {
				sum += num;
			}
		}
	}
	var total = document.getElementById('total');
	total.innerHTML = '$' + sum.toFixed(2);
};

var showPreview = function (link, width, height) {
	var id = link.href.replace(/[^a-z0-9]/ig, '');
	id = 'id' + id.substring(id.length - 14, id.length);
	var div = document.getElementById(id);
	if (!div) {
		div = document.createElement('DIV');
		div.style.position = 'absolute';
		div.style.width = width + 'px';
		div.id = id;
		div.style.height = height + 'px';
		div.style.background = '#FFF url(/images/common/progress.gif) no-repeat center';
		div.style.border = '1px solid #314560';
		div.style.display = 'none';
		div.innerHTML = '<img src="' + link.href + '" width="' + width + '" height="' + height + '" alt="" />';
		if (is_ie) {
			link.parentNode.appendChild(div);
		} else {
			document.body.appendChild(div);
		}
	}
	link.onmousemove = function (e) {
		e = e || event;
		link = this;
		var id = link.href.replace(/[^a-z0-9]/ig, '');
		id = 'id' + id.substring(id.length - 14, id.length);
		var byTop, div = document.getElementById(id);
		if (div) {
			var byTop = 0;
			if (document.body.scrollTop) {
				byTop = document.body.scrollTop;
			} else if (window.pageYOffset != undefined) {
				byTop = window.pageYOffset;
			}

			var inner = window.innerHeight ? window.innerHeight : document.body.clientHeight;
			byTop = e.clientY < inner - height ? e.clientY + byTop + 20 : e.clientY + byTop - height - 5;

			div.style.display = 'block';
			div.style.top = byTop + 'px';
			div.style.left = e.clientX + 'px';
		}
	}
};

var clearPreview = function (link) {
	var id = link.href.replace(/[^a-z0-9]/ig, '');
	id = 'id' + id.substring(id.length - 14, id.length);
	var div = document.getElementById(id);
	if (div) {
		div.parentNode.removeChild(div);
	}
};

var setSame = function (input) {
	var block = input.parentNode.parentNode.getElementsByTagName('DIV')[0];
	if (input.value == 1) {
		block.style.display = 'none';
	} else {
		block.style.display = 'block';
	}
};

var getDelivery = function (select) {
	(new request).get('/scripts/requests.php?action=delivery&id=' + select.options[select.selectedIndex].value, function (req) {
		var price = select.parentNode.getElementsByTagName('SPAN')[0];
		price.innerHTML = req.responseText;
		select.parentNode.lastChild.src = '/images/payments/' + select.options[select.selectedIndex].value + '.png';
	});
};

var setPayment = function (select) {
	var delivery = document.getElementById('fld-delivery').parentNode;
	if (select.options[select.selectedIndex].value == 2) {
		delivery.style.display = 'none';
	} else {
		delivery.style.display = 'block';
	}
	select.parentNode.lastChild.firstChild.src = '/images/payments/' + select.options[select.selectedIndex].value + '.png';
};

var initDelivery = function () {
	var select = document.getElementById('fld-payment');
	if (select) {
		var current = select.options[0].value;
		for (var i = 0; i < select.options.length; i++) {
			(new Image()).src = '/images/payments/' + select.options[i].value + '.png';
			if (select.options[i].selected) {
				current = select.options[i].value;
			}
		}
		var span = document.createElement('SPAN');
		span.innerHTML = '<img src="/images/payments/' + current + '.png" width="47" height="19" alt="" />';
		select.parentNode.appendChild(span);
	}
};

var initRotation = function () {
	var rotation = document.getElementById('rotation');
	if (rotation) {
		var photos = rotation.getElementsByTagName('P');
		window.__photo = photos.length - 1;

		photos[__photo].style.visibility = 'visible';
		var interval = setInterval(function () {
			var i = photos.length;
			window.__photo--;
			if (__photo < 0) {
				window.__photo = i - 1;
			}
			while (i-- > 0) {
				if (__photo == i) {
					var photo = photos[i];
					var photo2 = photos[__photo + 1 > photos.length - 1 ? 0 : __photo + 1];
					var opacity = 0, opacity2 = 1, step = 0.05;
					setOpacity(photo, opacity);
					setOpacity(photo2, opacity2);
					photos[i].style.visibility = 'visible';
					var interval2 = setInterval(function () {
						opacity += step;
						opacity2 -= step;
						if (opacity > 1) {
							setOpacity(photo, 1);
							photo2.style.visibility = 'hidden'; // hide
							setOpacity(photo2, 0); // hide
							clearInterval(interval2);
						} else {
							setOpacity(photo, opacity);
							setOpacity(photo2, opacity2);
						}
					}, 50);
				} else {
//					photos[i].style.visibility = 'hidden';
				}
			}
		}, 4000);
	}
};
var arr_int, tracked = null, drag = false;

var initHorScroll = function () {
	var div = document.getElementById('gallery');
	if (div) {
		if (div.getElementsByTagName('SPAN')[0]) {
			var arr_left  = div.getElementsByTagName('SPAN')[0].firstChild;
			var arr_right = div.getElementsByTagName('SPAN')[1].firstChild;

			var divs = div.getElementsByTagName('DIV');
			var container = divs[0].firstChild;
			var scroll_step = 30;
			var full_region = (divs.length - 1) * divs[1].clientWidth - div.clientWidth + 70;

			arr_left.onmouseout = arr_left.onmouseup = arr_right.onmouseout = arr_right.onmouseup = function () {
				clearInterval(arr_int);
			};
			arr_left.onclick = arr_right.onclick = function () {
				this.blur();
				return false;
			};

			arr_left.onmousedown = function () {
				var currPos = parseInt(container.style.left, 10) || 0;

				clearInterval(arr_int);
				arr_int = setInterval(function () {
					currPos += scroll_step;
					if (currPos > 0) {
						currPos = 0;
						clearInterval(arr_int);
					}

					container.style.left = currPos + 'px';
				}, 50);
				return false;
			};

			arr_right.onmousedown = function () {
				var currPos = parseInt(container.style.left, 10) || 0;

				clearInterval(arr_int);
				arr_int = setInterval(function () {
					currPos -= scroll_step;
					if (currPos < -full_region) {
						currPos = -full_region;
						clearInterval(arr_int);
					}

					container.style.left = currPos + 'px';
				}, 50);
				return false;
			};
		}
	}
};

window.onload = function () {
	var loading = document.createElement('DIV');
	loading.id = 'ajax-loading';
	loading.style.display = 'none';
	document.body.appendChild(loading);

	preloadImgs('nav/new_over.png', 'nav/clothing_over.png', 'nav/brands_over.png', 'nav/sale_over.png', 'nav/service_over.png', 'nav/charts_over.png', 'nav/faq_over.png', 'nav/payment_over.png', 'nav/contacts_over.png');

	initDefaults();
	initGallery();
	addHdrLink();
	initDelivery();
	initRotation();
	initHorScroll();

	try {
		Scroller.setAll();
		initLightbox();
	} catch (e) { }
};
