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

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

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

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

	this.get = function (path, analyze) {
		link.onreadystatechange = function () {
			if (link.readyState == 4 && link.status == 200) {
				if (analyze) {
					analyze(link);
				}
				
			}
		};
		link.open('GET', path, true);

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

Array.prototype.sum = function () {
	var i   = this.length;
	var sum = 0;
	while (i-- > 0) {
		sum += this[i];
	}
	return sum;
};

var substrCount = function (haystack, needle) {
	var total = 0, pos = 0;
	while (pos != -1) {
		pos = haystack.indexOf(needle);
		if (pos != -1) {
			total++;
			haystack = haystack.substring(pos + needle.length);
		}
	}
	return total;
};

var strRepeat = function (input, multiplier) {
	var result = '';
	while (multiplier-- > 0) {
		result += input;
	}
	return result;
};

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

var setOpacity = function (object, opacity) {
	if (is_ie) {
		object.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
	} else {
		object.style.opacity = opacity;
	}
};

var changeCountry = function (select) {
	window.location = '/countries/' + select.options[select.selectedIndex].value + '/';
};

var changeImg = function (link) {
	var parent = link.parentNode;
	var img    = parent.parentNode.getElementsByTagName('IMG')[0];
	var span, anchor, num, rel;

	var i, old, elements = new Array();
	for (i = 0, j = 0; i < parent.childNodes.length; i++) {
		if (parent.childNodes[i].nodeType == 1) {
			elements[j++] = parent.childNodes[i];
			if (parent.childNodes[i].nodeName == 'SPAN' && parent.childNodes[i].title) {
				old = parent.childNodes[i];
			}
		}
	}
	var total = elements.length - 2;

	rel = link.rel;
	if (rel) {
		num = rel;
		link = elements[rel];
	} else {
		num = link.title;
	}
	num = parseInt(num, 10);

	anchor = document.createElement('A');
	anchor.href = img.src;
	anchor.title = old.title;
	anchor.innerHTML = old.innerHTML;
	anchor.onclick = function () { return changeImg(this); };
	parent.replaceChild(anchor, old);
	anchor = null;

	span = document.createElement('SPAN');
	span.title = link.title;
	span.innerHTML = link.innerHTML;
	img.src = link.href;
	img.parentNode.href = link.href.replace('midi', 'maxi');
	parent.replaceChild(span, link);
	span = null;

	if (num == 1) {
		span = document.createElement('SPAN');
		span.className = 'pg-prev-off';
		span.innerHTML = '&lt;';
		parent.replaceChild(span, elements[0]);
		span = null;
	} else {
		anchor = document.createElement('A');
		anchor.href = elements[num - 1].href;
		anchor.className = 'pg-prev-on';
		anchor.innerHTML = '&lt;';
		anchor.rel = num - 1;
		anchor.onclick = function () { return changeImg(this); };
		parent.replaceChild(anchor, elements[0]);
		anchor = null;
	}

	if (num == total) {
		span = document.createElement('SPAN');
		span.className = 'pg-next-off';
		span.innerHTML = '&gt;';
		parent.replaceChild(span, elements[total+1]);
		span = null;
	} else {
		anchor = document.createElement('A');
		anchor.href = elements[num + 1].href;
		anchor.className = 'pg-next-on';
		anchor.innerHTML = '&gt;';
		anchor.rel = num + 1;
		anchor.onclick = function () { return changeImg(this); };
		parent.replaceChild(anchor, elements[total+1]);
		anchor = null;
	}

	return false;
};

var makeHovers = function () {
	preloadImgs('nav/nav_on_bg.jpg', 'nav/nav_info_on_bg.jpg', 'common/order_list_on.jpg');

	var items = document.getElementById('nav').getElementsByTagName('LI');
	var item, i = items.length;
	while (i-- > 0) {
		preloadImgs('nav/' + items[i].className + '_white_on.gif', 'nav/' + items[i].className + '_yellow_on.gif');
		items[i].onmouseover = function () {
			var item  = this.className;
			var child = this.firstChild;
			this.style.backgroundColor = 'transparent';
			this.style.backgroundImage  = 'url(/images/nav/nav_' + (item == 'info' ? 'info_' : '') + 'on_bg.jpg)';
			child.style.backgroundImage = 'url(/images/nav/' + item + '_' + (child.id ? 'yellow' : 'white') + '_on.gif)';

		};
		items[i].onmouseout = function () {
			this.removeAttribute('style');
			this.firstChild.removeAttribute('style');
		};
	}

	var order_list = document.getElementById('order-list').firstChild;
	order_list.onmouseover = function () {
		this.parentNode.style.backgroundImage = 'url(/images/common/order_list_on.jpg)';
	};
	order_list.onmouseout = function () {
		this.parentNode.removeAttribute('style');
	};
};

var centerPages = function () {
	var divs = document.getElementsByTagName('DIV');
	var obj, interval, j, i = divs.length;
	while (i-- > 0) {
		if (divs[i].className && divs[i].className.indexOf('pages') != -1) {
			divs[i].style.marginLeft = '-' + Math.round(divs[i].offsetWidth / 2) + 'px';
			j = 0.1;
			obj = divs[i];
			setOpacity(obj, j);
			divs[i].style.width = '160px';
			divs[i].style.visibility = 'visible';
			interval = setInterval((function () {
				if (j > 1) {
					clearInterval(interval);
				} else {
					j += 0.2;
					setOpacity(obj, j);
				}
			}), 50);
		}
	}
};

var getCities = function (main) {
	(new request).get('/scripts/requests.php?action=cities&country=' + main.options[main.selectedIndex].value, parseCities);
};

var parseCities = function (link) {
	var sub = document.getElementById('cities');
	sub.innerHTML = '';
	var select = link.responseXML.documentElement;
	var opts = select.childNodes.length;
	var i, j, opt, t_opt;
	opt = select.childNodes;

	for (i = 0; i < opts; i++) {
		t_opt           = document.createElement('OPTION');
		t_opt.value     = opt[i].getAttribute('value');
		t_opt.innerHTML = opt[i].firstChild.nodeValue;
		sub.appendChild(t_opt);
		t_opt = null;
	}
};

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

var check = {
	limit: function (field, min, max, msg, type) {
		var total = field.value.length;
		if (total < min || total > max) {
			return this.error(type, msg, field);
		} else {
			return this.clear(field);
		}
	},
	match: function (field, regexp, msg, type) {
		var expr;
		if (typeof regexp == "object") {
			regexp[1] = regexp[1] || "";
			expr = new RegExp(regexp[0], regexp[1]);
		} else {
			expr = new RegExp(regexp);
		}
		if (!field.value.match(expr)) {
			return this.error(type, msg, field);
		} else {
			return this.clear(field);
		}
	},
	equal: function (field, field2, msg, type) {
		if (field.value != field2.value) {
			return this.error(type, msg, field, field2);
		} else {
			return this.clear(field, field2);
		}
	},
	selected: function (field, quantity, msg, type) {
		var i   = field.options.length;
		var sel = 0;
		while (i-- > 0) {
			if (field.options[i].selected && ++sel == quantity) {
				return this.clear(field);
			}
		}
		return this.error(type, msg, field);
	},
	error: function (type, msg) {
		var error, i = arguments.length;
		while (i-- > 2) {
			arguments[i].style.backgroundColor = "#FFCC99";
			if (type) {
				error = arguments[i].parentNode.getElementsByTagName("P")[0];
				if (!error) {
					error = document.createElement("P");
					error.className = "error";
					error.innerHTML = msg;
					arguments[i].parentNode.appendChild(error);
					error = null;
				}
			}
		}

		if (!type) {
			alert(msg);
		}
		return true;
	},
	clear: function () {
		var error, i = arguments.length;
		while (i-- > 0) {
			arguments[i].style.backgroundColor = "#F2FFC2";
			error = arguments[i].parentNode.getElementsByTagName("P")[0];
			if (error && error.className == "error") {
				arguments[i].parentNode.removeChild(error);
			}
		}
		return false;
	}
};

var fixHeaders = function () {
	var h1 = document.getElementsByTagName('H1');
	var i = h1.length;
	while (i-- > 0) {
		h1[i].style.right = '-3px';
	}
};

var in_dir = substrCount(document.location.pathname, '/') - 1;
var up_dir = strRepeat('../', in_dir);

window.onload = function () {
	makeHovers();
//	fixHeaders();
	if (typeof sIFR == "function") {
		if (hasFlash) {
			sIFR.replaceElement(named({sSelector:'h2', sFlashSrc:up_dir+'images/flash/raleighc_bt.swf', sWmode: 'transparent'}));
			sIFR.replaceElement(named({sSelector:'.col-third h3', sFlashSrc:up_dir+'images/flash/raleighc_bt.swf', sWmode: 'transparent'}));
		}
	}
	makeDefaults();
	Scroller.setAll();
	initLightbox();
	centerPages();
	var greece = document.getElementById("greece");
	if (greece) {
		(new Image()).src = '/images/banners/greece_on.jpg';
		greece.onmouseover = function () {
			this.src = '/images/banners/greece_on.jpg';
		};
		greece.onmouseout = function () {
			this.src = '/images/banners/greece.jpg';
		};
	}
};
