/**************************************************************************************
*File: business.js
*Contents: Business Class.
**All rights reserved.
**************************************************************************************/


function createBusinessClass(baseURL,phpDir, fallBack,ratingsTab,businessType,boards,lng,markerIcon) {

   var business = function (xmlNode) {
      this.code = parseInt(xmlNode.getElementsByTagName("code")[0].firstChild.nodeValue);
      this.name = xmlNode.getElementsByTagName("name")[0].firstChild.nodeValue;
      this.address = xmlNode.getElementsByTagName("address")[0].firstChild.nodeValue;
      this.phone = getOptionalProperty(xmlNode,"phone");
      this.fax = getOptionalProperty(xmlNode,"fax");
      this.email = getOptionalProperty(xmlNode,"email");
      this.comments = getOptionalProperty(xmlNode,"comments");
      this.site = getOptionalProperty(xmlNode,"site");
      this.photo = getOptionalProperty(xmlNode,"photo");
      this.schedule = new Schedule(xmlNode);

      if(boards != null) {
         this.reviews = new Array();
         for(var i in boards) {
            this.reviews.push(getThreads(xmlNode,boards[i].tag));
         }
      }
      else
         this.reviews = null;

      this.upper = new Marker(xmlNode);
   
   };

   business.prototype.baseURL = baseURL;
   business.prototype.phpDir = phpDir;
   business.prototype.fallBack = fallBack;
   business.prototype.ratingsTab = ratingsTab;
   business.prototype.businessType = businessType;
   business.prototype.boards = boards;
   business.prototype.lng = lng;
   business.prototype.markerIcon = markerIcon;

   business.prototype.isReviewed = businessIsReviewed;
   business.prototype.hasKeyword = hasKeyword;
   business.prototype.hasKeywords = hasKeywords;
   business.prototype.getName = businessGetName;
   business.prototype.getAddress = businessGetAddress;
   business.prototype.toHtml = businessToHtml;
   business.prototype.contactToHtml = contactToHtml;
   business.prototype.scheduleToHtml = scheduleToHtml;
   business.prototype.ratingToHtml = ratingToHtml;
   business.prototype.photoToHtml = photoToHtml;
   business.prototype.createMarker = createMarker;
   business.prototype.show = showMarker;
   business.prototype.hide = hideMarker;
   business.prototype.toggle = toggleMarker;
   business.prototype.goTo = goToMarker;

   return business;

}

function businessGetName() {
   return this.name;
}

 function businessGetAddress() {
	return this.address;
}


function getChildName() {
   return this.upper.getName();
}

function getChildAddress() {
	return this.upper.getAddress();
}


function businessIsReviewed() {
   if(this.reviews != null) {
      for(i in this.reviews) {
         if(this.reviews[i].length != 0)
            return true;
      }
      return false;
   }
   return false;
}

function hasKeyword(word) {
   
   if(!this.keywords)
      return (this.name.toLowerCase().indexOf(word.toLowerCase()) != -1);

   if(this.name.toLowerCase().indexOf(word.toLowerCase()) != -1)
      return true;

   var i = 0;
   var l = this.keywords.length
   while (i < l) { 
      if(this.keywords[i].indexOf(word.toLowerCase())) 
         return true;
      i++;
   }
   return false;
}

function hasKeywords(words) {
   var i = 0;
   var l = words.length;
   while(i < l) {
      if(this.hasKeyword(words[i]))
         return true;
      i++;
   }

   return false;   
}

function businessToHtml() {
   var tabs = new Array();

   tabs.push(this.contactToHtml());

   if(this.schedule.entriesCount()!=0) {
      tabs.push(this.scheduleToHtml());
   } 

   if(this.photo != null)
      tabs.push(this.photoToHtml());

   tabs.push(this.ratingToHtml());
   tabs.push(drivingDirectionsForm(this.lng, this.name, this.address));

   return tabs;
}


function contactToHtml() {
   var str = new String();

   str = "<div style=\"white-space:nowrap;\"><div class=\"club_name\">" + this.name + "</div><br/>"; 

   if(this.lng == 'eng')
      str += "<strong>Address: </strong>" + this.address + "<br/>"; 
   else if(this.lng == 'fr')
     str += "<strong>Adresse : </strong>" + this.address + "<br/>"; 

   if(this.phone != null) {
      if(this.lng == 'eng')
         str += "<strong>Phone Number: </strong>" +  this.phone + "<br/>";
      else if(this.lng == 'fr')
        str += "<strong>T&#233;l&#233;phone : </strong>" +  this.phone + "<br/>"; 
   }

   if(this.fax != null) 
        str += "<strong>Fax: </strong>" +  this.fax + "<br/>"; 

   if(this.email != null) 
        str += "<strong>E-mail : </strong><a href=\"mailto:" +  this.email + "\">" + this.email + "</a><br/>"; 
      
   if(this.site != null) {
      if(this.lng == 'eng')
         str += "<strong>Web site: </strong><a target=\"_blank\" href=\"http://" + this.site + "\">" + this.name + "</a><br/>";
      else if(this.lng == 'fr')
         str += "<strong>Site internet : </strong><a target=\"_blank\" href=\"http://" + this.site + "\">" + this.name + "</a><br/>";
   }

   if(this.comments != null) {
      if(this.lng == 'eng')
         str +=  "<strong>Comments: </strong>" + this.comments + "<br/>";
      else if(this.lng == 'fr')
         str +=  "<strong>Commentaires : </strong>" + this.comments + "<br/>";
  }
   str = str + "</div>";

  return new GInfoWindowTab("Contact", str);
}

function scheduleToHtml() {
   var str = "<div style=\"white-space:nowrap;\">";
   str += this.schedule.toHtml(this.lng);
   str += "</div>";
   
   if(this.lng == 'fr')
      return new GInfoWindowTab("Horaire", str);
   else
      return new GInfoWindowTab("Hours", str);

}

function ratingToHtml() {

   var str = "<div style=\"white-space:nowrap;\">";
   
   if(this.lng == 'eng')
      sh_str = 'Latest posts on ';
   else
     sh_str = 'Derniers messages sur ';

   if(this.boards != null) {
      for(var i = 0; i < this.boards.length;i++) {
         if(this.boards[i].hasSearch()) {
            str += '<a href="'+ this.boards[i].url  + this.boards[i].sh_script +  this.name + '" target="_blank">' + sh_str + this.boards[i].name + '</a><br />'
         }
      }
   }

   if(this.isReviewed()) {
         if(this.lng == 'eng')
            str += "<strong>Selected Reviews: </strong>";
         else if(this.lng == 'fr')
            str += "<strong>Revues s&#233;lectionn&#233;e : </strong>";

         for(var i = 0; i < this.boards.length; i++) {
            if(this.reviews[i].length != 0) {
               str += "<strong><font color=\"blue\">" + this.boards[i].name + " </font></strong>(";
               for(var j = 0; j < this.reviews[i].length;j++) {
                  var threadNumber = j + 1;
                  str += " <a target=\"_blank\" href=\"" + this.boards[i].url + this.boards[i].th_script + this.reviews[i][j] + "\">r" + threadNumber + "</a> ";
               }
               
               str += ") ";
            }
         }
         str += "<br />";
   }


   getRating(this.baseURL, this.phpDir, this.ratingsTab, this.code,this.businessType,this.lng);
   
   str += "<span id=\"rating\"><br/></span>";
   str += "</div>";

   if(this.lng == 'fr')
      return new GInfoWindowTab("Revues",str);
   else
     return new GInfoWindowTab("Reviews", str); 

}


function getThreads(xmlNode,board) {
   var threads = new Array();
   var elems = xmlNode.getElementsByTagName(board);
   //GLog.write(board + ": " + elems.length + " threads");
   if(elems.length == 0)
      return threads;
   for(var i = 0; i < elems.length; i++) {
      threads.push(elems[i].firstChild.nodeValue);
   }

   return threads;
}

function photoToHtml() {
   var str = "<div style=\"white-space:nowrap;\">";
   str += "<img align=\"middle\" height=\"100\" width=\"100\" src=\"" + this.upper.photosPath + this.photo + "\" />"; 
   str += "</div>";

   return new GInfoWindowTab("Photo",str);
}

function drivingDirectionsForm(lng,business_name,business_addr) {

   if(lng == 'eng') {
      title = 'Directions';
      header = 'Get Driving Directions to: ';
      start_address = 'Start Address: ';
      button_label = 'Get Directions';
      language = 'en';
   }
   else {
      title = 'Itinéraire';
      header = 'Itinéraire vers : ';
      start_address = 'Lieu de départ : ';
      button_label = 'Soumettre';
      language = 'fr';
   }
      
   str = '<br/><div align="center"><strong>' + header + business_name + '</strong></div><br/>';
   str += '<form action="http://maps.google.com/maps" target="_blank">';
   str += '<input type="hidden" name="daddr" value="'+ business_addr + '" />';
   str += '<input type="hidden" name="f" value="d" />';
   str += '<input type="hidden" name="hl" value="'+ language + '" />';

   str += '<table>';
   str += '<tr><td><strong>' + start_address + '</strong></td>';
   str += '<td><input type="text" size="30" maxlength="255" name="saddr" value="" /></td></tr></table>';
   str += '<br /><div align="center"><button type="SUBMIT" value="Get Directions">'+ button_label + '</button></div>';
   
   str += '</form>';

   return new GInfoWindowTab(title, str);
}

//Wrappers for derived classes

function createChildMarker() {
   var position = this.upper.upper.position;
   var markerIcon = this.upper.markerIcon;
   var icon = null;

   if(position == null)
      position = this.upper.fallBack;

   if(markerIcon != null)
      icon = createMarkerIcon(markerIcon);

   this.upper.upper.marker  = new GMarker(position,{icon:icon,title:this.upper.name});	
   var this_business = this;		
   GEvent.addListener(this.upper.upper.marker, "click", function() {
	               if(this_business.upper.upper.info_shown) {
	                  this_business.hide();
	               }
	               else {
		          this_business.upper.upper.marker.openInfoWindowTabsHtml(this_business.toHtml());
		       }
		     });
					
   GEvent.addListener(this.upper.upper.marker, "infowindowopen", function() {this_business.upper.upper.info_shown = true;});					
   GEvent.addListener(this.upper.upper.marker, "infowindowclose", function() {this_business.upper.upper.info_shown = false;});
}

function showChild() {
    this.upper.show();
 }

function hideChild() {
    this.upper.hide();
 }

function toggleChild() {
    this.upper.toggle();
 }

function goToChild() {
   var position = this.upper.upper.position;
   if(position == null)
      position = this.upper.fallBack;

   this.upper.upper.map.panTo(position);
   this.show();
   this.upper.upper.marker.openInfoWindowTabsHtml(this.toHtml()); 
}

function childHasKeyword(word) {
   
   if(!this.upper.keywords)
      return (this.upper.name.toLowerCase().indexOf(word.toLowerCase()) != -1);

   if(this.upper.name.toLowerCase().indexOf(word.toLowerCase()) != -1)
      return true;

   var i = 0;
   var l = this.upper.keywords.length
   while (i < l) { 
      if(this.upper.keywords[i].indexOf(word.toLowerCase())) 
         return true;
      i++;
   }
   return false;
}




