﻿var biz_attr = new Array();

// lid - [packageid_uid]
function HasVideo(lid)
{
    if( biz_attr[lid] == null )
    {
        biz_attr[lid] = { video : false , coupon : false ,couponjs : "" };
    }
    biz_attr[lid].video = true;
}

function HasCoupon(lid)
{
    if( biz_attr[lid] == null )
    {
        biz_attr[lid] = { video : false , coupon : false ,couponjs : ""};
    }
    biz_attr[lid].coupon = true;
}

function RegisterCoupon(lid,sc)
{
    if( biz_attr[lid] == null )
    {
        biz_attr[lid] = { video : false , coupon : false ,couponjs : ""};
    }
    biz_attr[lid].couponjs = sc;
}
///////////

function CreateBizIcon(  zoom , pack)
{
    if(zoom>16) zoom=16;
    var p_icon = new GIcon();
    p_icon.image = icon_url;
    var size = 18.0*(zoom/16.0)*(zoom/16.0);
    p_icon.iconSize = new GSize(size, size);
    p_icon.iconAnchor = new GPoint(size/2, size/2 );
    p_icon.infoWindowAnchor = new GPoint(size/2, size/2);
    if( pack>10 )
    {
         p_icon = new GIcon();
         p_icon.image = "http://showmeontario.com/images/marker_blue.png";
         p_icon.shadow = "http://showmeontario.com/images/shadow50.png";
         p_icon.transparent = "http://maps.gstatic.com/intl/ru_ALL/mapfiles/markerTransparent.png";
         p_icon.shadowSize =new GSize(37, 34);
         p_icon.iconSize = new GSize(20, 34);
         p_icon.iconAnchor = new GPoint(10, 34 );
         p_icon.infoWindowAnchor = new GPoint(10, 34 ); 

    }else p_icon = G_DEFAULT_ICON;
    
    return p_icon;
}

function LocateOnMap(uid)
{
    var info = BizData[uid];
    var mapinfo = MapData[uid];
    if(info==null) 
    {
        alert("We are sorry, but we don't have this business on map.");
    }else
    {
        var Lat=info.getAttribute("Lat");
        var Long=info.getAttribute("Long");
    
        map.panTo(new GLatLng(Lat,Long));
        showInfoBalloon( mapinfo.point,mapinfo.title ,mapinfo.text );
    }
}



function FormatPhone(number)
{
    var s = String(number);
    number = s.replace(/\D/g,'');
    return "("+String(number).substring(0,3)+") "+String(number).substring(3,6)+" - "+String(number).substr(6);
}


function createMarkerBalloonOnTop(point,title, text,icon)
{
    var markerOpts = {
        "icon": icon,
        "zIndexProcess": function(mm)
        {
            return 10000;
        }
    };
    var marker = new GMarker(point,markerOpts);
    
 GEvent.addListener(marker, "click", function() {
       lastpoint = point;
       showInfoBalloon( point,title ,text );
      });
    return marker;    
}


var MapData = new Array();
var BizData = new Array();

function ShowBizItems()
{

    var aParams ="";//"?keyword="+keyword;
    map.clearOverlays();

    if(mgr!=null) 
    {
       for(a in mgr) // This is HARD way to delete object //
        {
            mgr[a] = function() {};
        }
        delete mgr;
    }
    mgr = new GMarkerManager(map);  

    var icon = new GIcon();
    if(icon_url!="")
    {
    icon.image = icon_url;
    icon.iconSize = new GSize(26, 26);
    icon.iconAnchor = new GPoint(13, 13);
    icon.infoWindowAnchor = new GPoint(12, 12);
    }else icon = G_DEFAULT_ICON;
    
    GDownloadUrl(items_url+aParams, function(data, responseCode) 
            {
                var bounds = new GLatLngBounds();
                bounds.extend( map.getCenter());
                var xml = GXml.parse(data);
                var markers = xml.documentElement.getElementsByTagName("bizViewListing");
                  for (var i = 0; i < markers.length; i++) 
                                {
                                    var point = new GLatLng(parseFloat(markers[i].getAttribute("Lat")),parseFloat(markers[i].getAttribute("Long"))
                                                 );
                                    if( parseFloat(markers[i].getAttribute("Lat"))<= 0) continue;
                                    
                                    bounds.extend(point);
                                    // Currently we don't support logo //
                                    var logo = "";
                                    
                                    var addr = markers[i].getAttribute("Address1")+" "+ markers[i].getAttribute("Address2");
                                    var title = String( addr ).length >25 ? String(addr).substr(0,25)+"...":addr;
                                    
                                    var packageId = parseInt( markers[i].getAttribute("PackageId") );
                                    var uid = parseInt( markers[i].getAttribute("uid") );
                                    
                                    var website = "";
                                    if( packageId > 10 )
                                    {
                                        var ur = markers[i].getAttribute("WebsiteUrl");
                                        if( ur == null ) ur = markers[i].getAttribute("WebsiteURL");
                                        
                                        if( ur != "" )
                                        {
                                            if( ur.indexOf('://') == -1 ) ur = "http://"+ur;
                                            website = "<a href='"+ur+"' target='_blank' style='float:left;margin-right:3px;display:block;'>"+"<img src='http://purrfectpages.com/images/public/buttons/bizdir/website.gif' target='_blank' style='border:none;' />"+"</a>";
                                        }
                                    }                                                                      
                                    
                                    if(markers[i].getAttribute("WebsiteUrl") == "")
                                    {
                                        website = "";
                                    }
                                    
                                    var desc ="";
                                    if(( packageId == 40 )||( packageId == 45 ))
                                    {
                                        desc=  "<div style='border-top:solid 1px #425095;padding-top:3px;margin-top:3px;color:#425095;font-size:10px;'>"+markers[i].getAttribute("BusinessDescription")+"</div>";
                                    }
                                    
                                    var coupon ="";
                                    var video ="";
                                    
                                    var lid = packageId+"_"+uid;
                                    
                                    if(biz_attr[lid] != null)
                                    {
                                        if( biz_attr[lid].video )
                                        {
                                            video = "<a style='display:block;float:left;margin-right:3px;' href='#' onclick='javascript:VideoComm("+uid+");return false;'>";
                                            video += "<img style='border:none;' alt='Video' src='http://purrfectpages.com/images/public/buttons/bizdir/video.gif' />";
                                            video += "</a>";
                                        }
                                        if( biz_attr[lid].coupon )
                                        {
                                            coupon = "<a style='display:block;float:left;margin-right:3px;' href='#' onclick='javascript:"+ biz_attr[lid].couponjs +";'>";
                                            coupon += "<img style='border:none;' alt='Coupon' src='http://purrfectpages.com/images/public/buttons/bizdir/coupon.gif' />";
                                            coupon += "</a>";
                                        }

                                    }
                                    
                                    
                                    
                                    var buttons = "<div style='height:25px;margin:5px auto;width:96px;'>"+coupon+website+video+"</div>";
                                    
                                    
                                    
                                                                      
                                    var text ="<div style='width:auto; _height:80px;height:auto !important;min-height:80px; font-weight:normal;font-size:14px;text-align:center;color:#000;'><div><b>"+ markers[i].getAttribute("BusinessName")+"</b></div><div style='margin-top:6px;'>"+ FormatPhone( markers[i].getAttribute("Phone") )+"</div>"+buttons+desc+"</div>";
                                    
                                    
                                    
                                    BizData[parseInt(markers[i].getAttribute("uid"))]=markers[i];
                                    MapData[parseInt(markers[i].getAttribute("uid"))] =  { point: point, title:title, text: text};
                                    for(z=10;z<=17;z++)
                                    {
                                        var newMarker =createMarkerBalloon(point,title,text,CreateBizIcon(z,markers[i].getAttribute("PackageId"))); 
                                        if( markers[i].getAttribute("PackageId") > 10 )
                                        {
                                            newMarker =createMarkerBalloonOnTop(point,title,text,CreateBizIcon(z,markers[i].getAttribute("PackageId"))); 
                                        }
                                        mgr.addMarker( newMarker , z , z );
                                        
                                    }
                                    

                                  }
                                
                                 
                                if((zoomlisting)&&(markers.length > 0)) 
                                {
                                    var zoom = map.getBoundsZoomLevel(bounds);
                                    if( map.getZoom < zoom )
                                    {
                                        map.setZoom(zoom); // set zoom level to fit all
                                    }
                                    map.setCenter(bounds.getCenter()); // set Center
                                }
                                
                                mgr.refresh();

          });
   
}

// This function shows video commercial for BizUser with provided uid //
var video_comm_frame = 0;
var video_comm_timer = null;
var video_comm_cnt = { left: -197, top:-70 , width:980 , height:440 ,cnt :null };

function ShowVideoCommBoxFrame()
{
    var cnt = video_comm_cnt.cnt;
    var offset_x = (video_comm_cnt.width/2)*((100-video_comm_frame)/100);
    var offset_y = (video_comm_cnt.height/2)*((100-video_comm_frame)/100);
 
    cnt.style.left = video_comm_cnt.left + offset_x + "px";
    cnt.style.top = video_comm_cnt.top + offset_y + "px";
    cnt.style.width = video_comm_cnt.width - 1.9*offset_x + "px";
    cnt.style.height = video_comm_cnt.height - 1.9*offset_y + "px";
    cnt.style.display = "block";
    SetOpacity(cnt,video_comm_frame / 120);
    video_comm_frame = MakeInc( video_comm_frame,0, 100, 10 );
    if( video_comm_frame >= 100 )
    {
        clearInterval(video_comm_timer);
        video_comm_timer = null;
        video_comm_frame = 0;
        cnt.style.left = video_comm_cnt.left + "px";
        cnt.style.top = video_comm_cnt.top + "px";
        cnt.style.width = video_comm_cnt.width +"px";
        cnt.style.height = video_comm_cnt.height + "px";
        $get("videopopupinner").style.display = "block";
    }
}

function HideVideoCommBoxFrame()
{
    SetOpacity(video_comm_cnt.cnt,(100-video_comm_frame) / 120);
    video_comm_frame = MakeInc( video_comm_frame,0, 100, 10 );
    if( video_comm_frame >= 100 )
    {
        clearInterval(video_comm_timer);
        video_comm_timer = null;
        video_comm_frame = 0;
        video_comm_cnt.cnt.style.display = "none";
        
        //
        
        if( typeof(smoOnHomeDD) != "undefined" )
        {
            $get(smoOnHomeDD).style.visibility = "visible";
        }
    }

}

function CloseVideoComm()
{
    $get("videopopupinner").style.display = "none";
     $get("video_comm_out").innerHTML = "";
    video_comm_timer = setInterval( HideVideoCommBoxFrame , 30 ); // let's animate it //
    
}

function VideoComm(uid)
{
////
    
    if( typeof(smoOnHomeDD) != "undefined" ) 
    if( $get(smoOnHomeDD) != null )
    {
        $get(smoOnHomeDD).style.visibility = "hidden";
    }
///
    var cnt = $get("videopopup");
    if($get("videopopupinner").style.display != "block")
    {
        video_comm_cnt.cnt = cnt;
        video_comm_timer = setInterval( ShowVideoCommBoxFrame , 30 ); // let's animate it //
    }
    $get("video_comm_out").innerHTML = "<img src='http://purrfectpages.com/images/public/ajax-loader1.gif' width='66' height='66' style='margin-left:440px;margin-top:150px; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=80);-moz-opacity:0.8;opacity:0.8;khtml-opacity:0.8' />";
    $get(hdn_video_id).value = uid;
    eval(video_comm_postback);
}

function MakeInc(ax,amin,amax,aspeed)
{
    if( ax < amax )
	{
		var t = ((ax-amin) / (amax - amin)) * (Math.PI / 2);
		if( t<0 ) 
		{
    		var t =(Math.PI / 8);
		}
	    	var amp = 2*Math.cos(  t  )*Math.sin(t )+ 0.2; // constant to start //
			ax += aspeed * amp; 
		}
	return ax;
}

// val in [0,1]
function SetOpacity(object,val)
{
    object.style.opacity = val;
    object.style.MozOpacity = val;
    object.style.KhtmlOpacity = val;
    object.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+val*100+")";
}

function TabChanged(sender,args)
{
    var mc = $get(mapcnt_id);
    $get(hdn_tabselected).value = sender.get_activeTabIndex();
    if( mc )
    {
        eval(full_postback);
    }
}

/* minimap */

var minimaptimer = 0;
var minimapframe = 0;

function ShowMiniMap(url)
{
    document.getElementById("minimap_img").src = url;
    if( minimaptimer == 0)
    {
        minimaptimer = setInterval( ShowMiniMapFrame , 30 );
    }
}

function CloseMiniMap()
{
    if( minimaptimer == 0)
    {
        minimaptimer = setInterval( HideMiniMapFrame , 30 );
    }
}

function ShowMiniMapFrame()
{
    SetOpacity( document.getElementById("minimap") , minimapframe/110);
    document.getElementById("minimap").style.visibility = "visible";
    document.getElementById("minimap").style.display = "block";
    minimapframe = MakeInc( minimapframe , 0 , 100 , 10);
    if( minimapframe > 100) 
    {
        minimapframe = 0;
        clearInterval( minimaptimer );
        minimaptimer = 0;
    }
}

function HideMiniMapFrame()
{
    SetOpacity( document.getElementById("minimap") ,  (100-minimapframe)/110);
    minimapframe = MakeInc( minimapframe , 0 , 100 , 10);
    if( minimapframe > 100) 
    {
        minimapframe = 0;
        clearInterval( minimaptimer );
        minimaptimer = 0;
        document.getElementById("minimap").style.visibility = "hidden";
        document.getElementById("minimap").style.display = "none";
    }
}
