//******************************************************************************
//******************************************************************************
// File: control_scripts.js                                                    *
// Last Updated: August 13, 2010                                               *
//******************************************************************************
// Purpose:
//  This file contains scripts associated with the Controller class.  In general,
// these are scripts common to every page on the Allendale Electronics web site.
//******************************************************************************
//******************************************************************************

//==============================================================================
// Controller()                                                                
//------------------------------------------------------------------------------
// This is the class declaration for Controller and its helper classes.
//==============================================================================

//______________________________
// C_Slideshow helper class.   *
//_____________________________*
function C_Slideshow() {
  // ***********************************
  // Private variables and functions.  *
  // ***********************************
  var alpha_ie = 100;
  var alpha = 1;
  var images = new Array();
  var current_image = 0;
  var preload_image = new Image();
  var running;
  var run_time;
  var target_id;
  var target;
  var is_explorer = false;
  
  //--------------------
  // priv_transition()
  //--------------------
  priv_transition = function() {
    priv_fade_out();
    var fade_in_call = setTimeout("priv_fade_in()", 3500)       
  }
  
  //--------------------
  // priv_fade_out
  //--------------------
  priv_fade_out = function() {
    var is_transparent = false;
    // Not IE..
    if (!is_explorer) {
      if (target.style.opacity <= 0) {
        is_transparent = true;
      } else {
        target.style.opacity = alpha;
        alpha -= 0.01;
      }
    } else if (is_explorer) {
      if (target.filters.item("DXImageTransform.Microsoft.Alpha").Opacity <= 0) {
        is_transparent = true;
      } else {
        target.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+alpha_ie+")";
        alpha_ie--; 
      } 
    }
  
    var interval;
    if (!is_transparent) {
      interval = setTimeout("priv_fade_out()", 30);
    } else if (is_transparent) {
      alpha = alpha_ie = 0;
    
      priv_next_image();
      target.src = images[current_image];
    
      clearTimeout(interval);
    }    
  }
  
  //--------------------
  //priv_fade_in()
  //--------------------
  priv_fade_in = function () {
    var is_opaque = false;
    // Other than IE..
    if (!is_explorer) {
      if (target.style.opacity >= 1) {
        is_opaque = true;
      } else {
        target.style.opacity = alpha;
        alpha += 0.01;
      }
    } else if (is_explorer){
      if (target.filters.item("DXImageTransform.Microsoft.Alpha").Opacity >= 100) {
        is_opaque = true;
      } else {
        target.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+alpha_ie+")";
        alpha_ie++;
      }
    } 
  
    var interval;
    if (is_opaque) {
      alpha = 1;
      alpha_ie = 100;
      clearTimeout(interval);
    } else {
      interval = setTimeout("priv_fade_in()", 30); 
    }  
  }
  
  //--------------------
  // priv_next_image()
  //--------------------
  priv_next_image = function() {
    if (current_image >= (images.length)-1) {
      current_image = 0;
    } else {
      current_image++;
    }
  }
  
  //**************************************
  // Privileged variables and functions. *
  //**************************************
  //--------------------
  // m_ss_debug
  //--------------------
  this.m_ss_debug = function() {
    var output = "C_SLIDESHOW DEBUG OUPUT\n"
                +"IE Alpha Level: "+alpha_ie+"\n"
                +"Other Alpha Level: "+alpha+"\n"
                +"Number of Images: "+images.length+"\n"
                +"Current Image: "+current_image+"\n"
                +"Is Explorer: "+is_explorer+"\n"
                +"Target ID: "+target_id+"\n";
    window.alert(output);  
  }
  
  //--------------------
  // m_init_show()
  //--------------------
  this.m_init_show = function(lvl, sources, duration, id, is_ie) {
    alpha_lvl_ie = lvl;
    alpha_lvl = (lvl/100);
    run_time = duration;
    target_id = id;
    is_explorer = is_ie;

    var i;
    for (i = 0; i < sources.length ; i++) {
      preload_image.src = sources[i];
      images.push(sources[i]);
    }  
  }
  
  //--------------------
  // m_startshow()
  //--------------------
  this.m_start_show = function() {
    target = document.getElementById(target_id);
    target.src = images[current_image]; 

    if (is_explorer) {
      target.style.filter = "progid:DXImageTransform.Microsoft.Alpha("
                                    +"opacity="+alpha_ie+")";
    } else {
      target.style.opacity = alpha;
    }    
  
    running = setInterval("priv_transition()", run_time);
  } 
  
}


//___________________________
// C_Browser helper class.  *
//__________________________*
function C_Browser() {
  // ***********************************
  // Private variables and functions.  *
  // ***********************************
  var is_explorer = false;
  var is_ie8 = false;
  var is_ie7 = false;
  var is_ie_old = false;
  var is_firefox = false;
  var is_chrome = false;
  var is_safari = false;
  var is_opera = false;
  var is_other = false;
  
  var has_flash = false;
  
  // *************************************
  // Privileged variables and functions. *
  // *************************************
  this.id = "undetected";

  //--------------------
  // m_browser_debug()
  //-------------------- 
  this.m_browser_debug = function() {
    var output = "C_BROWSER DEBUG OUTPUT\n"
                +"ID: "+id+"\n"
                +"is_explorer: "+is_explorer+"\n"
                +"is_ie8: "+is_ie8+"\n"
                +"is_ie7: "+is_ie7+"\n"
                +"is_ie_old: "+is_ie_old+"\n"
                +"is_firefox: "+is_firefox+"\n"
                +"is_chrome: "+is_chrome+"\n"
                +"is_safari: "+is_safari+"\n"
                +"is_opera: "+is_opera+"\n"
                +"is_other: "+is_other+"\n";
    window.alert(output);  
  }

  //--------------------
  // m_detect()
  //--------------------
  this.m_detect = function() {
    var appname = navigator.appName;
    var vendor = navigator.vendor;
  
    if (appname == "Microsoft Internet Explorer") {
      id = "explorer";
      is_ie8 = true;
      is_explorer = true;
    } else if (vendor == "Google Inc.") {
      id = "chrome";
      is_chrome = true;
    } else if (vendor == "Apple Computer, Inc.") {
      id = "safari";
      is_safari = true;
    } else if (appname == "Opera") {
      id = "opera";
      is_opera = true;
    } else {
      id = "other";
      is_other = true;
    }  
  }
  
  //--------------------
  // m_is_msie()
  //--------------------
  this.m_is_msie = function() {
    if (is_explorer) {
      return true;
    }
    else return false;
  }

  //--------------------
  // m_detect_flash()
  //--------------------
  this.m_detect_flash = function() {
	  var version;
	  var axo;
	  var e;

	  // NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	  try {
		  // version will be set for 7.X or greater players
		  axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		  version = axo.GetVariable("$version");
	  } catch (e) {
	  }
    
    if (version != null) {
      has_flash = true;   
    } else {
      has_flash = false;
    }
    
    if (navigator.mimeTypes["application/x-shockwave-flash"] ||
        navigator.plugins["Shockwave Flash"] ||
        navigator.plugins["Shockwave Flash 2.0"] ) {
          has_flash = true;;
        }         

  }
  
  //--------------------
  // m_has_flash()
  //--------------------
  this.m_has_flash = function() {
    if (has_flash) {
      return true;
    }
    else return false;
  }  
  
}


//++++++++++++++++++++++++++++++++++
// C_Controller control class.     +
//++++++++++++++++++++++++++++++++++
function C_Controller() {

  // *************************************
  // Privileged variables and functions. *
  // *************************************
  this.browser = new C_Browser();
  this.side_slide = new C_Slideshow();
  this.page_name = null;
  this.img_load_error = false; 
  
  //--------------------
  // m_display_banner()
  //--------------------
  this.m_display_banner = function() {
    if (this.browser.m_has_flash()) {
      document.getElementById("banner_img").style.display = "none";

      if (this.browser.m_is_msie()){
        document.write("<object id = 'banner_swf' classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000'"
                      +"codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,14,0'> \n");
        document.write("<param name = 'src' value = './flash/go1_banner.swf' /> \n");    
      } else {
        document.write("<object id='banner_swf' type='application/x-shockwave-flash' data='./flash/go1_banner.swf'> \n");   
      }
      document.write("<param name = 'quality' value = 'high' /> \n");
	    document.write("<param name = 'play' value = 'true' /> \n");
  	    document.write("<param name = 'loop' value = 'true' /> \n");
	    document.write("<param name = 'wmode' value = 'window' /> \n");
	    document.write("<param name = 'scale' value = 'showall' /> \n");
	    document.write("<param name = 'menu' value = 'false' /> \n");
	    document.write("<param name = 'devicefont' value = 'false' /> \n");
	    document.write("<param name = 'allowScriptAccess' value = 'sameDomain' /> \n");
      document.write("</object> \n");
      
    } else {
      document.getElementById("banner_img").style.display = "block";
    }
  }

  //--------------------
  // m_get_slides()
  //--------------------  
  this.m_get_slides = function(sources_arr) {
    var i;
    var path;
    for (i = 1; i <= 3; i++) {
      path = this.page_name + "/" + "image" + i + ".jpg";
      sources_arr[i-1] = "./ae_images/slide_shows/" + path;
    }    
  }
  
}

//==============================================================================
// End Controller()
//==============================================================================

//==============================================================================
// Main
//==============================================================================
//___________________________________
// General Utility Functions        *
//__________________________________*

//--------------------
// initialize()
//--------------------
function initialize() {
  control.side_slide.m_start_show();
}

//--------------------
// write_banner()
//--------------------
function write_banner() {
  control.m_display_banner();
}

//--------------------
// display_info()
//--------------------
function display_info() {
  var i;
  var output = "\n";
  for (i in navigator) {
    output += i+": "+navigator[i]+"\n";
  }
  
  document.getElementById("browser_output").value = output;
}


//--------------------
// setup_show()
//--------------------
function setup_show(name) {
  control.page_name = name;

  var src_arr = [];
  control.m_get_slides(src_arr);
  
  control.side_slide.m_init_show(100, src_arr, 12000, "unique_image", control.browser.m_is_msie());
}

//___________________________________
// End General Utility Functions        *
//__________________________________*

// Set up the controller.
var control = new C_Controller();
control.browser.m_detect();
control.browser.m_detect_flash();


// Init function and slide show are started once the page has loaded.
window.onload = initialize;


//==============================================================================
// End Main
//============================================================================== 
