﻿//Electric Vine namespace
var ELV = (typeof FNG == "object") ? FNG : {};

//creating popups class
ELV.popups = function(){
    
    //login
    this.login = function(){
        var u = new ELV.utils();

        var l_wrp = u.g("show_login");
        if(l_wrp){
        
            document.onclick = function(e){
                u.g("popup").style.display = "none";
            }

            u.g("popup").onclick = function(e){
                //e = e || event;
                if (!e)e = event; //IE fix
                e.cancelBubble = true;
            }
        
            l_wrp.onclick = function(e){
                if (!e)e = event; //IE fix
                e.cancelBubble = true;
                
                u.g("popup").style.display = "block";
                return false;
            }
        }
    };
    
    //show lessons
    this.nextLesson = function(){
        var u = new ELV.utils();
        var el = u.g("ctl00_major_content_placeholder_Repeater1_ctl00_LinkButton15");
        if(el){
            el.onclick = function(){
                u.g("cur_ass").style.display = "none";
                u.g("cur_ass2").style.display = "block";
                return false;
            }
        }
    };
};


ELV.calendar = function(){
    
    this.ini = function(){
        var u = new ELV.utils();
        
        u.toggleCheckBox("cal_note_check", "cal_note_wrp");
        u.toggleCheckBox("cal_lessons_check", "cal_lessons_wrp");

//        u.g("cal_repeat_drp").onchange = function(){

//            if(this.options[this.selectedIndex].value != "none"){
//                u.g("cal_repeat_wrp").style.display = "block";
//            }
//            else{
//                u.g("cal_repeat_wrp").style.display = "none";
//            }
//            
//        }
        
    };

};


ELV.utils = function(){
    this.g = function(id){
        return document.getElementById(id);
    }
    
    //http://javascript.crockford.com/code.html
    this.gc = function(className) {
        var results = [];
        walkTheDOM(document.body, function (node) {
            var a; 
            var c = node.className;
            var i;
            if (c) {
                a = c.split(' ');
                for (i = 0; i < a.length; i += 1) {
                    if (a[i] === className) {
                        results.push(node);
                        break;
                    }
                }
            }
        });
        return results;
    }
    
    this.gc2 = function(searchClass,node,tag) {
	    var classElements = new Array();
	    if ( node == null )
		    node = document;
	    if ( tag == null )
		    tag = '*';
	    var els = node.getElementsByTagName(tag);
	    var elsLen = els.length;
	    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	    for (i = 0, j = 0; i < elsLen; i++) {
		    if ( pattern.test(els[i].className) ) {
			    classElements[j] = els[i];
			    j++;
		    }
	    }
	    return classElements;
    }
    
    
    this.toggleCheckBox = function(CheckId, wrpId){
        var u = new ELV.utils();
        u.g(CheckId).onclick = function(){
            if(this.checked == true){
                u.g(wrpId).style.display = "block";
            }
            else{
                u.g(wrpId).style.display = "none";
            }
        }
    }
  
}

ELV.recordResults = function(rrA){
    var u = new ELV.utils();
    var cnt = 0;
    var ans = {}; //indicates if answer was given

    return {
        sel : function(elm, typ, id){
        
            if(!ans[id]){
                
                //indicates that this was answered: prevents from double answer
                ans[id] = true;
                
                var bcColor;
                //right/wrong answer
                if(this.rrAnswer.ans[id-1] == typ){
                    bcColor = "#6abf32";
                    cnt = cnt + 1;
                }
                else{
                    bcColor = "#e34c35";
                }
                
                if(cnt != 0){
                    u.g("rr_percentage").innerHTML = cnt + "0%";
                }
                //updates color
                u.gc2("rr_list_wrp" + id)[0].style.backgroundColor = bcColor;
                
            }
            
        }
    }
}

ELV.map = function(){
    
    //reference
    //http://code.google.com/apis/maps/documentation/reference.html
    
    var geocoder = null;
    
    this.iniMulti = function(){
        if (GBrowserIsCompatible()) {
            _centLL = new GLatLng(json.center.location_lng, json.center.location_lat);
            var mp = new ELV.map();
            mp.LoadMulti(_centLL, 13);
        }
    }
    
    this.loadFromAddress = function(data){
        var u = new ELV.utils();
        var geocoder = null;
        var address = data.markerHtml;

        if (GBrowserIsCompatible()) {
            map = new GMap2(u.g(data.elmId));
            //map.setCenter(new GLatLng(37.4419, -122.1419), data.zoomLevel);
            map.addControl(new GSmallMapControl());
            map.addControl(new GMapTypeControl());
            geocoder = new GClientGeocoder();

            if (geocoder) {
                geocoder.getLatLng(
                    data.address,
                    function(point) {
                        if (!point) {
                              u.g(data.elmId).style.display = 'none';
                        } else {
                          map.setCenter(point, data.zoomLevel);
                          var marker = new GMarker(point);
                          map.addOverlay(marker);
                          marker.openInfoWindowHtml(address);
                        }
                    }
                );
            }

        }
              
            

    }
    

    this.LoadMulti = function(centLL, cenZoom){
    
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));

        map.setCenter(centLL, cenZoom);
        map.addControl(new GSmallMapControl());

        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon("");
        baseIcon.iconSize = new GSize(20, 25);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(20, 2);
       
        // Creates a marker whose info window displays the letter corresponding
        // to the given index.
        function createMarker(point, index, name, address) {
          // Create a lettered icon for this point using our icon class
          var letter = String.fromCharCode("A".charCodeAt(0) + index);
          var letteredIcon = new GIcon(baseIcon);
         
         //map icon
         letteredIcon.image = "../../images/icon_map_" + (index + 1) + ".png";

           
          // Set up our GMarkerOptions object
          markerOptions = { icon:letteredIcon };
          var marker = new GMarker(point, markerOptions);
          
          var winHtml = "<strong>" + name + "</strong>" + "<br /><a href='writereview.aspx'>write a review</a> <a href='locationdetails.aspx'>more info&nbsp;»</a>" + "<br /><br />" + address

          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(winHtml);
          });
          return marker;
        }
        

        function loadMarkers2(){
            for (var i = 0; i < json.loc.length; i++) {
                var latlng = new GLatLng(json.loc[i].location_lng, json.loc[i].location_lat);
               
               //code for getting lang/lat from address
//                var geocoder = new GClientGeocoder();
//                var latlng2 = geocoder.getLatLng(
//                json2.loc[i].user_name,
//                    function(point) {
//                      if (!point) {
//                        alert(json2.loc[i].user_name + " nenasli");
//                          } else {
//                            return point;
//                          }
//                    }
//                );
                
                map.addOverlay(createMarker(latlng, i, json.loc[i].name, json.loc[i].address));
            }
        }
        
        loadMarkers2();
      }
    }
}

window.onload = function(){
    var p = new ELV.popups();
    p.login();
    
};
