function IsClickable( el ) {
    var clickable = ( ( el.tagName == 'A' || el.onclick ) && el.tagName != 'TR' ) ? true : false;
    if ( !clickable ) {
        el.ancestors().each(function(an){
            if ( ( an.tagName == 'A' || an.onclick ) && an.tagName != 'TR' ) clickable = true;
        });
    }
    return clickable;
}

function FireSelectEvent( event, ControlID, EventType, Item ) {
    if ( IsClickable( Event.findElement(event) ) ) return;
    SMLayoutManager.FireAjaxEvent( ControlID, 'SMTemplatedList', { EventType: EventType, Item: Item}, { Blocking: true });
}

window.ExamQube_Forms_Load=function(event) {
    var rtl = ( $$('html.oRTL').length == 0 ) ? false : true;
	var focused=event.eventName!='dom:loaded';
	$$('.oForm input, .oForm select, .oForm textarea').each(function(f){
		// exclude certain types of inputs
		switch(f.type){
			case 'button':case 'submit':case 'hidden':return;
		}
		if(f.hasClassName('SMEditor')){
			return;
		}
		// setup focus & blur events
		f.observe('focus',function(event){
			var container = f.up('.oFormRow0,.oFormRow1');
			if(container){
				container.addClassName('oFocused');
				container.removeClassName('oUnfocused');
			}
		});
		f.observe('blur',function(event){
			var container = f.up('.oFormRow0,.oFormRow1');
			if(container){
				container.addClassName('oUnfocused');
				container.removeClassName('oFocused');
			}
		});
		// focus the first element we find that's not a hidden input (e.g. viewstate)
		if(!focused&&f.visible()){
			try{
				f.focus();
				var container = f.up('.oFormRow0,.oFormRow1');
				if ( container ) container.addClassName('oFocused');
				focused=true;
			}
			catch(e){}
		}
		else{
			f.up('div').addClassName('oUnfocused');
		}
	});
	// add an asterisk to all required field labels
	$$('.oForm .oRequired').each(function(row){
		if ( !row.processed ) {
			var label = row.down('.oFormLabel');
			if ( label ) label.insert({top:'<span>*</span>'});
			row.processed = true;
		}
	});
	// build onclick event for more sections on forms
	$$('.oFormMore').each(function(el){
		if ( !el.processed ) {
            el.hide();
            var children = el.childElements();
            if ( children.length ) {
                children[0].addClassName('oFormMoreFirst');
                children[children.length-1].addClassName('oFormMoreLast');
            }
            el.processed = true;
        }
	});
	$$('.oToggleMore').each(function(a){
		if ( !a.processed ) {
			a.observe('click',function(event){
				$$(a.readAttribute('href')).each(function(el){
					el.toggle();
				});
				event.stop();
			});
			a.processed = true;
		}
	});
    // remove actions box!
    $$('.oListRowOpen').each(function(er){ er.removeClassName('oListRowOpen'); });
    $$('.oListMoreActionsBorder').each(function(er){ er.remove(); });
	
    var isIE7 = ( navigator.appVersion.indexOf("MSIE 7.") != -1 );
    
    // hover effects for an oList
	$$('.oList').each(function(l){
		l.select('.oListRow0,.oListRow1').each(function(tr){
            if ( !tr.hasClassName('proc') ) {
                tr.addClassName('proc');
                
                if ( !Prototype.Browser.MobileSafari ) {
                    tr.observe('mouseover',function(event){
                        this.addClassName('oListRowHover');
                    });
                    tr.observe('mouseout',function(event){
                        this.removeClassName('oListRowHover');
                    });
                }
                
                if ( !tr.hasClassName('oListClickable') && tr.down('.oListActions') ) {
                    
                    tr.addClassName( 'oListHasActions' );
                
                    tr.down('.oListActions').wrap('div',{'class':'oListActionsIcon'});
                    
                    tr.observe('mousedown',function(event) {
                        if ( IsClickable( Event.findElement(event) ) ) return;
                        
                        if ( !this.hasClassName('oListRowOpen') ) {
                            $$('.oListRowOpen').each(function(er){ er.removeClassName('oListRowOpen'); });
                            $$('.oListMoreActionsBox').each(function(er){ er.remove(); });
                            if ( $('oListMoreActionsBorder1') ) $('oListMoreActionsBorder1').remove();
                            if ( $('oListMoreActionsBorder2') ) $('oListMoreActionsBorder2').remove();
                            this.down('td').insert('<div class="oListMoreActionsBorder" id="oListMoreActionsBorder1"></div>');
                            this.down('td').insert('<div class="oListMoreActionsBorder" id="oListMoreActionsBorder2"></div>');
                            
                            var left, width, height;
                            if ( isIE7 ) {
                                left = 1;
                                height = this.down('td').getHeight() - 1;
                            } else if ( Prototype.Browser.IE ) {
                                left = 0;
                                height = this.getHeight() - 2;
                            } else if ( Prototype.Browser.WebKit ) {
                                left = 0;
                                height = this.getHeight() - 1;
                            } else {
                                left = 1;
                                height = this.getHeight() - 2;
                            }
                            width = this.getWidth();
                            
                            var pos = this.positionedOffset();
                            
                            if ( rtl ) {
                                this.down('.oListActions').setStyle({
                                    left:'0px',
                                    top:( pos.top + height ) + 'px'
                                });
                            } else {
                                this.down('.oListActions').setStyle({
                                    right:'0px',
                                    top:( pos.top + height ) + 'px'
                                });
                            }
                            
                            $('oListMoreActionsBorder1').setStyle({
                                top:( pos.top - 1 ) + 'px',
                                left:( pos.left - left ) + 'px'
                            });
                            $('oListMoreActionsBorder2').setStyle({
                                top:( pos.top + height ) + 'px',
                                left:( pos.left - left ) + 'px'
                            });
                            
                            this.addClassName('oListRowShouldOpen');
                            
                        } else {
                            $$('.oListRowOpen').each(function(er){ er.removeClassName('oListRowOpen'); });
                            
                        }
                        
                    });
                    
                    tr.observe('mouseup',function(event){
                        if ( IsClickable( Event.findElement(event) ) ) return;
                        
                        if ( this.hasClassName('oListRowShouldOpen') ) {
                            this.removeClassName('oListRowShouldOpen');
                            this.addClassName('oListRowOpen');
                        } else {
                            $$('.oListMoreActionsBorder').each(function(er){ er.remove(); });                            
                        }
                        
                    });
                    
                }
            }
		});
	});
    $$('.oListInForm').each(function(el){
        if ( el.down('.oListInFormActivator') ) {
            el.addClassName('oListInFormActive');
        } else {
            el.removeClassName('oListInFormActive');
        }
    });
    $$('.oListClickable').each(function(el){
		if ( !el.processed ) {
			el.observe('click',function(event){
                if ( el.hasClassName('oNoToggle') ) {
                    el.addClassName('oListRowSelected');
                } else {
                    el.toggleClassName('oListRowSelected');
                }
                if ( !el.hasClassName('oMultiple') ) {
                    el.siblings().each(function(s){
                        s.removeClassName('oListRowSelected');
                    });
                }
			});
			el.processed = true;
		}
    });

    // new list action manager
    $$('.oListComponent .oListRow0,.oListComponent .oListRow1').each(function(row){
    	if ( row.proc ) return;
    	row.proc = true;
		var actions = row.down('.oActions');
		if (row.hasClassName('RowActions') && row.hasClassName('Open') && !actions.hasClassName('Active'))
		{
			var pos = row.positionedOffset();
			row.addClassName('Open');
			actions.addClassName('Active');
			actions.setStyle({
				top:(pos[1] + row.getHeight() - actions.getHeight() - 7) + 'px'
			});
		}
    	row.observe('click',function(event){
            if ( IsClickable( Event.findElement(event) ) ) return;
			if ( !row.hasClassName('RowActions') )
			{
				if ( row.hasClassName('MultiSelect') ) {
					row.toggleClassName('Selected');
				} else if ( row.hasClassName('SingleSelect') ) {
					if ( row.hasClassName('Selected') ) {
						// find parent, the remove other selections
						row.up('.oListComponent').select('.oListRow0.Selected, .oListRow1.Selected').each(function(other){
							other.removeClassName('Selected');
						});
					} else {
						// find parent, the remove other selections
						row.up('.oListComponent').select('.oListRow0.Selected, .oListRow1.Selected').each(function(other){
							other.removeClassName('Selected');
						});
						// set selection
						row.addClassName('Selected');
					}
				}
			}
    		var actions = row.down('.oActions');
    		if ( actions ) {
	    		if ( actions.hasClassName('Active') || row.hasClassName('Open') ) {
					row.removeClassName('Open');
	    			actions.removeClassName('Active');
	    		} else {
					$$('.oListComponent .oActions.Active').each(function(off){
						off.removeClassName('Active');
					});
					$$('.oListComponent .Open').each(function(off){
						off.removeClassName('Open');
					});
                    var pos = row.positionedOffset();
					row.addClassName('Open');
		    		actions.addClassName('Active');
                    if ( row.hasClassName('RowActions') ) {
                        actions.setStyle({
                            top:(pos[1] + row.getHeight() - actions.getHeight() - 7) + 'px'
                        });
                    } else {
                        actions.setStyle({
                            top:(pos[1] + row.getHeight()) + 'px'//toggle.getHeight() + 4) + 'px'
                        });
                    }
	    		}
    		}
    	});
    });
	
};

Event.observe(document, 'dom:loaded', window.ExamQube_Forms_Load.bind(true));
Event.observe(document, 'SMAjax:CallBack', window.ExamQube_Forms_Load.bind(false));
