/*
*********************************************************************
Language:       Javascript 1.5
Web Site:       http://www.mainememory.net/
Copyright:      &copy;2008 Jmaie Peloquin <http://www.jamiepeloquin.com>
Created by:     Jamie Peloquin
*********************************************************************
*******************************************************************
Javascript worksheet
*******************************************************************
*/
var MHS;
if (typeof MHS == "undefined") { MHS = {}; }

// << Pop Windows using REL
//    Use rel="popup" in anchor links to open link in a new window
//    Use rel="popup_mov" to get a movie-sized popup instead.
function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
	var anchor = anchors[i];
	if ( anchor.getAttribute("href") ) {
	    if ( anchor.getAttribute("rel") == "popup" ) {
		anchor.target = "_blank";
	    }
	    else if ( anchor.getAttribute("rel") == "popup_mov" ) {
		anchor.onclick = openMovieWindow;
	    }
	}
    }
}

function openMovieWindow( event ) {
    // Open the target in a small and limited-UI window.
    pop_generic(
		'fwee',
		event.target.getAttribute('href'),
		400,
		400,
		'no',
		'no',
		'no',
		'no',
		'no',
		'no'
		);
    return false;
}

window.onload = externalLinks;
// >>

/* Pop Window Generic - Centered --------------------------------- */
/* EXAMPLE
<a onclick="pop_generic('NewWin','http://www.axondm.com','800','600','no','no','no','yes','yes','yes');return false;" href="http://www.axondm.com" title="Open in a new window (Javascript)">http://www.axondm.com</a>
*/
function pop_generic(NAME,URL,WD,HT,TOOL,LOCATION,MENU,SCROLL,STATUS,RESIZE){
	if(!NAME){NAME = "genWin"}
	if(!WD){WD = "780"}
	if(!HT){HT = "500"}
	if(!TOOL){TOOL = "yes"}
	if(!LOCATION){LOCATION = "yes"}
	if(!MENU){MENU = "yes"}
	if(!SCROLL){SCROLL = "yes"}
	if(!STATUS){STATUS = "yes"}
	if(!RESIZE){RESIZE = "yes"}
	var halfW = (WD/2)
 	var halfH = (HT/2)
   	var screenW = screen.availWidth
    var screenH = screen.availHeight

    var screenX = ((screenW / 2) - halfW) // is half the width of the window
    var screenY = ((screenH / 2) - halfH) // is half the height of the window

	var WINNAME = NAME+'_win'
	if(document.layers){ //Fixes Netscape 4 bug
    	var NAME = window.open(URL, WINNAME);
	}
	else{
		var NAME = window.open(URL, WINNAME, 'width='+WD+', height='+HT+', top='+screenY+', left='+screenX+', toolbar='+TOOL+', location='+LOCATION+', menubar='+MENU+', scrollbars='+SCROLL+', status='+STATUS+', resizable='+RESIZE);
    }
    NAME.focus();
}//END

/* Reset Form Input Field */
function form_input_reset($ID) {
    $ID.value = '';
}

/* Submit Form --------------------------------------------------- */
function form_submit($FORM) {
	document.getElementById($FORM).submit();
}

/* Confirm Delete ------------------------------------------------- */
/* Added by ADH - 7/14/05 ---------------------------------------- */
function confirm_delete(item_name) {
    var agree=confirm("Are you sure you want to delete this " + item_name + "?");
    if (agree) return true;
    else return false;
}

function uri_with_anchor(anchor) {
    l = document.location;
    return l.protocol + '//' + l.host + l.pathname + l.search + '#' + anchor;
}

/* CSS CONTROL --------------------------------------------------- */
// << BEGIN: Toggle Display
function cssToggleDisplay(OID,DISP) {
	// OID = Object's ID
	// DISP = block, inline, list-item
	var thisOID = document.getElementById(OID);
	if(thisOID.style.display == "none"){
		thisOID.style.display=DISP;
	}
	else{
		thisOID.style.display="none";
	}
}
// >> END: Toggle Display

// << BEGIN: Toggle Visibility ON
function cssToggleVisibilityOn(OID) {
	// OID = Object's ID
	var thisOID = document.getElementById(OID);
	thisOID.style.visibility="visible";
}
// >> END: Toggle Visibility ON

// << BEGIN: Toggle Visibility OFF
function cssToggleVisibilityOff(OID) {
	// OID = Object's ID
	var thisOID = document.getElementById(OID);
	thisOID.style.visibility="hidden";
}
// >> END: Toggle Visibility OFF

MHS.AlbumToolbar = function() {
    var D = YAHOO.util.Dom;
    var E = YAHOO.util.Event;

    var elTab;
    var elDrawer;

    var animOpen;
    var animClose;

    function toggleDrawer(e) {
        E.stopEvent(e);

        if (D.getStyle(elDrawer, 'width') == '401px') {
            animClose.animate();
        }
        else {
            animOpen.animate();
        }
    }

    return {
        toggleDrawer : toggleDrawer,
        init : function() {
            elTab    = D.get('album_drawer_tab');
            elDrawer = D.get('mycollection_drawer');

            E.addListener(elTab, 'click', MHS.AlbumToolbar.toggleDrawer);

            animOpen = new YAHOO.util.Anim( elDrawer, { width: { to: 401}, left : { to : -402 } }, 0.5 );
            animClose = new YAHOO.util.Anim( elDrawer, { width: { to: 31}, left : { to : -32 } }, 0.5 );
        }
    };
}();
YAHOO.util.Event.onDOMReady( MHS.AlbumToolbar.init );

MHS.Popup = function () {

    var D = YAHOO.util.Dom;
    var E = YAHOO.util.Event;

    var popup_window = null;

    // Find all <a> elements with a rel of 'popup', and attach listeners
    function findPopups() {
        var i, popups;

        function testLink(el) {
            if (el.rel.indexOf('popup') != -1) {
                return 1;
            }
            return 0;
        }

        popups = D.getElementsBy( testLink, 'a' );

        for ( i = 0; i < popups.length; i++) {
            E.addListener(popups[i], 'click', doPopup);
            popups[i].title = popups[i].title + " [Opens in pop-up window]";
        }
    }

    // Event listener for <a> elements, attached by findPopups()
    function doPopup(ev) {
        // look for rel attributes
        var rel_attr = this.rel.split(" ");

        // call the popup script
        popup(
            {   href   : this.href,
                type   : rel_attr[1],
                width  : rel_attr[2],
                height : rel_attr[3]
            }
        );

        // Cancel the default link action
        E.stopEvent(ev);
    }

    // Popup a window
    function popup(param_attrs) {

        // Default attributes
        var default_attrs = {
            standard : {
                name     : 'standard_window',
                width    : 750,
                height   : 500,
                tool     : "yes",
                location : "yes",
                menu     : "yes",
                scroll   : "yes",
                status   : "yes",
                resize   : "yes",
                popup_qs : "yes"
            },
            standard_noqs : {
                name     : 'standard_window',
                width    : 750,
                height   : 500,
                tool     : "yes",
                location : "yes",
                menu     : "yes",
                scroll   : "yes",
                status   : "yes",
                resize   : "yes",
                popup_qs : "no"
            },
            always_new : {
                name     : 'new_window_' + new Date().getTime(),
                width    : 850,
                height   : 650,
                tool     : "yes",
                location : "yes",
                menu     : "yes",
                scroll   : "yes",
                status   : "yes",
                resize   : "yes",
                popup_qs : "yes"
            },
            console : {
                name     : 'console_window',
                width    : 750,
                height   : 500,
                tool     : "no",
                location : "no",
                menu     : "no",
                scroll   : "no",
                status   : "no",
                resize   : "no",
                popup_qs : "yes"
            },
            help : {
                name     : 'help_window',
                width    : 750,
                height   : 500,
                tool     : "no",
                location : "no",
                menu     : "no",
                scroll   : "yes",
                status   : "no",
                resize   : "no",
                popup_qs : "yes"
            }
        };

        // Determine which default attrs to start from
        var type = param_attrs.type || 'standard';
        var attr = default_attrs[type];

        // Merge the parameter attributes with out defaults
        var i;
        for (i in param_attrs) {
            if (param_attrs[i]) {
                attr[i] = param_attrs[i];
            }
        }

        // Determine the location of the popup
        var screenX, screenY;
        screenX = ( (screen.availWidth / 2)  - (attr.width / 2) );
        screenY = ( (screen.availHeight / 2) - (attr.height / 2) );

        // Add 'popup=1' to the querystring of the href
        if (attr.popup_qs == "yes") {
            if (attr.href.indexOf('?') == -1)
                attr.href = attr.href + '?popup=1';
            else
                attr.href = attr.href + ';popup=1';
        }

        // Create our window features string
        var features;
        features = 'width=' + attr.width;
        features = features + ', height=' + attr.height;
        features = features + ', top=' + screenY;
        features = features + ', left=' + screenX;
        features = features + ', toolbar=' + attr.tool;
        features = features + ', location=' + attr.location;
        features = features + ', menubar=' + attr.menu;
        features = features + ', scrollbars=' + attr.scroll;
        features = features + ', status=' + attr.status;
        features = features + ', resizable=' + attr.resize;

        // Open and focus the new window
        popup_window = window.open( attr.href, attr.name, features );
        popup_window.focus();
    }

    return {
        popup : popup,
        init : function() {
           findPopups();
        }
    };
}();
YAHOO.util.Event.onDOMReady( MHS.Popup.init );


// Code based on http://snipplr.com/view/16108/truncate-a-string-to-a-set-length-breaking-at-word-boundaries/
// This method was originally called truncate(), but it conflicted with a
// similar method provided by Prototype.js.
String.prototype.truncate_mhs = function(size) {
    var bits, i;
    bits = this.split('');
    if (bits.length > size) {
        for (i = bits.length - 1; i > -1; --i) {
            if (i > size) {
                bits.length = i;
            }
            else if (' ' === bits[i]) {
                bits.length = i;
                break;
            }
        }
        bits.push('&hellip;');
    }
    return bits.join('');
}
MHS.Truncate = function() {
    var D = YAHOO.util.Dom;


    function truncateDivs() {
        var divs = D.getElementsByClassName( 'truncate_100' );

        for ( var i = 0; i < divs.length; i++) {
            var item = divs[i];
            item.innerHTML = item.innerHTML.truncate_mhs(100);
        }
    }

    return {
        init : function() {
            truncateDivs();
        }
    };
}();
YAHOO.util.Event.onDOMReady( MHS.Truncate.init );


//  REMOVE display:none AND PRINT
function printAll()
{
    var HiddenContent = $$('#content *').findAll(function(el) { return !el.visible(); });
    //console.log("HiddenContent="+HiddenContent.length);
    HiddenContent.each(function(s){
        s.show();
    });
        print();
    HiddenContent.each(function(s){
        s.hide();
    });
}
//

