﻿// This toggles an images source based on a predefined suffix to use as a constant
// and a reference to an image

// The suffix constant
var HOVER_SUFFIX = '_over';

function toggleImageOn(imageToToggle) {
    // Find the position of the last period which is where the extension begins
    var extensionPosition = imageToToggle.src.lastIndexOf('.');

    // Save the extension to a var
    var extension = imageToToggle.src.substring(extensionPosition, imageToToggle.src.length);

    // Grab the file name without the extension
    var fileName = imageToToggle.src.substring(0, extensionPosition);

    // Image source with hover suffix added
    imageToToggle.src = fileName + HOVER_SUFFIX + extension;
}

function toggleImageOff(imageToToggle) {
    // Find the position of the last period which is where the extension begins
    var extensionPosition = imageToToggle.src.lastIndexOf('.');

    // Save the extension to a var
    var extension = imageToToggle.src.substring(extensionPosition, imageToToggle.src.length);

    // Grab the file name without the extension
    var fileName = imageToToggle.src.substring(0, extensionPosition);

    // Apply the image source without hover suffix as long as that suffix is there
    if (imageToToggle.src.lastIndexOf(HOVER_SUFFIX) != -1) {
        imageToToggle.src = fileName.substring(0, fileName.length - HOVER_SUFFIX.length) + extension;
    }
}

// This allows a containing element of an image to
// be call the toggle image with just a reference to this
// if the image is the first child.
function toggleChildImageOn(whosChildImage) {
    toggleImageOn(whosChildImage.firstChild);
}

function toggleChildImageOff(whosChildImage) {
    toggleImageOff(whosChildImage.firstChild);
}

// This just contains a function that will hide and show the
// solutions divs.  I included this in its own JS file so that
// the browser can catch it since it will be used across multiple pages

function toggleDiv(which, contentid, expanded, collapsed) {
    var hiddenText = 'Expand to learn more';
    var visibleText = 'Collapse';
    var expandImage = collapsed;
    var collapseImage = expanded;

    if(expanded==collapsed)
    {
        //Don't swap images
        if ($('#' + which + '_txt').html() == hiddenText) {
            // Show div and change text & image
            $('#' + which + '_sub').show('slow'); // If don't want animation remove 'slow'
            $('#' + which + '_txt').html(visibleText);
            document.getElementById('ContentTrack').src = "TrackContent.aspx?CID=" + contentid;

        } else {
            // Hide div and change text & image
            $('#' + which + '_sub').hide('slow'); // If don't want animation remove 'slow'
            $('#' + which + '_txt').html(hiddenText);
        }
    } 
    else
    {
        if ($('#' + which + '_txt').html() == hiddenText) {
            // Show div and change text & image
            $('#' + which + '_sub').show('slow'); // If don't want animation remove 'slow'
            $('#' + which + '_txt').html(visibleText);
            document.images[which].src = collapseImage;
            document.getElementById('ContentTrack').src = "TrackContent.aspx?CID=" + contentid;

        } else {
            // Hide div and change text & image
            $('#' + which + '_sub').hide('slow'); // If don't want animation remove 'slow'
            $('#' + which + '_txt').html(hiddenText);
            document.images[which].src = expandImage;
        }
    }
}

function toggleDiv(which, contentid, text, expanded, collapsed) {
    var hiddenText = text;
    var visibleText = 'Collapse';
    var expandImage = collapsed;
    var collapseImage = expanded;

    if(expanded==collapsed)
    {
        if ($('#' + which + '_txt').html() == hiddenText) {
            // Show div and change text & image
            $('#' + which + '_sub').show('slow'); // If don't want animation remove 'slow'
            $('#' + which + '_txt').html(visibleText);
            document.getElementById('ContentTrack').src = "TrackContent.aspx?CID=" + contentid;

        } else {
            // Hide div and change text & image
            $('#' + which + '_sub').hide('slow'); // If don't want animation remove 'slow'
            $('#' + which + '_txt').html(hiddenText);
        }
    }
    else
    {
        if ($('#' + which + '_txt').html() == hiddenText) {
            // Show div and change text & image
            $('#' + which + '_sub').show('slow'); // If don't want animation remove 'slow'
            $('#' + which + '_txt').html(visibleText);
            document.images[which].src = collapseImage;
            document.getElementById('ContentTrack').src = "TrackContent.aspx?CID=" + contentid;

        } else {
            // Hide div and change text & image
            $('#' + which + '_sub').hide('slow'); // If don't want animation remove 'slow'
            $('#' + which + '_txt').html(hiddenText);
            document.images[which].src = expandImage;
        }
    }
}

function toggleForm()
{
    //alert('form toggle');        
    $('.hidden').show('slow', function()
    {
        $('input[type=text], textarea').val('');
        $('.show').hide('slow');
    });
    // update the hidden postback field to indicate to the server to treat this as an optin
    $('#OPTIN').val('true');
    
}

/*******************************************************************************************/
/* MATH FUNCTIONS
/*******************************************************************************************/
function formatCurrency(num, includeCents) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
if(includeCents)
	return (((sign)?'':'-') + '$' + num + '.' + cents);
else
	return (((sign)?'':'-') + '$' + num);
}

/*******************************************************************************************/
/* END MATH FUNCTIONS
/*******************************************************************************************/

// JavaScript Document

<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->

/*******************************************************************************************/
/* START IMAGE ROLLOVERS AND PRELOADING
/*******************************************************************************************/

//jQuery Image Preloading
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

// Use like: $.preLoadImages("image1.gif", "/path/to/image2.png");

//jQuery Rollover - Add Rollover as class
$(function() {
  $(".Rollover").hover(function() {
    $(this).attr("src", $(this).attr("src").split(".").join("_roll."));
  }, function() {
    $(this).attr("src", $(this).attr("src").split("_roll.").join("."));
  });
});

/*******************************************************************************************/
/* END IMAGE ROLLOVERS AND PRELOADING
/*******************************************************************************************/

