/* global.js for mpr006.  Requires jquery 1.3.2 or above.*/

/* PRINT PAGE */

function print_me() {
	if (window.print) { 
		window.print()
	} else {
		var platform = navigator.platform.toUpperCase()
		var browser = navigator.appName.toUpperCase()
		if (platform == "MACPPC" && browser == "NETSCAPE") {
			alert('Please use command-p to print.')
		} else {
			alert('Please use control-p to print.')
		}
	}
}

/* PAGE SHARE TOOLS - SUPPORTING FUNCTIONS */

function getShareDocumentTitle() {
	return document.title;
}

function getShareDocumentDescription() {
	return $("meta[name='description']").attr('content');
}

function setupDefaultShareContent() {
	/* Replaces default email a friend form values with values from the document. If these are specifically set
	in your document, omit the "sharedefault" class on the hidden fields. */
	var deliciousUrl = 'http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url='+encodeURIComponent(location.href)+'&amp;title=' + encodeURIComponent(getShareDocumentTitle());
	var diggUrl = 'http://digg.com/submit?phase=2&amp;url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(getShareDocumentTitle())+'&amp;bodytext='+encodeURIComponent(getShareDocumentDescription());

	$("form[name='emailStory'] input[name='title'].usesharedefault").attr('value',getShareDocumentTitle());
	$("form[name='emailStory'] input[name='description'].usesharedefault").attr('value',getShareDocumentDescription());
	$("form[name='emailStory'] A.usedeliciousdefault").click(function() {
		window.open(deliciousUrl, 'delicious','toolbar=no,width=700,height=400');
		return false;
	});
	$("form[name='emailStory'] A.usediggdefault").attr("href",diggUrl);
	$("#sharetools-email-submit").click( function() {
		$("#sharetools-email-form").submit();
	});
}

/* UTILITY FUNCTIONS */

function encode_utf8( s )
{
	return unescape( encodeURIComponent( s ) );
}

function decode_utf8( s )
{
	return decodeURIComponent( escape( s ) );
}

function addBookmark() {
	// taken from: http://calisza.wordpress.com/2008/11/03/javascript-jquery-bookmark-script/ by AK 5/28/09
	// replaces links with bookmarklink class to trigger the browser's add bookmark function.
	// add a "rel" attrib if Opera 7+   
    if(window.opera) {   
        if ($("a.bookmarklink").attr("rel") != ""){ // don't overwrite the rel attrib if already set   
            $("a.bookmarklink").attr("rel","sidebar");   
        }   
    }   
  
    $("a.bookmarklink").click(function(event){   
        event.preventDefault(); // prevent the anchor tag from sending the user off to the link   
        var url = this.href;   
        var title = this.title;   
  
        if (window.sidebar) { // Mozilla Firefox Bookmark   
            window.sidebar.addPanel(title, url,"");   
        } else if( window.external ) { // IE Favorite   
            window.external.AddFavorite( url, title);   
        } else if(window.opera) { // Opera 7+   
            return false; // do nothing - the rel="sidebar" should do the trick   
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)   
             alert('Unfortunately, this browser does not support the requested action.'  
             + ' Please try pressing CTRL+D (Cmd+D on Mac) to bookmark this page manually.');   
        }   
  
    });   
}

// Used in program page drop-down menus
function mergeDate()
{
	document.dateDrop.date.value = document.dateDrop.month.value + "-" + document.dateDrop.day.value + "-" + document.dateDrop.year.value;
}


// Radio program drop down menu for navigation
function gotoProgram(formID){
    $(formID).change(function(){
		var myIndex = $(this).attr('selectedIndex');
		window.location.href = $(this).find('option[index='+myIndex+']').val();
	});
}

/* JQUERY DOCUMENT READY CODE */

$(document).ready( function() {
	setupDefaultShareContent();
	addBookmark();
	gotoProgram('#findAProgram');
});

