var __popup, marker__, emarker, tempMarker; // event popup and marker global variable
$(document).ready(function(){
    // show/hide signin panel
    $('a#a_signin').click(function(){
    $('div#q_login').slideToggle();
    }); 
  
    $('#q_pass, #q_email').focus(function(){
    	$('#q_email').addClass('focused');
    	$('#q_pass').addClass('focused');
    }).focusout(function(){
    	$('#q_email').removeClass('focused');
    	$('#q_pass').removeClass('focused');	
    });    
    $(document).click(function(e){ 	
	    var element = $('div#q_login');	    	
	    if(element.is(':visible')){    	
	    	var div = element.offset();  
	    	var obj = {
	    		X: div.left,
	    		Y: div.top,
	    		H: element.height()
	    	};    	
	    	var cursor = {
	    		X: e.clientX,
	    		Y: e.clientY
	    	};      
			if((cursor.X < obj.X) || (cursor.Y > (obj.Y + obj.H))){
				if($('#q_pass, #q_email').hasClass('focused')){ 
					//element.show();           
					// nothing here...
				}else{
					element.slideUp();
				}
			}
		}
    });
    //---
    //---
    $('a#a_evm').hover(function(){ 
        $(this).parent().next()
        .css({
            position: 'absolute',
            'z-index': 12
        }).slideToggle();
        //$(this).parent().css({'z-index': 12});
        //$(this).children().slideToggle();
    });

  // set focus
  $('#s_street').css({color: '#999999'});
  $('#grey').focus(function(){
    $('#s_street').focus();
  });

  $('.dv_btn_d > p > img').click(function(){
      if($('#dv_dym').css('right') == '-21.5%'){
         $('#dv_dym').animate({right: '-15px'}, 600);
         $(this).attr('src', 'http://city24.ge/img/slidens.png');
      }else{
         $('#dv_dym').animate({right: '-21.5%'}, 600);
         $(this).attr('src', 'http://city24.ge/img/slidenn.png');
      }
  });
  //---------------------------------------------------
  //-- choose city click ------------------------------
  $('a#a_ch_city').bind('click', function(){
      $(this).toggleClass('tss');
      $('.dv_c_list').toggle();
  });
  $('.ul_c_list > li > a').bind('click', function(){
      $('#a_ch_city').text($(this).text())
        .attr('name', $(this).attr('class'));
      $('a#a_ch_city').removeClass('tss');
      $('.dv_c_list').hide();
  });
  //---------------------------------------------------
  //---------------------------------------------------
  $("#s_street").keydown(function(e){
       var key =  (e.which) ? e.which : e.keyCode;
       switch( key ){
        case 13:
          //  pars_search();
          break;
        case 40:
        case 38:
           // pars_search();
           break;
        case 39:
            var grey_value = $('#grey').val();
            if(grey_value) $('#s_street').val(grey_value);
            break;
       } // switch
  });
//------------------------------------------------------
//------------------------------------------------------
}); // document.ready
//------------------------------------------------------
// show / hide categories list
function catsShowHide(action){
    if(action !== undefined && action !== null){
        switch(action){
            default: break;
            case 'show':
                $('div#dv_lf_side').children('ul').show();
                $('.dv_sh_c_m_plus').hide();
                $('.dv_sh_c_m_minus').show();
                break;
            case 'hide':
                $('div#dv_lf_side').children('ul').next().next().next()
                .next().next().next().next().next().next().next().next()
                .next().next().next().next('ul').hide();
                $('.dv_sh_c_m_plus').show();
                $('.dv_sh_c_m_minus').hide();
                break;
        }
    }
}
//--
function __addEventMarker(id){    
    $('* div.olPopup').remove();
   	// if(this.popup != undefined && this.popup != null){
   		// $('* div.olPopup').remove();
   		// //this.popup.destroy();
   	// }
//    	 
   	
    if(emarker !== undefined && emarker !== null){
       emarker.clearMarkers();
       emarker = undefined; 
    }
    //--
    
    var arr =  $('.hlonlat_' + id + '').val().split(',');
    var set = {};
    set = {
        lonlat: [ arr[0], arr[1] ],
        popup: '<div id="dv_ll_scl_rs" class="dvll_' + id + '">' +
                    $('.dvll_' + id + '').html() +
                '</div>'
    };
    emarker = new OpenLayers.Layer.Markers("emarkers");
    map.addLayer(emarker);   
    icons = new Array();
    icons[0] = new Array('http://www.city24.ge/img/event_marker.png','31','22','0.5','1');
    addMarker( emarker,parseFloat(set.lonlat[0]),parseFloat(set.lonlat[1]),set.popup,true, 0, 0 );   
    map.zoomToExtent(emarker.getDataExtent());
    $('div.olPopupContent > .dvll_' + id + ' > div.d_lonlat').remove();
    $('#dv_ll_scl_rs').css({
        height: 'auto',
        border: 'medium none',
        'min-height': '130px',
        'padding-bottom': 0
    });
}
//----------------------------------------------------------------
// link generation
$(document).ready(function(){
    $("#a_g_link").click(function(){
       $("#divLink").slideToggle('fast');
       var obj = {
           linkCity:   document.getElementById('s_street_city_link').value,
           linkStreet: document.getElementById('s_street_link').value,
           scats_link: document.getElementById('s_street_subcat').value
       };
       if(obj.scats_link.length > 0 || obj.linkStreet.length > 0 ){
           if( obj.scats_link.length > 0 ){
               obj.main_link = 'http://www.city24.ge/index.php?zoom=16&catS='+obj.scats_link+'';
           }else{
               obj.main_link = 'http://www.city24.ge/index.php?zoom=16&city='+obj.linkCity+'&street='+obj.linkStreet+'';
           }
           obj.main_link   = encodeURI(obj.main_link);
       }else{
           //obj.main_link = encodeURI(obj.main_link);
           var pos = map.getCenter();
           var a = pos.transform(map.getProjectionObject(),map.displayProjection);
           obj.main_link = 'http://www.city24.ge/index.php?zoom='+map.zoom+'&lon='+a.lat+'&lat='+a.lon+'';
       }
       $("#txtLink").val(obj.main_link);
       var path = '<iframe id="idframe" width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + obj.main_link + '&map=tr"></iframe><br /><div style="margin-left: 25px; margin-top: 10px;"><small><a target="_blank" href="' + obj.main_link + '" style="color:#0000FF;text-align:left">View Larger Map</a></small></div>';
       $("#txtiframe").val(path);
       $("#aCustom").attr({href:'' + obj.main_link + '&prof=gm&cust=ct'});
    });
    // close
    $("#imgClose, #map").click(function(){
        $('#divLink').slideUp('fast');
    });
    // select
    $("#divLink > div > input, #txtarea2").live('click', function(){
        $(this).select();
    });
    // set map size
    $('input[name="rSize"]').click(function(){
        var id = $(this).attr('id');
        var width, height;
        obj_width  = '#txtwidth';
        obj_height = '#txtheight';
        $(''+obj_width+', '+obj_height+'').attr('disabled','disabled');
        if(id !== undefined && id !== null){
            switch(id){
                default:
                    width = 350;
                    height = 250;
                    break;
                case 'rSizeSmall':
                    width = 350;
                    height = 250;
                    break;
                case 'rSizeMedium':
                    width = 450;
                    height = 350;
                    break;
                case 'rSizeLarge':
                    width = 650;
                    height = 550;
                    break;
                case 'rSizeCustom':
                    $(''+obj_width+', '+obj_height+'').removeAttr('disabled');
                    width  = $(''+obj_width+'').val();
                    height = $(''+obj_height+'').val();
                    $(''+obj_width+'').focus();
                    break;
            }
            $('#map').css({
                width: width + 'px',
                height: height + 'px'
            });
            load_link_fr();
            $('#txtarea2').next()
            .append('<span style="display:none;" id="sp_map_width">'+
                width+'</span><span style="display:none;" id="sp_map_height">'+
                height+'</span>');
        }
    });
    //----
    $('#txtwidth').keyup(function(){
       $('#map').css({width: $(this).val()+'px'});
       load_link_fr();
    });
    //----
    $('#txtheight').keyup(function(){
       $('#map').css({height: $(this).val()+'px'});
       load_link_fr();
    });
});
// listing map moving
//----------------------------
// load to texarea iframe link
function load_link_fr(){

  this.path = '';
  this.width =  $('#map').css('width');
  this.height = $('#map').css('height');
  var obj = {
      street: get_url('street'),
      city:   get_url('city'),
      zoom:   get_url('zoom'),
      lon:    get_url('lon'),
      lat:    get_url('lat'),
      http:   'http://www.city24.ge/'
  };
  var link;
  var link_a;
  var org_name_l;
  
  if (obj.street){
      org_name_l = obj.street;
  }else{
      org_name_l = 'me';
  }
  
  if(obj.lon.length > 0){
      link = '?zoom='+obj.zoom+'&lon='+obj.lon+'&lat='+obj.lat+'';
  }else{
      link = '?zoom='+obj.zoom+'&gm=gm&cust=ct&city='+obj.city+'&street='+obj.street;
      link_a = '?zoom='+obj.zoom+'&city='+obj.city+'&street='+obj.street;
  }
  //--
  this.path = '<iframe id="idframe" width="'+this.width+'" height="'+this.height+'" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+obj.http + link +'&map=tr"></iframe><div style="font-family: Verdana; font-size: 10px; font-weight: bold; margin-left: -13px; margin-top: -24px; text-align: right; width: '+this.width+';"><small><a target="_blank" href="'+obj.http + link_a +'" style="color:#ff1f4c;">Find ' + org_name_l + ' on City24.ge</a></small></div>';

  //--
  $('#txtarea2').val(this.path);
}
// organization filter
function org_f(this_this){
    var id = this_this.id;
    this_this = this_this.value;
    var arr = [];
    var patt;
    arr = $(".ll_st_s_street").children("li").children('a');
    $(".ll_st_s_street").children('li').hide(); 
    jQuery.each(arr, function(i, val){
        patt = new RegExp("^"+this_this, "gi");        
        if($(val).text().match(patt)){           
           $(val).parent().show();
        }
    }); 
    // clear background
    if(this_this.length > 0){
       $('#' + id).css({'background-position': '-500pt 50pt'});
    }else{
       $('#' + id).css({'background-position': 'left center'});         
    }    
}
// organization filter background setting
function org_f_blur(this_this){
    var id = this_this.id;
    var value = this_this.value;
    var obj = $('#' + id);
    if(value.length > 0){
       obj.css({'background-position': '-500pt 50pt'});
    }else{
       obj.css({'background-position': 'left center'});         
    }
}
//---
function org_f_focus(this_this){    
    $('#' + this_this.id).css({'background-position': '-500pt 50pt'});
}
//------------
// parsing url
function get_url(obj){
  obj = obj.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+obj+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// show/hide subcategories
function sh_scats(element){
    var obj = $(element);
    var arr = [];
    arr = $(obj).parent().children(".dv_icli_c");
    if(obj.text().trim() == '...'){
        jQuery.each(arr, function(i, val){
           if(i > 2) $(val).show();
        });
       obj.text('fewer...');
    }else{
        jQuery.each(arr, function(i, val){
           if(i > 2) $(val).hide();
        });
       obj.text('...');
    }
}
//-------- ...yet not working correctly --------------------------

