function delegate(object, method, argument) {
    return function() { return method.apply(object, new Array(argument)); };
}

function $(id) {
	var e = document.getElementById(id);
	if (e == null) {
		return null;
	}
	return e;
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if(typeof(window.pageYOffset) == 'number') {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} 
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	} 
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    	//IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
  	}
 	return [scrOfX, scrOfY];
}

var Mouse = {
	x: 0,
	y: 0,
	previousX: 0,
	previousY: 0,
	onMove: null
};

var Alert = {
	id:	"alert",

	show: function(message) {
		var element = $(Alert.id);
		if (element == null) {
			return;
		}
		element.innerHTML = "";
		element.style.visibility = "visible";
		var flashvars = "";
		flashvars += "htmlControl=true&";
		flashvars += "message=" + message + "&";
		Flash.create(Alert.id, host + "/swf/" + lang_id + "x/alert.swf", 400, 180, flashvars);
		$(Alert.id).style.left = ((document.body.offsetWidth - $(Alert.id).offsetWidth) / 2) + "px";
		$(Alert.id).style.top = ((document.body.offsetHeight + getScrollXY()[1] - $(Alert.id).offsetHeight)) / 2 + "px";
	},
	
	hide: function() {
		var element = $(Alert.id);
		if (element == null) {
			return;
		}
		element.style.visibility = "hidden";
	}
	
};

var ProductAlt = {
	id:	"product-alt",

	show: function(hoverUrl, name) {
		var element = $(ProductAlt.id);
		if (element == null) {
			return;
		}
		element.innerHTML = "";
		element.style.visibility = "visible";
		var flashvars = "";
		flashvars += "htmlControl=true&";
		flashvars += "hoverUrl=" + hoverUrl + "&";
		flashvars += "name=" + name + "&";
		Flash.create(ProductAlt.id, host + "/swf/" + lang_id + "x/hover.swf", 363, 190, flashvars);
		Mouse.onMove = ProductAlt.update;
		ProductAlt.update();
	},
	
	update: function() {
		var element = $(ProductAlt.id);
		if (element == null) {
			return;
		}
		element.style.left = Mouse.x - 185 + "px";
		element.style.top = Mouse.y - 200 + "px";
	},

	hide: function() {
		var element = $(ProductAlt.id);
		if (element == null) {
			return;
		}
		element.style.visibility = "hidden";
		Mouse.onMove = null;
	}
	
};

var SizeAlt = {
	id:	"size-alt",
	target: null,
	
	show: function(size, target, categoryId) {
		var element = $(SizeAlt.id);
		if (element == null) {
			return;
		}
		element.innerHTML = "";
		element.style.visibility = "visible";
		element.style.left = Mouse.x - 220 + "px";
		element.style.top = Mouse.y - 100 + "px";
		SizeAlt.target = target;
		if (categoryId == '3'){
		    var file = "size_selection_ladies.swf";
		} else {
		    var file = "size_selection.swf";
		}
		Flash.create(SizeAlt.id, host + "/swf/" + lang_id + "x/" + file, 426, 200, "htmlControl=true&size=" + size);
	},
	
	select: function(size) {
		SizeAlt.hide();	
		$(SizeAlt.target).SetVariable("new_size", size);
		$(SizeAlt.target).SetVariable("update", "true");
	},

	hide: function() {
		var element = $(SizeAlt.id);
		if (element == null) {
			return;
		}
		element.style.visibility = "hidden";
	}
	
};

var Flash = {

	create: function(elementId, src, width, height, flashvars) {
		// flash version check
		var playerVersion = [0, 0, 0];
		if(navigator.plugins && navigator.mimeTypes.length) {
			var x = navigator.plugins["Shockwave Flash"];
			if (x && x.description) {
				playerVersion = x.description.replace(/([a-z]|[A-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/, ".").split(".");
			}
		}
		else if (window.ActiveXObject){
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
				playerVersion = axo.GetVariable("$version").split(" ")[1].split(",");
			} 
		   	catch (e) {
				try {
					var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
					playerVersion = axo.GetVariable("$version").split(" ")[1].split(",");
				} 
			   	catch (e) {
			   	}
		   	}
		}
		if (playerVersion[0] < 8) {
			document.location = host + "/noflash.html";
			return;
		}

		// flash object
		var container = document.getElementById(elementId);
		src += "?" + version;
		if (navigator.userAgent.indexOf("MSIE") > -1) {
			var object = document.createElement("OBJECT");
			container.appendChild(object);
			object.id = elementId + "-object";
			object.width = width;
			object.height = height;
			object.classid = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
			object.codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab";
			object.movie = src;
			object.wmode = "transparent";
			object.flashvars = flashvars;
		}
		else {
			container.innerHTML = "<embed id=\"" + elementId + "-object\" src=\"" + src + "\" quality=\"high\" wmode=\"transparent\" width=\"" + width + "\" height=\"" + height + "\" allowScriptAccess=\"sameDomain\" flashvars=\"" + flashvars + "\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
		}
	}

}

var Cart = {

	add: function(productId, amount, size, categoryName, categoryId) {
		var request = new AjaxRequest();
		request.onSuccess = Cart.onSuccess;
		request.onFailure = Cart.onFailure;
		request.get("?p=xml&a=cart_add&product_id=" + productId + "&amount=" + amount + "&size=" + size + "&categoryName=" + categoryName+ "&categoryId=" + categoryId);
	},
	
	onSuccess: function() {
		$("menu-flash-object").SetVariable("update", "true");
		if (lang_id == "cs") {
			Alert.show("Vaše zboží bylo přidáno do košíku.");
		}
		if (lang_id == "en") {
			Alert.show("Your cart was updated.");
		}
	},

	onFailure: function() {
		alert("failed!");
	}

}

document.onmousemove = function(e) {
	if (window.event) {
		Mouse.x = event.clientX + getScrollXY()[0];
		Mouse.y = event.clientY + getScrollXY()[1];
	}
	else {
		Mouse.x = e.pageX;
		Mouse.y = e.pageY;
 	}
	if (Mouse.onMove != null) {
		Mouse.onMove();
	}
	Mouse.previousX = Mouse.x;
	Mouse.previousY = Mouse.y;
}
