var Win;
function popUp (url, name, width, height, center, resize, scroll, posleft, postop) {

	if (posleft != 0) { X = posleft }
    if (postop  != 0) { Y = postop  }

    if (!scroll) { scroll = 'yes' }
    if (!resize) { resize = 'yes' }

    if ((parseInt (navigator.appVersion) >= 4 ) && (center)) {
      X = (screen.width  - width ) / 2;
      Y = (screen.height - height) / 2;
    }
    
    if (scroll != 0) { scroll = 1 }

	if (!Win || Win.closed)
	{
	    // display the popup window
	    Win = window.open(url, name,'width='+width+',height='+height+',top='+Y+',left='+X+',resizable='+resize+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no');   
	}
	else
	{
		// window is already open, update with content
		Win.location = url;
	}    
    Win.focus();
}

function openWin (url, name) {

	if (mainWin) {
		mainWin.focus();
		mainWin.location = url;
	}
	else if (window.opener) {
		window.opener.location = url;
		window.opener.focus();
	}
	else {
	    var mainWin = window.open(url, name );   
	}
}

var url = 'http://www.willowglen.org';

document.addEvent('domready',function() {
	$$('.rollover').addEvent('mouseover', function(e) {
		this.src = url+'/images/'+this.name+'_on.png';															
	 });
	$$('.rollover').addEvent('mouseout', function(e) {
		this.src = url+'/images/'+this.name+'.png';															
	 });
	
});

/**
 * Build a small windowlet in the active window
 * 
 * @param message
 * @return
 */
function makeWindowlet(message) {

    if (!$('messageWin')) {
        
        var popsmall = new Element('div',{'id':'messageWin', 'class':'messageWin', 'html':message});
        var mask = new Element('div',{'id':'mask','class':'mask'});
        
        popsmall.injectAfter('bodywrap');
        mask.injectBefore('bodywrap');
    }   
}


/**
 * Confirmation Dialog Box
 * 
 * @param message text message to show user 
 * @return
 */
function acknowledge(message, title) {
                    
	makeWindowlet();
        if (!title) {
            title = "Update Message";
        }
        var message = "<p style=\"padding: 0px; 10px;\"><h3 style=\"margin-bottom:10px;\">"+title+":</h3>"+message+"</p><div style=\"text-align:center; margin-top:20px;\"><input type=\"button\" id=\"ok\" value=\"OK\"></div>";
        $('messageWin').set('html',message);

        $('ok').addEvent('click',function(){
            $('messageWin').dispose();
            $('mask').dispose();
        });     
}


/**
 * Approval Dialog Box
 * 
 * Reads text stored in the title property of the passed element and uses it as an approval message. 
 * User confirms or cancels action. If user confirms, the passed URL is loaded. 
 * 
 * @param updateElm
 * @return
 */
function confirmationPrompts(title, updateElm) {
	
    $$('.confirm').addEvent('click',function(e){
        e.stop();

        if (this.type == 'button') {
            var url = this.alt;         
        }
        else {
            var url = this.href;
        }
        makeWindowlet();
        
        var messageText = this.title;
        
        var message = '<h3>'+title+'</h3><p>'+messageText+'</p><div style="text-align:center;"><input type="button" id="cancel" value="Cancel"> &nbsp; <input type="button" id="confirm" value="Continue"></div>';

        $('messageWin').set('html',message);
        
        $('cancel').addEvent('click',function(){
            $('messageWin').dispose();
            $('mask').dispose();
        });
        
        $('confirm').addEvent('click',function(){
            if (updateElm) {
                var confAjax = new Request.HTML({
                    url: url,
                    update: updateElm,
                    onSuccess: function() {
                        confirmationPrompts();     
                        $$('.overlay').addEvent('click',function(e){
                            e.stop();
                            var string = "?";
                            var url = this.href;
                            confAjax.get(url);
                        });                         
                    }
                }).get();
                $('messageWin').dispose();
                $('mask').dispose();   
                title = '';
            }
            else {
                window.location = url;
            }
        });                 
    });
}

