
PA.makeNS( 'PA.utils.DOM' );

PA.utils.DOM.getCSSPropertyValue = function( doc, cssTitle, selector, prop ) {		
	var val         = null;
	do { 
		if( !cssTitle ) {
			break;
		}
		var i       = PA.utils.Array.getArrIndexByPropValue( doc.styleSheets, 'title', cssTitle );
		if( i < 0 ) {
			break;
		}
		var css     = doc.styleSheets[i];
		var rules   = ( css.rules ? 'rules' : 'cssRules' );
		i           = PA.utils.Array.getArrIndexByPropValue( css[rules], 'selectorText', selector );
		if( i < 0 ) {
			break;
		}
		val         = css[rules][i].style[prop];	
	} while( false );
	
	return val;
}
	
PA.utils.DOM.getElemStyle = function( ref, styleProp ) {	// note that this functino not always works... but is the best one can do (from quirksmode)
	var el 		= ( 'string' == typeof( ref ) ? document.getElementById( ref ) : ref );
	if( el.currentStyle ) {
		var y 	= el.currentStyle[styleProp];
	} else if( window.getComputedStyle ) {
		var y 	= document.defaultView.getComputedStyle( el, null ).getPropertyValue( styleProp );
	}		
	return y;
}

	
PA.utils.DOM.catchBubble = function( e, wnd ) {
	if( !e ) {
		e = wnd.event;
	}
	if( e ) {
		e.cancelBubble = true;
		if( e.stopPropagation ) {
			e.stopPropagation();
		}
	}
	return e;
}

PA.utils.DOM.changeStyle = function( el, p, val ) {
//	if( el && el.setAttribute ) {
//		el.setAttribute( 'style', ' ' + p + ': ' + val +';' );
//	} else if( el && el.style && el.style.setAttribute ) {	// IE
//		el.style.setAttribute( 'cssText', ' ' + p + ': ' + val +';' );
//	}
	if( el && el.style ) {
		el.style[p] = val;
	}
	
}

PA.utils.DOM.getDisplay = function( el, val ) {
	var value 		= null;
	if( el && el.style ) {
		if( 'undefined' == typeof( val ) ) {
			value 	= ( 'none' == el.style.display ? 'block' : 'none' );
		} else {
			value 	= ( val ? 'block' : 'none' );
		}
	}
	return value;
}

PA.utils.DOM.toggleDisplay = function( el, val ) {
	if( el && el.style ) {
		PA.utils.DOM.changeStyle( el, 'display',  PA.utils.DOM.getDisplay( el, val ) );
		return ( 'none' != el.style.display );
	}
	return null;
}

PA.utils.DOM.toggleVisibility = function( el, val ) {
	if( el && el.style ) {
		var value;
		if( 'undefined' == typeof( val ) ) {
			value 	= ( 'none' == el.style.visibility ? 'visible' : 'hidden' );
		} else {
			value 	= ( val ? 'visible' : 'hidden' );
		}
		PA.utils.DOM.changeStyle( el, 'visibility',  value );
		return ( 'none' != el.style.visibility );
	}
	return null;
}

PA.utils.DOM.incrementHeight = function( id, incr, doc ) {
	var el					= doc.getElementById( id );
	if( el ) {
		el.style.height		= el.scrollHeight + incr;		
	}
}

PA.utils.DOM.isDisplayed = function( el ) {
	if( !el ) {
		return false;
	}
	do { 
		if( 'none' == el.style.display ) {
			return false;
		}
	} while( ( el = el.parentNode ) && el.tagName != 'BODY' );
	
	return true;
}

PA.utils.DOM.isIE = function() {
	return ( 'Microsoft Internet Explorer' == navigator.appName );
}

PA.utils.DOM.isChrome = function() {
	return ( navigator.userAgent.toLowerCase().indexOf( 'chrome' ) > -1 );
}

PA.utils.DOM.ltIE7 = function() {
	var rv 				= false;
	if( PA.utils.DOM.isIE() ) {
		var v			= navigator.appVersion;
		var n 			= v.indexOf( 'MSIE' );
		if( n >= 0 ) {
			var vv 	= parseInt( v.substring( n+4, v.length ) );
			if( vv < 7 ) {
				rv 		= true;
			}
		}
	}
	return rv;
}

PA.utils.DOM.isSafari = function() {	// Safari
	return ( navigator.userAgent.toLowerCase().indexOf( 'safari' ) >= 0 );

}

PA.utils.DOM.disableDocBehaviours = function( doc ) {
		// no context menu
	doc.oncontextmenu		= function() { return false; };
		// no selection
	doc.onselectstart		= new Function ( 'return false' );	// IE
	if( !PA.utils.DOM.isIE() ) {	// non-IE
		doc.onmousedown 	= function() { return false; };
		doc.onclick 		= function() { return true; };
	}
}

PA.utils.DOM.pageBeenSaved = function( wnd ) {
	return ( wnd.location.href.indexOf( 'file:///' ) >= 0 );
}

PA.utils.DOM.getDomain = function( wnd ) {
	var n 	= wnd.location.href.lastIndexOf( '/' );
	var fwd	= true;
	if( n < 0 ) {
		n 	= wnd.location.href.lastIndexOf( '\\' );
		fwd	= false;
	}
	if( n < 0 ) {
		alert( 'Page address is incorrect.' );
		return '';
	}
	return decodeURI( wnd.location.href.substr( 0, n ) + ( fwd ? '/' : '\\' ) );
}				

