/**
 *	Common Entix Web Framework Functions
 *		Copyright 2007-2011 Entix (http://entix.nl/)
 *		[ Requires the MooTools framework (1.2.x+) ]
 */

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 escapeFrames() {
	if (top !== self) top.location.href = self.location.href;
}

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 swapVisible(hide, show) {
	if ($(show)) {
		if ($(hide))
			new Fx.Reveal($(hide)).dissolve();
		(function(){new Fx.Reveal(this).reveal()}).delay(200, $(show));
	}
}

function getOpacity(elm) {
	var ret = 1;
	if (Browser.Engine.trident)
		ret = elm.getComputedStyle('filter').test(/opacity=([^)]*)/i || '') ? (parseFloat(RegExp.$1) / 100) : '';
	else
		ret = elm.getComputedStyle('opacity');
	
	return (ret === "" ? 1 : ret);
}

function setOpacity(oElm, iPerc) {
	if(Browser.Engine.trident)
		oElm.style.filter = "alpha(opacity="+iPerc+")";
	else
		oElm.style.opacity = (iPerc/100);
}

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;
		}
	};
}

function toggleNextBlockVis(oElm, sTag) {
	if (!document.getElementsByTagName) return;
	if (!sTag) sTag = 'div';
	var block = oElm.getNext(sTag);
	if (!block)
		return;
	if (block.getStyle('display') == 'none') {
		block.set('reveal', {duration: 150, transition: Fx.Transitions.Sine.easeOut});
		block.reveal();
	} else {
		block.dissolve();
	}
}

function revealListBlock(sAnchor, sTag) {
	if (!sTag) sTag = 'div';
	oElm = $(sAnchor.split('#')[1]);
	if (oElm)
		toggleNextBlockVis(oElm, sTag);
}

function inArray(needle, haystack) {
	var key = '';
	for (key in haystack) {
		if (haystack[key] == needle) {
			return true;
		}
	}
	return false;
}

function centerHeightCheck() {
	var mtop = $('container').getStyle('margin-top').toInt();
	var cheight = $('container').getSize().y;
	var avail = window.getSize().y;
	if (cheight > avail)
		mtop = avail * -0.5;
	else
		mtop = cheight * -0.5;
	$('container').setStyle('margin-top', mtop);
}

function applyPopupEvents() {
	if ($('overlay')) {
		$('overlay').addEvent('click', function(e) {
			hidePopups();
		});
	}
	
	$$('a[href]').each(function(el) {
		var url = el.get('href');
		if (url != null && url.substring(0,7) != 'http://' && url.substring(0,8) != 'https://') {
			var hash = url.indexOf('#')+1;
			if (hash > 0) {
				var target = url.substring(hash);
				if (target == '' || $(target)) {
					el.addEvent('click', function(e) {
						e.stop();
						showPopup(target);
					});
					el.set('href',target);
				}
			}
		}
	});
}
function showPopup(id) {
	if (id == '') {
		hidePopups();
	} else if ($(id)) {
		overlayOn();
		$(id).show();
	}
}
function hidePopups() {
	overlayOff();
	$$('.hidden').each(function(el) {
		el.hide();
	});
}
function overlayOn() {
	$('overlay').fade('hide'); // opacity 0
	$('overlay').setStyle('display', 'block'); // display on
	$('overlay').fade(0.7); // fade in
}
function overlayOff() {
	$('overlay').get('tween').start('opacity',0).chain(function() { // fade out
		$('overlay').setStyle('display', 'none'); // display none
	});
}

function verticalPageCenterCheck() {
	// if page seems to be centered vertically, check for height problems
	if ($('container') && $('container').getStyle('top').toInt() > 0 && $('container').getStyle('margin-top').toInt() < 0) {
		centerHeightCheck();
		var timer;
		window.addEvent('resize', function(){
			$clear(timer);
			timer = (function(){
				centerHeightCheck();
			}).delay(50);
		});
	}
}

/* DOM Ready events */
window.addEvent('domready',function() {
	if (!window.EntixAdmin || !EntixAdmin) {
		// make <a rel="external" work
		extUrls();
		// check for centering height problems
		verticalPageCenterCheck();
		// add events for 'popup' links
		applyPopupEvents();
	}
});

/* On load events */
window.addEvent('load', function() {
	if (!window.EntixAdmin || !EntixAdmin) {
		// Random Image loop loader
		if(window.entixImgLoopInit){entixImgLoopInit();}
		// Gallery loader
		if(window.entixGalleryInit){entixGalleryInit();}
		// Product loader
		if(window.entixProductInit){entixProductInit();}
		// init google maps
		if(window.entixMapInit){entixMapInit();}
	}
});


/* 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() {
	// escape frames
	escapeFrames();
	// check rendering engine specific classnames
	//classRenderCheck();
	// Random Image loop loader
	if(window.entixImgLoopInit){entixImgLoopInit();}
	// Gallery loader
	if(window.entixGalleryInit){entixGalleryInit();}
	// Product loader
	if(window.entixProductInit){entixProductInit();}
	// Map loader
	if(window.entixMapInit){entixMapInit();}
	// List event
	if(window.entixListInit){entixListInit();}
	// Password loader
	if(window.entixPassInit){entixPassInit();}
	// IE Enter submit form
	if(navigator.userAgent.indexOf("MSIE") != -1 && $('enter_submit_form')) {
		addFormSubmitEvent($('enter_submit_form'));
	}
})
*/

