/* copyright (c) 2007 Dream Row; by Revin Guillen */

//createStyleRule('#panel1', 'display:none;opacity:0;filter:alpha(opacity=0);');
//createStyleRule('#panel2', 'display:none;opacity:0;filter:alpha(opacity=0);');
//createStyleRule('#panel3', 'display:none;opacity:0;filter:alpha(opacity=0);');
createStyleRule('#pWrap', 'display:none');

function addLoader(f) {
	var old = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = f;
	} else {
		window.onload = function() {
			old();
			f();
		}
	}
}

function nudgeElement(elem, offset, endval) {
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById(elem)) return false;

	var links = document.getElementById(elem).getElementsByTagName('a');
	if(!links){ return; }
	var e = links[0];
	if (e.movement) {
		clearTimeout(e.movement);
	}
	if (!e.style.paddingLeft) e.style.paddingLeft = '15px';

	var x = parseInt(e.style.paddingLeft);
	if (x == endval) return true;

	if (offset > 0) { // moving out
		if (x < endval) x = Math.min(x+offset, endval);
	} else if (offset < 0) { // moving in
		if (x > endval) x = Math.max(x+offset, endval);
	}
	e.style.paddingLeft = x+'px';

	var rpt = 'nudgeElement("'+elem+'", '+offset+', '+endval+')';
	e.movement = setTimeout(rpt,10);
}

function enableAnimation(elem) {
	if (!document.getElementById) return false;
	var e = document.getElementById(elem);
	if(e){
		e.onmouseover = function() {
			nudgeElement(elem, -1, 3);
		}
		e.onmouseout = function() {
			nudgeElement(elem, 1, 15);
		}
	}
}

// -----------------------------------------------
/*	dynamicCSS.js v1.0 <http://www.bobbyvandersluis.com/articles/dynamicCSS.php>
	Copyright 2005 Bobby van der Sluis
	This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

function createStyleRule(selector, declaration) {
	if (!document.getElementsByTagName || !(document.createElement || document.createElementNS)) return;
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_iewin = (is_ie && (agt.indexOf("win") != -1));
	var is_iemac = (is_ie && (agt.indexOf("mac") != -1));
	if (is_iemac) return; // script doesn't work properly in IE/Mac
	var head = document.getElementsByTagName("head")[0]; 
	var style = (typeof document.createElementNS != "undefined") ?  document.createElementNS("http://www.w3.org/1999/xhtml", "style") : document.createElement("style");
	if (!is_iewin) {
		var styleRule = document.createTextNode(selector + " {" + declaration + "}");
		style.appendChild(styleRule); // bugs in IE/Win
	}
	style.setAttribute("type", "text/css");
	style.setAttribute("media", "screen"); 
	head.appendChild(style);
	if (is_iewin && document.styleSheets && document.styleSheets.length > 0) {
		var lastStyle = document.styleSheets[document.styleSheets.length - 1];
		if (typeof lastStyle.addRule == "object") { // bugs in IE/Mac and Safari
			lastStyle.addRule(selector, declaration);
		}
	}
}

/*	DOM manipulation functions;
	Can only be used after a page has fully loaded
*/

function setElementStyleById(id, propertyName, propertyValue) {
	if (!document.getElementById) return;
	var el = document.getElementById(id);
	if (el) el.style[propertyName] = propertyValue;
}

function setElementStyle(element, propertyName, propertyValue) {
	if (!document.getElementsByTagName) return;
	var el = document.getElementsByTagName(element);
	for (var i = 0; i < el.length; i++) {
		el[i].style[propertyName] = propertyValue;
	}
}

function setElementStyleByClassName(cl, propertyName, propertyValue) {
	if (!document.getElementsByTagName) return;
	var re = new RegExp("(^| )" + cl + "( |$)");
	var el = document.all ? document.all : document.getElementsByTagName("body")[0].getElementsByTagName("*"); // fix for IE5.x
	for (var i = 0; i < el.length; i++) {
		if (el[i].className && el[i].className.match(re)) {
			el[i].style[propertyName] = propertyValue;
		}
	}
}
// -----------------------------------------------


addLoader(function() {
	enableAnimation('snHome');
	enableAnimation('snResume');
	enableAnimation('snBooking');
	enableAnimation('snMedia');
	enableAnimation('snVideo');
	enableAnimation('snGallery');
	enableAnimation('snCalendar');
	enableAnimation('snDance');
	enableAnimation('snKore');

	var bhome = $('bhome');
	var panel1 = $('panel1');
	var panel2 = $('panel2');
	var panel3 = $('panel3');
	Element.setStyle($('pWrap'), {display:'block'});
	if (panel1) {
		var heading = panel1.getElementsByTagName('h2')[0];
		Element.setStyle(heading, {opacity:'0', filter:'alpha(opacity=0)', display:'none'});
		Element.setStyle(panel1, {display:'block'});
		if (bhome) {
			Element.setStyle('headWel', {opacity:'0', filter:'alpha(opacity=0)'});
			//Element.setStyle('tWrap', {opacity:'0', filter:'alpha(opacity=0)', display:'none'});
			Element.setStyle('gWrap', {opacity:'0', filter:'alpha(opacity=0)'});
			Effect.Grow(panel1, {queue: {position:'front', scope:'panel'}});
			Effect.Appear(heading, {duration:.5, queue: {position:'end', scope:'panel'}});
			Effect.Appear('tWrap', {duration:.5, queue: {position:'end', scope:'panel'}});
			Effect.Appear('gWrap', {duration:.5, queue: {position:'end', scope:'panel'}});
		} else {
			Element.setStyle('tWrap', {opacity:'0', filter:'alpha(opacity=0)', display:'none'});
			Effect.Grow(panel1, {queue: {position:'front', scope:'panel'}});
			Effect.Appear(heading, {duration:.5, queue: {position:'end', scope:'panel'}});
			Effect.Appear('tWrap', {duration:.5, queue: {position:'end', scope:'panel'}});
		}
	}
	if (panel2) {
		Element.setStyle(panel2, {opacity:'0', filter:'alpha(opacity=0)', display:'block'});
		Effect.Appear(panel2);
	}
	if (panel3) {
		console.log("panel3");
		var heading = panel3.getElementsByTagName('h2')[0];
		Element.setStyle(heading, {opacity:'0', filter:'alpha(opacity=0)', display:'none'});
		Element.setStyle('tWrap', {opacity:'0', filter:'alpha(opacity=0)', display:'none'});
		Effect.SlideDown(panel3, {queue: {position:'front', scope:'panel'}});
		Effect.Appear(heading, {duration:.5, queue: {position:'end', scope:'panel'}});
		Effect.Appear('tWrap', {duration:.5, queue: {position:'end', scope:'panel'}});
	}
});
