jQuery.noConflict(); 

jQuery(document).ready(function(jQuery) {


jQuery.noConflict(); 

// form validation
jQuery.validationEngineLanguage.newLang();

jQuery('form').validationEngine({
	success		: function() { document.forms[ jQuery(this).attr("id") ].submit(); },
	failure		: function() {}
});

// administration menus
jQuery('.crm-admin-menu .menu-panel').hide();

jQuery('.crm-admin-menu .menu-header-').collapser({
	target: 'next',
	effect: 'slide',
	changeText: 0,
	expandClass: 'expIco',
	collapseClass: 'collIco'
}, function(){
	jQuery('.crm-admin-menu .menu-panel').slideUp(200);
});

// open the menu marked .o
jQuery('.crm-admin-menu li.current').parent().parent().show();

jQuery('.crm-admin-menu .menu-header')
	.button()
	.click(function() {
		jQuery( this ).next().click(); // show the menu
		/*jQuery( this ).next().next().find( 'ul li a:first' ).each( function() { 
								jQuery(this).trigger('click'); }); // click the first item*/
	})
	.next()
		.button( {
			text: false,
			icons: {
				primary: "ui-icon-triangle-1-s"
			}
		})
		.click(function() {
			//alert( 'arrow' );
		})
		.parent()
			.buttonset();
			

// targeted, collapsable divs
// set the targetID id in the target attribute of the selectable element
jQuery('.collapsable').each( function() {
	
	var t = '.collapsable-target-'+jQuery(this).attr('targetID');
	jQuery(this).collapser({ 
		target: t,
		effect: 'slide',
		changeText: false,
		expandClass: 'expIco',
		collapseClass: 'collIco'
	});
	jQuery(this).addClass((jQuery(t).is(':hidden'))?'expIco':'collIco'); // hack, becuase the plugin doesn't init properly.. must fix it later.
});

jQuery('.radio-container').buttonset();
jQuery('.checkbox-container').buttonset();


// Select dropdown replacement
jQuery('.ns-fancy-select').each( function() {
	
	var source = jQuery(this); 
    var selected = source.find('option:selected');
    var options = jQuery('option', source);
    var newDD_id = "fancy-dropdown-"+Math.floor(Math.random()*100+1);
    
    source.before('<dl id="'+newDD_id+'" class="ns-fancy-dropdown"></dl>');
    jQuery('#'+newDD_id).append('<dt><a href="javascript:void(0);">' + selected.text() + 
        '<span class="value">' + selected.val() + 
        '</span></a></dt>');
    jQuery('#'+newDD_id).append('<dd><ul></ul></dd>');
	
    options.each( function() {
        jQuery('#'+newDD_id+' dd ul').append('<li><a href="javascript:void(0);">' + 
            jQuery(this).text() + '<span class="value">' + 
            jQuery(this).val() + '</span></a></li>');
    });
        
    jQuery('#'+newDD_id+'.ns-fancy-dropdown dt a').click( function() {
	    jQuery('#'+newDD_id+'.ns-fancy-dropdown dd ul').toggle();
	});
	
	jQuery(document).bind( 'click', function(e) {
	    var $clicked = jQuery(e.target);
	    if (! $clicked.parents().hasClass('ns-fancy-dropdown'))
	        jQuery('#'+newDD_id+'.ns-fancy-dropdown dd ul').hide();
	});
	
	jQuery('#'+newDD_id+'.ns-fancy-dropdown dd ul li a').click( function() {
	    var text = jQuery(this).html();
	    jQuery('#'+newDD_id+'.ns-fancy-dropdown dt a').html(text);
	    jQuery('#'+newDD_id+'.ns-fancy-dropdown dd ul').hide();
	    
	    source.val( jQuery(this).find('span.value').html() );
	});
	
	source.addClass('hidden');

});

// link buttons
jQuery('a.link-button').button();

// set the position of the curser in an input/textarea
/*jQuery.fn.setCursorPosition = function(pos) {
	this.each(function(index, elem) {
		if (elem.setSelectionRange) {
			elem.setSelectionRange(pos, pos);
		} else if (elem.createTextRange) {
			var range = elem.createTextRange();
			range.collapse(true);
			range.moveEnd('character', pos);
			range.moveStart('character', pos);
			range.select();
		}
	});
	return this;
};
*/

jQuery('.action-notify').highlightFade({color:'#DCFAC9',speed:4000,iterator:'sinusoidal'}); //#FF0
jQuery('.action-important').highlightFade({color:'red',speed:4000,iterator:'sinusoidal'});

jQuery( 'table.sortable' ).tablesorter(); 


        
}); //doc ready




        
