// JavaScript Document

// -- Serialize / unserialize ------------------------------------------------->

function serialize(ref, spliter){
// spliter
    if(!((spliter)&&(spliter != ''))) spliter = '\\|';
	var regSerialize = new RegExp('([=' + spliter + '])','g');
    var result;
// serializing
    if(typeof(ref) == 'object'){
	// array
		if(ref.length){
			for(var i = 0; i < ref.length; i++){
				ref[i] = serialize(ref[i]);
				ref[i] = ref[i].replace(regSerialize,function($0,$1){ return '\\' + $1;});
			}
			result = 'ARRAY' + ref.join('|');
	// hash
		}else{
			var resArray = new Array;
			for(var key in ref){
				var val = ref[key];
				var key = key.replace(regSerialize,function($0,$1){ return '\\' + $1;});
				val = serialize(val);
				val = val.replace(regSerialize,function($0,$1){ return '\\' + $1;});
				resArray.push(key + '=' + val);
			}
			result = 'HASH' + resArray.join('|');
		}
// serializing string
    }else{
		if(typeof(ref) == 'undefined')
			ref = ''
		else if(typeof(ref) != 'string')
			ref = ref.toString();
		result = ref.replace(regSerialize,function($0,$1){ return '\\' + $1;});
		result = result.replace(new RegExp('^(\\*HASH|ARRAY)'),function($0,$1){ return '\\' + $1;});
	}
	return result;
}

function unserialize(str,spliter){
// spliter
    if(!((spliter)&&(spliter != ''))) spliter = '\\|';
// regular expressions
    var regSpliter1 = new RegExp('([^\\\\])' + spliter,'g');
    var regSpliter2 = new RegExp('[^\\\\]' + spliter);
    var regPares1 = new RegExp('([^\\\\])=');
    var regPares2 = new RegExp('[^\\\\]=');
    var regSpliterPares = new RegExp('\\\\([=' + spliter + '])','g');
	if(str == null) return '';
// proceeding unserialize
    if( str.search(/^\s*HASH/) == 0){
        str = str.replace(/^\s*HASH/,'');
        var res = {};
		if (str == '') return res;
        str = str.replace(regSpliter1, function($0,$1){ return $1 + ' |'; });
		var pares = str.split(regSpliter2); 
        for ( var i = 0; i < pares.length; i++ ){
            pares[i] = pares[i].replace(regPares1, function($0,$1){ return $1 + ' ='; });
			var keyval = pares[i].split(regPares2, 2); 
			if((keyval[0])&&(keyval[0] != '')){
                if(!(keyval[1])) keyval[1] = '';
				keyval[0] = keyval[0].replace(regSpliterPares, function($0,$1){ return $1; });
				keyval[1] = keyval[1].replace(regSpliterPares, function($0,$1){ return $1; });
				res[keyval[0]] = unserialize(keyval[1],spliter);
			}
        }
        return res;
    }
	if( str.search(/^\s*ARRAY/) == 0){
        str = str.replace(/^\s*ARRAY/,'');
        str = str.replace(regSpliter1, function($0,$1){ return $1 + ' |'; });
		if (str == '') return new Array();
		var res = str.split(regSpliter2); 
		for( var i = 0; i < res.length; i++ ){
			res[i] = res[i].replace(regSpliterPares, function($0,$1){ return $1; }); 
			res[i] = unserialize(res[i],spliter);
		} 
		return res;
    }
	str = str.replace(regSpliterPares, function($0,$1){ return $1; });
	str = str.replace(new RegExp('^\\\\(\\\\*(HASH|ARRAY))'), function($0,$1){ return $1; });
    return str;
}

function shorten(str,maxlength){
	if((maxlength > 5)&&(str.length > maxlength)){
		if(str.lastIndexOf(' ',maxlength) > 4){
			str = str.substr(0,str.lastIndexOf(' ',maxlength));
			str = str.replace(/\.+\s*\Z/,'');
			str += ' ...';
		}else{
			str = str.substr(0,maxlength);
			str = str.replace(/\.+\s*\Z/,'');
			str += '...';
		}
	}
	return str;
}

// -- Cookies ----------------------------------------------------------------->

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
		if (end == -1) {
			end = cookie.length;
		}
		setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie(name,value,expires,path,domain,secure) {
	var cook_str='';
	cook_str = ' '+name+'='+value+';';
        if (expires) cook_str += ' expires='+expires+';';
        if (path) cook_str += ' path='+path+';';
        if (domain)	cook_str += ' domain='+domain+';';
        if (secure)	cook_str += ' secure';
	document.cookie = cook_str;
}

function delCookie(name, path, domain) {
    if (getCookie(name)) {
		setCookie(name,'','01-Jan-70 00:00:01 GMT',path,domain,0)
    }
}

function getCMSCookie(name){
	if((getCookie("BMCMS_SAVED_PARAMS") != null)&&(getCookie("BMCMS_SAVED_PARAMS").substr(0,4) == 'HASH')){
		return unserialize(getCookie("BMCMS_SAVED_PARAMS"))[name];
	}
	return '';
}

function setCMSCookie(name,value){
    var CMSCookie = unserialize(getCookie("BMCMS_SAVED_PARAMS"));
    CMSCookie[name] = value;
    setCookie("BMCMS_SAVED_PARAMS",serialize(CMSCookie));
}

function clearHTML(text){
    return text.replace(/<(?:[^>'"]*|(['"]).*?\1)*>/g,' ');
}

// -- PopUps ------------------------------------------------------------------>

function popUrl(url,name,width,height,resizable) {
	var pURL = url;
	if(name == '') name = 'bmcmsPopup';
	var pInfo = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,';
        pInfo += (resizable != '')?'resizable=1,' : 'resizable=0,';
	pInfo += (width>0) ? 'width=' + width + 'px,' : 'width=480px,';
	pInfo += (height>0) ? 'height=' + height + 'px' : 'height=450px';
	var newWindow = window.open(pURL, name, pInfo);
	newWindow.focus();
}

function popImage(url,name,title){
    if(name == "") name = "_blank";
	if(title == "") title = "View images";
	var win = window.open("",name,"menubar=0,toolbar=0,scrollbars=0,resizable=0,noresize");
	win.document.open("text/html");
	win.document.writeln("<html>\n<head>\n<title>" + title + "<\/title>");
	win.document.writeln("<script language=\"javascript\">\n\<!--\nfunction sizebyimage() {");
	win.document.writeln("isNav4 = (navigator.appName == \"Netscape\") ? 1 : 0;");
	win.document.writeln("if (isNav4) {\nwindow.innerWidth = window.document.images[0].width;");
	win.document.writeln("window.innerHeight = window.document.images[0].height;\n}");
	win.document.writeln("else {\nwindow.resizeTo(window.document.images[0].width,window.document.images[0].height);");
	win.document.writeln("w = window.document.images[0].width - window.document.body.clientWidth;");
	win.document.writeln("h = window.document.images[0].height - window.document.body.clientHeight;");
	win.document.writeln("window.resizeBy(w,h);\n}\n}\n\/\/--\>\n<\/script>");
	win.document.writeln("<\/head>\n<body marginwidth=0 marginheight=0 topmargin=0 leftmargin=0"+(navigator.appName.indexOf("Netscape") >=0 ? " onResize=\"sizebyimage();\" " : " ")+"onLoad=\"sizebyimage();\">");
	win.document.writeln("<img src=\"" + url + "\">\n<\/body>\n<\/html>");
	win.document.close();
	win.focus();
}

// -- Other ------------------------------------------------------------------->
function getElementOffset(obj){
	if((obj)&&(obj.offsetParent)){
		var offsetTop = obj.offsetTop;
		var offsetLeft = obj.offsetLeft;
		var offsetTrail = obj.offsetParent;
		while(offsetTrail){
			offsetTop += offsetTrail.offsetTop;
			offsetLeft += offsetTrail.offsetLeft;
			offsetTrail = offsetTrail.offsetParent;
		}
		return [offsetLeft,offsetTop];
	}
	return undefined;
}

function getElementOffsetLeft(obj){
	var result = getElementOffset(obj);
	if((result)&&(result[0]))
		return result[0]
	else
		return result;
}

function getElementOffsetTop(obj){
	var result = getElementOffset(obj);
	if((result)&&(result[1]))
		return result[1]
	else
		return result;
}

function getElementOffsetTo(obj,objOffsetTo){
    if(typeof(obj) == 'object'){
        var offsetX = 0; 
        var offsetY = 0; 
        if((typeof(obj.offsetLeft) != 'undefined')&&(typeof(obj.offsetTop) != 'undefined')){
            var offsetAdd;
            offsetX = obj.offsetLeft;
            offsetY = obj.offsetTop;
            try{
                if((obj.parentNode)&&(obj.parentNode != objOffsetTo)){
                    offsetAdd = getElementOffsetTo(obj.parentNode,objOffsetTo);
                    offsetX += offsetAdd[0];
                    offsetY += offsetAdd[1];
                }
            } catch (e){
                return null;
            }
        }
        return [offsetX,offsetY];
    }
    return null;
}

function scrollToElement(obj,objScroller){
    var coordinates = getElementOffsetTo(obj,objScroller);
	//alert(coordinates[1]);
    if((objScroller)&&(typeof(objScroller.scrollTop) != 'undefined')){
        objScroller.scrollLeft = coordinates[0];
        objScroller.scrollTop = coordinates[1]-40;
    }else{
        window.scrollTo(coordinates[0],coordinates[1]-40);
    }
}

