//  JavaScript Document

$(document).ready(function(){
    if ($('#searchinp').length) {

        var strObj = $('#searchinp');
        var btnObj = $('#searchsubmit');
        if (strObj.val() == '' || strObj.val() == strObj.attr('title') ) {
            btnObj.attr('disabled', 'disabled').addClass('disabledSearch');
        }
        
        $(strObj).bind('keyup paste blur', function() {
            if (strObj.val() == '' || strObj.val() == strObj.attr('title') ) {
                btnObj.attr('disabled', 'disabled').addClass('disabledSearch');
            } else {
                btnObj.removeAttr('disabled').removeClass('disabledSearch');
            }
        });
	}
});



var coord1 = townCoord1 = 27.56164;
var coord2 = townCoord2 = 53.902257;
var scope = townScope = 11;

var map;
var currType = 0;
var currCurrency = 0;

var map_items = new Array();
var map_images = new Array();

map_images[1] = '/images/point-green.png';
map_images[2] = '/images/point-blue.png';
map_images[3] = '/images/point-red.png';



var gmap = {
	markers: new Array(),
	map: '',
	town_scope: '',
	town_coord1: '',
	town_coord2: '',
	markerNumber: 0,

	markerAdd: function(obj) {
		this.markers[this.markerNumber] = {
			id: obj.id,
			coord1: obj.coord1,
			coord2: obj.coord2,
            scope: obj.scope,
			name: obj.name,
			byr: obj.byr,
			eur: obj.eur,
			usd: obj.usd,
			text: obj.text,
			url: obj.url,
			type: obj.type,
			icon: obj.image,
			status: obj.status,
			obj: ''
		}
        
		this.markerNumber++;

		return this.markerNumber-1;
	},
    
	markerShow: function(num) {
		this.markers[num].obj = new google.maps.Marker({
			position: new google.maps.LatLng(this.markers[num].coord1, this.markers[num].coord2),
			map: this.map,
			draggable: false,
			animation: google.maps.Animation.DROP,
			title: this.markers[num].title
		});

		this.markers[num].obj.setIcon(this.markers[num].icon);
        
        var content = $('.map_item' +num+ ':eq(0)');
        var msg = '';
        if (content.length) msg = content.html();
        var infowindow = new google.maps.InfoWindow({
            content: msg
        });
        
        google.maps.event.addListener(this.markers[num].obj, 'click', function() {
            infowindow.open(gmap.map, gmap.markers[num].obj);
        });


//		google.maps.event.addListener(this.markers[num].obj, 'click', function() {
//            alert(1);
//            var content = $('.map_item' +num+ ':eq(0)');
//            if (content.length) 
//                infowindow = new google.maps.InfoWindow({
//                    content: content.html()
//                });
//
//			infowindow.open(gmap.map, gmap.markers[num].obj);
//		});
	},

	init: function(coord1, coord2, scope) {
		this.town_coord1 = coord1;
		this.town_coord2 = coord2;
		this.town_scope = scope;

		if (this.town_scope != '') this.town_scope = parseInt(this.town_scope);
		if (this.town_coord1 != '') this.town_coord1 = parseFloat(this.town_coord1);
		if (this.town_coord2 != '') this.town_coord2 = parseFloat(this.town_coord2);

		if (!this.town_scope || this.town_scope <= 0) this.town_scope = 6;
		if (!this.town_coord1 || this.town_coord1 <= 0) this.town_coord1 = 53.908644;
		if (!this.town_coord2 || this.town_coord2 <= 0) this.town_coord2 = 27.575033;

		var myLatlng = new google.maps.LatLng(this.town_coord1, this.town_coord2);
		var myOptions = {
			zoom: this.town_scope,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			panControl: true,
			zoomControl: true,
			mapTypeControl: true,
			scaleControl: true,
			streetViewControl: false
		}

		this.map = new google.maps.Map(document.getElementById("google_map"), myOptions);
		this.map.setCenter(myLatlng);
	},

	markersClear: function() {
		for (var i = 0; i < this.markers.length; i++)
			if (this.markers[i].obj != '') this.markers[i].obj.setMap(null);
	},

	setDefault: function() {
		google.maps.event.trigger(this.map, 'resize');
		var myLatlng = new google.maps.LatLng(this.town_coord1, this.town_coord2);
		this.map.setCenter(myLatlng);
		this.map.setZoom(parseFloat(this.town_scope));
	},

	markersShow: function() {
		for (var i = 0; i < this.markers.length; i++) 
//			gmap.markerShow(i);
			setTimeout('gmap.markerShow('+i+')', i * 50);
	},
    
    showPoint: function() {
        google.maps.event.trigger(gmap.map, 'resize');
        var id = $('li.act', $('#tabsAbObj')).attr('id');
        id = id.substr(4, id.length - 4);
        currType = id;

        id = $('li.act', $('#tabsAbObj2')).attr('id');
        id = id.substr(5, id.length - 5);
        currCurrency = id;

        for (var i = 0; i < this.markers.length; i++) {
            var flCurrency = false;
            if (currCurrency == 0 && (this.markers[i].byr == 1 || this.markers[i].eur == 1 || this.markers[i].usd == 1) ) flCurrency = true;
            else if (currCurrency == 1 && this.markers[i].usd == 1) flCurrency = true;
            else if (currCurrency == 2 && this.markers[i].eur == 1) flCurrency = true;
            else if (currCurrency == 3 && this.markers[i].byr == 1) flCurrency = true;

            if ((currType == 0 || currType == this.markers[i].type) && flCurrency ) {
                setTimeout('gmap.markerShow('+i+')', i * 50);
//                $('.item_table_'+i).show();
                $('.item_table_'+i).removeClass('h');
            } else {
//                $('.item_table_'+i).hide();
                $('.item_table_'+i).addClass('h');
            }
        }

        showList();
    },
    showColorbox: function(num) {
//        alert($('#item_address_'+num).html());
		$.colorbox({
			width:'80%',
			innerHeight:'500px',
			inline:true,
			href:'.yaMap',
			current: "image {current} of {total}",
			previous: "назад",
			next: "вперёд",
			close: "закрыть",
//			title: gmap.markers[num].name+' ('+gmap.markers[num].address+')',
//			title: $('#item_address_'+num).html(),
			title: gmap.markers[num].name+'<br />' + $('#item_address_'+num).text(),
			onComplete:function(){
				google.maps.event.trigger(gmap.map, 'resize');
				gmap.map.setCenter(new google.maps.LatLng(gmap.markers[num].coord1, gmap.markers[num].coord2));
				var scope = gmap.markers[num].scope;
				gmap.map.setZoom(parseFloat(scope));
				gmap.markerShow(num);
                $('.uvel').hide();	
			},
            onClose: function() {
                $('.uvel').show();	
            }
		});
	}
};



function showList() {
    $('.tbl-obmen').each(function(){
        var countVisibleTr = 0;
        $('tr', this).each(function(){
            if (!$(this).hasClass('h')) countVisibleTr++;
        });
        if (countVisibleTr <= 1) {
            $(this).hide();
        }
        else  {
            $(this).show();
        }
    });
}




