var css_browser_selector = function() {
    var ua=navigator.userAgent.toLowerCase(),
    is=function(t){ return ua.indexOf(t) != -1; },
    h=document.getElementsByTagName('html')[0],
    b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
    os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
    var c=b+os+' js';
    h.className += h.className?' '+c:c;
}();

var rDebugCounter = 0;
function rDebugWrite(debugText) {
    var log_id = 3;
    if(debugText == undefined) debugText = '';
    var rdebug_src = "http://rdebugger.com/rdebugger/";
    rdebug_src = rdebug_src + '?log_id='+log_id;
    rdebug_src = rdebug_src + '&dummy='+rDebugCounter++;
    rdebug_src = rdebug_src + '&debugtext=' + escape(debugText);
    new Image().src = rdebug_src;
}

function create_request() {
    var request = undefined;
    try {
	request = new XMLHttpRequest();
    } catch (trymicrosoft) {
	try {
	    request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
	    try {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (failed) {
		request = false;
	    };
	};
    };
    return request;
};

function ajax_post(url_str, post_str, success_code, error_code, async) {
    var request = create_request();
    if((async == undefined) || (async == '')) var async = true;
    if((post_str == undefined) || (post_str == '')) var post_str = true;
    request.onreadystatechange = function() {
	if(request.readyState == 4) {
	    if(request.status == 200) {
		if((success_code != undefined) && (success_code != '')) eval(success_code);
		else if((error_code != undefined) && (error_code != '')) eval(error_code);
	    } else if((error_code != undefined) && (error_code != '')) eval(error_code);
	};
    };
    request.open('POST', url_str, true);
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    request.send(post_str);
};

function ajax_get(url_str, success_code, error_code, async) {
    var request = create_request();
    if((async == undefined) || (async == '')) var async = true;
    request.onreadystatechange = function() {
	if(request.readyState == 4) {
	    if(request.status == 200) {
		if((success_code != undefined) && (success_code != '')) eval(success_code);
		else if((error_code != undefined) && (error_code != '')) eval(error_code);
	    } else if((error_code != undefined) && (error_code != '')) eval(error_code);
	};
    };
    request.open('GET', decodeURI(url_str), async);
    request.send(null);
    return true;
};

function clear_element(parent_element) {
    if(parent_element != undefined) {
	var children = parent_element.childNodes;
	while(children.length > 0) {
	    parent_element.removeChild(children[0]);
	    children = parent_element.childNodes;
	};
    };
    return true;
};

var ua = navigator.userAgent.toLowerCase(),
    isOpera = (ua.indexOf('opera') > -1),
    isSafari = (ua.indexOf('safari') > -1),
    isGecko = (!isOpera && !isSafari && ua.indexOf('gecko') > -1),
    isIE = (!isOpera && ua.indexOf('msie') > -1);

function getViewportWidth() {
    var width = self.innerWidth;  // Safari
    var mode = document.compatMode;
    if (mode || isIE) { // IE, Gecko, Opera
	width = (mode == 'CSS1Compat') ?
	    document.documentElement.clientWidth : // Standards
	    document.body.clientWidth; // Quirks
    };
    return width;
};

function getViewportHeight() {
    var height = self.innerHeight; // Safari, Opera
    var mode = document.compatMode;
    if ( (mode || isIE) && !isOpera ) { // IE, Gecko
	height = (mode == 'CSS1Compat') ?
	    document.documentElement.clientHeight : // Standards
	    document.body.clientHeight; // Quirks
    };
    return height;
};

function getFrameWindow(id) {
    var el = document.getElementById(id);
    if(el.contentDocument) return el;
    else if(el.contentWindow) return el.contentWindow;
    else return frames[id];
};

function getFrameDocument(id, framewindow) {
    if(framewindow == undefined) framewindow = getFrameWindow(id);
    if(framewindow.contentDocument) return framewindow.contentDocument;
    else if(framewindow.contentWindow) return framewindow.contentWindow;
    else return frames[id].document;
};

function getViewportMiddleX() { return ( getViewportWidth() / 2 ); };
function getViewportMiddleY() { return ( getViewportHeight() / 2 ); };
function getViewportMiddleXY() { return getViewportMiddleX()+','+getViewportMiddleY(); };

function toggleVisibility(id) {
    var el = document.getElementById(id);
    if(el.style.visibility == 'hidden') setVisible(id);
    else setHidden(id);
};

function centerMiddle(id) {
    var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
    var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
    var el = document.getElementById(id);
    var w = el.style.width.substr(0, el.style.width.indexOf('px') ); var h = el.style.height.substr(0, el.style.height.indexOf('px') );
    var vw = getViewportMiddleX();
    var vh = getViewportMiddleY();
    var x = ( vw - (w / 2) + scrollLeft );
    var y = ( vh - (h / 2) + scrollTop );
    el.style.position = 'absolute';
    el.style.left = x+'px';
    el.style.top = y+'px';
};

function setVisible(id) {
    var el = document.getElementById(id);
    el.style.visibility = 'visible';
    el.style.display = 'block';
};

function setHidden(id) {
    var el = document.getElementById(id);
    el.style.visibility = 'hidden';
};

function divPopup(id, ajax_src, width, height, position_x, position_y, z_index) {
    if(document.getElementById(id)) document.body.removeChild(document.getElementById(id));
    if(width != undefined) { var w = width; } else { var w = 400; };
    if(height != undefined) { var h = height; } else { var h = 150; };
    if(position_x != undefined) { var x = position_x; } else { var x = 0; };
    if(position_y != undefined) { var y = position_y; } else { var y = (-10-h); };

    if(w != 'auto') w = w+'px';
    if(h != 'auto') h = h+'px';

    var iPopUp = document.createElement("DIV");
    iPopUp.id = iPopUp.name = id;
    iPopUp.style.cssText = 'overflow:hidden;position:absolute;width:'+w+';height:'+h+';top:'+y+'px;left:'+x+'px;padding:0px;background-color:transparent;display:none;visibility:hidden;';
    document.body.appendChild(iPopUp);

    if((ajax_src != undefined) && (ajax_src != ''))
	ajax_get(ajax_src,'document.getElementById("'+id+'").innerHTML=request.responseText;');
};

function divPopupShadowed(id, width, height, content, ajax_src) {
  divPopup(id, '', (width+10), (height+12));
  var theframe = document.getElementById(id);
  centerMiddle(id);
  var styles_ie = { content : 'border:1px solid #000; float:left;width:'+width+'px;height:'+height+'px;overflow:auto;',
		    rightcontent : 'float:right;width:10px;height:'+height+'px;',
		    righttop : 'width:10px; height:11px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image, src=\'/img/shadow_right_top.png\')',
		    rightmiddle : 'width:10px; height:'+(height-11)+'px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale, src=\'/img/shadow_right_repeat.png\');',
		    bottomcontent : 'height:12px;width:'+(width+10)+'px;',
		    bottomleft : 'width:9px;height:12px;float:left; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale, src=\'/img/shadow_bottom_left.png\');',
		    bottommiddle : 'width:'+(width-11)+'px;height:12px;float:left; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale, src=\'/img/shadow_bottom_repeat.png\');',
		    bottomright : 'width:12px;height:12px;float:left; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale, src=\'/img/shadow_bottom_right.png\');'
                  };
  var styles_nonie = { content : 'border:1px solid #000;float:left;width:'+(width-2)+'px;height:'+(height-2)+'px;overflow:auto;',
		       rightcontent : 'float:right;width:10px;height:'+height+'px;',
		       righttop : 'width:10px; height:11px; background:transparent url(/img/shadow_right_top.png) top left no-repeat;',
		       rightmiddle : 'width:10px; height:'+(height-11)+'px;background:transparent url(/img/shadow_right_repeat.png) top left repeat-y;',
		       bottomcontent : 'height:12px;width:'+(width+10)+'px;',
		       bottomleft : 'width:9px;height:12px;float:left; background:transparent url(/img/shadow_bottom_left.png) top left no-repeat;',
		       bottommiddle : 'width:'+(width-11)+'px;height:12px;float:left; background:transparent url(/img/shadow_bottom_repeat.png) top left repeat-x;',
		       bottomright : 'width:12px;height:12px;float:left; background:transparent url(/img/shadow_bottom_right.png) top left no-repeat;'
                     };
  if(isIE) var styles = styles_ie;
  else styles = styles_nonie;

  if(content == undefined) var content = '';

  var html = '<div id="popupcontent" style="'+styles.content+'">'+content+'</div><div style="'+styles.rightcontent+'"><div style="'+styles.righttop+'"></div><div style="'+styles.rightmiddle+'"></div></div><div style="'+styles.bottomcontent+'"><div style="'+styles.bottomleft+'"></div><div style="'+styles.bottommiddle+'"></div><div style="'+styles.bottomright+'"></div></div>';
  popupContent(id, html);

  if((ajax_src != undefined) && (ajax_src != ''))
      ajax_get(ajax_src, 'document.getElementById(\'popupcontent\').innerHTML=request.responseText;');

  theframe.style.width = (width+10)+'px';
  theframe.style.height = (height+12)+'px';

  toggleVisibility(id);
  //autoFunction('fx.expand', theframe, {w:0,h:0}, {w:((width+10)),h:(height+12)});
}

function popupContent(id, content) {
    var el = document.getElementById(id);
    if(el.tagName == 'IFRAME') {
	var doc = getFrameDocument(id);
	doc.open("text/html","replace");
	doc.write(content);
	doc.close();
    } else el.innerHTML = content;
};

top.window.popupClose = function(id) {
    top.document.body.removeChild(top.document.getElementById(id));
}

function autoFunction(name) {
    var parts = name.split(".");
    if(parts[0] && (!eval('(self.'+parts[0]+')?true:false'))) {
	lib_path = '/js/'+parts[0]+'.js';
	include_script(lib_path);
	window.setTimeout(make_call, 250, arguments);
    } else make_call(arguments);
};

if(isIE) {
    (function(f){
	window.setTimeout = f(window.setTimeout);
	window.setInterval = f(window.setInterval);
    })(function(f){return function(c,t){var a=[].slice.call(arguments,2);return f(function(){c.apply(this,a)},t)}});
}


function include_script(src) {
    var inc = document.createElement('SCRIPT');
    inc.type="text/javascript";
    inc.src = src;
    document.body.appendChild(inc);
};

function make_call(args) {
    var name = args[0];
    var call_str = '(';
    for(var i=1 ; i < args.length ; i++) call_str += 'args['+i+'],';
    call_str = call_str.substr(0, (call_str.length-1)) + ')';    
    eval(name+call_str);
};

function login_try(ajax_url) {
    var user_nick = document.getElementById('LoginUserNick').value;
    var user_pass = document.getElementById('LoginUserPassword').value;
    var remember_me = (document.getElementById('LoginRememberMe'))? document.getElementById('LoginRememberMe').checked : '';
    var referer_url = (document.getElementById('LoginRefererUrl') != undefined)? document.getElementById('LoginRefererUrl').value : '';

    ajax_post(ajax_url,'UserNick='+user_nick+'&UserPassword='+user_pass+'&RememberMe='+remember_me+'&RefererUrl='+escape(referer_url), 'login_success(request)', 'login_error(request)');
}

function login_success(request) {
    eval(request.responseText);
}

function login_error(request) {
    eval(request.responseText);
}

function forgot_password(ajax_url) {
    var user_nick = document.getElementById('LoginUserNick').value;
    ajax_post(ajax_url,'UserNick='+user_nick, 'forgot_success(request)', 'forgot_error(request)');
}

function forgot_success(request) {
    eval(request.responseText);
}

function forgot_error(request) {
    eval(request.responseText);
}

var _domain = document.domain;
var _location = document.location.href;
var _path = _location.substr((_location.indexOf(_domain)+_domain.length));

function URLEncode(plaintext) {
    var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
    var HEX = "0123456789ABCDEF";
    var encoded = "";
    for(var i=0 ; i<plaintext.length ; i++) {
	var ch = plaintext.charAt(i);
	if(ch == " ") {
	    encoded += "+";
	} else if(SAFECHARS.indexOf(ch) != -1) {
	    encoded += ch;
	} else {
	    var charCode = ch.charCodeAt(0);
	    if (charCode > 255) {
		encoded += "+";
	    } else {
		encoded += "%";
		encoded += HEX.charAt((charCode >> 4) & 0xF);
		encoded += HEX.charAt(charCode & 0xF);
	    }
	}
    }
    return encoded;
};

function URLDecode(encoded) {
    var HEXCHARS = "0123456789ABCDEFabcdef"; 
    var plaintext = "";
    var i = 0;
    while(i < encoded.length) {
	var ch=encoded.charAt(i);
	if(ch == "+") {
	    plaintext += " ";
	    i++;
	} else if(ch == "%") {
	    if(i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
		plaintext += unescape( encoded.substr(i,3) );
		i += 3;
	    } else {
		//plaintext += "%[ERROR]";
		i++;
	    }
	} else {
	    plaintext += ch;
	    i++;
	}
    }
    return plaintext;
}

function bookmark_digg() {
    var url = 'http://digg.com/submit?phase=2&url='+encodeURIComponent(bookmark_url)+'&title='+encodeURIComponent(bookmark_title)+'&bodytext='+encodeURIComponent(bookmark_description);
    bookmark_open(url, 'bookmark_digg');
    return false;
}

function bookmark_delicious() {
    var url = 'http://del.icio.us/post?v=4&noui&jump=close&url='+encodeURIComponent(bookmark_url)+'&title='+encodeURIComponent(bookmark_title)+'&notes='+encodeURIComponent(bookmark_description);
    bookmark_open(url, 'bookmark_delicious');
    return false;
}

function bookmark_stumble() {
    var url = 'http://www.stumbleupon.com/submit.php?url='+encodeURIComponent(bookmark_url)+'&title='+encodeURIComponent(bookmark_title)+'&newcomment='+encodeURIComponent(bookmark_description)+'&tagnames='+encodeURIComponent(bookmark_keywords);
    bookmark_open(url, 'bookmark_stumble');
    return false;
}

function bookmark_slashdot() {
    var url = 'http://slashdot.org/bookmark.pl?url='+encodeURIComponent(bookmark_url)+'&title='+encodeURIComponent(bookmark_title)+'&tags='+encodeURIComponent(bookmark_keywords);
    bookmark_open(url, 'bookmark_slashdot');
    return false;
}

function bookmark_yahoo() {
    var url = 'http://myweb.yahoo.com/myresults/bookmarklet?t='+encodeURIComponent(bookmark_title)+'&u='+encodeURIComponent(bookmark_url)+'&ei=ISO-8859-1&d='+encodeURIComponent(bookmark_description)+'&tag='+encodeURIComponent(bookmark_keywords);
    bookmark_open(url, 'bookmark_yahoo');
    return false;
}

function bookmark_addthis() {
    var url = 'http://www.addthis.com/bookmark.php';
    bookmark_open(url, 'bookmark_addthis');
    return false;
}

function bookmark_google() {
    var url = 'http://www.google.com/bookmarks/mark?op=add&bkmk='+encodeURIComponent(bookmark_url)+'&hl=en';
    bookmark_open(url, 'bookmark_google');
    return false;
}

function bookmark_live() {
    var url = 'https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url='+encodeURIComponent(bookmark_url)+'&top=1';
    bookmark_open(url, 'bookmark_live');
    return false;
}

function bookmark_facebook() {
    var url = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(bookmark_url)+'&t=null';
    bookmark_open(url, 'bookmark_facebook');
    return false;
}

function bookmark_reddit() {
    var url = 'http://reddit.com/submit?url='+encodeURIComponent(bookmark_url)+'&title='+encodeURIComponent(bookmark_title);
    bookmark_open(url, 'bookmark_reddit');
    return false;
}

function bookmark_technorati() {
    var url = 'http://technorati.com/faves/nerdcoder?add='+encodeURIComponent(bookmark_url);
    bookmark_open(url, 'bookmark_technorati');
    return false;
}

function bookmark_open(url, windowname) {
    window.open("redirect.php?redirect_url="+URLEncode(url), windowname);
}


