﻿///////////////////////////////////////////////////////////////////////////////
//
//  wcoDefault.js
//
// © 2007-2010 Wco iEnterprise Solutions. All Rights Reserved.
// This file is licensed as part of the DataPortal 3.0 Managed Web Presence Solution, for details look here: http://www.wco.com.au
//
///////////////////////////////////////////////////////////////////////////////
var scriptCount = 0;
var scriptCounter = 0;
var imageCount = 0;
var imageCounter = 0;

function ready () {
  //window.resizeTo(1024, 768);   // old 15" & 17" monitor
  //window.resizeTo(1280, 1024);  // standard 17" & 19" monitor
  //window.resizeTo(1355, 768);   // widescreen 15" laptop monitor
  //window.resizeTo(1440, 900);   // widescreen 19" monitor
  //window.resizeTo(1600, 1200);  // standard 20" monitor
  //window.resizeTo(1680, 1050);  // widescreen 20" & 22" monitor
  //window.moveTo(0, 0);  window.resizeTo(1920, 1200);  // widescreen 24" & 27" monitor

  // initialise the communication link to the DataPortal
  //         init(<parent>, <portalGuid>, <websiteId>, <languageCultureName>)
  dataportal.init(this, '655BD726-8A3B-4C4C-89E1-427D8B44CEDD', 8, 'en-AU');

  // **** and ACTIVATE THE WEBSITE by calling the DataPortal Web service and retrieving the Home Page data
  dataportal.setCallback(createDelegate(this, this.set));
  dataportal.fetchCurrentObject(1, 8, 87699, 5, 'WebSite', 100, 8);
}

function set () {
  // now, retrieve the core website data
  // and initialise the website display by dynamically loading the required stylesheet and javascript modules and setting the base attributes
  var websiteData = arguments[0];
  var childCollection = arguments[1];

  // load the required modules
  switch (unescape(websiteData.Skin.replace(/&amp;/g, '&'))) {
    case 'Professional - Question & Answer':
      // initialise the pre-load counters
      //scriptCount = 5;
      scriptCount = 1;
      scriptCounter = 0;
      //imageCount = 21;
      imageCount = 0;
      imageCounter = 0;

      // load the desired stylesheet template
      $('link #skin').attr('href', 'css/cs2010web.css');

      // load the set of javascript modules required to run the website application
      go(websiteData, childCollection);
//      $.getScript('scripts/wcoSecurityManager.js', function() { go(websiteData, childCollection); });
//      $.getScript('scripts/wcoSlideShow.js', function() { go(websiteData, childCollection); });
//      $.getScript('scripts/wcoContentManager.js', function() { go(websiteData, childCollection); });
//      $.getScript('scripts/wcoNavigator.js', function() { go(websiteData, childCollection); });
//      $.getScript('scripts/wcoPageManager.js', function() { go(websiteData, childCollection); });
// During development, replace the dynamically loaded modules with these hard-coded lines in the Default.htm page
//    <script type="text/javascript" src="scripts/jquery-1.4.2.js"></script>
//    <script type="text/javascript" src="scripts/wcoPageManager-Custom.js"></script>
//    <script type="text/javascript" src="scripts/wcoSecurityManager.js"></script>
//    <script type="text/javascript" src="scripts/wcoSlideShow.js"></script>
//    <script type="text/javascript" src="scripts/wcoContentManager.js"></script>
//    <script type="text/javascript" src="scripts/wcoNavigator.js"></script>
//    <script type="text/javascript" src="scripts/wcoPageManager.js"></script>

      // and pre-load the collection of images used in the interface
//      var images = new Array();
//      images[0] = 'ssl/images/page_bkg.jpg';
//      images[1] = 'ssl/images/titlebar_header.jpg';
//      images[2] = 'ssl/images/Section1_Btn_out-en.jpg';
//      images[3] = 'ssl/images/Section2_Btn_out-en.jpg';
//      images[4] = 'ssl/images/Section3_Btn_out-en.jpg';
//      images[5] = 'ssl/images/Section4_Btn_out-en.jpg';
//      images[6] = 'ssl/images/Section5_Btn_out-en.jpg';
//      images[7] = 'ssl/images/Section6_Btn_out-en.jpg';
//      images[8] = 'ssl/images/sidebarSectionBkg.png';
//      images[9] = 'ssl/images/btn-open-hilite.png';
//      images[10] = 'ssl/images/btn-open-out.png';
//      images[11] = 'ssl/images/btn-open-over.png';
//      images[12] = 'ssl/images/btn-close-hilite.png';
//      images[13] = 'ssl/images/btn-close-out.png';
//      images[14] = 'ssl/images/btn-close-over.png';
//      images[15] = 'ssl/images/btn-exit-hilite.png';
//      images[16] = 'ssl/images/btn-exit-out.png';
//      images[17] = 'ssl/images/btn-exit-over.png';
//      images[18] = 'ssl/images/MapLinkBkg.png';
//      images[19] = 'ssl/images/LanguageBarBkg.png';
//      images[20] = 'ssl/images/lightbox-thumbnail-border.png';

//      $(images).each(function(index, imgsrc) {
//        //alert(index + ': ' + imgsrc);
//        var img = new Image();
//        $(img).load(function() {
//          imageCounter += 1;
//          if (imageCounter == imageCount) {
//            go(websiteData, childCollection);
//          }
//        }).error(function() {
//          imageCounter += 1;
//          if (imageCounter == imageCount) {
//            go(websiteData, childCollection);
//          }
//        }).attr('src', imgsrc);
//      });
      break;
    case 'Custom':
      // load the desired stylesheet template
      $('link #skin').attr('href', 'css/cs2010web-custom.css');

      var bodyCss = {
        'background-color':websiteData.BackgroundColour
      };
      $('body').css(bodyCss);

      scriptCount = 1;
      //go(websiteData, childCollection);
      //scriptCount = 1;
      //$.getScript('scripts/wcoContentManager.js', function() { go(websiteData, childCollection); });
      //$.getScript('scripts/wcoNavigator.js', function() { go(websiteData, childCollection); });
      $.getScript('scripts/wcoPageManager-Custom.js', function() { go(websiteData, childCollection); });
      break;
    default:
      alert('Sorry... the "' + unescape(websiteData.Skin.replace(/&amp;/g, '&')) + '" interface skin is not available!');
      break;
  }

}

function go () {
  if (scriptCounter < scriptCount) {
    scriptCounter += 1;
  }

  // check if all required modules have been loaded
  if (scriptCounter == scriptCount && imageCounter == imageCount) {
    var websiteData = arguments[0];
    var childCollection = arguments[1];

    // setup the page background  - "url('../ssl/images/page_bkg.jpg')"   - #74857D
    // websiteData.BackgroundImage = http://www.contraceptiveservices.com.au/ssl/images/page_bkg.jpg
//    var bodyCss = {
//      'background-color':websiteData.BackgroundColour,
//      'background-image':"url('" + websiteData.BackgroundImage + "')",
//      'background-position':'center top',
//      'background-repeat':'no-repeat',
//      'background-attachment':'fixed'
//    };
//    $('body').css(bodyCss);

    // instantiate a global working copy of the PageManager object
    pageManager = new HtmlPageManagerObject();
    // hook up the handler for the window resize event
    $(window).resize(createDelegate(pageManager, pageManager.onWindowResize));

    // and launch the website application by initialising the the PageManager
    pageManager.init(websiteData, childCollection);
  }

}

function preloadImages () {
  if (document.images) { //check for compatible browser
    var imgFiles = preloadImages.arguments; //get the array of images to be preloaded into memory
    if (document.preloadArray == null) { document.preloadArray = new Array(); } //create the document array to hold the images
    var i = document.preloadArray.length; //create the array counter

    //load the images into the document array
    with (document) {
      for (var j = 0; j < imgFiles.length; j++) {
        preloadArray[i] = new Image();
        preloadArray[i++].src = imgFiles[j];
      }
    }
  }
}

function createDelegate(instance, method) {
  return function () {
    return method.apply(instance, arguments);
  }
}


