//jQuery.noConflict();

//инициализация
$(document).ready(init);

function init(){
 /* $("table.formtable tr:even td").attr("className", "greybackground");
 */

  if ( $.browser.msie )  	if ($.browser.version == '6.0'){  	  $(document).ready(
  	    function () {
  	      $('a.styled-button').each(
  	       function(){
  	        var buf = $(this).css('backgroundColor');
  	        $(this).css('background', 'none');
  	        $(this).css('backgroundColor', buf);
  	        var hovercolor = '#4581C9';
  	        if ($(this).parent().attr('id') == 'addblock')
  	          var hovercolor = '#658F45';

  	        $(this).hover(function(){ $(this).css('backgroundColor', hovercolor);   }, function(){ $(this).css('backgroundColor', buf); });
  	      });
  	    }
  	  );
  }

  $('div.stars').each( function(){
  	 var stat_id_str = $(this).attr('id');
  	 var stat_id = stat_id_str.substring(5, stat_id_str.length);
  	 //settitle(stat_id,1	);  	 $(this).find('a').hover (
        	function () {
            	$(this).addClass("selected");
            	var level = $(this).attr("title");
            	//$("#stars a").each( function () { if ( (1*$(this).attr("title")) < level ) $(this).addClass("selected"); } );
            	$("#star_"+stat_id+" a").each( function () { if ( (1*$(this).attr("title")) < level ) $(this).addClass("selected"); } );
        	},
        	function () {
            	//$("#stars a").removeClass("selected");
            	$("#star_"+stat_id+" a").removeClass("selected");
        	}
    		);
    }
   );

   //$("#stars a").click(onStarClick);
   $('div.stars').each( function(){   	 var stat_id_str = $(this).attr('id');
  	 var stat_id = stat_id_str.substring(5, stat_id_str.length);
  	 var add_div = 0;

  	 var ell = $(this).find('div.star_margin');
  	 if (ell.attr('className'))
  	   add_div = 1;
  	 $(this).find('a').click ( function () {  	 	  return onStarClick(stat_id, this, add_div);  	 	}
  	 );   }
   );}

function onStarClick (id, th, add_div) {  $.get("/jslib/jvote.php", {"id": id, 'add_div': add_div, "mark": $(th).attr("title") }, updateStats);
  return false;
}

function updateStats(data) {
  //alert(data+'___'+id);

  var ss = data.split("+");
  var el = $('#star_'+ss[0]);
  if (!el.attr('id'))
    alert('Ваш голос засчитан');
  else {  	el.html(ss[1]);  }
}

function onFoldersLoadSuccess(xml) {


      var ss = xml.split("+");

	var item_text = ss[0];
	var at_group_text = ss[2];

	$("div.attr").css("display", "none");
	$("#subfolders").html(ss[0]);

  var showmap = parseInt(ss[3]);
  if (showmap)
    $('#mapblock').css('display', 'block');
  else{  	$('#mapblock').css('display', 'none');
  	$('#point').attr('value', '');
  	$('#zoom').attr('value', '');  }


	if(at_group_text){
		$("#"+at_group_text).css("display", "block");
		$("#type").find("option[@value='+at_group_text+']").attr("selected", "selected");
	}

  DisplayPriceImg(ss[1]);
}

function GetFolders(idparent) {

	if(parent==0){
		document.getElementById("subfolders").innerHTML = "";
		return;
	}

	document.getElementById("subfolders").innerHTML = "Загружаются подразделы, подождите...";

	$.get('/jslib/jfolders.php', {parent: idparent}, onFoldersLoadSuccess);
}


function DisplayAtGroup(value){
	$("div.attr").css("display", "none");
	var id = value.split("-");
	var group_id = "group-"+id[0];

	DisplayDiv(group_id);

	DisplayPriceImg(id[1]);

	var showmap = parseInt($("input[@name='gmaplist["+id[1]+"]']:hidden").attr('value'));
  if (showmap)
    $('#mapblock').css('display', 'block');
  else{
  	$('#mapblock').css('display', 'none');
  	$('#point').attr('value', '');
  	$('#zoom').attr('value', '');
  }

}

function DisplayDiv(ID) {


	atts = document.getElementsByName('attributes');
	for (i = 0; i < atts.length; i++){
		item = atts[i];
		item.style.display = "none";
	}


	if(ID)
		document.getElementById(ID).style.display = (document.getElementById(ID).style.display == "none" ) ?  "" :   "none";
}

function DisplayPriceImg(id){	 if (!id)
	   return;

   var pricetext = $("input[@name='pricelist["+id+"]']:hidden").attr('value');
   var imgtext = $("input[@name='picturelist["+id+"]']:hidden").attr('value');
   //settitle(pricetext+'_'+id, 1);
   if (pricetext){     $('#pricep').css('display', 'block').find('span').text(pricetext).end().find('input:text').attr('value', '');   }
   else{   	 $('#pricep').css('display', 'none').find('span').text('').end().find('input:text').attr('value', '');   }

   if (imgtext){     $('#pictureblock').css('display', 'block').find('span').text(imgtext).end().find('input:checkbox').attr('checked', '');   }else{     $('#pictureblock').css('display', 'none').find('span').text('').end().find('input:checkbox').attr('checked', 'true');   }}

function CheckAll(ch){
  var value = '';
  if (ch)
    value = 'checked';

	$('input:checkbox', 'table.user-items').attr('checked', value);

	return false;}

//выделение чекбокса п оназванию
function SelectDir(id, th){
	var el = $(th).parent().find("input[type='checkbox']");

	if (!el)
	  return false;

  $("#orgfolders input[type='checkbox']").attr('checked', '');
  $(el).attr('checked', 'checked');
 /*
	if ($(el).attr('checked'))
	  $(el).attr('checked', '');
	else
	  $(el).attr('checked', 'checked');
 */
  UpdateMap();

	return false;}

//обновляем карту
function UpdateMap(){	var map;
  var geoXml;
  var toggleState = 0;

  var add_path = '?folders=99999'

  $('#popupwindow').fadeIn(1000);


  $('#orgfolders').find("input[type='checkbox']").each(function(i){
    if (this.checked){    	//if (add_path.length > 0) add_path = add_path + '_';
      add_path = add_path + '_' + this.value;
    }
  });

  //if (add_path.length > 0) add_path = '?folders=99999' + add_path;

	if (GBrowserIsCompatible()) {
    var geoXml = new GGeoXml("http://golitsyno.info/xml.php"+add_path, function() {
  		if (geoXml.loadedCorrectly()) {
    	   geoXml.gotoDefaultViewport(map);
  		}
	  });

  map = new GMap2(document.getElementById("map"));
  map.setCenter(new GLatLng(55.751656,37.405701), 13);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());
  map.enableScrollWheelZoom();
  map.addOverlay(geoXml);
  }

  $('#popupwindow').fadeOut(1000);
}

//обрезаем контент текстового блока
function setcontent(th, cnt, set){  if (set)	  $(th).attr('value', $(th).attr('value').substring(0, cnt));
	$('#currentsymbolsnumber').html($(th).attr('value').length);
}

//заголовок меняем в документе
function settitle(st, add){	if (add)
	  document.title = document.title+" | "+st;
	else
	  document.title = st;}

//выбор опции # n
function seloption(th, n){	th = th.parentNode;	$("input[@value='"+n+"']:radio", th).attr('checked', 'checked');
//	alert($(th).attr('name'));}


function Notepad (item, direction) {
  $.get("/jslib/notepad.php", {"item": item, 'direction': direction }, updatelink);
  return false;
}

function updatelink(data) {
  //alert(data);

  var ss = data.split("+");

  var cnt = parseInt(ss[0]);

  //var td = parseInt(ss[1]);

  $('#notepadlink span').text(cnt);

  if (cnt)  	$('#notepadlink').css('display', 'block');  else
    $('#notepadlink').css('display', 'none');



  var el = $('#notepad_'+ss[1]);
  if (!el.attr('id'))
    alert('Объявление добавлено в Ваш блокнот');
  else {
  	el.html(ss[2]);
  }

}
