﻿var SEARCH_URL = "";
var SITE_URL = "";
$(document).ready(function() {
	// remove ad on search results page
	var pathname = window.location.pathname;
	var pagename = pathname.substring(pathname.lastIndexOf('/'), pathname.length);
	

   // get the current Site, Page, and Search Scope
   //var thisSite = $().SPServices.SPGetCurrentSite();
   //thisSite = thisSite.substring(thisSite.indexOf(window.location.host) + window.location.host.length)
   //SITE_URL = thisSite.substring(0, thisSite.lastIndexOf("/"));
   // using logo's URL to avoid Webs.asmx permission issues.
   SITE_URL = $("div#logo a:eq(0)").attr("href"); 

   var thisPage = document.location.pathname;
   thisPage = thisPage.substring(thisPage.lastIndexOf("/")+1);
   var thisScope = SITE_URL.substring(12); //new String("/Physicians/").length
   if(thisScope.indexOf("/") >= 0) { thisScope = thisScope.substring(0, thisScope.indexOf("/")); }
   SITE_URL = "/Physicians/" + thisScope;
	SEARCH_URL = SITE_URL + "/Pages/SearchResults.aspx?s=" + thisScope + "&k=";

   var query = "";

   $("#searchBtn").click(function () {
      searchSite();
      return (false)
   });
   
   $("#searchField").keypress(function (e) {
      if (e.keyCode == 13) {
         searchSite();
         return (false)
      }
   }).val(getParameterByName("k"));
      
   $("#InputKeywords").keypress(function (e) {
      if (e.keyCode == 13) {
         searchSite();
         return (false)
      }
   }).val(getParameterByName("k"));

   // get AVID Site Banners
   //query = "<Query><Where><Eq><FieldRef Name='SubSitePath'/><Value Type='Text'>" + thisSite + "</Value></Eq></Where></Query>";
   query = "<Query><Where><Contains><FieldRef Name='SubSitePath' /><Value Type='Text'>" + thisPage + "</Value></Contains></Where></Query>";

   $().SPServices({
      webURL: SITE_URL,
      operation: "GetListItems",
      listName: "AVID Site Banners",
      async: false,
      CAMLQuery: query,
      completefunc: processBanner
   });

   // get AVID Related Links (if the div is present)...
   if($("#divlinks").length > 0) {
      //query = "<Query><Where><Eq><FieldRef Name='SubSitePath'/><Value Type='Text'>" + thisSite + "</Value></Eq></Where></Query>";
      query = "<Query><Where><Contains><FieldRef Name='SubSiteUrl'/><Value Type='Text'>" + thisPage + "</Value></Contains></Where></Query>";

      $().SPServices({
         webURL: SITE_URL,
         operation: "GetListItems",
         listName: "AVID Related Links",
         async: true,
         CAMLQuery: query,
         completefunc: processRelatedLinks
      });
   }
});

function searchSite(){
   var $searchField = $("#InputKeywords");
   var searchText = new String($searchField.val());

   if(searchText.length > 0) { window.location = SEARCH_URL + searchText; }
   else { $searchField.css('border', 'solid 1px red').focus(); }
}

function changeFontSize(newFontSize){ 
// 2010.12.12 MAL - using jQuery selectors and chained function calls.
   var $content = $(".pageContent, #subContent");
   if($content){
      if(newFontSize == 1) {
         $content.css("cssText", "font-size:1.0em; line-height:1.1em;")
            .find("a").css("cssText", "font-size:1em; line-height:1.1em;").end()
            .find("h1").css("cssText", "font-size:1.5em; line-height:1.6em; font-weight:bold;").end()
            .find("h2").css("cssText", "font-size:1.4em; line-height:1.5em;").end()
            .find("h3").css("cssText", "font-size:1.3em; line-height:1.4em;").end()
            .find("h4").css("cssText", "font-size:1.2em; line-height:1.3em;").end()
            .find(".event_detail_header").css("cssText", "font-size:1.0em; line-height:1.1em; font-weight:bold;").end()
      } else {
         $content.css("cssText", "font-size:" + newFontSize + "em; line-height:" + (newFontSize + .1) +"em")
            .find("a").css("cssText", "font-size:" + newFontSize + "em; line-height:" + (newFontSize + .1) +"em;").end()
            .find("h1").css("cssText", "font-size:" + (newFontSize + .5) + "em;  line-height:" + (newFontSize + .4) +"em; font-weight:bold;").end()
            .find("h2").css("cssText", "font-size:" + (newFontSize + .4) + "em;  line-height:" + (newFontSize + .3) +"em;").end()
            .find("h2").css("cssText", "font-size:" + (newFontSize + .3) + "em;  line-height:" + (newFontSize + .2) +"em;").end()
            .find("h2").css("cssText", "font-size:" + (newFontSize + .2) + "em;  line-height:" + (newFontSize + .1) +"em;").end()
            .find(".event_detail_header").css("cssText", "font-size:" + (newFontSize) + "em;  line-height:" + (newFontSize) +"em; font-weight:bold;")
      }
   }
}

// related links list name result returned here
function processRelatedLinks(xData, status) {
   var GroupName = "";
   $("[nodeName=rs:data]", xData.responseXML).find("[nodeName=z:row]").each(function(){
      GroupName = $(this).attr("ows_Title");
   });

   if (GroupName.length > 0) {
      var query = "<Query><Where><Eq><FieldRef Name='AVIDRelatedLinks'/><Value Type='Lookup'>" + GroupName + "</Value></Eq></Where><OrderBy><FieldRef Name='Weight' Ascending='True' /></OrderBy></Query>";

      $().SPServices({
         webURL: SITE_URL,
         operation: "GetListItems",
         listName: "AVID Related Links Items",
         async: true,
         CAMLQuery: query,
         completefunc: processRelatedLinksItems
      });
   }
}

// results from the related links query are returned here
function processRelatedLinksItems(xData, status) {
   var relatedLinks = "";
   // need to use [nodeName] selector for max browser compatability.
   var $rows = $("[nodeName=rs:data]", xData.responseXML).find("[nodeName=z:row]");

 	// make sure we have set at least one related link.
   if($rows.length > 0) {
      relatedLinks += "<div id=\"relatedDocsTop\" alt=\"Related Information\" title=\"Related Information\"></div>";
      relatedLinks += "<div id=\"relatedDocs\">";
      $rows.each(function(index){
         if(index > 0) { relatedLinks += "<div class=\"relatedLine\"></div>"; }      
         var url = $(this).attr("ows_NavigateUrl").split(",")[0];
         var liHtml = "<div class=\"related\"><a href=\"" + url + "\" title=\"" + $(this).attr("ows_Title") + "\">" + $(this).attr("ows_Title") + "</a></div>";
         relatedLinks += liHtml;
      });
      relatedLinks += "</div>";
      relatedLinks += "<div id=\"relatedDocBottom\"></div>";
      $("#divlinks").append(relatedLinks); 
   }
}

// results from the related links query are returned here
function processNav(xData, status) {
   var loc = $(location).attr("href").toLowerCase();
   var html = "";
   // need to use [nodeName] selector for max browser compatability.
   $("[nodeName=rs:data]", xData.responseXML).find("[nodeName=z:row]").each(function(index){
      var url = $(this).attr("ows_URL");
      var iComma = url.indexOf(", ");
      var target = url.slice(0, iComma).toLowerCase();
      var title = url.substring(iComma+2);
      var desc = $(this).attr("ows_Comments");
      if(!desc) { desc = title; } 

      html += "<a class=\"";
      if(loc.indexOf(target) >= 0) { html += "navBtnsOn\" "; }
      else { html += "navBtns\" "; }
      html += "href=\"" + target + "\" title=\"" + desc + "\"><span>" + title + "</span></a>";
   });
   $("#navigation").html(html);
}


// results from banner query returned here
function processBanner(xData, status) {
   var bannerUrl = 
      $("[nodeName=rs:data]", xData.responseXML)
         .find("[nodeName=z:row]")
         .eq(0) //get the first one by default
         .attr("ows_ImageUrl");
   if(bannerUrl && bannerUrl.length > 0) {
      bannerUrl = bannerUrl.split(",")[0]; // parse the ImageUrl format (comes as "URL, Title")
      $("#mainBanner").css("background-image", "url('" + bannerUrl + "')");
   }
}

// parse Querystring name/value
function getParameterByName( name ){
   name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
   var regexS = "[\\?&]"+name+"=([^&#]*)";
   var regex = new RegExp( regexS );
   var results = regex.exec( window.location.href );
   if( results == null )
      return "";
   else
      return decodeURIComponent(results[1].replace(/\+/g, " "));
}
