// JavaScript Document - LAST MODIFICATION: 11-05-2007
// MADE BY: RottenEye @ dbwt | www.dbwt.co.uk (Please don't remove this line)
// If you have any doubt referring to this document please don't be afraid, contact me at rotten_eye@iol.pt


var dbwt = new Object();
	dbwt.browserVer = null;



dbwt.getBrowserVer = function(){/*
  Desc: get the browser name and verssion; */

	if( document.all && navigator.language )// opera
		return "opera";
	else if( !document.all )// NN / FF
		return "gecko"; 
	else if( document.all && !navigator.language ){// IE
		if( -1 == navigator.appVersion.indexOf("MSIE 7") ){
			return "ie6";
		}else if( -1 != navigator.appVersion.indexOf("MSIE 7") )
			return "ie7";
	}
}


dbwt.getBrowser = function(browser){/*
  Desc: compare a given browser verssion with the current browser verssion and return true or false;
  browser (string) = browser verssion to compare; */

	browser = browser.toLowerCase();
	if( this.browserVer === browser )
		return true;	
	else if( browser == "ie" && this.browserVer.substr(0, 2) == browser )
		return true;
	return false;
}


dbwt.addonLoadEvent = function(functionName, browser, replaceEvt){/*
  Desc: add a function to the page onload event;
  functionName (string) = function name to be executed on page load;
  browser (string)      = browsers where the event will be attached, possible values: all | gecko | IE | IE6 | IE7 | opera;
  replaceEvt (bool)     = tell the function to replace the existing event methods with the new expression, default: false; */

	browser = browser.toLowerCase();
	if( !replaceEvt ) var replaceEvt = false;

	if( browser != "all" && this.browserVer != browser ){
		if( this.browserVer.substr(0, 2) != browser )
			return false;
	}
	if( functionName.substr(functionName.length - 1, 1) != ")" && functionName.substr(functionName.length - 1, 1) != ";" )
		functionName += "();";
	else if( functionName.substr(functionName.length - 1, 1) != ";" )
		functionName += ";";

	return this.setEvent(window, "onload", functionName, replaceEvt);
}


dbwt.addonUnloadEvent = function(functionName, browser, replaceEvt){/*
  Desc: add a function to the page onload event;
  functionName (string) = function name to be executed on page load;
  browser (string)      = browsers where the event will be attached, possible values: all | gecko | IE | IE6 | IE7 | opera;
  replaceEvt (bool)     = tell the function to replace the existing event methods with the new expression, default: false; */

	browser = browser.toLowerCase();
	if( !replaceEvt ) var replaceEvt = false;

	if( browser != "all" && this.browserVer != browser ){
		if( this.browserVer.substr(0, 2) != browser )
			return false;
	}

	if( functionName.substr(functionName.length - 1, 1) != ")" && functionName.substr(functionName.length - 1, 1) != ";" )
		functionName += "();";
	else if( functionName.substr(functionName.length - 1, 1) != ";" )
		functionName += ";";

	return this.setEvent(window, "onunload", functionName, replaceEvt);
}


dbwt.addEventToElements = function(element, _event, executeExp, property, propValue, replaceEvent){/*
  Desc: function that add an event to a group of elements;
  element (string)    = is the element where you whant apply the event (ex: td);
  _event (string)     = event name (ex: onclick);
  executeExp (string) = expression / code to execute when the event is triggered;
  property (string)   = is the property, of the element, to search for (ex: class) - if null is ignored;
  propValue (string)  = value of the element property "property" - if null is ignored;
  replaceEvent (bool) = tell the function to replace the existing event methods with the new expression, default: false; */

	var elmts, curElem, testExp;
	elmts = document.getElementsByTagName(element);

	if( !_event ) var _event = "onclick";
	if( !replaceEvent ) var replaceEvent = false;
	if( executeExp.substr(executeExp.length - 1, 1) != ";" ) executeExp += ";";

	switch( property ){
		case "class":
			if( this.getBrowser("ie") )
				property = "className";
			break;
	}

	for( var k = 0, len = elmts.length; k < len; k++ ){
		curElem = elmts[k]; testExp = "true";

		if( property ){
			if( propValue )
				testExp = "curElem.getAttribute('" + property + "') == '" + propValue + "'";
			else
				testExp = "curElem.getAttribute('" + property + "')";
		}
		if( eval(testExp) )
			this.setEvent(curElem, _event, executeExp, replaceEvent);
	}
}


dbwt.setScroll = function(obj, x, y){/*
  Desc: set the window scroll position
  x (int)                   = position for the horizontal scroll bar;
  y (int)                   = position for the vertical sroll bar;
  object (object or string) = window object or window name that will receive the new scroll position;*/

	if( typeof obj == "string" )
		obj = eval(obj);
	obj.scrollTo(x, y);
}


dbwt.urlencode = function(url){/*
  Desc: this function encode a string to be used as a url;
  url (string) = string to be encoded; */

	return escape(url).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}


dbwt.navigate = function(url, target, history){/*
  Desc: this function replace or navigate to a specific page, in a given target;
  url (string)    = destiny url;
  target (string) = target where the page will be loaded: _blank is skipped, _self (default) will load the url in the same window / frame where this function is called
					and if none of this values are used, the function will try to load the url in a frame named with same value of the target var;
  history (bool)  = true will replace the url in the browser history, false (default) will do the default browser behavior; */

	if( !target ) target = "_self";
	if( !history ) history = true;

	if( target == "_blank" ){
		return false;
	}else if( target == "_self" ){
		if( history == true ) window.location.href = url;
		else window.location.replace(url);
	}else if( target == "_top" ){
		if( history == true ) top.location.href = url;
		else top.location.replace(url);
	}else{
		if( history == true ) document.frames[target].location.href = url;
		else document.frames[target].location.replace(url);
	}
}


dbwt.OpenWindow = function(url, pName, _width, _height, userFeatures){/*
  Desc: open a popup window centered in the screen and returns a reference to the new created window;
  url (string)          = url to open in the new window;
  pName (string)        = nname for the new window;
  _width (int)          = witdth of the new window;
  _height (int)         = height of the new window;
  userFeatures (string) = optional string that contains settings to use in the last param of the window.open method; */

	var wFeatures;
	if( !(_width == null || _height == null) ){
		if( screen ){
			var leftpos = (screen.width / 2) - (_width / 2);
			var toppos  = ( (screen.height / 2) - (_height / 2) ) - 20;
		}

		if( userFeatures ){
			wFeatures = "";
			if( userFeatures.indexOf("width=") == -1 )
				wFeatures += "width=" + _width + ",";
			if( userFeatures.indexOf("height=") == -1 )
				wFeatures += "height=" + _height + ",";
			if( userFeatures.indexOf("top=") == -1 )
				wFeatures += "top=" + toppos + ",";
			if( userFeatures.indexOf("left=") == -1 )
				wFeatures += "left=" + leftpos + ",";
			wFeatures += userFeatures;
			if( wFeatures.substr(wFeatures.length - 1, 1) == "," )
				wFeatures = wFeatures.substr(0, wFeatures.length - 1);
		}else{
			wFeatures = "width=" + _width + ",height=" + _height + ",top=" + toppos + ",left=" + leftpos;
		}

		return window.open(url, pName, wFeatures);
	}else
		return window.open(url, pName);
}


dbwt.display_text_banner = function(box, text, num_spaces, speed, _1){/*
  Desc: transform an input text type into a text banner;
  box (string / object) = text box to transform;
  text (string)         = text to display in the banner, if not defined, the value of the text box will be used;
  num_spaces (int)      = spaces to add to the base string;
  speed (int)           = banner speed;
  NOTE: you have to add this function on page load to properly create the text banner; */

	var fText;
	if( typeof box == "string" ) box = document.getElementById(box);
	if( !text ) var text = box.value;

	if( !_1 ){// if is the first time
		for( var k = 0; k <= num_spaces; k++ )
			text += " ";
	}else
		text = text.substr(1) + text.substr(0, 1);

	box.value = text;
	// start / restart timeout
	return setTimeout("dbwt.display_text_banner('" + box.id + "', null, " + num_spaces + ", " + speed + ", true)", speed);
}


dbwt.htmlentities = function(_string, quoteStyle){/*
  Desc: convert some caractres to her equivalents html entity;
  _string (string)    = string to convert;
  quoteStyle (string) = possible values: ENT_COMPAT | ENT_QUOTES | ENT_NOQUOTES;
						ENT_COMPAT   -> Will convert double-quotes and leave single-quotes alone.
						ENT_QUOTES   -> Will convert both double and single quotes.
						ENT_NOQUOTES -> Will leave both double and single quotes unconverted.*/

	if( !quoteStyle ) quoteStyle = "ENT_QUOTES";

	_string = _string.replace(/[&]/gi, "&amp;");
	_string = _string.replace(/[<]/gi, "&lt;");
	_string = _string.replace(/[>]/gi, "&gt;");

	if( quoteStyle == "ENT_COMPAT" ){
		_string = _string.replace(/["]/gi, "&quot;");
	}else if( quoteStyle == "ENT_QUOTES" ){
		_string = _string.replace(/["]/gi, "&quot;");
		_string = _string.replace(/[']/gi, "&#39;");
	}
	return _string;
}


dbwt.correctPNG = function(){/*
  Desc: correct the png images render problem in IE6- ; */

	var img, imgName, imgID, imgClass, imgTitle, imgStyle, strNewHTML;

	for( var i = 0, len = document.images.length; i < len; i++){
		img     = document.images[i];
		if( typeof img !== "object" ) continue;
		imgName = img.src.toLowerCase();

		if( imgName.substr(imgName.length - 3) == "png" ){
			imgID    = ( img.id ) ? ( 'id="' + img.id + '" ' ) : ( "" );
			imgClass = ( img.className ) ? ( 'class="' + img.className + '" ' ) : ( "" );
			imgTitle = ( img.title ) ? ( 'title="' + img.title + '" ' ) : ( 'title="' + img.alt + '" ' );
			imgStyle = "display:inline-block;" + img.style.cssText;

			if (img.align == "left")    imgStyle  = "float:left;"  + imgStyle;
			if (img.align == "right")   imgStyle  = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle  = "cursor:hand;" + imgStyle;

			strNewHTML = '<span ' + imgID + imgClass + imgTitle
			+ ' style="' + 'width:' + img.width + 'px; height:' + img.height + 'px;' + imgStyle
			+ 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader'
			+ '(src=\'' + imgName + '\', sizingMethod=\'image\');"></span>';
			img.outerHTML = strNewHTML;
			i = i - 1;
		}
	}
}


dbwt.getPageRealSize = function(){/*
  Desc: Get the page measures, including scroll; */

	if( document.documentElement )
		var _document = document.documentElement;// Quirk Mode
	else
		var _document = document.body;// Strict Mode

	if( window.innerHeight && window.scrollMaxY ){// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	}else if( _document.scrollHeight > _document.offsetHeight ){// all but Explorer Mac
		yWithScroll = _document.scrollHeight;
		xWithScroll = _document.scrollWidth;
	}else{// works in Explorer 6+, Mozilla (not FF) and Safari
		yWithScroll = _document.offsetHeight + _document.offsetTop;
		xWithScroll = _document.offsetWidth + _document.offsetLeft;
  	}

	arrayPageSizeWithScroll = new Array(xWithScroll, yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}


dbwt.setFlashMeasures = function(objID, minWidth, minHeight, maxWidth, maxHeight){/*
  Desc: Set the width and/or the height of the tag object that holds the flash movie
  		Making possible to specify a max/min width/height for the flash movie (when you want it 100% of the page)
		If the window is >minWidth && <maxWidth, than the flash movie width will be 100%, if not, minWidth or maxWidth will be used;
  objID (string)  = id of the OBJECT tag that have the flash movie;
  minWidth (int)  = minimum width allowed for the flash movie to have;
  minHeight (int) = minimum height allowed for the flash movie to have;
  maxWidth (int)  = maximum width allowed for the flash movie to have;
  maxHeight (int) = maximum height allowed for the flash movie to have; */

	if( !minWidth ) minWidth = 0;
	if( !minHeight ) minHeight = 0;
	if( !maxWidth ) maxWidth = 0;
	if( !maxHeight ) maxHeight = 0;

	this.setEvent(window, "onload", "dbwt.flash_targetFlashObject = document.getElementById('" + objID + "');", false);
	this.setEvent(window, "onload", "dbwt.onResizeRepairFlashMeasures(" + minWidth + ", " + minHeight + ", " + maxWidth + ", " + maxHeight + ");", false);
	this.setEvent(window, "onresize", "dbwt.onResizeRepairFlashMeasures(" + minWidth + ", " + minHeight + ", " + maxWidth + ", " + maxHeight + ");", false);
}




/**
	PRIVATE METHODS
**/



dbwt.setEvent = function(elem, _event, _exp, replaceEvt){/*
  Desc: this function attach an exp to a an element event, preserving the this scope;
  elem (object)   = element object to receive the expression in a given event;
  _event (string) = event name (ex: onclick);
  exp (string)    = expression / code to execute when the event "_event" is triggered;
  replaceEvt (bool) = tell the function to replace the current event expression with the new one, default: false; */

	if( !elem || !_exp ) return false;
	if( !_event ) var _event = "onclick";
	if( !replaceEvt ) var replaceEvt = false;

	var curEvt, curEvt_str, newFunction;

	_event     = _event.toLowerCase();
	curEvt     = eval("elem." + _event);
	curEvt_str = ( typeof curEvt == "function" ) ? (" " + curEvt) : ("");
	curEvt_str = curEvt_str.replace(/return[\s]/ig, "");// removing returns
	curEvt_str = curEvt_str.replace(/(.|\r\n|\n)*[\{]/, "");
	curEvt_str = curEvt_str.replace(/[\}][\s]?$/, "");

	if( replaceEvt === true )
		newFunction = new Function( _exp );
	else
		newFunction = new Function( curEvt_str + _exp );
	eval("elem." + _event + " = " + newFunction);
	return true;
}


dbwt.onResizeRepairFlashMeasures = function(minWidth, minHeight, maxWidth, maxHeight){/*
  Desc: Set the width and/or the height of the tag object that holds the flash movie
  		Making possible to specify a max/min width/height for the flash movie (when you want it 100% of the page)
		If the window is >minWidth && <maxWidth, than the flash movie width will be 100%, if not, minWidth or maxWidth will be used;
  minWidth (int)  = minimum width allowed for the flash movie to have;
  minHeight (int) = minimum height allowed for the flash movie to have;
  maxWidth (int)  = maximum width allowed for the flash movie to have;
  maxHeight (int) = maximum height allowed for the flash movie to have; */

	var fl_obj = this.flash_targetFlashObject;
	if( !fl_obj ) return false;

	var sWidth  = document.body.clientWidth;
	var sHeight = document.body.clientHeight;

	if( sWidth != null && sWidth > maxWidth )
		fl_obj.style.width = maxHeight + "px";
	else if( sWidth != null && sWidth <= minWidth )
		fl_obj.style.width = minWidth + "px";
	else
		fl_obj.style.width = "100%";

	if( sHeight != null && sHeight > maxHeight )
		fl_obj.style.height = maxHeight + "px";
	if( sHeight != null && sHeight <= minHeight )
		fl_obj.style.height = minHeight + "px";
	else
		fl_obj.style.height = "100%";
}


/* --------------------------------------------------------------------------------------------------- */

dbwt.init = function(){
	this.browserVer = this.getBrowserVer();
	if( this.getBrowser("IE") )
		this.setEvent(window, "onload", "dbwt.correctPNG();", false);
}
dbwt.init();