﻿//************************************************************************************* 
// File     :   mfsw_core_0.1.js
// Requires :   mf_domLibrary.0.1.js
// Author   :   Rusty Swayne (rss)
// Origin   :   mindfly.com
// Purpose  :   General functions executed on pages to manipulate site styles and events          
//*************************************************************************************


	
//  Purpose :   Queues functions to be executed when docoment loads
//  Author  :   Simon Willison - http://simon.incutio.com/archive/2004/05/26/addLoadEvent
//  Origin  :   adactio.com
//  Modified:   2/14/2007
function addLoadEvent(func) {
	
	var oldonload = window.onload;

	if (typeof window.onload != 'function') {

		window.onload = func;

	} else {

		window.onload = function() {

			oldonload();

			func();

		}
	}
}

//  Purpose :   Queues function to be executed when document loads
//  Author  :   Rusty Swayne
//  Origin  :   mindfly.com
function addOnresizeEvent(func) {

    var oldonresize = window.onresize;
    
    if(typeof window.onresize != 'function') {
    
        window.onresize = func;
    
    } else {
    
        window.onresize = function() {
        
            oldresize();
            
            func();
            
        }
    }
}


// Purpose  :   add event handlers to already existing elements
//              Replaces Scott Andrew's function to fix ie. "this" bug
// Author   :   John Resig
// Origin   :   http://ejohn.org/projects/flexible-javascript-events/
// Modified :   8/22/2007
function addEvent( obj, type, fn ) { 
  if ( obj.attachEvent ) { 
    obj['e'+type+fn] = fn; 
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
    obj.attachEvent( 'on'+type, obj[type+fn] ); 
  } else 
    obj.addEventListener( type, fn, false ); 
} 
function removeEvent( obj, type, fn ) { 
  if ( obj.detachEvent ) { 
    obj.detachEvent( 'on'+type, obj[type+fn] ); 
    obj[type+fn] = null; 
  } else 
    obj.removeEventListener( type, fn, false ); 
}


//  Purpose :   Adds a css class to a dom element
//  Author  :   Jeremy Keith
//  Origin  :   adactio.com
//  Modified:   2/14/2007
function addClass(element,value) {

	if (!element.className) {
	
		element.className = value;
	
	} else {
	
		newClassName = element.className;
	
		newClassName+= " ";
		
		newClassName+= value;
		
		element.className = newClassName;
		
	}
}


//  Purpose :   Finds all elements of tagName passed with given className
//  Author  :   Rusty Swayne
//  Origin  :   mindfly.com
//  Modified:   2/14/2007
function getElementsByTagNameAndClass(element, tagName, className) {
    
    var resultsArray = new Array();
    
    var tags = element.getElementsByTagName(tagName);
    
    for(var i = 0; i < tags.length; i++) {
        if(tags[i].className && tags[i].className == className) {
            resultsArray.push(tags[i]);
        }
    }
    
    return resultsArray;
    
} 


// Purpose :	 Returns the browser window dimmentions
// Author : James Edwards and Cameron Adams ("The Javascript Anthology") 
// Origin : http://ecmascript.stchur.com/2006/09/06/the_ie_resize_bug_revisited/
// Modified : 3/13/2007
function getViewportSize()	{

    var size = [0, 0];
    if (typeof window.innerWidth != 'undefined')
    {
     size = [ window.innerWidth, window.innerHeight ];
    }
    else if (typeof document.documentElement != 'undefined' &&
             typeof document.documentElement.clientWidth != 'undefined' &&
             document.documentElement.clientWidth != 0)
    {
     size = [ document.documentElement.clientWidth, document.documentElement.clientHeight ];
    }
    else
    {
     size = [ document.getElementsByTagName('body')[0].clientWidth,
              document.getElementsByTagName('body')[0].clientHeight ];
    }
    
    return size;    
	
}
