$(document).ready(function(){
	leftMenu();
	switchMenu();
	turnPage(); 
});

function switchMenu(){
	$("#tree ul li div").click(function(){
		if ($(this).hasClass('plus')){
			$(this).removeClass('plus');
			$(this).addClass('minus');
			$(this).next().next().show();
		}else{
			$(this).removeClass('minus');
			$(this).addClass('plus');
			$(this).next().next().hide();
		}
	});
}
function leftMenu(){	
	$('#menu a:first').addClass('selected');		
	$('#menu a').click(function(){
		$('#menu a').each(function(){
			if($(this).hasClass('selected')){
				$('#'+this.id).show()
				$('#'+this.id).removeClass(this.id+'_a').addClass(this.id+'_up');
				$(this).removeClass('selected');
				$('#'+this.id+'_').hide();
			}			
		})
		$('#'+this.id).removeClass(this.id+'_up').addClass(this.id+'_a');
		//$('#'+this.id+' > img').attr('src','/img/ui/'+this.id+'_active.gif');
		$(this).addClass('selected');
		$('#'+this.id+'_').show();		
		return false;
	});
}

function turnPage(){
	$(".turn_page").change(function () {
        var val;
        $(this).children().each(function (i, selected) {
            if($(selected).attr('selected')== true)
                val = $(selected).val()
        });
        window.location.pathname +='?page='+val;
    })
}

function loadCity(id, name, zoom){
	var map = new YMaps.Map(document.getElementById(id));
	map.addControl(new YMaps.Zoom());
	var geocoder = new YMaps.Geocoder(name, {results: 1, boundedBy: map.getBounds()});

	YMaps.Events.observe(geocoder,geocoder.Events.Load,function (){
	    if (this.length()){   
	    	var geoResult = this.get(0);
	    	//map.panTo(geoResult.getGeoPoint())	
	    	map.setCenter(geoResult.getGeoPoint(), zoom)
	    }else{alert("Ничего не найдено")}
	})
	YMaps.Events.observe(geocoder,geocoder.Events.Fault,function (error){alert("Произошла ошибка: "+error.message)});
	return map;
}

function loadDataInMap(linkObj, map, coll){	
	var city = $('#city > option:first');
	var val = city.val();				
	$.getJSON(linkObj+val+'/', function(json){
		$.each(json, function(i,s){		
			if (s.lng == 'None' && s.lat =='None'){
				var tc = new YMaps.Geocoder(s.address,{results:1,boundedBy:map.getBounds()});
	            YMaps.Events.observe(tc, tc.Events.Load, function (){
	                if (this.length()){
	                var placemark = new YMaps.Placemark(this.get(0).getGeoPoint(),{style:"map#tc"});
	                placemark.description ='<div style="width:180px"><b>'+s.name+'</b></div>'; 
                	placemark.description +='<div style="width:180px">'+s.address+'</div>';
                	placemark.description +='<div style="width:180px"><a href="'+s.link+'">подробнее</a></div>';
					coll.add(placemark);
	                }});
			}else{
				var point = new YMaps.GeoPoint(s.lng,s.lat);
				var placemark = new YMaps.Placemark(point,{style:"map#tc"});
				placemark.description ='<div style="width:180px"><b>'+s.name+'</b></div>'; 
        		placemark.description +='<div style="width:180px">'+s.address+'</div>';
        		placemark.description +='<div style="width:180px"><a href="'+s.link+'">подробнее</a></div>';
				coll.add(placemark);
			}						
		});				
	});
	return coll;		
}

function loadCityMap(link){
	$.getJSON(link, function(data){
		$('#city option').remove();
		var options = '';
		$.each(data,function(i,s){options+='<option value="'+s.slug+'">'+s.name+'</option>';});
		$('#city').append(options);
	});
}

//$import('http://example.com/myfile.js')
function $import(src){
    var scriptElem = document.createElement('script');
    scriptElem.setAttribute('src',src);
    scriptElem.setAttribute('type','text/javascript');
    document.getElementsByTagName('head')[0].appendChild(scriptElem);
}

// подключаем вместе со случайным GET-параметром, чтобы избежать кеширования
function $importNoCache(src){
    var ms = new Date().getTime().toString();
    var seed = "?" + ms; 
    $import(src + seed);
}

function loadProrerty(href){
	if ($('#properties').length > 0){
		$.getJSON(href, function(json){
			$('body').data('json', json);
		});	
	}
}

function propGoods(){	
	$('.prop_arrow').toggle(
		function(){			
			if ($('#prop_open').length > 0)
				$('#prop_open').remove();			
			var ul = $('<ul id="prop_open"/>');
			var li = '';
			
			var prn = $(this).parent();
			var id = $(prn).attr('id');
			var input = $('#'+id+' input:first');
			var set_val_input = parseInt($('#new_'+input.attr('id')).val());
			var json = $('body').data('json');
			li += '<li id="">-------</li>';
			$.each(json[input.attr('id')].items, function(i, item){
				if (i == set_val_input) {
					li += '<li class="selected" id="' + i + '">' + item + '</li>';
				} else {
					li += '<li id="' + i + '">' + item + '</li>';
				}
			});
			ul.append($(li));
			$(this).parent().append(ul);
			$('#prop_open > li')
			.mouseover(function(){
				$(this).addClass('over');
			})
			.mouseout(function(){
				$(this).removeClass('over');
			})
			.click(function(){
				var prn = $(this).parent().parent();
				var id = $(prn).attr('id');
				var input = $('#'+id+' input:first');
				$('#new_'+input.attr('id')).val($(this).attr('id'));				
				input.val($(this).html());
				$(this).parent().remove();
			});
			$(document).bind('click', removeProperty);
			return false;
		},
		function(){
			removeProperty();
			return false;
		}
	);
}

function removeProperty(){
	$('#prop_open').remove();
	$('document').unbind('click');	
}
