String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); };
String.prototype.lc = String.prototype.toLowerCase;
String.prototype.uc = String.prototype.toUpperCase;

String.prototype.bind = function(binds) {
        stmt = this
        for(k in binds) {            
            re = new RegExp('(\\W):'+k+'\\b', 'g')
            stmt = stmt.replace(re, "$1" + binds[k]);
        }
        return stmt
    }
    
var Pancho = { 'null' : null };
Pancho.uploader = {'uploader': {'null': {'fileID': null, 'uploading': false}}}

Pancho.uploader.init = function(prefix, swf_url, selectimage_url, upload_url, preview_url) {
    YAHOO.widget.Uploader.SWFURL = swf_url; 
    var uploader = new YAHOO.widget.Uploader( prefix+"uploaderUI", selectimage_url ); 
    
    Pancho.uploader.uploader[prefix] = {
        'uploader': uploader,
        'prefix': prefix,
        'upload_url': upload_url,
        'preview_url': preview_url,
        'fileID': null }    
    
    uploader.addListener('contentReady', function() {Pancho.uploader.handleContentReady(prefix)})
    uploader.addListener('fileSelect', function(e) {Pancho.uploader.onFileSelect(prefix, e)})
    uploader.addListener('uploadStart', function(e) {Pancho.uploader.onUploadStart(prefix, e)})
    uploader.addListener('uploadProgress', function(e) {Pancho.uploader.onUploadProgress(prefix, e)})
    uploader.addListener('uploadCancel', function(e) {Pancho.uploader.onUploadCancel(prefix, e)})
    uploader.addListener('uploadComplete', function(e) {Pancho.uploader.onUploadComplete(prefix, e)})
    uploader.addListener('uploadCompleteData', function(e) {Pancho.uploader.onUploadResponse(prefix, e)})
    uploader.addListener('uploadError', function(e) {Pancho.uploader.onUploadError(prefix, e)})
    Pancho.uploader.updatePreview(prefix)
}

Pancho.uploader.handleContentReady = function(prefix) {
    // Allows the uploader to send log messages to trace, as well as to YAHOO.log
	Pancho.uploader.uploader[prefix]['uploader'].setAllowLogging(true);

	// Restrict selection to a single file (that's what it is by default,
	// just demonstrating how).
	Pancho.uploader.uploader[prefix]['uploader'].setAllowMultipleFiles(false);

	// New set of file filters.
	var ff = new Array({description:"Images", extensions:"*.*;*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.tif;*.tiff;"});

	// Apply new set of file filters to the uploader.
	Pancho.uploader.uploader[prefix]['uploader'].setFileFilters(ff);
}

Pancho.uploader.onFileSelect = function(prefix, event) {
	for (var item in event.fileList) {
	    if(YAHOO.lang.hasOwnProperty(event.fileList, item)) {
			YAHOO.log(event.fileList[item].id);
			Pancho.uploader.uploader[prefix]['fileID'] = event.fileList[item].id;
		}
	}
	Pancho.uploader.uploader[prefix]['uploader'].disable();

	var filename = document.getElementById(prefix+"fileName");
	filename.innerHTML = event.fileList[Pancho.uploader.uploader[prefix]['fileID']].name;

	var progressbar = document.getElementById(prefix+"progressBar");
	progressbar.innerHTML = "";
}

Pancho.uploader.upload = function(prefix) {
	if (Pancho.uploader.uploader[prefix]['fileID'] != null) {
		Pancho.uploader.uploader[prefix]['uploader'].upload(Pancho.uploader.uploader[prefix]['fileID'], Pancho.uploader.uploader[prefix]['upload_url']);
		Pancho.uploader.uploader[prefix]['fileID'] = null;
	}
}

Pancho.uploader.handleClearFiles = function(prefix) {
	Pancho.uploader.uploader[prefix]['uploader'].clearFileList();
	Pancho.uploader.uploader[prefix]['uploader'].enable();
	Pancho.uploader.fileID = null;

	var filename = document.getElementById(prefix+"fileName");
	filename.innerHTML = "";

	var progressbar = document.getElementById(prefix+"progressBar");
	progressbar.innerHTML = "";
	
    document.getElementById(prefix + "status_element").innerHTML = ""	
    document.getElementById(prefix + "preview_element").innerHTML = ""	    
   if(prefix=='-') {
        // not being used with a formset
        document.getElementById("id_image").value = ''  
    } else {
        document.getElementById("id_" + prefix + "image").value = ''
    } 
}

Pancho.uploader.onUploadProgress = function(prefix, event) {
	prog = Math.round(300*(event["bytesLoaded"]/event["bytesTotal"]));
  	progbar = "<div style=\"background-color: #f00; height: 5px; width: " + prog + "px\"/>";

	var progressbar = document.getElementById(prefix + "progressBar");
	progressbar.innerHTML = progbar;
}

Pancho.uploader.onUploadComplete = function(prefix, event) {
	uploader.clearFileList();
	uploader.enable();

	progbar = "<div style=\"background-color: #f00; height: 5px; width: 300px\"/>";
	var progressbar = document.getElementById(prefix + "progressBar");
	progressbar.innerHTML = progbar;	
}

Pancho.uploader.onUploadStart = function(prefix, event) {	
    Pancho.uploader.uploader[prefix]['uploading'] = true
    document.getElementById(prefix + "status_element").innerHTML = "<span>Uploading File ...</span>"    
    document.getElementById("form_submit_button").value = "Please Wait ...";    
    document.getElementById("form_submit_button").disabled=true;
}

Pancho.uploader.onUploadError = function(prefix, event) {
    Pancho.uploader.uploader[prefix]['uploading'] = false    
    document.getElementById(prefix + "status_element").innerHTML = "<span>Error Uploading File</span>"    
    Pancho.uploader.enableSubmit()    
}

Pancho.uploader.onUploadCancel = function(prefix, event) {
    Pancho.uploader.uploader[prefix]['uploading'] = false    
    document.getElementById(prefix + "status_element").innerHTML = "<span>Canceled Uploading File</span>"        
    Pancho.uploader.enableSubmit()    
}

Pancho.uploader.onUploadResponse = function(prefix, event) {
    Pancho.uploader.uploader[prefix].uploading = false    
    document.getElementById(prefix + "status_element").innerHTML = "<span>File Uploaded!</span>"
   if(prefix=='-') {
        // not being used with a formset
        document.getElementById("id_image").value = event.data        
    } else {
        document.getElementById("id_" + prefix + "image").value = event.data        
    } 
 
    Pancho.uploader.enableSubmit()
    Pancho.uploader.updatePreview(prefix)
}

Pancho.uploader.updatePreview = function(prefix) {
   if(prefix=='-') {
        // not being used with a formset
        data = document.getElementById("id_image").value
    } else {
        data = document.getElementById("id_" + prefix + "image").value
    } 

    if(data) {
    var request = YAHOO.util.Connect.asyncRequest('GET', Pancho.uploader.uploader[prefix]['preview_url'] + data + "/", {
        success: function(o) {
            document.getElementById(prefix + "preview_element").innerHTML = o.responseText
        },
        failure: function(o) {
            document.getElementById(prefix + "preview_element").innerHTML = "Couldn't get preview."            
        }
    })
} else {
	document.getElementById(prefix+"preview_element").innerHTML = ''
}  

}

Pancho.uploader.enableSubmit = function() {
    enable = true
    for(prefix in Pancho.uploader.uploader) {
        if(Pancho.uploader.uploader[prefix].uploading) {
            enable = false
        }
    }
    if(enable) {
        document.getElementById("form_submit_button").value = "Save"
        document.getElementById("form_submit_button").disabled=false;
    }    
}

Pancho.cropper = { cropper : null };

Pancho.cropper.init = function(prefix, init_coords) {
    Pancho.cropper.horizontal_element = prefix + "horizontal_orientation"
    Pancho.cropper.vertical_element = prefix + "vertical_orientation"
    Pancho.cropper.ff_element = prefix + "free_form"
    Pancho.cropper.image_element = prefix + "image"
    Pancho.cropper.top_element = prefix + "image_top"
    Pancho.cropper.left_element = prefix + "image_left"
    Pancho.cropper.width_element = prefix + "image_width"
    Pancho.cropper.height_element = prefix + "image_height"            
    Pancho.cropper.init_coords = init_coords
    
    Pancho.cropper.createCropper();
    YAHOO.util.Event.addListener(Pancho.cropper.horizontal_element, "click", Pancho.cropper.createCropper);
    YAHOO.util.Event.addListener(Pancho.cropper.vertical_element, "click", Pancho.cropper.createCropper);
    YAHOO.util.Event.addListener(Pancho.cropper.ff_element, "click", Pancho.cropper.createCropper);    
}

Pancho.cropper.createCropper = function() {
	width = Pancho.cropper.init_coords[2]    
	height = Pancho.cropper.init_coords[3]
	
    if(YAHOO.util.Dom.get(Pancho.cropper.ff_element).checked) {
        initWidth = width 
        initHeight = height
    }
	
    if(YAHOO.util.Dom.get(Pancho.cropper.horizontal_element).checked) {
        initWidth = width 
        initHeight = width / (802/537)
    }
    
    if(YAHOO.util.Dom.get(Pancho.cropper.vertical_element).checked) {
        initWidth = height / (802/537)
        initHeight = height
    }        

	if(Pancho.cropper.cropper) { 
		Pancho.cropper.cropper.destroy()
	}

	Pancho.cropper.cropper = new YAHOO.widget.ImageCropper(Pancho.cropper.image_element, {
		initialXY: [Pancho.cropper.init_coords[1], Pancho.cropper.init_coords[0]],
		initWidth: initWidth,
		initHeight: initHeight,
		ratio: !(YAHOO.util.Dom.get(Pancho.cropper.ff_element).checked), 
		keyTick: 5
	})	
	
	Pancho.cropper.updateCoords()
	Pancho.cropper.cropper.on('moveEvent', Pancho.cropper.updateCoords)
}

Pancho.cropper.updateCoords = function() {
    var coords = Pancho.cropper.cropper.getCropCoords()
	YAHOO.util.Dom.get(Pancho.cropper.top_element).value = coords.top
	YAHOO.util.Dom.get(Pancho.cropper.left_element).value = coords.left
	YAHOO.util.Dom.get(Pancho.cropper.width_element).value = coords.width
	YAHOO.util.Dom.get(Pancho.cropper.height_element).value = coords.height
}

Pancho.util = {'null': null}

Pancho.util.addOneProjectPhoto = function() {
    var photos,
        max_value,
        new_photo_element,
        new_photo_html,
        replacement_string;
        
    photos = YAHOO.util.Dom.getElementsByClassName('photo', 'div')
    max_value = YAHOO.util.Dom.get('id_photos-TOTAL_FORMS').value
    YAHOO.util.Dom.get('id_photos-TOTAL_FORMS').value = parseInt(max_value)+1
    new_photo_element = document.createElement('div')
    new_photo_element.className = 'photo top-border'
    new_photo_html = photos[0].innerHTML.replace(/photos-\d+/gm, "photos-"+max_value)
    replacement_string = 'input name="photos-'+max_value+'-image" value="" id="id_photos-'+max_value+'-image" type="hidden" /'
    // the image id has to be replaced before the html is inserted because of the ondomready event that will fire and get the image thumbnail
    new_photo_html = new_photo_html.replace(
    	        // <input name="photos-7-image" value="1" id="id_photos-7-image" type="hidden">                                
            	// <input name="photos-4-image" value="1" id="id_photos-4-image" type="hidden">
            	/input\sname="photos-\d+-image"\svalue="\d*"\sid="id_photos-\d+-image"\stype="hidden"/m,
    	    	replacement_string)
    new_photo_element.innerHTML = new_photo_html    	    	
    YAHOO.util.Dom.insertAfter(new_photo_element, YAHOO.util.Dom.getLastChild('photos'))
    YAHOO.util.Dom.get('id_photos-'+max_value+'-id').value = ''    
    YAHOO.util.Dom.get('id_photos-'+max_value+'-order').value = '0'    
    YAHOO.util.Dom.get('id_photos-'+max_value+'-name').value = ''    
    YAHOO.util.Dom.get('id_photos-'+max_value+'-section_name').value = ''    
    YAHOO.util.Dom.get('id_photos-'+max_value+'-photographer').value = '' 
    YAHOO.util.Dom.get('photos-'+max_value+'-preview_element').innerHTML = '' 
    YAHOO.util.Event.onDOMReady(function() {
        Pancho.uploader.init('photos-'+max_value+'-', 
        "/site_media/uploader/assets/uploader.swf", 
        "/site_media/img/selectFileButton.png", 
        "/media/upload/", 
        "/media/thumbnail")  
    })
}

Pancho.util.addOnePerson = function() {
    var people,
        max_value,
        new_element,
        new_element_html,
        replacement_string;
        
    people = YAHOO.util.Dom.getElementsByClassName('person', 'div')
    max_value = YAHOO.util.Dom.get('id_person-TOTAL_FORMS').value
    YAHOO.util.Dom.get('id_person-TOTAL_FORMS').value = parseInt(max_value)+1
    new_element = document.createElement('div')
    new_element.className = 'person'
    new_element_html = people[0].innerHTML.replace(/person-\d+/gm, "person-"+max_value)
    replacement_string = 'input name="person-'+max_value+'-image" id="id_person-'+max_value+'-image" type="hidden" value=""'
    // the image id has to be replaced before the html is inserted because of the ondomready event that will fire and get the image thumbnail
    new_element_html = new_element_html.replace(
    	        // <input name="photos-7-image" value="1" id="id_photos-7-image" type="hidden">                                
            	// <input name="photos-4-image" value="1" id="id_photos-4-image" type="hidden">
            	/input\sname="person-\d+-image"\svalue="\d*"\sid="id_person-\d+-image"\stype="hidden"/m,
    	    	replacement_string)
    new_element_html = new_element_html.replace(
                /<div\sclass="yui-editor-container".*<textarea/m,
                "<textarea")    	    	
    new_element.innerHTML = new_element_html    	
    YAHOO.util.Dom.insertAfter(new_element, YAHOO.util.Dom.getLastChild('people'))
    // YAHOO.util.Dom.get('id_photos-'+max_value+'-id').value = ''    
    // YAHOO.util.Dom.get('id_photos-'+max_value+'-order').value = '0'    
    // YAHOO.util.Dom.get('id_photos-'+max_value+'-name').value = ''    
    // YAHOO.util.Dom.get('id_photos-'+max_value+'-section_name').value = ''    
    // YAHOO.util.Dom.get('id_photos-'+max_value+'-photographer').value = ''
}

Pancho.util.buildSelectTag = function(data) {
    var data,
        new_options;
        
	data = YAHOO.lang.JSON.parse(data)
	new_options = ''
	for(var i=0; i<data.values.length; i++) {
		new_options = new_options + '<option value="'+ data.values[i].value +'" ' + (data.values[i].selected ? 'selected="selected"' : '') + '>'+ data.values[i].display +'</option>'
	}
	return new_options
}

Pancho.util.updateSelectTag = function(element, url) {
    var request = YAHOO.util.Connect.asyncRequest('GET', url, {
        success: function(o) {
            document.getElementById(element).innerHTML = Pancho.util.buildSelectTag(o.responseText)
        },
        failure: function(o) {
            YAHOO.util.Dom.insertBefore('Error Updating List', YAHOO.util.Dom.get(element))
        }
    });    
}

Pancho.util.categoryDialog = null;

Pancho.util.addCategory = function(url) {
    YAHOO.util.Connect.asyncRequest('GET', url, {
        success: function(o) {
            var category_form_container,
                category_form;
                
            category_form_container = new YAHOO.util.Element(YAHOO.util.Dom.get('category_form_container'))
            while (category_form_container.hasChildNodes()) {
                category_form_container.removeChild(category_form_container.get('firstChild'));
            }
            category_form = document.createElement('div')
            category_form.id = 'category_form'
            category_form.innerHTML = o.responseText
            category_form_container.appendChild(category_form)

            Pancho.util.categoryDialog = new YAHOO.widget.Dialog('category_form', {
                width: '600px',
                fixedcenter: true,
                visible: false,
                constraintoviewport: true,
                buttons: [  { text: "Save", handler: function(o) { this.submit() }, isDefault: true },
                            { text: "Cancel", handler: function(o) { this.cancel(); } } ] } )
            Pancho.util.categoryDialog.callback = {
                success: function(o) {
                    document.getElementById('id_category').innerHTML = Pancho.util.buildSelectTag(o.responseText)
                },
                failure: function(o) {
                    alert('Failure: ' + o.responseText)
                }
            }
            Pancho.util.categoryDialog.render()
            Pancho.util.categoryDialog.show()
        },
        failure: function(o) {
            YAHOO.util.Dom.insertBefore('Error Opening Dialog', YAHOO.util.Dom.get('add_category_link'))
        }
    })
}

Pancho.util.createRichTextField = function(element, image_upload_url) {
    var myEditor = new YAHOO.widget.Editor(element, {
        width: '800px',
        height: '300px',
        animate: true,
        dompath: true,
        focusAtStart: true,
        autoHeight: true,  
        handleSubmit: true,
        css: " \
	html, body { \
		background-color: #000000; \
		color: #999999; \
		margin: .5em; \
                font-family: Arial, Verdana, Helvetica, sans-serif; \
	} \
	\
	body { \
               font-size: 86%; \
 	} \
	\
	a, a:visited { \
		color: #999999; \
		text-decoration: none; \
	} \
	\
	a: active, a:hover { \
               color: #FFFFFF; \
        } \
	\
	b, strong { \
                color: #FFFFFF; \
                font-weight: bold; \
        } \
	\
        h1, h2, h3, h4, h5, h6 { \
		margin-top: 1em; \
                color: #FFFFFF; \
                margin-bottom: .25em; \
        } \
	\
	ul {\
		margin: 0px; \
		padding: 0px; \
	} \
 	\
        li { \
            	list-style-image: url(/site_media/img/plus.png); \
            	margin-left: 1.2em; \
        }"
    })

    yuiImgUploader(myEditor, element, image_upload_url,'Filedata')
    
    myEditor.render()   
}

Pancho.util.rotator = {'null': null}
Pancho.util.rotator.elements = {0: null}
Pancho.util.rotator.current_element = 0
Pancho.util.rotator.timer = null
Pancho.util.rotator.rotator_index = 0

Pancho.util.rotator.init = function(container) {
    // carousel can't do fading animation :-(
    // var carousel = new YAHOO.widget.Carousel(container, {
    //     autoPlayInterval: 2000, 
    //     autoPlay: 2000,
    //     animation: {speed: 2, effect: function() {
    //             YAHOO.util.Dom.getChildren("projects");
    //             YAHOO.util.DOM.
    //             alert(carousel.get("selectedItem"));
    //           }},
    //     isCircular: true, 
    //     numVisible: 1
    // });
    // carousel.render();
    // carousel.show();
    // carousel.startAutoPlay();

    // indexing the rotators and reseting the previous index to have no elements allows us to reset the rotator when the image is manually swapped
    Pancho.util.rotator.elements[Pancho.util.rotator.rotator_index] = false
    Pancho.util.rotator.rotator_index = Pancho.util.rotator.rotator_index+1
    Pancho.util.rotator.elements[Pancho.util.rotator.rotator_index] = YAHOO.util.Dom.getChildren(YAHOO.util.Dom.get(container))
    Pancho.util.currentImage = Pancho.util.rotator.current_element        
    if(Pancho.util.rotator.rotator_index == 1) {// only auto start the first time
        if(Pancho.util.rotator.elements[Pancho.util.rotator.rotator_index].length > 1) {
            Pancho.util.rotator.timer = setTimeout(function () {
                Pancho.util.rotator.rotate(Pancho.util.rotator.rotator_index);
            }, 5000)
        }
    }
    return Pancho.util.rotator.rotator_index;
}

Pancho.util.rotator.rotate = function(rotator_index) {
    if(Pancho.util.rotator.elements[rotator_index]) {
        if(Pancho.util.rotator.current_element+1 == Pancho.util.rotator.elements[rotator_index].length) {
            next_element = 0
        } else {
            next_element = Pancho.util.rotator.current_element+1
        }
    
        var fadeOut = new YAHOO.util.Anim(Pancho.util.rotator.elements[rotator_index][Pancho.util.rotator.current_element], 
            {opacity: {to: 0}}, 
            1)
        var fadeIn = new YAHOO.util.Anim(Pancho.util.rotator.elements[rotator_index][next_element],
            {opacity: {to: 1}},
            1)
        
        fadeIn.animate()
        fadeOut.animate()
    
        Pancho.util.rotator.current_element = next_element
    
        Pancho.util.rotator.timer = setTimeout(function () {
            Pancho.util.rotator.rotate(rotator_index);
        }, 5000)    
    }
    
    Pancho.util.currentImage = Pancho.util.rotator.current_element    
}

Pancho.util.currentImage = 0

Pancho.util.swapImage = function(index, photos_element, thumbnails_element) {
    var photos,
        thumbnails = [],
        index;
    
    photos = YAHOO.util.Dom.getChildren(YAHOO.util.Dom.get(photos_element))
    if(thumbnails_element) {
        thumbnails = YAHOO.util.Dom.getChildren(YAHOO.util.Dom.get(thumbnails_element)) 
    } 
    
    if(index>=thumbnails.length) {
        index = 0
    }
    
    // fade out old image
    var fadeOut = new YAHOO.util.Anim(photos[Pancho.util.currentImage+1], 
        {opacity: {to: 0}}, 
        1)
    // fade in new image
    var fadeIn = new YAHOO.util.Anim(photos[index+1], 
        {opacity: {to: 1}}, 
        1)
    if(thumbnails_element) {
        // fade out old thumbnail
        var fadeOutThumb = new YAHOO.util.Anim(YAHOO.util.Dom.getFirstChild(YAHOO.util.Dom.getFirstChild(YAHOO.util.Dom.getFirstChild(thumbnails[Pancho.util.currentImage]))),
            {opacity: {to: .5}},
            1)
        // fade in new thumbnail
        var fadeInThumb = new YAHOO.util.Anim(YAHOO.util.Dom.getFirstChild(YAHOO.util.Dom.getFirstChild(YAHOO.util.Dom.getFirstChild(thumbnails[index]))),
            {opacity: {to: 1}},
            1)
    }
        
    fadeOut.animate()
    fadeIn.animate()
    if(thumbnails_element) {
        fadeOutThumb.animate()
        fadeInThumb.animate()
    }
    
    Pancho.util.currentImage = index
}

Pancho.util.nextImage = function(photos_element, thumbnails_element) {
    Pancho.util.swapImage(Pancho.util.currentImage+1, photos_element, thumbnails_element);
}

var ua = YAHOO.env.ua,
    oAnim;  // Animation instance
	bStopPropagation = false // variable used to make sure the menu isn't hidden on clicking after being displayed on rollover

	YAHOO.util.Event.on(this, "click", function (p_oEvent) {
		if (bStopPropagation) {
			YAHOO.util.Event.stopEvent(p_oEvent);
		}	
	});

/*
     "beforeshow" event handler for each submenu of the MenuBar
     instance, used to setup certain style properties before
     the menu is animated.
*/
function onSubmenuBeforeShow(p_sType, p_sArgs) {

    var oBody,
        oElement,
        oShadow,
        oUL;


    if (this.parent) {
		bStopPropagation = true;
		
        oElement = this.element;

        
        /*
            Stop the Animation instance if it is currently 
            animating a Menu.
        */ 
    
        if (oAnim && oAnim.isAnimated()) {
        
            oAnim.stop();
            oAnim = null;
        
        }


        /*
            Set the body element's "overflow" property to 
            "hidden" to clip the display of its negatively 
            positioned <ul> element.
        */ 

        oBody = this.body;


        //  Check if the menu is a submenu of a submenu.

        if (this.parent && 
            !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
        

            /*
                There is a bug in gecko-based browsers where 
                an element whose "position" property is set to 
                "absolute" and "overflow" property is set to 
                "hidden" will not render at the correct width when
                its offsetParent's "position" property is also 
                set to "absolute."  It is possible to work around 
                this bug by specifying a value for the width 
                property in addition to overflow.
            */

            if (ua.gecko) {
            
                oBody.style.width = oBody.clientWidth + "px";
            
            }
            
            
            /*
                Set a width on the submenu to prevent its 
                width from growing when the animation 
                is complete.
            */
            
            if (ua.ie == 7) {

                oElement.style.width = oElement.clientWidth + "px";

            }
        
        }


        oBody.style.overflow = "hidden";


        /*
            Set the <ul> element's "marginTop" property 
            to a negative value so that the Menu's height
            collapses.
        */ 

        oUL = oBody.getElementsByTagName("ul")[0];

        oUL.style.marginTop = ("-" + oUL.offsetHeight + "px");
    
    }

}


/*
    "tween" event handler for the Anim instance, used to 
    syncronize the size and position of the Menu instance's 
    shadow and iframe shim (if it exists) with its 
    changing height.
*/

function onTween(p_sType, p_aArgs, p_oShadow) {

    if (this.cfg.getProperty("iframe")) {
    
        this.syncIframe();

    }

}


/*
    "complete" event handler for the Anim instance, used to 
    remove style properties that were animated so that the 
    Menu instance can be displayed at its final height.
*/

function onAnimationComplete(p_sType, p_aArgs, p_oShadow) {

    var oBody = this.body,
        oUL = oBody.getElementsByTagName("ul")[0];


    oUL.style.marginTop = "";
    oBody.style.overflow = "";
    

    //  Check if the menu is a submenu of a submenu.

    if (this.parent && 
        !(this.parent instanceof YAHOO.widget.MenuBarItem)) {


        // Clear widths set by the "beforeshow" event handler

        if (ua.gecko) {
        
            oBody.style.width = "";
        
        }
        
        if (ua.ie == 7) {

            this.element.style.width = "";

        }
    
    }
    
}


/*
     "show" event handler for each submenu of the MenuBar 
     instance - used to kick off the animation of the 
     <ul> element.
*/

function onSubmenuShow(p_sType, p_sArgs) {

    var oElement,
        oShadow,
        oUL;

    if (this.parent) {

        oElement = this.element;
        oShadow = oElement.lastChild;
        oUL = this.body.getElementsByTagName("ul")[0];
    

        /*
             Animate the <ul> element's "marginTop" style 
             property to a value of 0.
        */

        oAnim = new YAHOO.util.Anim(oUL, 
            { marginTop: { to: 0 } },
            .25, YAHOO.util.Easing.easeOut);


        oAnim.onStart.subscribe(function () {

            oShadow.style.height = "100%";
        
        });


        oAnim.animate();


        /*
            Subscribe to the Anim instance's "tween" event for 
            IE to syncronize the size and position of a 
            submenu's shadow and iframe shim (if it exists)  
            with its changing height.
        */

        if (YAHOO.env.ua.ie) {
            
            oShadow.style.height = oElement.offsetHeight + "px";


            /*
                Subscribe to the Anim instance's "tween"
                event, passing a reference Menu's shadow 
                element and making the scope of the event 
                listener the Menu instance.
            */

            oAnim.onTween.subscribe(onTween, oShadow, this);

        }


        /*
            Subscribe to the Anim instance's "complete" event,
            passing a reference Menu's shadow element and making 
            the scope of the event listener the Menu instance.
        */

        oAnim.onComplete.subscribe(onAnimationComplete, oShadow, this);
    
    }

}