﻿function brpDbHandler(container, routeWidget,opts){

 	var me = this;

	me.container = container;
	me.routeWidget = routeWidget;
    me.opts = opts;
    me.dbprint = false;
    me.dbmail = false;
    me.dbsave = false;
    me.dbsocial = false;
   this.addHiddenForm();     
}

brpDbHandler.prototype.addHiddenForm = function(){

	$(this.container).append('<form method=post action="" id="FrmSend" name="FrmSend" target="mailresponse" style="display:none; width:0px; height:0px;"><input type=hidden id="RouteInfo" name="RouteInfo" value=""/><input type=hidden id="PrintInfo" name="PrintInfo" value=""/><input type=hidden id="Route" name="Route" value=""/><input type=hidden id="SocUrl" name="SocUrl" value=""/><input type=hidden id="Guid" name="Guid" value=""/></form>');
	
    $(this.container).append('<iframe id="mailresponse" name="mailresponse" src="about:blank" style="display:none; width:0px; height:0px"></iframe>'); 
}

brpDbHandler.prototype.hasAction = function(actBtn,docemail,docname){

      if (!this.routeWidget.route)
      {
         alert('plan eerst een route');
         return false;
      }  

      this.dbprint = $(actBtn).hasClass('dbprint');  
      this.dbmail = $(actBtn).hasClass('dbmail');  
      this.dbsave = $(actBtn).hasClass('dbsave');  
      this.dbsocial = $(actBtn).hasClass('dbsocial');  
      
      if ((!docname) || (docname == ''))
      {
         alert('Geef je route eerst een naam.');
         return false;
      }
      else 
      {
        this.routeWidget.routeName = docname;
      }  

      if ((this.dbmail)&&(docemail =='')){
        alert('Vul eerst je email-adres in.');
        return false;
      }    

      
      return (this.dbprint || this.dbmail || ((this.dbsave ||this.dbsocial) && (!this.routeWidget.routeID)));
} 

brpDbHandler.prototype.setupData = function(doctype,docemail,docprivate,sendm,social){
    
    function printInfo(doctype,mail,priv,tomail,tomob,template) {
        var me = this;    
        me.DocType = doctype;
        if (mail)
          me.Mail = mail;
        else
          me.Mail = '';  
        me.Private = priv;
        me.ToMail = tomail;
        me.ToMobile = tomob;
        me.Template = template;
    }
    
    function routeInfo(rid,rdist,name,trip,locs,rtype,sp,ep,pois) {
        var me = this;    
        if (rid)
           me.RouteID = rid;
        else
           me.RouteID = -1;           

        me.RouteDist = rdist;
        me.RouteName = name;
        me.RoundTrip = trip;
        me.Locations = locs;
        me.RouteType = rtype;
        me.StartPoint = sp;
        me.EndPoint = ep;
        me.Pois = "";
        for(var Idx in pois){
           if (pois[Idx].visible)
            me.Pois += "|"+Idx; 
        }
     }
    
    function routeDescription(c,t) {
        var me = this;
        me.coords = c;
        me.rows = t;
    }
    

    
    var me = this;
    var pinfo = new printInfo(doctype,
                              docemail,
                              docprivate,
                              this.dbmail,
                              sendm,
                              me.opts.template);
    var data = new routeInfo(me.routeWidget.routeID,
                             me.routeWidget.route.distance,
                             me.routeWidget.routeName,
                             me.routeWidget.roundTrip,
                             me.routeWidget.locations,
                             me.routeWidget.routeType,
                             me.routeWidget.locations[0],
                             me.routeWidget.locations[me.routeWidget.locations.length-1],
                             me.routeWidget.pois);
    $("#FrmSend").attr("action",frpPrintUrl);
    $("#SocUrl").val("");
    if ((doctype != "saveroute") &&(doctype != "mailroute")&&(doctype != "mobileroute")) 
      $("#FrmSend").attr("target","_blank;");
    else
    {
      $("#FrmSend").attr("target","mailresponse"); 
      if (doctype == "saveroute") 
        alert('Uw route is opgeslagen.');
      else  
        alert('Uw route is verstuurd.');
    }
    if (social) {
      var sId = doctype.replace("soc","");
      $("#SocUrl").val(me.createSocialLink(sId,me.routeWidget.routeName) );  
    }
    
                            
    $("#RouteInfo").val($.toJSON(data));
    $("#PrintInfo").val($.toJSON(pinfo));

    if ((!this.routeWidget.guid)&&(!this.routeWidget.routeID))
        this.routeWidget.guid = jQuery.Guid.New();        

    if (me.routeWidget.guid)
     $("#Guid").val(me.routeWidget.guid);
    else 
     $("#Guid").val("");
     
    //if (doctype == "printroute") {
        $("#Route").val($.toJSON(new routeDescription(me.routeWidget.allCoords(),me.routeWidget.renderForPrint())));
    //}
    
    return true;
}
brpDbHandler.prototype.doSubmit = function() {

    $("#FrmSend").submit(); 
}

brpDbHandler.prototype.createSocialLink = function(Idx, title) {
    if ((Idx >=0) && (Idx <=12)) {    
        var str_url_full;


        var str_url_coded= this.opts.baseDocument + '?RouteID';
        str_url_coded=str_url_coded.replace('#','');
        var str_title_coded=title;
        var str_txt_coded='Een mooie route: '+str_title_coded;
        var arr_bm=new Array();
        arr_bm[1]=new Array("Twitter",1,4,"http://twitter.com/home","?status=replace_title:+replace_url");
        arr_bm[2]=new Array("Facebook",1,3,"http://www.facebook.com/sharer.php","?u=replace_url");
        arr_bm[3]=new Array("Hyves",1,2,"http://www.hyves.nl/profilemanage/add/tips/","?name=replace_title&text=replace_txt replace_url&type=10&rating=5");
        arr_bm[4]=new Array("LinkedIn",1,10,"http://www.linkedin.com/shareArticle","?mini=true&url=replace_url&title=replace_title&summary=replace_txt&source=falk");
        arr_bm[5]=new Array("MySpace",1,12,"http://www.myspace.com/Modules/PostTo/Pages/","?u=replace_url&t=replace_title");
        arr_bm[6]=new Array("Google Bookmarks",1,6,"http://www.google.com/bookmarks/mark","?op=add&bkmk=replace_url&title=replace_title&annotation=replace_txt");
        arr_bm[7]=new Array("Stumble it!",1,7,"http://www.stumbleupon.com/submit","?url=replace_url&title=replace_title&language=NL");
        arr_bm[8]=new Array("Del.icio.us",1,8,"http://del.icio.us/post","?v=4;url=replace_url;title=replace_title");
        arr_bm[9]=new Array("Digg",1,5,"http://digg.com/submit","?phase=2&url=replace_url&title=replace_title&bodytext=replace_txt");
        arr_bm[10]=new Array("eKudos",1,11,"http://www.ekudos.nl/artikel/nieuw","?url=replace_url&title=replace_title&desc=replace_txt");
        arr_bm[11]=new Array("NUjij",1,1,"http://nujij.nl/jij.lynkx","?t=replace_title&u=replace_url&b=replace_txt");
        arr_bm[12]=new Array("Symbaloo",1,9,"http://www.symbaloo.com/nl/add/","url=replace_url&title=replace_title&icon=http://www.okokorecepten.nl/i/socialbookmarks/symbaloo.gif");
        
        str_url_full=arr_bm[Idx][3]+arr_bm[Idx][4];
        str_url_full=str_url_full.replace(/replace_title/g,escape(str_title_coded));
        str_url_full=str_url_full.replace(/replace_url/g,escape(str_url_coded));
        str_url_full=str_url_full.replace(/replace_txt/g,escape(str_txt_coded));
	    str_url_full=str_url_full.replace(/replace_id/g,escape(str_txt_coded));
	   
	    return str_url_full;
	}
	else 
	  return "";
}
