Function.prototype.bind = function () {
	if (arguments.length < 2 && arguments[0] === undefined) {
		return this;
	}
	var thisObj = this,
	args = Array.prototype.slice.call(arguments),
	obj = args.shift();
	return function () {
		return thisObj.apply(obj, args.concat(Array.prototype.slice.call(arguments)));
	};
};

Function.bind = function() {
	var args = Array.prototype.slice.call(arguments);
	return Function.prototype.bind.apply(args.shift(), args);
};

Function.prototype.delay = function (timeout) {
	var __method = this, args = Array.prototype.slice.call(arguments, 1);
	timeout = timeout * 1000
	return window.setTimeout(function() {
		return __method.apply(__method, args);
	}, timeout);
};

function open_media(path, width, height)
{
	var w = width;
	var h = height;
	var h_win = h;

	var left = parseInt((screen.availWidth / 2) - (width / 2));
	var top = parseInt((screen.availHeight / 2) - (height / 2));

	if (path.indexOf(".wmv") > 0) {
		h_win = parseInt(height) + 50;
	}

	window.open(APP.path.root + 'media/play?path='+path+'&h='+h+'&w='+w,'media','width='+w+',height='+h_win+',left='+ left +',top='+ top +',screenX='+ left +',screenY='+ top +',resizable=yes,scrollbars=no,toolbar=no,location=no,status=no,menubar=no,copyhistory=no');
	return;
}

function getElem(elemID) {
	var obj;
	if (document.all) {
		obj = document.all(elemID);
	} else if (document.getElementById) {
		obj = document.getElementById(elemID);
	} else if (document.layers) {
		obj = document.layers[elemID];
	}
	return obj;
}

function findPosX(obj)
{
	var curleft = 0;
	if (!obj) {
		return false;
	}
	if (obj.offsetParent) {
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (!obj) {
		return false;
	}
	if (obj.offsetParent) {
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

var mapX = 0;
var mapY = 0;
var idShow = -1;

function showMenu(locID) {
	hideMenu();
	idShow = locID;
	var obj = getElem("menu" + locID);
	$(obj).show();
	var overWidth = (tempX > document.body.offsetWidth - 200)*1;

	var deltaX = findPosX(getElem("middle"));
	deltaY = findPosY(getElem("middle"));

	$(obj).setStyle({
		left: tempX - 200 * overWidth - 10 - deltaX,
		top: tempY - 10 - deltaY
	});
}

function hideMenu() {
	$("divform").hide();
	var visible = $("menu-" + idShow);
	if (visible) {
		visible.hide();
	}
}

function setCoord(event) {
	hideMenu();

	var object = document.getElementById("schema");
	var mapX = findPosX(object);
	var mapY = findPosY(object);

	var delta_scroll = document.body.scrollTop;
	if (delta_scroll > 0) {
		mapY = mapY -(-delta_scroll);
	}

	var overWidth = (mapX > document.body.offsetWidth - 350) * 1;
	var overHeight = (mapY > document.body.offsetHeight - 130) * 1;

	var deltaX = findPosX(getElem("middle"));
	var deltaY = findPosY(getElem("middle"));

	getElem("divform").style.left = mapX - deltaX -  350 * overWidth + 'px';
	getElem("divform").style.top = mapY - deltaY - 130 * overHeight + 'px';
	getElem("divform").style.display = "";
}

APP.setupOrderingQuestions = function ()
{
	$$('table.question-ordering input[type="text"]').invoke('observe', 'click', function () {
		this.up('table').select('input[type="text"]').invoke('removeClassName', 'active');
		this.addClassName('active');
	});

	$$('table.question-ordering a.move-answer').invoke('observe', 'click', function (event) {
		event.stop();
		var active = this.up('table').down('input.active[type="text"]');
		if (!active) {
			return false;
		}

		var target = null;
		switch (this.rel) {
			case 'up':
				var row = active.up('tr').previous('tr');
				if (row) {
					target = row.down('input[type="text"]');
				}
				break;
			case 'down':
				var row = active.up('tr').next('tr');
				if (row) {
					target = row.down('input[type="text"]');
				}
				break;
		}
		if (!target) {
			return false;
		}

		var activeID = active.previous('input[type="hidden"]');
		var targetID = target.previous('input[type="hidden"]');

		var tmp = active.value;
		active.value = target.value;
		target.value = tmp;

		tmp = activeID.value;
		activeID.value = targetID.value;
		targetID.value = tmp;

		active.removeClassName('active');
		target.addClassName('active');
	});
};

function getPageDimensions()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	dimensions = {};
	if(yScroll < windowHeight){
		dimensions.height = windowHeight;
	} else {
		dimensions.height = yScroll;
	}

	if(xScroll < windowWidth){
		dimensions.width = windowWidth;
	} else {
		dimensions.width = xScroll;
	}

	return dimensions;
}

Element.addMethods({
    stripe: function (element) {
        var tbody = element.childElements().find(function (element) {
            return element.match('tbody');
        });
        tbody.childElements().invoke('removeClassName', 'list_tr1').invoke('removeClassName', 'list_tr2').each(function (row, index) {
			row.observe("mouseover", function () {
				this.addClassName("list_trOver");
			}).observe('mouseout', function () {
				this.removeClassName('list_trOver');
			});

            if (index % 2 == 1) {
                row.addClassName('list_tr1');
            } else {
				row.addClassName('list_tr2');
			}
        });
        return $(element);
    }
});

document.observe('dom:loaded', function() {
	$$('th input[type="checkbox"].check-all').invoke('observe', 'click', function (event) {
		var column = this.up('tr').select('th').indexOf(this.up('th'));
		var checkboxes = this.up('table').select('tr td:nth-child('+ (column + 1) +') input[type="checkbox"][disabled!="true"]');
		checkboxes.each((function (checkbox) {
			checkbox.checked = this.checked;
		}).bind(this));
	});

	$$('.toggle').invoke('observe', 'click', function (event) {
		event.stop();
		if ($(this.rel)) {
			$(this.rel).toggle();
		}
	});

    $$('table.table_list').invoke('stripe');

	(function () {
		$$('div.success').invoke("hide");
	}).delay(10);
});

