BROWSER = new Object();
BROWSER.isOpera = navigator.userAgent.indexOf("Opera") > -1;
BROWSER.isIE    = navigator.userAgent.indexOf("MSIE") > 1 && !BROWSER.isOpera; 
BROWSER.isMoz   = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !BROWSER.isOpera;

if (typeof($) != "function") $ = function(ID){ return document.getElementById(ID); }

function redirect(id) {
   alert("This link works only in front-end site!")
}

function popupimg(img) {
    var d = new Date()
    var ID = d.getDate()+""+d.getMonth()+1+""+d.getFullYear()+""+d.getHours()+""+d.getMinutes()+""+d.getSeconds();

    var loc = "/enlarge.php?src="+img;
	var win = window.open(loc, "_new"+ID,"toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=200,height=200");
	win.location.href = loc;
	win.focus();
}

/* highlist elements onmouse over event
 */
function highlight(groupElements, classOver) {
   var i, j, Obj, groupObjects = [];
   for(j=1; j<100; j++) {
   	   for(i in groupElements) {
   	   	   Obj = $(groupElements[i]+j);
           if (Obj) {
               Obj.ind = j; 
               groupObjects[groupObjects.length] = Obj;
           }
       }
   }
   
   for(i in groupObjects) {
      Obj = groupObjects[i];
      Obj.classDefault = Obj.className;
      Obj.classOver = classOver;
      Obj.groupObjects = groupObjects;
      
      Obj.onmouseover = function() {
      	  this.className = this.classOver;
      	  for(var i in this.groupObjects){
      	      var Obj = this.groupObjects[i]; 
      	  	  if (Obj.ind == this.ind) Obj.className = Obj.classOver;
      	  }
      }
      Obj.onmouseout = function() {
      	  this.className = this.classDefault;
      	  for(var i in this.groupObjects){
      	  	  var Obj = this.groupObjects[i];
      	  	  if (Obj.ind == this.ind) Obj.className = Obj.classDefault;
      	  }
      }
   }
}

function openLink(link) {
   location.href = link;
}

function getCityByCountryId(countryId, selectedCity){
    jQuery.ajax({
        type: 'POST',
        url: '/ajaxCityByCountryId.php',
        data: 'countryId='+countryId+'&selectedCity='+selectedCity,
        success: function(html){
            jQuery("#login_city").html(html);
        }
    });     
}

function initRegistrationStep1(formUser){
    $().ready(function() {      
        var countryId = $('#login_country').val();
        getCityByCountryId(countryId, formUser);
        
        $('#login_country').change(function(){
            var countryId = $('#login_country').val();
            getCityByCountryId(countryId, 0);
        })
    });  
}

var map = null;
var geocoder = null;
var marker;
function initRegistrationStep2(data){

      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        
        // Create our "tiny" marker icon
        var kkIcon = new GIcon(G_DEFAULT_ICON);
        kkIcon.image = data.iconMembers;
        kkIcon.iconSize = new GSize(20, 23);
        kkIcon.iconAnchor = new GPoint(10, 23);
        kkIcon.shadow = data.iconShadow;
        kkIcon.shadowSize = new GSize(30, 22);
        // Set up our GMarkerOptions object
        markerOptions = { icon:kkIcon };

        if (data.centerPoint != false) {
            var coords = data.centerPoint.split(",")
            map.setCenter(new GLatLng(parseFloat(coords[0]), parseFloat(coords[1])), 15);
            marker = new GMarker(map.getCenter(), markerOptions);
            map.addOverlay(marker);
            document.getElementById('login_gm_point').value = map.getCenter();
        } else {
            map.setCenter(new GLatLng(51.4613, 14.8535), 4);
        }

        map.setUIToDefault();
        
        //GOverviewMapControl 
        var mapControl = new GMapTypeControl();
        map.addControl(mapControl);
        map.addControl(new GOverviewMapControl());
        //map.disableScrollWheelZoom();
        //map.enableDoubleClickZoom()();
        
        geocoder = new GClientGeocoder();
        
        GEvent.addListener(map,"click", function(overlay, latlng) {     
          if (latlng) { 
            if (marker){
                map.removeOverlay(marker);
            }
            var myHtml = "The GPoint value is: " + latlng + " at zoom level " + map.getZoom();
            //map.openInfoWindow(latlng, myHtml);
            marker = new GMarker(latlng, markerOptions);
            map.addOverlay(marker);
            document.getElementById('login_gm_point').value = latlng;
          }
        });
      }

}


function showAddress(address) {
    //document.getElementById('login_gm_point').innerHTML = 'beerDescr';
    if (geocoder) {
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                    document.getElementById('errorAutoFindPosition').innerHTML = "error auto find position";
                } else {
                    if (marker){
                        map.removeOverlay(marker);
                    }
                    map.setCenter(point, 15);
                    marker = new GMarker(point, markerOptions);
                    map.addOverlay(marker);
                    //marker.openInfoWindowHtml(address);
                    //alert(point);
                    document.getElementById('login_gm_point').value = point;
                }
            }
        );
    }
}


function initRegistrationStep3() {
    var id = 0;
    
    $(function(){
        // sorts all the combo select boxes
        function sortBoxes(){           
            $('select.left, select.right').find('option').selso({
                type: 'alpha', 
                extract: function(o){ return $(o).text(); } 
            });
            
            // clear all highlighted items
            $('select.left, select.right').find('option:selected').removeAttr('selected');
        }
        
        function sortTable(){  

            var myData = new Array();
            var myDataVal = new Array();

            var i = 0;
                
            $('#beerTypes tr td.fild').each(function(el){
                var a = $(this).children();
                var elid = a.attr('id');
                var val = a.attr('value');

                myData[i] = elid;
                myDataVal[i] = val;
                i++;
             });
                     
            $('#beerTypes').find('tr').selso({
                type: 'alpha', 
                orderBy:'td.beerTitle',                
                extract: function(o){ return $(o).text(); } 
            });
            
            if (i>0){
                for(j=0; j<i; j++){
                    document.getElementById(myData[j]).value = myDataVal[j];
                }
            }
        }

        // add/remove buttons for combo select boxes
        $('input.add').click(function(){
            var left = $(this).parent().parent().find('select.left option:selected');
            var right = $(this).parent().parent().find('select.right');

         $("#beer_type_not_selected option:selected").each(function(el){
            var a = $(this);
            var str = a.attr('text');
            $('#beerTypes').append('<tr id="c'+a.attr('value')+'"><td class="beerTitle"><label for="beerDescr['+a.attr('value')+']">'+str+'</label></td><td class="fild"><textarea id="beerDescr['+a.attr('value')+']" class="txtAreaBig2" rows="2" cols="30" name="beerDescr['+a.attr('value')+']"></textarea></td></tr>');
         });


            right.append(left);
            sortBoxes();    
            sortTable();

        });
    
        $('input.remove').click(function(){
            var left = $(this).parent().parent().find('select.left');
            var right = $(this).parent().parent().find('select.right option:selected');
            
            $("#beer_type_selected option:selected").each(function(el){
            var a = $(this);
            var elid = '#c'+a.attr('value');
            $(elid).remove();
         });
            
            left.append(right);
            sortBoxes();
        }); 
        
        // double-click moves an item to the other list
        $('select.left').dblclick(function(){
            $(this).parent().find('fieldset input.add').click();
        });
        
        $('select.right').dblclick(function(){
            $(this).parent().find('fieldset input.remove').click();
        });
    });

    $().ready(function() {  
        $('form').submit(function() {  
            $('#beer_type_selected option').each(function(i) {  
                $(this).attr("selected", "selected");  
            });  
        }); 
        
        
        //add new keykeg type
//        $('#addKeykegType').click(function() {  
//            var KeykegTypeNmae = $('#new_keykeg_type').val();
//            if(KeykegTypeNmae == ''){
//                $('#new_keykeg_type_message').html(addKeykegTypeErrorMessage);
//            }else{
//                var KeykegTypeId = addNewKeykegType(KeykegTypeNmae);
//                
//                //$('#new_keykeg_type').val() = '';
//                document.getElementById('new_keykeg_type').value = '';
//            }
//        }); 
        //end - add new keykeg type
       
    }); 

        function sortBoxes1(){           
            $('select.left, select.right').find('option').selso({
                type: 'alpha', 
                extract: function(o){ return $(o).text(); } 
            });
            
            // clear all highlighted items
            $('select.left, select.right').find('option:selected').removeAttr('selected');
        }
        
        function sortTable1(){  

            var myData = new Array();
            var myDataVal = new Array();

            var i = 0;
                
            $('#beerTypes tr td.fild').each(function(el){
                var a = $(this).children();
                var elid = a.attr('id');
                var val = a.attr('value');

                myData[i] = elid;
                myDataVal[i] = val;
                i++;
             });
                     
            $('#beerTypes').find('tr').selso({
                type: 'alpha', 
                orderBy:'td.beerTitle',                
                extract: function(o){ return $(o).text(); } 
            });
            
            if (i>0){
                for(j=0; j<i; j++){
                    document.getElementById(myData[j]).value = myDataVal[j];
                }
            }
        }
    
    function addNewKeykegType(KeykegTypeNmae){
        jQuery.ajax({
            type: 'POST',
            url: '/ajaxAddNewKeykegType.php',
            data: 'KeykegTypeNmae='+KeykegTypeNmae,
            success: function(html){
                //jQuery("#login_city").html(html);
                var KeykegTypeId = html;
                $('#beer_type_selected').append('<option id="opt'+KeykegTypeId+'" value="'+KeykegTypeId+'">'+KeykegTypeNmae+'</option>');
                $('#beerTypes').append('<tr id="c'+KeykegTypeId+'"><td class="beerTitle"><label for="beerDescr['+KeykegTypeId+']">'+KeykegTypeNmae+'</label></td><td class="fild"><textarea id="beerDescr['+KeykegTypeId+']" class="txtAreaBig2" rows="2" cols="30" name="beerDescr['+KeykegTypeId+']"></textarea></td></tr>');
                
                sortBoxes1();    
                sortTable1();
                $('#new_keykeg_type_message').html(KeykegTypeNmae+" "+newKeykegTypeAddedMessage);
            }
        });     
    } 

}   
