// MM Functions
function preloadImages()
{
  if(document.images)
  {
    if(!document.imageArray) 
        document.imageArray = new Array();
    var i,j = document.imageArray.length, args = preloadImages.arguments;

    for(i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!=0)
      {
        document.imageArray[j] = new Image;
        document.imageArray[j++].src = args[i];
      }
    }
  }
}

/* Common functionality */
function getBasename(path, suffix) 
{
    var b = path.replace(/^.*[\/\\]/g, '');
    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) 
    {
        b = b.substr(0, b.length-suffix.length);
    }
    return b;
}

function in_array( val, arr )
{
	for ( var i in arr )
	{
		if (arr[i]==val)
			return true;
	}
	return false;
}

function getUniqID()
{
	var now = new Date();
	var rnd1 = Math.ceil( Math.random()*17 );
	var rnd2 = Math.ceil( Math.random()*13 );
	return parseInt( now.getTime() * rnd1 / rnd2 );
}

function include(js) {
	$('head').append('<script type="text/javascript" src="js/'+js+'"></script>');
}

/* Cookies */
function createCookie(name,value,days) 
{
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) 
{
    createCookie(name,"",-1);
}

/* strings */
function replace_char(s,c,n)
{
	var re = new RegExp('^(.{'+ n +'}).(.*)$','');
	return s.replace(re,'$1'+c+'$2');
}

if ('undefined' == typeof String.prototype.ltrim) {
  String.prototype.ltrim = function() {
    return this.replace(/^\s+/, '');
  }
}

if ('undefined' == typeof String.prototype.rtrim) {
  String.prototype.rtrim = function() {
    return this.replace(/\s+$/, '');
  }
}

if ('undefined' == typeof String.prototype.trim) {
  String.prototype.trim = function() {
    return this.replace(/^\s+/, '').replace(/\s+$/, '');
  }
}

/* URL decode|encode */
var _BGD_Url = {
    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },
 
    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },
 
    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
 
        for (var n = 0; n < string.length; n++) 
        {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },
 
    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
 
        while ( i < utftext.length ) 
        {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

/* popups */
function mpopup (url, id)
{
    var wn = 'pw'+id;
    window.open(url, wn);
    return false;
}

/***   Browser Detection   ***/
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

function iefixselectswidth(markerclass)
{
	if( BrowserDetect.browser=='Explorer' )
	{
		$("select." + markerclass).each(function(){
			var w = parseInt($(this).css("width"));
			if( !isNaN(w) && w )
			{
				var direction = ($(this).css("direction")=='ltr') ? "direction:rtl;" : "direction:ltr;";
				var display = ( $(this).css("display")=='block' ) ? "block" : "inline-block";
				$(this).wrap('<div style="display:'+display+'; width:'+w+'px; overflow:hidden;'+direction+'" />');
				$(this)
					.focus(function(){
						var w = parseInt($(this).css("width"));
						if( !isNaN(w) && w )
						{
							$(this).data("origWidth", $(this).css("width"));
							$(this).css("width", "auto");
						}
						$(this).data("infocus", "yes");
					})
			
					.mouseover(function(){
						var w = parseInt($(this).css("width"));
						if( !isNaN(w) && w )
						{
							$(this).data("origWidth", $(this).css("width"));
							$(this).css("width", "auto");
						}
					})
			
					.change(function(){
						$(this).css("width", $(this).data("origWidth"));
					})
			
					.mouseout(function(){
						if( $(this).data("infocus") != "yes" )
							$(this).css("width", $(this).data("origWidth"));
					})
			
					.blur(function(){
						$(this).css("width", $(this).data("origWidth"));
						$(this).data("infocus", "no");
					});
			}
		});
	}
}
/******************************************************/
BrowserDetect.init(); // BrowserDetect.browser | BrowserDetect.version | BrowserDetect.OS
var usebgiframe = (BrowserDetect.browser=='Explorer' && BrowserDetect.version<7) ? true : false;
