//Browser detect
function getInternetExplorerVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
		rv = parseFloat(RegExp.$1);
	}
	return rv;
}
isie=('\v'=='v');
if(isie) ie_v = getInternetExplorerVersion();
else ie_v = 0;
if(navigator.appName=='Opera')isopera=true;
else isopera=false;
if(navigator.appName=='Netscape')ismozilla=true;
else ismozilla=false;

function sj_getserverurl() {
	var anchs = document.getElementsByTagName("script");
	for (i=0;i<anchs.length;i++) {
		var item = anchs[i].src;
		if (item.substring(item.length - 8) == 'forum.js') {
			return item.substring(0, item.length - 16);
		}
	}
}

//Trim function
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}
function $f(ei){return document.getElementById(ei);};
function $fce(tn){return document.createElement(tn);};
function $del(obj){
	if(typeof obj == 'string') obj = $f(obj);
	if(!obj) return;
	obj.parentNode.removeChild(obj);
};

//Get identity
identity = $f('js_wiki_forum').getAttribute('contentID') || $f('js_wiki_forum').getAttribute('Identity');

//Get server URL
serverURL = sj_getserverurl();
if (serverURL=='http://edwik.com/forum/') serverURL='http://up4um.com/';
RealServerURL = serverURL+'connector/';
RequestURL = serverURL+'connector/connector.php';
if(serverURL.substring(0, 5)=='https') var UseHttps = '1';
else  var UseHttps = '0';
forum_qid = Math.random().toString();
forum_qid = forum_qid.substr(3,5)+identity;

//Parse href to get forum history parameters
forum_parseURL = function(){
	fullURL = document.location.href;
	if((fullURL.charAt(fullURL.length-1))=='#') fullURL = fullURL.substring(0, fullURL.length-1);
	var forumURLstep = fullURL.indexOf('#forum=');
	if(forumURLstep>0) {
		parentURL = fullURL.substring(0, forumURLstep);
		forumURL = fullURL.substring(forumURLstep+7, fullURL.length);
	}
	else {
		parentURL=fullURL;
		forumURL='';
	}
}

//Refresh activity state of "forward" and "backward" buttons on-forum
forum_historyButtons=function(){
	var forw = $f('forw');
	var back = $f('back');
	if(typeof Forum_history=='undefined' || !forw || !back)return;

	if(Forum_history_pos==(Forum_history.length-1)) {
		forw.className='non_act';
		forw.href='javascript:;';
	}
	else {
		forw.className='';
		forw.href='javascript:pbForum.history(\'fwd\')';
	}
	if(Forum_history_pos==0) {
		back.className='non_act';
		back.href='javascript:;';
	}
	else {
		back.className='';
		back.href='javascript:pbForum.history(\'back\')';
	}
}
forum_parseURL();

//Cycling check. If href changed = refresh content
HistMovement=false;
checkURL = fullURL;
URLcheck = setInterval(function() {
	forum_historyButtons();
	if(checkURL!=document.location.href){
		forum_parseURL();
		if(forumURL.length==0) {
			checkURL=document.location.href;
			if($f('forum_up2305um09')) pbForum.forum_close();
		} else {
			//if(forumURL != 'admin') // admin - чтобы не обновляло, когда в админку заходим
			// щас по факту при админке проходит пустой запрос.
			pbForum_start.sendParam(RequestURL+'?q='+forumURL,pbForum_start.GetRequestData);
		}
	}
	checkURL=document.location.href;
}, 150);

//Creating buttons
button_load = function(){
	if($f('button_forum_up2305um09_open')) $f('button_forum_up2305um09_open').style.display = 'none';
	if($f('js_wiki_forum').style.display == 'none') $f('js_wiki_forum').style.display = 'block';
	if(!$f('button_forum_up2305um09_close')){
		var button = $fce('div');
		button.id = 'button_forum_up2305um09_close';
		if (Forum_settings.top!='150') button.style.top=Forum_settings.top+'px';
		button.innerHTML = '<div class="but_help" onclick="helpwin(\'help\');" ><div class="over_frame"><span>'+forumTextPrev.forum_help+'</span></div><iframe class="in_forum"></iframe></div>'+
		'<div class="but_forum" onclick="Forum_GoGo();"><div class="over_frame"><span>'+forumTextPrev.forum+'</span></div><iframe class="in_forum"></iframe></div>';
		document.body.appendChild(button);
	} else $f('button_forum_up2305um09_close').style.display = 'block';
	if(forumURL.length>0) {
		Forum_GoGo();
	}
}

//Help window popup
helpwin = function (mode) {
	if ($f('popup_help_up2305um09')) return;
	var div = $fce('div');
	div.id = 'popup_help_up2305um09';
	var str = '<div class="popup">'+
		'<div class="popup_content">'+
			'<div class="button_close" onclick="helpwin_close()"></div>'+
			'<h2>'+((mode == 'help') ? forumTextPrev.help_header : forumText.newforum) +'</h2>'+
			'<div id="widget_email">'+
				'<p>'+((mode == 'help') ? forumTextPrev.help_intro : forumText.newforum_info)+'</p>'+
				'<p>'+((mode == 'help') ? forumTextPrev.help_intro2 : '')+'</p>'+
				'<div class="row">'+forumTextPrev.your_email+': <div class="input"><input id="get_login" type="text" /></div></div>'+
				'<div class="row">'+forumTextPrev.more+' <a href="http://up4um.com">up4um.com</a></div>'+
			'</div>'+
			'<div id="widget_pass" style="display:none;">'+
				'<p>'+forumTextPrev.your_email+': <strong id="checked_email"></strong></p>'+
				'<p id="widget_pass1">'+forumTextPrev.reg+'</p>'+
				'<div class="row" id="widget_pass2">'+forumTextPrev.pass+': <div class="input passw"><input id="get_pass" type="password"/></div></div>'+
				'<div class="row" id="widget_pass3">'+forumTextPrev.again+': <div class="input passw"><input id="get_pass2" type="password"/></div></div>'+
			'</div>'+
			'<div id="widget_get_code" style="display:none;">'+
				'<div class="row">'+forumTextPrev.insert_code+': <div class="textarea"><textarea id="forum_widget_code"></textarea></div></div>'+
			'</div>'+
			'<div id="help_buttons" class="button_bar">'+
				'<a id="widget_button_cancel" class="button cancel" href="javascript:helpwin_close();">'+forumTextPrev.close+'</a>'+
				'<a id="widget_button_ok" class="button ok" href="javascript:getforum();">'+forumTextPrev.get+'</a>'+
			'</div>'+
		'</div>'+
		'<iframe frameborder="0" class="in_forum"></iframe>'+
	'</div>';
	div.innerHTML = str;
	document.body.appendChild(div);
}
helpwin_close = function(){
	$del('popup_help_up2305um09');
}
helpwin_back = function(){
	$f("widget_email").style.display='block';
	$f("widget_pass").style.display='none';
	var cancel = $f('widget_button_cancel');
	cancel.innerHTML=forumTextPrev.close;
	cancel.href = 'javascript:helpwin_close();';
}

//Send request in "get own forum" dialog
getforum = function (){
	var login = $f('get_login').value;
	var url = RequestURL + '?q=newcode&lang='+Forum_settings.lang+'&email=' + login;
	var r = /^\w+([-+.\']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if(!r.test(login)) return alert(forumTextPrev.email_wrong);
	var login = encodeURIComponent(login);
	if($f('widget_pass').style.display != 'none') {
		if($f('get_pass2').value!=$f('get_pass').value) return alert(forumTextPrev.pass_coins);
		if($f('get_pass2').value.length<2||$f('get_pass2').value.length<2) return alert(forumTextPrev.pass_short);
		pass = encodeURIComponent($f('get_pass2').value);
		url += '&pass='+ pass;
	}
	pbForum_start.sendParam(url,pbForum_start.GetRequestData);
}

//Linking scripts
function link_script(link, object, callback){
	var obj = document.createElement("script");
	obj.type="text/javascript";
	obj.src = serverURL + 'scripts/'+link+'.js';
	obj.setAttribute('charset', 'utf-8');
	document.getElementsByTagName('head')[0].appendChild(obj);
	if (callback) {
		var interval = setInterval(function() {
			if (eval('typeof '+object) != 'undefined') {
				clearInterval(interval);
				callback();
			}
		}, 50);
	};
	return obj;
}

if(typeof forum_query == 'undefined') forum_query = link_script('query');

function link_scripts(){
	if(typeof forum_lang == 'undefined') forum_lang = link_script('lang/lang_'+Forum_settings.lang);
	if(typeof forum_action == 'undefined') forum_action = link_script('action');
	if(typeof forum_content == 'undefined') forum_content = link_script('content');
	else forum_onload();
}

//Linking styles
function link_style(name){
	var s = $fce('link');
	s.href = serverURL +'css/'+name+'.css' ;
	s.rel = 'stylesheet';
	s.type = 'text/css';
	s.charset = "utf-8";
	document.getElementsByTagName('head')[0].appendChild(s);
}
function link_primary_styles(){
	link_style('button');
	if (Forum_settings.isinverted == '1') link_style('button_inv');
	link_style('popup_help');
	if(Forum_settings.css>0) link_style('popup_help_'+Forum_settings.css);
	if (Forum_settings.isinverted == '1') var inv = '_inv'; else var inv = '';
	link_style('button_'+Forum_settings.css+'_'+Forum_settings.lang+inv);
	if(identity==179 || identity==473) link_style('button_002_trans');
	if(identity==252) {
		link_style('button_up4um');
		link_style('button_up4um_ru');
	}
	if(identity==433) {
		link_style('button_up4um');
		link_style('button_up4um_en');
	}
	if (ie_v==6) {
		link_style('button_ie6');
		link_style('popup_help_ie6');
	}
	if (ie_v>=7) {
		link_style('button_ie7');
		link_style('popup_help_ie7');
	}
	all_styles_loaded = false;
}
function link_forum_styles(){
	if(!all_styles_loaded) {
		link_style('forum');
		link_style('page_profile');
		link_style('page_post');
		if(Forum_settings.css>0) link_style('forum_'+Forum_settings.css);
		if (ie_v==6) {
			link_style('ie6');
		}
		if (ie_v>=7) {
			link_style('ie7');
		}
		all_styles_loaded = true;
	}
}

//Background shadow
function Forum_shadow(){
	var obj = $f('forum_shadow');
	if(!obj) {
		d = $fce('div');
		var body = document.getElementsByTagName('body')[0];
		var container = (body) ? body : document.body;
		d.setAttribute('id', 'forum_shadow');
		d.style.cssText = 'background-color:#000;height:100%;width:100%;opacity: 0.4;-moz-opacity: 0.4;-khtml-opacity: 0.4;filter: alpha(opacity=40);position:fixed;_position:absolute;top:0;left:0;z-index:99999;';
		container.appendChild(d);
	}
}

//Changing buttons on forum open
function Forum_buttons(){
	if($f('button_forum_up2305um09_close')) $f('button_forum_up2305um09_close').style.display = 'none';
	if($f('button_forum_up2305um09_open')) {
		$f('button_forum_up2305um09_open').style.display = '';
		return;
	}
	var div = $fce('div');
	div.id = 'button_forum_up2305um09_open';
	div.innerHTML = '<div class="but_new" onclick="helpwin(\'open\');"><div class="over_frame"><span>'+forumTextPrev.new_forum+'</span></div><iframe class="in_forum"></iframe></div>'+
	'<div class="but_copy" onclick="pbForum.popup(\'copy\');"><div class="over_frame"><span>'+forumTextPrev.copy_forum+'</span></div><iframe class="in_forum"></iframe></div>';
	document.body.appendChild(div);
	$f('button_forum_up2305um09_close').style.top=Forum_settings.top+'px';
	$f('button_forum_up2305um09_open').style.top=Forum_settings.top+'px';
}

//Run forum
function Forum_GoGo() {
	var t=document.getElementsByTagName("embed");
	for(var z=0;z<t.length;z++){t[z].setAttribute("wmode","opaque");}
	var t=document.getElementsByTagName("object");
	for(var z=0;z<t.length;z++){t[z].setAttribute("wmode","opaque");}//Flash opacity

	helpwin_close();
	link_scripts();
	link_forum_styles();

	if($f('forum_hidden')){
		for (var i=0;i<document.getElementsByTagName('object').length;i++){   
			document.getElementsByTagName('object')[i].style.visibility = 'hidden';
		};
		$del('forum_hidden');
	}

	Forum_shadow();
	Forum_buttons();
	document.body.style.overflow='hidden';//Scrolling on parent page = off
}
