// Helper Routines
function ddConfirm(text, ans) {
    try {
        ans = ans ? ans : 'YES';
        var answer = prompt(text);
        if (answer && answer == ans) {
            return true;
        } else {
            return false;
        }

    } catch (ex) {

    alert(ex);
    }

}

function setActiveStyleSheet(title) {
    var isFound = 0;
    $$('link').each(function (el) {
        if (el.getProperty('rel') && el.getProperty('rel').contains('style') && el.getProperty('title')) {
            el.disabled = true;
            if (el.getProperty('title') == title) { 
                el.disabled = false; 
                isFound = 1;
            }
        }             
    });
    if (isFound != 1) {
        alert("Could not locate the stylesheet titled '" & title & "'");
    }
}        

var email = {
    p1 : "australian",
    p2 : "plants",
    p3 : "jo",
    p4 : "",
    p5 : "com",
    Address : function() {
        return this.p3 + this.p4 + '@' + this.p1 + this.p2 + '.' + this.p5;
    }
};

function SendEmail(name) {
    
    if (!name) {
        name = "Australian Native Plants Nursery";
    } else if ($type(name) == 'element') {
        name = name.innerHTML;
    }
    
    var adr = email.Address();
    
    if (confirm('Send an email to ' + name + '\n\n( ' + adr + ' )')) {
        location.href = 'mailto:' + name + ' <' + adr + '>';        
    }
    
    return false;
}

function EmailAddress() {
    
    document.write(email.Address());
}

var WhatIs = {
    privacy : 'Please be assured that we respect your right to privacy \n' +
                'and will never give, sell, or trade your personal information \n' +
                'with any company or organization except by law.',
                
    Privacy : function () {
        alert(this.privacy);
    },
               
    Phone : function () {
        var msg = 'If you would like to be contacted by phone, \n' +
                  'include your phone number in your message. \n\n' +
                  this.privacy;
                  
       alert(msg);        
    },
    
    Captcha : function () {
        var msg = 'There are spammers that use contact forms like this one \n' +
                  'to send spam email advertisements. To combat the spam servers, \n' + 
                  'you are asked to answer a basic arithmetic problem. \n\n' +
                  'Place your answer to the math question in the box provided.';
                  
       alert(msg);    
    }
}
    
var dd = {
    Toggle: function (ctrl) {
        ctrl = $(ctrl);
        if (ctrl) {
            if (ctrl.getStyle('display') && ctrl.getStyle('display') == 'none') {
                ctrl.setStyle('display','');
            } else {
                ctrl.setStyle('display','none');
            }
        }
    },
    MouseOver: function (target) {
		if (!target) { return; } 
		if (target.get('tag') != 'img') {
		    target = target.getElement('img');
		}       
		if (target) { 
			target.setProperty('src',target.getProperty('src').replace('.gif','-ro.gif')); 
		}
    },
    MouseOut: function (target) {
		if (!target) { return; } 
		if (target.get('tag') != 'img') {
		    target = target.getElement('img');
		}       
		if (target) { 
			target.setProperty('src',target.getProperty('src').replace('-ro.gif','.gif')); 
		}
    }    
};

var CartHelper = new Class({
    initialize: function(options) {
        this.options = $merge({
			itemClass: 'addToCart'
			}, options || {});
			
	    $$('.' + this.options.itemClass).each( function(el) {
	        var img = el.getElement('img');
	        if (img) {
                img.addEvents({
				    mouseover: this.onMouseOver.bind(this),
				    mouseout: this.onMouseOut.bind(this)
			    });	        
			}
	    }.bind(this));
    },
    
    onMouseOver: function (evt) {
        evt = new Event(evt);
		
		var target = evt.target;
		if (!target) { return; } 
		if (target.get('tag') != 'img') {
		    target = target.getElement('img');
		}       
		if (target) { 
			target.setProperty('src',target.getProperty('src').replace('.gif','-ro.gif')); 
		}
    },
    onMouseOut: function (evt) {
        evt = new Event(evt);
		
		var target = evt.target;
		if (!target) { return; } 
		if (target.get('tag') != 'img') {
		    target = target.getElement('img');
		}       
		if (target) { 
			target.setProperty('src',target.getProperty('src').replace('-ro.gif','.gif')); 
		}
    }
});
    
var ddRollover = new Class({
	initialize: function(options){
		this.options = $merge({
			elements: [],
			backgroundOVER: '#CFCA4E',
			backgroundOUT: '#F6E8B1',
			backgroundOUT_SELECTED: null,
			heading: null,
			target: null
			}, options || {});
		
		this.options.elements.each( function(el) {
		
			//extract the span
			var span = null;
			if (el.getElement('span')) {
				span = el.getElement('span');
				//set the title of the link
				if (!el.title) {
					el.title = span.innerHTML;
				}
			}

            if (span) {
			    //bind the mouseover and mouseout events to the span title
			    span.addEvents({
				    mouseover: this.mouseover.bind(this),
				    mouseout: this.mouseout.bind(this)
			    });
            }
			
			//change to the image
			el = el.getElement('img');
			if (!el) { return; }
			
			//bind the mouseover and mouseout events to the image
			el.addEvents({
				mouseover: this.mouseover.bind(this),
				mouseout: this.mouseout.bind(this)
			});
			
			//find parent
			try {
			    el.parent = el.getParent();
			    while (el.parent && el.parent.get('tag') != 'li' && el.parent.get('tag') != 'body') {
				    el.parent = el.parent.getParent();
			    }
			    if (!el.parent || el.parent.get('tag') != 'li') { el.parent = null; }						
			} catch (err) {
			    console.log($type(el.parent), err);
			}
			
		}.bind(this));	
	},
	mouseover: function(event) {
		event = new Event(event);
		
		var target = event.target;
		if (!target) { return; } 
		while (target && target.get('tag') != 'li' && target.get('tag') != 'body') {
		    target = target.getParent();
		}       
		if (target) { 
			target.setStyle('background',this.options.backgroundOVER); 
		}
	},
	mouseout: function(event) {
		event = new Event(event);
		var target = event.target;
		if (!target) { return; }
		while (target && target.get('tag') != 'li' && target.get('tag') != 'body') {
		    target = target.getParent();
		}       
		if (target) {
		    if (target.hasClass('selected') && this.options.backgroundOUT_SELECTED) {
			    target.setStyle('background',this.options.backgroundOUT_SELECTED); 
		    } else {
			    target.setStyle('background',this.options.backgroundOUT); 
			}
		}
	}

});
