

function getById(id){
	return document.getElementById(id);
}

/*networking info*/
var file_name = 'location.php';
var tmp_text = 'loading';
function changeSubField(id_field){

	role_obj = getById('role');
	role_obj.innerHTML = '';

	str = 'sel=get_netw_fields&par=id_sub_field&id_spr='+id_field;
	destination_obj = getById('sub_field');
	anisochronous = true;
	ajaxRequest(file_name, str, destination_obj, tmp_text, anisochronous);
}
function changeRole(id_sub_field){
	str = 'sel=get_netw_fields&par=id_role&id_spr='+id_sub_field;
	destination_odj = getById('role');
	anisochronous = true;
	ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous);
}

function logoffAfter(sec){
	setTimeout(function(){location.href='index.php?sel=auto_logoff'},(sec*1000));
}
function closeAfter(sec){
	setTimeout(function(){window.close();},(sec*1000));
}

function showNotice(msg){
	if (!msg) return;
	$("<div id='temp'>"+msg+"</div>").appendTo("body").addClass("notice_div").fadeIn("slow").fadeTo(2000,1).fadeOut("slow");
}

function getJQData(data){
	if (!data){
		showNotice('system error!!!');
		return false;
	}
	var resp;
	eval("resp = " + data);
	if (!resp){
		showNotice('system error!!!');
		return false;
	}
	return resp;
}

function ajaxLoadingImage(status, id_container){
	if (status)
		$("#"+id_container).html('<img id="img_loading" src="'+TEMPLATE_PATH+'/images/ajax-loader.gif" alt="" border="0" />');
	else
		$("#"+id_container).html('');
}


function addTag(id_item){
	var file_name;
	var input_tag_val;
	file_name = $("#file_name").val();
	input_tag_val = $("#input_tag"+id_item).val();
	str = "ajax=1&sel=addtag&tag="+input_tag_val+"&id_item="+id_item;
	$.post(file_name, str, afterAddTag);
}

function afterAddTag(data){
	if (!data) return;
	var resp;
    eval("resp = " + data);
    if (!resp) return;
	showNotice(resp["msg"]);
	if (resp["code"] == '1'){
		var id_item
		id_item = resp['id_item'];

		/*$("#all_tags_container"+id_item).show();*/
		$("#my_tags_container"+id_item).show();
		$("#input_tag"+id_item).val('');
		$.each(resp['tags_info'], function(i, n){
			/*$("#all_tags"+id_item).append('<span><a href="'+n.search_link+'" title="'+n.tag_count+'">'+n.tag+'</a>&nbsp;&nbsp;</span>').css('display','none').fadeIn("slow");*/
			$("#my_tags"+id_item).append('<span id="my_tag'+n.id+'">'+n.tag+'&nbsp;<a href="#" onclick="delTag(\''+n.id+'\'); return false;" class="del_x_link">x</a>&nbsp;&nbsp;</span>').css('display','none').fadeIn("slow");
		});
	}
}

function delTag(id){
	file_name = $("#file_name").val();
	data = 'ajax=1&sel=deltag&id='+id;
	$.post(file_name, data, afterDelTag);
}

function afterDelTag(data){
	if (!data) return;
	var resp;
    eval("resp = " + data);
    if (!resp) return;
	showNotice(resp["msg"]);
	if (resp["code"] == '1'){
		$('#my_tag'+resp.id_tag).fadeOut('fast');
	}
}

function bookmarkSitePage(page_link, type, descr){
	page_link = escape(page_link);
	str = 'ajax=1&bookmark_url='+page_link+'&bookmark_type='+type+'&bookmark_descr='+descr;
	$.post('organizer.php?sel=bookmark_site_page',str, function(data){
		var resp = getJQData(data);
		if (!resp) return;
		showNotice(resp["msg"]);
	});
}

function checkLoginDublicate(login, fname, file_name, id_source, id_destination){
	$("#"+id_destination).empty();
	if (login.length < 5) return false;
	str = 'ajax=1&sel=check_login_dublicate&login='+login+'&fname='+fname;
	$.post(file_name, str, function(data){
		var resp = getJQData(data);
		if (!resp) return;
		var html_code='';
		if (resp.msg != undefined){
			html_code += '<font class="error_msg">'+resp.msg+'</font>';
		}
		if (resp.data){
			///such login already exist
			$.each(resp.data, function(i,val){
				html_code += '<br /><a href="#" onClick="$(\'#'+id_source+'\').val(\''+val+'\'); $(\'#'+id_destination+'\').empty(); $(\'#'+id_source+'\').focus();" >'+val+'</a>';
			});
		}
		if (html_code != ''){
			$("#"+id_destination).append(html_code);
		}
	});
}

function abuseReportForm(){ 
	if ($("#abuse_form_parent").html() && $("#abuse_form_parent").remove()) return;
	$("#abuse_link").before("<div id='abuse_form_parent' style='display:inline; position:relative;'>"+$("#abuse_form_tpl").html()+"</div>");
	$("#abuse_form").addClass('abuse_form');
	var height = ($("#abuse_form").height()+$("#abuse_link").height());
	$("#abuse_form").css('top','-'+(height+23)+'px');
	$("#abuse_form").css('left','0px');

}

function sendAbuseReport(id_user, id_item, type_item){
	var type_msg = $("input[name='ab_type_msg']:checked").attr('value');
	var msg = $("#ab_msg").val();
	if (!id_user || !id_item || !type_item || !type_msg || !msg) return;
	str = 'ajax=1&sel=save_abuse_report&id_user='+id_user+'&id_item='+id_item+'&type_item='+type_item+'&type_msg='+type_msg+'&msg='+msg;
	$.post(SEO_SITE_ROOT+"/backend.php", str, function(data){
		$("#abuse_form").html(data);
		var height = ($("#abuse_form").height()+$("#abuse_link").height());
		$("#abuse_form").css('top','-'+(height+53)+'px');
		$("#abuse_form").css('left','0px');
		$("#abuse_form").css('white-space','nowrap');
		$("#abuse_form").fadeIn('fast').fadeTo(1000,1).fadeOut(2000,function(){$("#abuse_form_parent").remove()});
	})
}

function answerPoll(poll_id){
	launchWindow(SEO_SITE_ROOT + '/poll.php?sel=vote_poll&greybox=1&poll_id='+poll_id, '', 370, 400);
	return;
}

function addPoll(){
	if (window.poll_from_param === undefined){
		poll_from_param='';
	}
	launchWindow(SEO_SITE_ROOT + '/poll.php?sel=add_poll_form&par='+poll_from_param, '', 500, 690);
}

function getRandColor(){
	min=0;
	max=16777215;
	random_int = getRandom(min,max);
	rand_color_hex = d2h(random_int);
	return rand_color_hex;
}

function getRandom(min,max){
	rand = Math.floor(min+(max-min)*Math.random());
	return rand;
}

function d2h(d) {
	return d.toString(16);
}

function h2d(h) {
	return parseInt(h,16);
}

function showNoticeOnEl(msg,jq_obj_el){
	if (!msg) return
	var offset = jq_obj_el.offset();
	var x = offset.left;
	var y = offset.top;
	var rand = Math.random()*10;
	var rand_str = rand.toString();
	rand = rand_str.replace(".","");
	$("<div id='temp"+rand+"'>"+msg+"</div>").appendTo("body").addClass("notice_div1_on_el").fadeIn("slow").fadeTo(2000,1).fadeOut("slow",function(){});
	$("#temp"+rand).click(function(){
		$(this).remove();
	});
	$("#temp"+rand).css("top",(y+15)+"px");
	$("#temp"+rand).css("left",(x+5)+"px");
}

function showVGCommentOnEl(id_gift, msg,jq_obj_el){
	if (!msg) return
	var offset = jq_obj_el.offset();
	var x = offset.left;
	var y = offset.top;
	var rand = Math.random()*10;
	var rand_str = rand.toString();
	rand = rand_str.replace(".","");

	if (comment_enabled[id_gift]){
		comment_enabled[id_gift]=0;
		$("#temp"+id_gift).remove();
	}else{
		comment_enabled[id_gift]=1;
		$("<div id='temp"+id_gift+"'>"+msg+"</div>").appendTo("body").addClass("notice_div1_on_el").fadeIn("slow").fadeTo(2000,1);
		$("#temp"+id_gift).click(function(){
			comment_enabled[id_gift]=0;
			$(this).remove();
		});
		$("#temp"+id_gift).css("top",(y+14)+"px");
		$("#temp"+id_gift).css("left",x+"px");
	}

}
jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

function launchWindow(file, str, width, height, not_use_corner) {

	if(!not_use_corner) {
		not_use_corner = 0;
	}

	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').fadeIn(300);
	$('#mask').fadeTo(300,0.8);
	var flag = true;
	if(height != undefined && height > 0){
		if ($(window).height() < height) height= $(window).height()-70;
	}
	else{
		flag = false;
	}
	if(width != undefined && width > 0){
		if ($(window).width() < width) width = $(window).width()-70;
	}
	else{
		flag = false;
	}
	if(flag){
		$('.window').css({'width':width,'height':height});
	}

	ajaxLoadingImage(1, 'content');

	$('.window').center();
	$('.window').fadeIn(300);

	if(file)
	$.post(file, str, function(data){
		$('#content').html(data);
	if (not_use_corner != 1) {
		$('.window').corner('cc:#313131');
	}
	});
	
	if (not_use_corner != 1) {
		$('.window').delay(10).corner('cc:#313131');
	}
}

function launchInvite(width, height) {
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').fadeIn(300);
	$('#mask').fadeTo(300,0.8);

	if ($(window).height() < height) height= $(window).height()-70;
	if ($(window).width() < width) width = $(window).width()-70;

	$('.window').css({'width':width,'height':height});

	$('.window').center();
	$('.window').fadeIn(300);
	$('.window').corner('cc:#313131');
}

function launchWindowAlbums(file, str, width, height) {
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').fadeIn(300);
	$('#mask').fadeTo(300,0.8);

	if ($(window).height() < height) height= $(window).height()-70;
	if ($(window).width() < width) width = $(window).width()-70;
//
	$('.window').css({'width':width,'height':height});

	ajaxLoadingImage(1, 'content');

	$('.window').center();
	$('.window').fadeIn(300);
	$('.window').corner('cc:#313131');
	$.post(file, str, function(data){
		$('#content').html(data),
		$('#content').css('height',$(window).height()-120),
		//$('#content').css('width',width),
		$('#content').css('padding-top','10px');
		//$('#content').css('overflow','auto');
		//height = $('#show').height(),

	});

}

