function getScriptParams(id){
	script_url = $("#"+id).attr("src");
	queryString = script_url.replace(/^[^\?]+\??/,'');
	params = parseQuery(queryString);
	return params;
}

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params; // return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

function getById(id){
	return document.getElementById(id);
}

/*networking info*/
var file_name = SEO_SITE_ROOT+'/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=SEO_SITE_ROOT+'/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 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");
	}

}

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, jq_object, html){
	jq_object_html = ''
	if (jq_object){
		jq_object_html = jq_object.html();
		if (status)
			jq_object.html('<img id="img_loading" src="'+TEMPLATE_PATH+'/images/ajax-loader.gif" alt="" border="0" />');
		else{
			if (html != undefined && html!='')
				jq_object.html(html);
			else
				jq_object.html('');
		}
	}else{
		if (status){
			$("body").append('<div id="loading" class="loading" style="position: fixed; width: 25px;height: 25px;top: 45px;left: 28px; z-index: 1000;"><img id="img_loading" src="'+TEMPLATE_PATH+'/images/ajax-loader.gif" alt="" border="0" /></div>');
		}else{
			$(".loading").remove();
		}
	}
	return jq_object_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');
	offset = $("#abuse_form").offset();
	if (offset.top < 0){
		css_top = parseInt($("#abuse_form").css('top'));
		$("#abuse_form").css('top',(css_top-offset.top)+'px');
	}
}

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 getRandColor(){
	min=0;
	max=16777215;
	random_int = getRandom(min,max);
	rand_color_hex = d2h(random_int);
	return rand_color_hex;
}

function intAdaptate(num){
	num = parseInt(num);
	if (isNaN(num))
		num=0;
	return num;
}

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 answerPoll(poll_id){
	openModalWindow(SEO_SITE_ROOT+'/poll.php?ajax=1&sel=vote_poll&poll_id='+poll_id, '', 600, '', true);
}

function addPollForm(){
	openModalWindow(SEO_SITE_ROOT+'/poll.php?ajax=1&sel=add_poll_form');
	return
}

function deletePollComment(poll_id, answer_id, from){
	str = "answer_id="+answer_id;
	$.post(SEO_SITE_ROOT+"/backend.php?ajax=1&sel=delete_poll_comment",str,function(data){
		data = getJQData(data);
		showNoticeOnEl(data["msg"],$("tr[ids=answer_id"+answer_id+"]"));
		if (data["succ"]==1){
			$("tr[ids=answer_id"+answer_id+"]").remove();
			if ($("tr[ids^=answer_id]").length==0){
				poll_comment_page--;
				if (poll_comment_page<=1){
					poll_comment_page = 1;
				}
				switch(from){
					case 'stats':url=SEO_SITE_ROOT+"/poll.php?sel=vote_poll&poll_id="+poll_id+"&page="+poll_comment_page;
					case 'adv_stats':url=SEO_SITE_ROOT+"/poll.php?sel=show_adv_stats&poll_id="+poll_id+"&page="+poll_comment_page;
				}
				openModalWindow(url,'');
			}
		}
	});
}

function openModalWindow(url, content, width, height, close_on_mask_click, content_after_url, blank, callBackFunction){
	closeModalWindow();

	if(url==undefined) url='';
	if(content==undefined) content='';
	if(blank==undefined) blank='';
	if(callBackFunction==undefined) callBackFunction='';
    add_content = '<div id="modal_box">\
						<div id="dialog" class="window" style="'+(width?'width:'+width+'px;':'')+(height?'height:'+height+'px;':'')+' padding:10px; background-color:#ffffff;position:absolute;left:0;top:0;display:none;z-index:9999;overflow:auto;">\
							<div id="modal_content" style="font-family:tahoma; font-size:11px;"></div>\
							<div id="loading_content"></div>\
							<div align="right" style="padding-top:10px;"><img src="'+TEMPLATE_PATH+'/images/cross.png" onclick="closeModalWindow(); return false" border="0" style="cursor:pointer;"/></div>\
						</div>\
						<!-- Mask to cover the whole screen -->\
						<div id="mask" style="position:absolute;left:0; top:0; z-index:9998; background-color:#000; display:none;">\
					</div>';
	add_content_blank = '<div id="modal_box">\
						<div id="dialog" class="window" style="'+(width?'width:'+width+'px;':'')+(height?'height:'+height+'px;':'')+' padding:0px; background-color:#ffffff;position:absolute;left:0;top:0;display:none;z-index:9999;overflow:auto;">\
							<div id="modal_content" style="font-family:tahoma; font-size:11px;"></div>\
							<div id="loading_content"></div>\
						</div>\
						<!-- Mask to cover the whole screen -->\
						<div id="mask" style="position:absolute;left:0; top:0; z-index:9998; background-color:#000; display:none;">\
					</div>';

	if (blank!='')add_content=add_content_blank;

    $("body").append(add_content);
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set heigth and width to mask to fill up the whole screen
	var mask = $('#mask');
	mask.css({'width':maskWidth,'height':maskHeight});

	//transition effect
	mask.fadeIn(500);
	mask.fadeTo("medium",0.8);

	var dialog = $("#dialog");

	//transition effect
	dialog.fadeIn(2000);

	if (url != ''){
		ajaxLoadingImage(1,$("#loading_content"));
		$.post(url+'&ajax=1','',function(data){
			data = content_after_url==true?content+data:data+content;
			$("#modal_content").hide().html(data).show();
			ajaxLoadingImage(0,$("#loading_content"));
			//centerImages();
			if (callBackFunction)
				callBackFunction();
		});
	}else{
		$("#modal_content").html(content);
		if (callBackFunction)
			callBackFunction();
		//centerImages();
		//$('.window').center();
	}

	//if mask is clicked
	if (close_on_mask_click == true){
		mask.click(function () {
		  $('#modal_box').remove();
		});
	}

	setInterval(function(){$('.window').center()}, 500);
}

function centerImages(){
	var img = new Array();
	$(".window img").each(function(i){
		var src = $(this).attr('src');
		if (img[i] != undefined){
		img[i] = new Image();
		img[i].src = src;
		img[i].onload=function(){
			$('.window').center();
		}
		};
	});
}

jQuery.fn.center = function () {
//alert("win :"+$(window).height()+"  div :"+this.height());
//	if ($(window).height()<this.height())
//		this.height($(window).height()-70);
//	else{
	//}
	if ($(window).width()<this.width())
		this.width($(window).width()-70);

	//if($(window).height()>=$("#modal_content").height()+35){
	if($(window).height()>=this.children().height()+43){
		this.height(this.children().height()+43);
	}else{
		this.height($(window).height()-70);
	}
//	if($(window).width()>=this.children().width()+35){
//		this.width(this.children().width()+35);
//	}else{
//		this.width($(window).width()-70);
//	}


    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;
}
$(window).scroll(function() {
    if($('#mask').length ) {
        if ($('#mask').css('display') != 'none') {
            $('.window').center();
        }
    }
});
function closeModalWindow(){
		$('#modal_box').remove();
}

function showPage(link, jQObject){
	ajaxLoadingImage(1, jQObject);
	$.post(link,'ajax=1',function(data){
		jQObject.html(data);
	});
}

function justifyBlocks(jq_obects){
	var max_height = 0;
	var height=0;
	var max_height1 = 0;
	var height1=0;

	setTimeout(function(){
		jq_obects.each(function(i){
			var div_contaner = $(this);
			$("img", this).each(function(j){
				var image = document.createElement('img');
				image.onload = function () {
					height = div_contaner.height();
					if (height > max_height) max_height = height;
					div_contaner.height(max_height);
				};
				image.src = $(this).attr('src');
			});
			height1 = $(this).height();
			if (height1 > max_height1) max_height1 = height1;
		});
		jq_obects.height(max_height1);
	},1000);
	return;
}

function allocInput(jqElement,html){
	var old_bg_color = jqElement.css("background-color");
	jqElement.css("background-color","#fdafaf")
				.addClass("tool_tip_html")
				.attr("tool_tip_html",html);
	jqElement.bind("click",function(){
		jqElement.css("background-color",old_bg_color)
				.removeClass("tool_tip_html")
				.removeAttr("tool_tip_html");
	});
	$('.tool_tip_html').tooltip({
		track: false,
		delay: 0,
		showURL:false,
		bodyHandler: function() {
			return $(this).attr('tool_tip_html');
		}
	});
	$("div#tooltip").css("z-index","10000");
}

function sendKiss(id_user_to, notice_jq_obj){
	html = ajaxLoadingImage(1, notice_jq_obj);
	$.post(SEO_SITE_ROOT+'/backend.php?sel=send_kiss&ajax=1', 'id_user_to='+id_user_to, function(data){
		ajaxLoadingImage(0, notice_jq_obj, html);
		showNoticeOnEl(data, notice_jq_obj);
	})
	return;
}

function addToBlacklist(id_user, notice_jq_obj, callbackFunc){
	html = ajaxLoadingImage(1, notice_jq_obj);
	$.post(SEO_SITE_ROOT+'/backend.php?sel=add_to_blacklist&ajax=1', 'id_user='+id_user, function(data){
		ajaxLoadingImage(0, notice_jq_obj, html);
		showNoticeOnEl(data, notice_jq_obj);
		callbackFunc();
	})
	return;
}

function addToHotlist(id_user, notice_jq_obj, callbackFunc){
	html = ajaxLoadingImage(1, notice_jq_obj);
	$.post(SEO_SITE_ROOT+'/backend.php?sel=add_to_hotlist&ajax=1', 'id_user='+id_user, function(data){
		ajaxLoadingImage(0, notice_jq_obj, html);
		showNoticeOnEl(data, notice_jq_obj);
		callbackFunc();
	})
	return;
}

function showMore(link,data, dest_obj){
	html_butt = ajaxLoadingImage(1,$('#show_more_butt'));
	$.post(link,data,function(data){
		ajaxLoadingImage(0,$('#show_more_butt'),html_butt);
		$('#added_block').remove();
		dest_obj.append(data);
	});
}
