/******
  ALL Javascript code should be included in this JS file.
  Functions should be grouped together by original source, and calls to those functions
  added to the end of this file.

  CURRENTLY INCLUDED:
    Google Analytics (unobtrusive version)
    SIFR & siff-config
    Tideway Page Behaviour

  GO TO END OF FILE FOR IMMEDIATE / ONLOAD CALLS
******/

var scriptserver = document.domain;


/***A more elegant implementation of google analytics - by Al Stevens 20 July 2008***/
/* Simplifies onload, you will no longer have to add an onload event call just call addLoadEvent */

function addLoadEvent(func,arg){
  if (!arg){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = func;
    } else {
      window.onload = function() {
        oldonload();
        func();
      }
    }
  } else {  
    /*if the onload event has an argument/parameter cater for that*/
    if (arg){
      oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func(arg); 
      }
      window.onload = function() {
        oldonload();
        func(arg); 
      }
    }
  }
}

/*Loads GA and schedules future GA calls(asynchronous)*/
function loadGAScript(){
  if (scriptserver == 'www.tideway.com' || scriptserver == 'webuat.tideway.com' ||
      scriptserver == 'discovery.bmc.com' || scriptserver == 'uat.discovery.bmc.com' ) {
    /* Check browser for Dom compatibility */
    if (!document.getElementsByTagName) return false;

    window._gaq = window._gaq || [];
    window._gaq.push(callGA);

    /*Determines whether the page is using a secure or unsecure protocol*/
    var gaJsHost = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www');

    /*Writes in the script to the document head*/
    var ga = document.createElement('script');
    ga.src = gaJsHost +'.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  }
}


/*Calls the analytics function*/
function callGA(){
  if (scriptserver == 'www.tideway.com') {
    window.pageTracker = _gat._getTracker("UA-153939-1");
    window.pageTracker._setDomainName(".tideway.com");
    if(typeof getCommunityUserStatus == "function")
	    window.pageTracker._setCustomVar(1,"UserType",getCommunityUserStatus(),1);
    window.pageTracker._trackPageview();
  }
  else if (scriptserver == 'discovery.bmc.com') {
    window.pageTracker = _gat._getTracker("UA-153939-1");
    if(typeof getCommunityUserStatus == "function")
	     window.pageTracker._setCustomVar(1,"UserType",getCommunityUserStatus(),1);
    window.pageTracker._trackPageview();
  }
  else if (scriptserver == 'webuat.tideway.com') {
    window.pageTracker = _gat._getTracker("UA-153939-4");
    if(typeof getCommunityUserStatus == "function")
	    window.pageTracker._setCustomVar(1,"UserType",getCommunityUserStatus(),1);
    window.pageTracker._trackPageview();
  }
  else if (scriptserver == 'uat.discovery.bmc.com') {
    window.pageTracker = _gat._getTracker("UA-153939-4");
    if(typeof getCommunityUserStatus == "function")
	     window.pageTracker._setCustomVar(1,"UserType",getCommunityUserStatus(),1);
    window.pageTracker._trackPageview();
  }
}



/******
  TIDEWAY PAGE BEHAVIOUR
******/

function swapBlocks (current, requested) {
  $(current).fadeOut(300, function() {
    $(requested).fadeIn(300);
  });
}

function openTab (parent, tabcontentid, tabid) {
    var o = $(parent + " .visibleblock");
    if(o[0].id == $(tabcontentid)[0].id) return;
    $(tabcontentid).css('opacity', 0).addClass('visibleblock');
    o.removeClass('visibleblock');
    $(parent + " li.current").removeClass('current');
    $(parent + " " + tabid).addClass('current');
    $(tabcontentid).fadeTo(600, 1, function() {
      $(tabcontentid).css('opacity','');
    });
    try{
    if (scriptserver == 'www.tideway.com' || scriptserver == 'webuat.tideway.com' ||
        scriptserver == 'discovery.bmc.com' || scriptserver == 'uat.discovery.bmc.com'){
      pageTracker._trackEvent('Tabs:'+window.location.pathname, 'Click', tabid);
    }
    }catch(e){};
}

var qStringPairs = new Array();

function getQueryString() {
  var query = window.location.search.substring(1);
  var parms = query.split('&');
  for (var i=0; i<parms.length; i++) {
    var pos = parms[i].indexOf('=');
    if (pos > 0) {
      var key = parms[i].substring(0,pos);
      var val = parms[i].substring(pos+1);
      qStringPairs[key] = val;
    }
  }
}

function setOpenTab() {
  qStringPairs['twset'] = null;
  qStringPairs['twtab'] = null;
  qStringPairs['twblock'] = null;
  getQueryString();
  // alert("#" + qStringPairs['twset'] + ", #"+qStringPairs['twblock'] + ", #"+qStringPairs['twtab']);
  if (qStringPairs['twset'] && qStringPairs['twblock'] && qStringPairs['twtab']) {
    openTab("#"+qStringPairs['twset'], "#"+qStringPairs['twblock'], "#"+qStringPairs['twtab']);
    if (scriptserver == 'www.tideway.com' || scriptserver == 'webuat.tideway.com' ||
        scriptserver == 'discovery.bmc.com' || scriptserver == 'uat.discovery.bmc.com'){
      pageTracker._trackEvent('Tabs:'+window.location.pathname, 'startTabOpen', qStringPairs['twtab']);
    }
  }
}

/******
  Immediate script calls
******/

loadGAScript();

/******
  OnLoad Script Calls
******/

addLoadEvent(setOpenTab);


