/* START MAIN SCRIPT
FOR ALL PAGES */

var browser_type = 'msie';
var W=navigator.userAgent.toLowerCase();
if(W.indexOf("opera")!=-1){browser_type='opera';}else
if(W.indexOf("msie")!=-1){browser_type='msie';} else
if(W.indexOf("mozilla")!=-1){browser_type='firefox';}

function sign_in() {
	document.getElementById('sign_in_div').style.display = "block";
}

function closeSignIn() {
	document.getElementById('sign_in_div').style.display = "none";
}

function closeSignInRegistration(){
	document.getElementById('sign_in_block_div').style.display = 'none'
}

function pop_up(url, width, height) {
	
	var left = (screen.width  - width)  / 2;
	var top = (screen.height - height) / 3 + document.body.scrollTop;
	
	document.getElementById('pop_up_div').style.top = top+"px";
	document.getElementById('pop_up_div').style.left = left+"px";
	
	document.getElementById('pop_up_div').style.width = width+"px";
	document.getElementById('pop_up_div').style.height = height+"px";
	document.getElementById('pop_up_div').style.display = "block";
	document.getElementById('pop_up_frame').src = url;
}

function closePopUp() {
	parent.document.getElementById('pop_up_div').style.display = "none";
}

function confirmdel(url) {
	message = 'Вы уверены что хотите удалить?';
	
	if (confirm(message)) {
		if (browser_type == "firefox") {
			self.location.href = url;
		}
		else {
			document.getElementById('link').href = url;
			document.getElementById('link').click();
		}
	}
}

function createPrivateMessage() {
	document.getElementById('div_trans').style.height = document.body.scrollHeight+"px";
	document.getElementById('div_trans').style.display = "block";
	var width = "700";
	var height = "380";
	var left = (screen.width  - width)  / 2;
	var top = (screen.height - height) / 3 + document.body.scrollTop;
	document.getElementById('div_create_pm').style.width = width+"px";
	document.getElementById('div_create_pm').style.height = height+"px";
	document.getElementById('div_create_pm').style.left = left+"px";
	document.getElementById('div_create_pm').style.top = top+"px";
	showEditor('pm_content', 'ClientShort', 360, 200);
	document.getElementById('div_create_pm').style.display = "block";
	loadPMFriendList();
}

function loadPMFriendList() {
	var url = base_url + "/js/PM_load_friend_list.js";
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;
	document.getElementById('js').appendChild(jsel);	
}

function addPMFrindToSendToFrd(id) {
	if (browser_type == 'msie') var nick_name = document.getElementById('pm_frd_list_'+id).innerText;
	else var nick_name = document.getElementById('pm_frd_list_'+id).textContent;
	
	if (document.getElementById('to_nick_name')) {
		document.getElementById('to_nick_name').value = nick_name;
		validPMField('to_nick_name');
	}
}

function pm_close() {
	document.getElementById('div_create_pm').style.display = "none";
	document.getElementById('div_trans').style.display = "none";
}

function replyPm(parent_id) {
	var url = base_url+"/js/pm_reply.js&parent_id="+parent_id;
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;
	document.getElementById('js').appendChild(jsel);
}

function sendPrivateMessage(nick_name) {
	document.getElementById('to_nick_name').value = nick_name;
	validPMField('to_nick_name');
	//alert(1);
	createPrivateMessage();
}

function validPMField(fld) {
	
	var var_data = "fld="+fld;
	var_data += "&value="+document.getElementById(fld).value;
	
	var gotError = function(err) {
			return;
	};

    var gotInfo = function(data) {
			if (data.success == "1") {
				changePmIco(fld, true);
			}
			
			if (data.error == "1") {
				changePmIco(fld, false);
			}
			return;
	};

    var url = "/ajax_request/check_registration.php";
    var opts = {
        "data": var_data,
        "method": "POST",
        "url": url,
        "onError": gotError,
        "onData": gotInfo

    };
	
    HTTPReq.getJSON(opts);

}

function changePmIco(fld, res) {
	if (res == true) document.getElementById('img_pm_'+fld).src = "/images/tick.png";
	else document.getElementById('img_pm_'+fld).src = "/images/publish_x.png";
}

function checkPmForm() {
	var message = "";
	
	var nickNameImgArr = document.getElementById('img_pm_to_nick_name').src.split('/');
	
	if (document.getElementById('to_nick_name').value == "") {
		message += 'Введите Имя Получателя';
	}
	else if (nickNameImgArr[nickNameImgArr.length - 1] != "tick.png") {
		message += 'Несуществующее Имя Получателя';
	}
	
	if (document.getElementById('pm_header').value == "") {
		if (message != "") message += "\n";
		message += 'Выберите Тему Сообщения';
	}

	if (FCKeditorAPI.GetInstance('pm_content').GetXHTML(true) == "") {
		if (message != "") message += "\n";
		message += 'Введите Тело Сообщения';
	}
	
	if (message == "") {
		return true;
	}
	else {
		alert(message);
		return false;
	}
}

function changeBorder(change, blc, id) {
	document.getElementById('td_border_'+blc+'_'+id+'_lt').style.backgroundImage = "url(/images/home_block/"+change+"001.jpg)";
	document.getElementById('td_border_'+blc+'_'+id+'_t').style.backgroundImage = "url(/images/home_block/"+change+"003.jpg)";
	document.getElementById('td_border_'+blc+'_'+id+'_rt').style.backgroundImage = "url(/images/home_block/"+change+"002.jpg)";
	
	document.getElementById('td_border_'+blc+'_'+id+'_l').style.backgroundImage = "url(/images/home_block/"+change+"004.jpg)";
	document.getElementById('td_border_'+blc+'_'+id+'_r').style.backgroundImage = "url(/images/home_block/"+change+"005.jpg)";
	
	document.getElementById('td_border_'+blc+'_'+id+'_lb').style.backgroundImage = "url(/images/home_block/"+change+"006.jpg)";
	document.getElementById('td_border_'+blc+'_'+id+'_b').style.backgroundImage = "url(/images/home_block/"+change+"008.jpg)";
	document.getElementById('td_border_'+blc+'_'+id+'_rb').style.backgroundImage = "url(/images/home_block/"+change+"007.jpg)";
	
	document.getElementById('td_border_'+blc+'_'+id+'_lg').style.backgroundImage = "url(/images/home_block/"+change+"009.jpg)";
	document.getElementById('td_border_'+blc+'_'+id+'_rg').style.backgroundImage = "url(/images/home_block/"+change+"010.jpg)";
}

var sBasePath = "/library/FCKeditor/";


function showEditor(id, toolbar, width, height) {
	if (document.getElementById(id).style.display != "none") {
		var oFCKeditor = new FCKeditor(id);
		
		oFCKeditor.ToolbarSet = toolbar;
		oFCKeditor.BasePath	= sBasePath;
		oFCKeditor.Height	= height;
		oFCKeditor.Width	= width;
		oFCKeditor.ReplaceTextarea();
	}
	else FCKeditorAPI.GetInstance(id).EditorDocument.body.innerHTML = document.getElementById(id).value;
}

function pop_up_login() {
	var width = 330;
	var height = 410;
	var left = (screen.width  - width)  / 2;
	var top = (screen.height - height) / 3 + document.body.scrollTop;
	
	document.getElementById('sign_in_block_div').style.top = top+"px";
	document.getElementById('sign_in_block_div').style.left = left+"px";
	
	document.getElementById('sign_in_block_div').style.width = width+"px";
	document.getElementById('sign_in_block_div').style.height = height+"px";
	document.getElementById('reg_node').value = 'no_open_block';
	document.getElementById('sign_in_node').value = 'no_open_block';
	document.getElementById('sign_in_block_div').style.display = "block";
}

// подсветка рядов таблиц и переход по ссылке при клике
$(document).ready(function() {
	
	$('div.tab table tr,div.users table tr,div.innertube table.actions_table tr, .tablesorter tr').hover(function() {
		  $(this).addClass('over');
		}, function() {
		  $(this).removeClass('over');
		}
	);
	
	$("table.tarif tr a.direct").click(function() {
		var aDir = $(this).attr("href");
		if (undefined != aDir) {
			window.location = aDir;
		}
        return false;
    });
	
	$("div.tab table tr a.direct").click(function() {
		var aDir = $(this).attr("href");
		if (undefined != aDir) {
			window.location = aDir;
		}
        return false;
    });
	
	$("table.tarif tr, .users table tr, .stavka table tr").click(function() {
		var aGo = $(this).find("a.go").attr("href");
		var aDir = $(this).find("a").attr("href");
		if (undefined != aGo) {
			window.location = aGo;
		} else if (undefined != aDir) {
			window.location = aDir;
		}
		return false;
    });
    
	
	$(function () {
	    var tabContainers = $('div.tabs > div');
	    
	    $('div.tabs ul.tabNavigation a').click(function () {
	        $(this).parent().parent().parent().find('div.tab').hide().filter(this.hash).show();
	        $(this).parent().parent().find('a').removeClass('selected');
	        $(this).addClass('selected');
	        return false;
	    });
	    $('div.tabs ul.tabNavigation').each(function() {
	    	$(this).find('a').filter(':first').click();
	    });
	});
	
	//Bind remove confirmation
	$('a.remove').click(function(){
		message = 'Вы уверены что хотите удалить?';
	
		if (confirm(message) == false) {
			return false;
		}
	});

	
});
