var query, oneItemToQuery;
var multiQuery = [];
var rez;
var foundElems;
var input_oldCity;
var reg_href = new RegExp("\\S*apps/", "i"); //RegExp for base-URL search, ended by 'apps/' 
var base_url = document.location.href.match(reg_href);
var oldCity_config;
/* settings for checkbox values to show searching form */
var checkboxValuesForOldCity = [3, 11, 21, 26, 29, 30];

/* function show_result() {
	var t = 'Old cities searching v0.3';
	var a = '#TB_inline?height=600&width=503&inlineId=oldCity_rez';
	var g = false;
	tb_show(t,a,g); //ThinkBox function
	//this.blur();
	return false;
} */

function oldCity_init() {
  oldCity_config = $.parseJSON($('#oldCity_config').val());
  if (oldCity_config.oldCity_visible) {
    $('#asf select.asf_fields').each(function(glob_temp, element) {
      if (checkboxValuesForOldCity.indexOf(parseInt($(this).val())) != -1) {
        $('#oldCityNames_'+glob_temp).removeClass('oldCityNamesHidden');
      }
      $(this).change(function() {
        if (checkboxValuesForOldCity.indexOf(parseInt($(this).val())) != -1) 
          $('#oldCityNames_'+glob_temp).removeClass('oldCityNamesHidden');
        else $('#oldCityNames_'+glob_temp).addClass('oldCityNamesHidden');
      });
      $('#oldCityNames_'+glob_temp).click(function() {
        oldCity('id_ed_'+glob_temp);      
      });
    });
    $('#colorbox')
      .delegate('div.oc_found', 'click', function() {
        $(this).toggleClass('selected');  
      })
      .delegate('div.oc_found', 'mouseover', function() {
        $(this).addClass('over');  
      })
      .delegate('div.oc_found', 'mouseout', function() {
        $(this).removeClass('over');  
      })
      .delegate('#btn_goChoose', 'mouseover', function() {
        $(this).addClass('btn_goChoose_over');
      })
      .delegate('#btn_goChoose', 'mouseout', function() {
        $(this).removeClass('btn_goChoose_over');  
      })
      .delegate('#btn_goChoose', 'click', function() {
        var selectedElems = $('.selected');
        if (selectedElems.length > 0) {
          rez = "'";
          $(selectedElems).each(function () {
            rez += "', '" + $(this).text();
          });
          rez +="'";
          input_oldCity.value = rez.slice(4);
        }
        $('#cboxClose').click();
      });       
  }
}

function oldCity(id) {
	input_oldCity = document.getElementById(id);

	/* GET-query for AJAX */
	multiQuery = [];
	if (input_oldCity.value.indexOf(",") != -1) { 
		multiQuery = input_oldCity.value.toLowerCase().replace(/['"]/g, '').split(", ");
    oneItemToQuery = "'" + multiQuery[0] + "'";
	}
	else if (input_oldCity.value.search(/['"]|\S\s\S/) != -1) {
		multiQuery[0] = input_oldCity.value.toLowerCase().replace(/['"]/g, '');
    oneItemToQuery = "'" + multiQuery[0] + "'";
  }
  else {
  	multiQuery[0] = input_oldCity.value.toLowerCase();
    oneItemToQuery = multiQuery[0];    
  }
  query = base_url + 'old_cities/mns_old_city_form.php?query=' + oneItemToQuery +
          '&lang=' + index_config.lang; // AJAX searching need only one value. 

	/* ajax query and post-processing */
/* 	$.get(query,function(data){
  	$('body').append("<div id='oldCity_rez'>"+data+"</div>");
		//$('#oldCity_rez').append(data);
		$('.oc_found').each(function () {
			if (array_search($(this).text().toLowerCase(), multiQuery) !== false) {
				$(this).addClass('selected');
			}    
    })
    show_result();
		//scroll(0,0);
    //    $('#oldCity_rez').empty();
		oldCity_processor();
	}); */
	$.get(query,function(data){
    $.colorbox({
      title: '<strong>Old cities searching v1.0</strong> <span style="color:#9B1D20">'+
             $('#oldCityNames_0').attr('title')+'</span>',
      innerWidth: '1000px',
      html: data
    });
		//$('#oldCity_rez').append(data);
 		$('.oc_found').each(function () {
			if (array_search($(this).text().toLowerCase(), multiQuery) !== false) {
				$(this).addClass('selected');
			}    
    })
		oldCity_processor();

	}); 
}

function oldCity_processor() {
	/* events for "Choose" button */

	
	/* definition of events to select items */
/* 	$('.oc_found').each(function () {
		$(this).click(function () {
			$(this).toggleClass('selected');
		});
    $(this).mouseover(function () {
			$(this).addClass('over');
		});
    $(this).mouseout(function () {
			$(this).removeClass('over');
		});
	}) */
}

function array_search( needle, haystack, strict ) {	
/* ------------------------------------------------
* Searches the array for a given value and returns the corresponding key if successful
* +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
*/
	var strict = !!strict;
	for(var key in haystack){
		if( (strict && haystack[key] === needle) || (!strict && haystack[key] == needle) ){
			return key;
		}
	}
	return false;
}

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}
