﻿    var CWmap;
    var CWgeocoder;
    var curColorTargetID="";                            //hold the current colored cell used (to distinguish forecolor /backcolor / framecolor)
    var curColor="ffaaff";                              //temp var used accross events
    var curBackGround="img/box/Inox.jpg";               //backGround image at start
    var mapTypeControl=null;
    var smallControl;
    var largeControl;
    var clockAction=""; //mh=moreHeight, lw=lessWidth,...
    var stopTheClock=false;
    var running=false;

    //some default values
    if(typeof(grRoot)=='undefined')
        grRoot='http://www.gotreception.com/';
    else if(grRoot=="")
        grRoot='http://www.gotreception.com/';
    var CWurl= grRoot + "GREmbed.aspx?domain=www.gotreception.com";
    var width=380;
    var height=480;    var background="img/box/Inox.jpg";        var bgcolor="e4e4e4";    var textColor="000000";    var frameColor="bababa";    var widgetBorderColor="999999";    var minWidth = 280;    function startClock(action)    {                running=true;        if(typeof(action)!='undefined')        {            if(action!="")            {                clockAction=action;                stopTheClock=false;            }        }        if(!stopTheClock)         {                       if(clockAction=='lw')                        lessWidth();                        else if(clockAction=='mw')                        moreWidth();            else if(clockAction=='lh')                        lessHeight();            else if(clockAction=='mh')                        moreHeight();                              setTimeout('startClock("")',200);                  }        else            running=false;    }    function stopClock()    {        stopTheClock=true;    }    function validateWidth()
    {   
        if(isNaN($('width').value))        
            $('width').value=380;                 
        if($('width').value < minWidth)
        {
            $('width').value = minWidth;
            stopTheClock=true;
        }
        
        if($('width').value > 2000)
        {
            stopTheClock=true;
            $('width').value = 2000;                    
        }

        width = $('width').value    
        preview();
    }
    function moreWidth()
    {
        validateWidth();
        $('width').value = eval($('width').value) + 10;
        validateWidth();        
    }
    function lessWidth()
    {
        validateWidth();        
        $('width').value = eval($('width').value) - 10;
        validateWidth();
    }
    function validateHeight()
    {   
        if(isNaN($('height').value))    
            $('height').value=570;
        if($('height').value < 200)
            $('height').value = 200;
        if($('height').value > 2000)
            $('height').value = 2000;        
        height = $('height').value    
        preview();
    }
    function moreHeight()
    {
        validateHeight();
        $('height').value = eval($('height').value) + 10;
        validateHeight();
    }
    function lessHeight()
    {
        validateHeight();        
        $('height').value = eval($('height').value) - 10;
        validateHeight();
    }
    function $(ele)
    {
        return document.getElementById(ele);
    }
    function preview()
    {   
        $('cell').width=eval($('width').value);                         //cell is the main cell that hold the widget
        $('CWmap').style.width=(eval($('width').value)-16) + "px";      //-25
        devY=$('cc').clientHeight+$('aTable').clientHeight;        
        $('CWmap').style.height=eval($('height').value-90) + "px";
        $('cTable').style.height=eval($('height').value)+"px";
        if($('CWmap').clientHeight<300)
        {
            CWmap.removeControl(largeControl);        
            CWmap.addControl(smallControl);        
        }
        else
        {
            CWmap.removeControl(smallControl);        
            CWmap.addControl(largeControl);        
        }            
        if(typeof(CWmap)!='undefined')
            CWmap.checkResize();
    }
    function generate()
    {
        if($('CWmap').style.display=="block")
            checkAddress('left'); //non-blocking check facility                       
        result = "";
        result += "<iframe scrolling=\"no\" src =\"";
        result += CWurl;
        result += "&zoom=" + CWmap.getZoom();
        result += "&latitude=" + CWmap.getCenter().lat(); //latitude;
        result += "&longitude=" + CWmap.getCenter().lng(); //longitude;
        result += "&width=" + width;
        result += "&height=" + height;
        if($('rdo1').checked)
        {
            result += "&bgcolor=" + bgcolor;        }        result += "&background=" + background;        result += "&textColor=" + textColor;        result += "&frameColor=" + rgbToHex(frameColor);        result += "&widgetBorderColor=" + rgbToHex(widgetBorderColor);
        result += "&defaultLocation=" + $('textLocation').value;    
        result += "&showMap=" + $('chkShow').checked;    
        strCar="";
        for(var j=0; j< maxcarrier; j++)
        {
            if($('ctl00_CW_CarriersCheckboxes_'+j).checked)
            {
                strCar += (carrierName[j]=="AT&T"?"ATnT":carrierName[j]) + "_";
            }    
        }
        result += "&carriers=" + strCar + "\" "; //carriers
        result += "width=\"" + width + "\" ";
        result += "height=\"" + ( eval(height) + 15 ) + "\" ";
        result += "frameborder=\"0\" ></iframe>";
        $('result').value=result;
        $('txtLocation').value = $('textLocation').value;
    }    
    
    function changeBG(bgid)
    {   
        var imgurl = $(bgid).attributes['src'].value;
        $('cTable').style.backgroundImage="url(" + imgurl + ")";
        curBackGround=imgurl;
        background=imgurl;
        $('rdo0').checked=true;
    }
   
    function showColorDiv(e,i)      //e is the event object, i is the id of the rectangle colored cell that is clicked
    {
        $('colorDiv').style.display="block";
        //document.all is IE, and the e.pageX is for FF
        x = (document.all ? (e.clientX + document.documentElement.scrollLeft) : e.pageX); // - $('holder').offsetLeft ;
        y = (document.all ? (e.clientY + document.documentElement.scrollTop) : e.pageY); // - $('holder').offsetTop ;                
        $('colorDiv').style.left= x +"px";
        $('colorDiv').style.top= y + "px";        
        curColorTargetID = i;
        curColor = $(curColorTargetID).style.backgroundColor;
        if(i=='foreColorCell')
            textColor=$(curColorTargetID).style.backgroundColor;
        else if(i=='backColorCell')
            bgcolor=$(curColorTargetID).style.backgroundColor;
        else if(i=='frameColorCell')
            frameColor=rgbToHex($(curColorTargetID).style.backgroundColor);
        else if(i=='widgetBorderColorCell')
            widgetBorderColor=rgbToHex($(curColorTargetID).style.backgroundColor);
    }    
    
    function hideColorDiv()
    {
       $('colorDiv').style.display="none";
       
       if($(curColorTargetID)!=null)
            $(curColorTargetID).style.backgroundColor=curColor;       
       
       if(curColorTargetID=='foreColorCell')
       {
            $('cTable').style.color=textColor;
            $('aTable').style.color=textColor;
            $('ctl00_CW_CarriersCheckboxes').style.color=textColor;
       }
       else if(curColorTargetID=='backColorCell')   
       {       
            if($('rdo1').checked == true)
            {
                $('cTable').style.backgroundColor=bgcolor;
                $('cTable').style.backgroundImage="";
            }
            else
            {
                $('cTable').style.backgroundColor="";
                $('cTable').style.backgroundImage="url(" + background + ")";
            }
            
       }
       else if(curColorTargetID=='frameColorCell')
       {
            $('CWmap').style.border="solid 2px #" + rgbToHex(frameColor);
       }
       else if(curColorTargetID=='widgetBorderColorCell')
       {
            $('cTable').style.border="solid 2px #" + rgbToHex(widgetBorderColor);
       }
    }    
    function tryColor(c)                                //c is the colored cell in color table that received mouse click
    {
        color=c.style.backgroundColor;        
        $('colorbox').style.backgroundColor=color;
        $('rgbinput').value=rgbToHex(color)
        if(curColorTargetID=='foreColorCell')
        {
            $('cTable').style.color=color;
            $('aTable').style.color=color;
            $('ctl00_CW_CarriersCheckboxes').style.color=color;            
        }
        else if(curColorTargetID=='backColorCell')   
        {        
            $('cTable').style.backgroundColor=color;
            $('cTable').style.backgroundImage="";
        }
        else if(curColorTargetID=='frameColorCell')
        {
            $('CWmap').style.border="solid 2px #" + rgbToHex(color);
        }
        else if(curColorTargetID=='widgetBorderColorCell')
        {
            $('cTable').style.border="solid 2px #" + rgbToHex(color);
        }
    }
    function tryKeyedInColor()
    {
        value=$('rgbinput').value;        
        if(value.length!=6)
            return;
        color="#"+value;        
        $('colorbox').style.backgroundColor=color;        
        if(curColorTargetID=='foreColorCell')
        {
            $('cTable').style.color=color;
            $('aTable').style.color=color;
            $('ctl00_CW_CarriersCheckboxes').style.color=color;            
        }
        else if(curColorTargetID=='backColorCell') 
        {        
            $('cTable').style.backgroundColor=color;
            $('cTable').style.backgroundImage="";
        }
        else if(curColorTargetID=='frameColorCell')
        {
            $('CWmap').style.border="solid 2px " + color;
        }
        else if(curColorTargetID=='widgetBorderColorCell')
        {
            $('cTable').style.border="solid 2px " + color;
        }
    }
    function useColor(c)
    {        
        color=c.style.backgroundColor;
        $(curColorTargetID).style.backgroundColor=color;
        curColor = $(curColorTargetID).style.backgroundColor     
        if(curColorTargetID=='foreColorCell')
        {
                $('cTable').style.color=color;
                $('aTable').style.color=color;
                $('ctl00_CW_CarriersCheckboxes').style.color=color;
                textColor=color;
                textColor=rgbToHex(textColor);
        }
        else if(curColorTargetID=='backColorCell') 
        {
                $('cTable').style.backgroundColor=color;        
                $('cTable').style.backgroundImage="";        
                bgcolor=rgbToHex(color);
        } 
        else if(curColorTargetID=='frameColorCell')
        {
            $('CWmap').style.border="solid 2px #" + rgbToHex(color);
            frameColor=rgbToHex(color);            
        }
        else if(curColorTargetID=='widgetBorderColorCell')
        {
            $('cTable').style.border="solid 2px #" + rgbToHex(color);
            widgetBorderColor=rgbToHex(color);            
        }       
               
        if(curColorTargetID=='backColorCell')
            $('rdo1').checked = true;
        hideColorDiv();
    }
    function checkColorRadio()                              //in case user click radio buttons => change widget accordingly
    {
        if($('rdo1').checked == true)
             //$('bTable').style.backgroundColor=bgcolor;
             $('cTable').style.backgroundColor=bgcolor;
        else
        {
             //$('bTable').style.backgroundColor="";
             $('cTable').style.backgroundColor="";
             $('cTable').style.backgroundImage="url(" + curBackGround + ")";
        }
    }     
    function restoreColor()
    {        
    }    
    function showMap(show)
    {
        if(show)
        {
            $('CWmap').style.display="block";
            $('cc').style.display="block";    
            $('spacer1').style.display="block";
            $('spacer2').style.display="block";        
            $('height').value= $('cc').clientHeight + $('aTable').clientHeight  + $('CWmap').clientHeight  + 10; //24
            $('lh').disabled=false;
            $('mh').disabled=false;
            $('height').disabled=false;
            height=$('height').value;
        }
        else
        {
            $('CWmap').style.display="none";
            $('cc').style.display="none";
            $('spacer1').style.display="none";
            $('spacer2').style.display="none";
            $('height').value=60;
            $('cTable').style.height="60px";
            height=60;
            $('lh').disabled=true;
            $('mh').disabled=true;
            $('height').disabled=true;
        }
    }
    
    function rgbToHex(rgbstr)
    {        
        if(rgbstr.indexOf("rgb")!=0)
        {            
            if(rgbstr.indexOf("#")==0)
                return rgbstr.substr(1,6);            
            else
                return rgbstr;
        }    
        cArr=rgbstr.replace('r','');
        cArr=cArr.replace('g','');
        cArr=cArr.replace('b','');
        cArr=cArr.replace('(','');
        cArr=cArr.replace(')','');
        cArr=cArr.split(',');
        r = eval(cArr[0]);
        g = eval(cArr[1]);
        b = eval(cArr[2]);        
        r=r.toString(16);
        g=g.toString(16);
        b=b.toString(16);        
        return (r.length==2?r:'0'+r)+(g.length==2?g:'0'+g)+(b.length==2?b:'0'+b);
    }
    var customize=false;
    function customizeColor()
    {
        customize=!customize;
        $('ct').style.visibility=customize?"visible":"hidden";
        $('customizeLink').innerHTML=customize?"Customize":"Customize";
        $('customizeDivBack').style.width=$('holder').clientWidth + "px";
        $('customizeDivBack').style.height=$('holder').clientHeight + "px";
        $('customizeDivBack').style.display=customize?"block":"none";
        if($(curColorTargetID)!=null)
            hideColorDiv();
        else
            $('colorDiv').style.display="none";
    }
    function changeTheme(bgid,forecolor,framecolor, widgetcolor)
    {
            $('cTable').style.color="#"+forecolor;
            $('aTable').style.color="#"+forecolor;            
            $('ctl00_CW_CarriersCheckboxes').style.color="#"+forecolor;                  
            $('foreColorCell').style.backgroundColor="#"+forecolor;        
            textColor=forecolor;
            $('CWmap').style.border="solid 2px #" + framecolor;
            $('cTable').style.border="solid 2px #" + widgetcolor;
            $('frameColorCell').style.backgroundColor="#" + framecolor;
            frameColor=rgbToHex($('frameColorCell').style.backgroundColor);  
            changeBG(bgid);    
    }
    
    //=================   
    var WIDTH=1024; var HEIGHT=768;
    
    function CWload() {
        
      //$('txtState').style.width=$('txtCity').clientWidth + "px";
      if(typeof(GBrowserIsCompatible)=='undefined')
        return;
        
      if (GBrowserIsCompatible()) {
        CWmap = new GMap2(document.getElementById("CWmap"));
        CWmap.checkResize();
        CWmap.setCenter(map.getCenter(), map.getZoom()); 
        smallControl=new GSmallMapControl();
        largeControl=new GLargeMapControl();
        CWmap.addControl(largeControl);        
        mapTypeControl = new GMapTypeControl();
        CWmap.addControl(mapTypeControl);
        baseIcon = new GIcon();
        baseIcon.iconSize = new GSize(27, 35);
        baseIcon.iconAnchor = new GPoint(6, 35);
        baseIcon.infoWindowAnchor = new GPoint(5, 1);        
        
        var pos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0,15));
        pos.apply(document.getElementById("logoMap"));
        CWmap.getContainer().appendChild(document.getElementById("logoMap")); 
        
        CWtoggleOverlay(-1, true);
        CWgeocoder = new GClientGeocoder();
        GEvent.addListener(CWmap, "dragend", CWrefreshComments);     
        GEvent.addListener(CWmap, "zoomend", CWrefreshComments);     

        GEvent.addListener(CWmap, "click", CWmapClick);         //click the comments/towers markers on the map => pop info window
                      
        GEvent.addListener(CWmap, "dragstart", function()
          {
            if (CWcomments != null)
            {
                    for (i=0; i < CWcomments.length; i++) 
                    {
                        CWmap.removeOverlay(CWcomments[i].marker);        
                    }
            }
          });     
          
        flat = CWmap.getBounds().getSouthWest().lat();
        flng = CWmap.getBounds().getSouthWest().lng();
        tlat = CWmap.getBounds().getNorthEast().lat();
        tlng = CWmap.getBounds().getNorthEast().lng();
        CWfetchComments(flat, flng, tlat, tlng, 0); //this is embeded => no date filter, set DateFilter=30 days
      }
      
    }        
    
    //=====================
    function checkAddress(whichPane)  //call ajax to get lat/long and move the map to that location
    {
        var addr;
        if(whichPane=='left')
           addr =$('textLocation').value;            
        else       
            addr =$('txtLocation').value;
        
        if(typeof(CWgeocoder)!='undefined')
            CWgeocoder.getLocations(addr, CWgotAddress);
    }
           
    //animation the widget screen
    function showAppearStart()
    {
        $('cover').style.opacity =0;
        $('cover').style.display="block";
        $('anim').style.display="none";
        $('holder').style.display="none";
        $('anim').style.width="0px";
        $('anim').style.height="0px";        
        $('CWcontent').style.display="none";        
        WIDTH=screen.availWidth;
        HEIGHT=screen.availHeight;
        $('holder').style.left = (WIDTH - $('anim').clientWidth ) / 2 + "px";
        $('holder').style.top = ((HEIGHT - $('anim').clientHeight)/2 +document.documentElement.scrollTop)+ "px";        
        dimmer();    
    }    

    function dimmer()
    {
         if(eval($('cover').style.opacity)<0.6)
         {
            $('cover').style.opacity  = eval($('cover').style.opacity) + 0.15;
            $('cover').style.filter = "alpha(opacity=" + (eval($('cover').style.opacity)*100) + ");"; 
            setTimeout('dimmer()',10);            
         }
         else
         {
            $('anim').style.display="none";                        
            $('holder').style.display="block";
            $('CWcontent').style.display="block";
            $('holder').style.left = (WIDTH - $('holder').clientWidth ) / 2 + "px";
            $('holder').style.top = Math.max(((HEIGHT - $('holder').clientHeight)/2 +document.documentElement.scrollTop - 80), 0)+ "px";        
            if(typeof(CWmap)!='undefined')
            {
                CWmap.checkResize();
                CWmap.setCenter(map.getCenter(),map.getZoom());
            }
            for(var ind=0; ind< maxcarrier; ind++)
            {
                $('ctl00_CW_CarriersCheckboxes_'+ind).checked=CWcarrierDisplay[ind]
            }
         }
    }
    function undimmer()
    {
        if(eval($('cover').style.opacity)>0)
         {
            $('cover').style.opacity  = eval($('cover').style.opacity) - 0.15;
            $('cover').style.filter = "alpha(opacity=" + (eval($('cover').style.opacity)*100) + ");"; 
            setTimeout('undimmer()',10);            
         }
         else
         {            
            $('cover').style.opacity=0; 
            $('holder').style.display='none';
            $('cover').style.display='none'; 
            $('anim').style.width='0px';
            hideColorDiv();
         }       
    }
