var map = null;
var progressBar = null;
var show_last_gresult = null;

var companyid = 0;
var search_id = 0;

var marker = [];
var mi = 0;




function Ginitialize(mapid, notfill, cid)
{
      if (mapid == undefined)
      {
           mapid = 'gmap';
      }

      if (GBrowserIsCompatible())
      {
            map = new GMap2(fetch_object(mapid));
            map.setCenter(new GLatLng(56.785, 24.501), 7);
            map.setUIToDefault();
            
            
            if (notfill != 1)
            {
                 show_last_gresult = false;
                 GDownloadUrl('http://' + document.domain + '/geoxml.htm?searchid=' + search_id + '&companies=' + objarr, fetch_gxml_response);
            }
            else if (notfill == 1 && cid != undefined)
            {
                 show_last_gresult = true;
                 GDownloadUrl('http://' + document.domain + '/geoxml.htm?searchid=' + search_id + '&companies=' + cid, fetch_gxml_response);
            }
      }
}


function fetch_gxml_response(data, responseCode)
{
       if (data == null)
       {
             return false;
       }

       var set_to_center = false;
       var xml = GXml.parse(data);
       var markers = xml.documentElement.getElementsByTagName("marker");

       if (show_last_gresult === false && fetch_cookie(COOKIE_PREFIX + 'search_t') == 'product')
       {
            var set_to_center = true;
       }
       
       for (var i = 0; i < markers.length; i++)
       {
                    if (markers[i].getAttribute("lat") == 0 && markers[i].getAttribute("lng") == 0)
                    {
                            continue;
                    }

                    mi += 1;

                    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
                    marker[mi] = new GMarker(point, { title: markers[i].getAttribute("name") });
                    
                    if (show_last_gresult === true)
                    {
                            map.setCenter(point, 15);
                    }
                    else if (set_to_center === true)
                    {
                            map.setCenter(new GLatLng(56.974, 24.099), 11);
                    }
                    map.addOverlay(marker[mi]);
                    
                   
                    marker[mi].url  = markers[i].getAttribute("url");
                    marker[mi].info = markers[i].getAttribute("info");
                     
                    if (markers[i].getAttribute("mark") != 0)
                    {
                        marker[mi].setImage('images/blank_red.gif');
                        addListeners(marker[mi], 'red.gif');
                    }
                    else
                    {
                        marker[mi].setImage('images/blank_blue.png');
                        addListeners(marker[mi], 'blue.png');
                    }
       }
}


function addListeners(marker_tmp, color)
{
         GEvent.addListener(marker_tmp, 'click', function() {
                GEvent.trigger(marker_tmp, 'mouseout');
                closemap();
                window.location = marker_tmp.url;
         });

         GEvent.addListener(marker_tmp, 'mouseover', function() {
                marker_tmp.openInfoWindowHtml(marker_tmp.info);
                marker_tmp.setImage('images/blank.png');
         });

         GEvent.addListener(marker_tmp, 'mouseout', function() {
                marker_tmp.setImage('images/blank_' + color);
                map.closeInfoWindow();
         });
}



function switch_photo(val, stop)
{
  if (stop)
  {
    clearInterval(slideshow_interval);
  }

       var mainImg = fetch_object('mainPhoto');


       if (val == 'next' || val == 'prev')
       {
             for (photo in photoarr)
             {
                   if (photoarr[photo] == curr_photoid)
                   {
                          if (val == 'next')
                          {
                                photo++;
                          }
                          else
                          {
                                photo--;
                          }
                          

                               if (photo >= photoarr.length)
                               {
                                     photo = 0;
                               }
                               else if (photo < 0)
                               {
                                     photo = photoarr.length - 1;
                               }



                          curr_photoid = photoarr[photo];
                          prev_photoid = curr_photoid;

                          var $active = $('#photos IMG.active');

                          if ( $active.length == 0 ) $active = $('#photos IMG:last');

                          var $next =  $active.next().length ? $active.next()
                              : $('#photos IMG:first');

                          $active.addClass('last-active');

                          $next.css({opacity: 0.0})
                              .addClass('active')
                              .animate({opacity: 1.0}, 650, function() {
                                  $active.removeClass('active last-active');
                              });


//                          mainImg.src = "image.htm?type=photo&fileid=" + photoarr[photo] + "&contentid=" + companyid + "&imgtype=4&th=1";

                          break;
                   }
             }

			 for (photo in photoarr)
			{
			   if (photoarr[photo] == curr_photoid)
					document.getElementById('show_image_span'+(parseInt(photo)+1)).style.color = 'black';
			   else
				   document.getElementById('show_image_span'+(parseInt(photo)+1)).style.color = 'white';
			}
       }
       else
       {
//             mainImg.src = "image.htm?type=photo&fileid=" + val + "&contentid=" + companyid + "&imgtype=4&th=1";

        var $active = $('#photos IMG.active');

        if ( $active.length == 0 ) $active = $('#photos IMG:last');

        var $next =  $('#photos #photo'+val);

        $active.addClass('last-active');

        $next.css({opacity: 0.0})
            .addClass('active')
            .animate({opacity: 1.0}, 650, function() {
                $active.removeClass('active last-active');
            });

             curr_photoid = val;
			 for (photo in photoarr)
			{
			   if (photoarr[photo] == curr_photoid)
					document.getElementById('show_image_span'+(parseInt(photo)+1)).style.color = 'black';
			   else
				   document.getElementById('show_image_span'+(parseInt(photo)+1)).style.color = 'white';
			}
       }
}


function fetch_filter(filter_object, searchid, doref)
{
      var cookiestring = '';
      var count_ce = collapse_elementarr.length;

      objform = fetch_object(filter_object);
      cols = objform.elements;


      for(i=0;i<cols.length;i++)
      {
           if(cols[i].type == 'checkbox')
           {
                 if(cols[i].checked == true && PHP.in_array(cols[i].name, collapse_elementarr) == -1)
                 {
                       count_ce += 1;
                       collapse_elementarr[count_ce] = cols[i].name;
                 }
                 else if (cols[i].checked == false && PHP.in_array(cols[i].name, collapse_elementarr) != -1)
                 {
                       delete collapse_elementarr[PHP.in_array(cols[i].name, collapse_elementarr)];
                 }
           }
      }
      
      
      if (collapse_elementarr.length > 0)
      {
             for (c in collapse_elementarr)
             {
                   if (cookiestring == '')
                   {
                         cookiestring = collapse_elementarr[c];
                   }
                   else
                   {
                         cookiestring = cookiestring + ',' + collapse_elementarr[c];
                   }
             }
             set_cookie(COOKIE_PREFIX + 'collapse_companies', cookiestring);


             if (doref == 1)
             {
                   set_cookie(COOKIE_PREFIX + 'collapse_searchid', searchid);
                   window.location = '/search.htm?do=find&searchid=' + search_id;
             }
      }
}


function clear_filter(doref)
{
      set_cookie(COOKIE_PREFIX + 'collapse_searchid', '');
      set_cookie(COOKIE_PREFIX + 'collapse_companies', '');

      if (doref == 1 && collapse_elementarr.length > 0)
      {
              window.location = '/search.htm?do=find&searchid=' + search_id;
      }
}


function showmap(companyid)
{
       var obj = fetch_object('showmap_div');
       var my_width  = 0;
       var my_height = 0;

       var objwidth  = parseInt(obj.style.width) ? parseInt(obj.style.width) : 450;
       var objheight = parseInt(obj.style.height) ? parseInt(obj.style.height) : 375;


       // get client width and height
       if (typeof(window.innerWidth) == 'number')
       {
                my_width  = window.innerWidth;
                my_height = window.innerHeight;
       }
       else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
       {
                my_width  = document.documentElement.clientWidth;
                my_height = document.documentElement.clientHeight;
       }
       else if (document.body && (document.body.clientWidth || document.body.clientHeight))
       {
                my_width  = document.body.clientWidth;
                my_height = document.body.clientHeight;
       }


       obj.style.left = ((my_width  - objwidth)  / 2) + "px";
       obj.style.top  = ((my_height - objheight) / 2 + Ywindow()) + "px";
       obj.style.position = 'absolute';
       obj.style.zIndex   = 99;
       obj.style.display = 'block';

       Ginitialize('gmap', 1, companyid);
}


function closemap()
{
       var showmap_obj = fetch_object('showmap_div');
       
       if (showmap_obj != undefined)
       {
              showmap_obj.style.display = 'none';
       }
}


function fetch_search_t(search_type)
{
        if (search_type != undefined)
        {
                set_cookie(COOKIE_PREFIX + 'search_t', search_type);
                clear_filter();

                return true;
        }
        else
        {
                var stype = fetch_object('search_form').search_type;

                for (var ti = 0; ti < stype.length; ti++)
                {
                        if (stype[ti].value == 'google' && stype[ti].checked)
                        {
                                var w = window.open('http://www.google.lv/search?hl=lv&q=' + PHP.trim(fetch_object('inpt').value));
                                w.focus();

                                return false;
                        }
                        else if (stype[ti].checked)
                        {
                                set_cookie(COOKIE_PREFIX + 'search_t', stype[ti].value);
                                clear_filter();
                       
                                return true;
                        }
                }
       }

       return false;
}

function erase_search_word(word)
{
        inpt_object = fetch_object('inpt');
        
        if (inpt_object == undefined)
        {
                return false;
        }
        else
        {
                if (inpt_object.value == word)
                {
                        inpt_object.value = '';
                }
        }
}


function fetch_autocomplete_t()
{
       var stype = fetch_object('search_form').search_type;
       var type_param = 'product';

       for(var ti = 0; ti < stype.length; ti++)
       {
              if(stype[ti].value != 'google' && stype[ti].checked)
              {
                       type_param = stype[ti].value;
              }
       }


       $("#inpt").setOptions({
                 formatItem: liFormat,
                 onItemSelect: selectItem,
                 selectFirst: false,
                 matchContains: true,
                 minChars: 1,
                 delay: 100,
                 cacheLength: 0,
                 max: 13,
                 scroll: false,
                 extraParams: { 'type' : type_param }
       });
}

