/*
 *	Common Entix Web CMS Functions
 */

function extUrls() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	var browser = navigator.appName;
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			if (browser == "Microsoft Internet Explorer" || browser == "Netscape")
				anchor.target = "_blank";
			else
				anchor.onclick = "javascript:window.open('"+anchor.getAttribute("href")+"'); return false;";
		}
	}
}

function pageScroll(id) {
    document.getElementById(id).scrollTop+=10; // horizontal and vertical scroll increments
    scrolldelay = setTimeout('pageScroll(\''+id+'\')',100); // scrolls every 100 milliseconds
}

function stopScroll() {
	clearTimeout(scrolldelay);
}

function $(stringId) {
	return document.getElementById(stringId);
}

function $style(oElement, CssProperty) {
	if(oElement.currentStyle) {
        var convertToCamelCase = CssProperty.replace(/\-(.)/g, function(m, l){return l.toUpperCase()});
        return oElement.currentStyle[convertToCamelCase];
	} else if (window.getComputedStyle) {
		var elementStyle = window.getComputedStyle(oElement, "");
		return elementStyle.getPropertyValue(CssProperty);
	}
}

function setOpacity(oElm, iPerc) {
	if(navigator.userAgent.indexOf("MSIE") != -1)
		oElm.style.filter = "alpha(opacity="+iPerc+")";
	else
		oElm.style.opacity = (iPerc/100);
}

function getTotalHeight() {
	if(navigator.userAgent.indexOf("MSIE") != -1)
		return parseInt(document.body.offsetHeight);
	else 
		return parseInt(window.innerHeight);
}

function getElemHeight(oElem) {
	var h=0;
	if (navigator.userAgent.indexOf("MSIE") != -1) {
		h = oElem.offsetHeight;
	} else {
		h = document.defaultView.getComputedStyle(oElem, "").getPropertyValue("height");
		h = h.split('px');
		h = h[0];
	}
	return h;
}

function addFormSubmitEvent(form) {
	var inputs = form.getElementsByTagName('input');
	for (var i=0;i < inputs.length;i++)
		addInputSubmitEvent(form, inputs[i]);
}

function addInputSubmitEvent(form, input) {
	input.onkeydown = function(e) {
		e = e || window.event;
		if (e.keyCode == 13) {
			form.submit();
			return false;
		}
	};
}


/* onLoadEvent helper function */
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

/* page load entry (gets executed when page is loaded) */
addLoadEvent(function() {
	// check for height problems
	if ($('container')) {
		var mtop = $style($('container'), 'margin-top');
		mtop = mtop.substring(0,mtop.length-2);
		if (mtop < 0) {
			var cheight = getElemHeight($('container'));
			var avail = getTotalHeight();
			if (cheight > avail) {
				//alert('too small, missing:' + (cheight - avail));
				mtop = parseInt(mtop) + Math.floor((cheight - avail) / 2);
				$('container').style.marginTop = (mtop) + 'px';
			}
		}
	}
	// make <a rel="external" work
	extUrls();
	// Gallery loader
	if(window.loadCheckForShot){loadCheckForShot();}
	// IE Enter submit form
	if(navigator.userAgent.indexOf("MSIE") != -1 && $('enter_submit_form')) {
		addFormSubmitEvent($('enter_submit_form'));
	}
})
