// for admin mode

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

/*networking info*/
var file_name = '../location.php';
var tmp_text = 'Loading data...';
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 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){

	jQFixStart();
	if (!msg){
		jQFixEnd();
		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(){
		$(this).remove();
	});
	$("#temp"+rand).click(function(){
		$(this).remove();
	});
	$("#temp"+rand).css("top",(y+15)+"px");
	$("#temp"+rand).css("left",(x+5)+"px");

	jQFixEnd();
	return;
}


function ajaxLoadingImage(status, jq_object, html){

	jQFixStart();
	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"><img id="img_loading" src="'+TEMPLATE_PATH+'/images/ajax-loader.gif" class="ajax_loader" alt="" border="0" /></div>');
			//$("body").append('<div id="loading" class="loading"><div class="edge"><!-- --></div><div class="base"></div><div class="edge"><!-- --></div></div>');
		}else{
			$("#loading").remove();
		}
	}
	//jquery and prototype fix
	jQFixEnd();
	return jq_object_html;
}

/////jquery and prototype fix
proto_temp = '';
function jQFixStart(){
	if ($() == undefined && proto_temp==''){
		proto_temp = $;
		$=jQuery;
	}
}

function jQFixEnd(){
	if (proto_temp!=''){
		$=proto_temp;
	}
	proto_temp='';
}

function getJQData(data){
	if (!data){
		showNotice('system error!!!');
		return false;
	}
	var resp;
	eval("resp = " + data);
	if (!resp){
		showNotice('system error!!!');
		return false;
	}
	return resp;
}
///settings array("name=>value")
function setSetting(name, value, jq_obj){
	jQFixStart();
	ajaxLoadingImage(1,'');
	str = "ajax=1&sel=set_setting&name="+name+"&val="+value;
	$.post(SITE_ROOT+"/backend.php", str, function(data){
		resp = getJQData(data);
		if (resp["code"] == '1'){
			showNoticeOnEl(resp["msg"],jq_obj);
		}
		ajaxLoadingImage(0,'');
	});
	jQFixEnd();
}

function setSettings(settings_str, jq_obj){
	ajaxLoadingImage(1,'');
	str = "ajax=1&sel=set_settings&"+settings_str;
	jQFixStart();
	$.post(SITE_ROOT+"/backend.php", str, function(data){
		resp = getJQData(data);
		if (resp["code"] == '1'){
			showNoticeOnEl(resp["msg"],jq_obj);
		}
		ajaxLoadingImage(0,'');
	});
	jQFixEnd();
}


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

function setCookie(c_name,value,expiredays)
{
	if (expiredays == 0) expiredays=30;
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function SelectRegion(id_country, destination, destination2) {
	if (id_country != '') {
		destination.innerHTML = "Loading data...";
		str="ajax=1&sel=region&id_country=" + id_country;
		$.post("admin_location.php", str, function(data){
			destination.innerHTML = data;
		});
	}else{
		destination.innerHTML = "Country is not selected";
	}
	destination2.innerHTML = "";
}

function SelectCity(id_region, destination) {
	if (id_region != '') {
		destination.innerHTML = "Loading data...";
		str = "ajax=1&sel=city&id_region=" + id_region;
		$.post("admin_location.php",str, function(data){
			destination.innerHTML=data;
		});
	} else {
		destination.innerHTML = "Region is not selected";
	}
}

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 openModalWindow(url, content, width, height, close_on_mask_click, content_after_url, blank){
	closeModalWindow();
	if(url==undefined) url='';
	if(content==undefined) content='';
	if(blank==undefined) blank='';
    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");


			$('.window').center();
	//transition effect
	dialog.fadeIn(2000);

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

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

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

jQuery.fn.center = function () {
	if ($(window).height()<this.height()) this.height($(window).height()-70);
	if ($(window).width()<this.width())	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') != undefined && $('#mask').length ) {
        if ($('#mask').css('display') != 'none') {
            $('.window').center();
        }
    }
});
function closeModalWindow(){
		$('#modal_box').remove();
}

function allocInput(jqElement,html){
	var old_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_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");
}
