/* -------------------------------------------------------
	SweetDropDown v.1.2 (Oct.5 2007)
	Developped by: Stephane Caron
	Comments: For my Longueuil homies!
-------------------------------------------------------- */

$(document).ready( function() {
	// Init the drop down with the initial drop down value and the speed at which you want to drop downs to open
	// Params. (initial text, speed (fast, slow, normal, 100, 200, ...), opacity)
	sweetDropDown.init('Select',1,1);
});

sweetDropDown = {
	init : function(defaultText, speed, opacity) {
		// Get the desired speed
		(speed !=  undefined) ? sweetDropDown.speed = speed : sweetDropDown.speed = 'fast';
		
		// Get the desired opacity
		(opacity != undefined) ? sweetDropDown.opacity = opacity : sweetDropDown.opacity = 1;
		
		// Set the default text
		if(defaultText){ sweetDropDown.defaultText  = defaultText; }else{ sweetDropDown.defaultText = "&nbsp;"; }

		// Build the drop downs here
		$('select').each(function(i){ sweetDropDown.buildDropDown($(this), speed); });
		
		// Acts like a blur event on the drop down, need to close the opened dropdown
		$(document).bind('click', function(){
			if(sweetDropDown.openedDropdown){ sweetDropDown.openedDropdown.slideUp(sweetDropDown.speed); }
		});
		
		// Add the event to control the dropDowns with arrows
		$('div.sweetDropDown').bind('keypress', function(e) {
			// track last key pressed
			lastKeyPressCode = e.keyCode;
			switch(e.keyCode) {
				case 38: // up
					if(sweetDropDown.selectedElement){
						sweetDropDown.moveSelect(sweetDropDown.selectedElement, -1);
						e.preventDefault();
					}
					break;
				case 40: // down
					if(sweetDropDown.selectedElement){
						sweetDropDown.moveSelect(sweetDropDown.selectedElement, 1);
						e.preventDefault();
					}
					break;
				case 13: // return
					if(sweetDropDown.selectedElement){
						e.preventDefault();
						sweetDropDown.select(sweetDropDown.selectedItem, 0);
						$(sweetDropDown.selectedElement).css('color',sweetDropDown.initialColor);
					}
					break;
				case 9: // tab
					if(sweetDropDown.selectedElement){
						sweetDropDown.select(sweetDropDown.selectedItem);
						$(sweetDropDown.selectedElement).css('color',sweetDropDown.initialColor);
					}
					break;
			}
		});
	},
	
	buildDropDown : function(elementToReplace, speed) {
		
		// Main div for the drop down
		myDiv = document.createElement('div');
		$(myDiv).addClass('sweetDropDown');
		
		// Add the select class
		if($(elementToReplace).attr('class')) $(myDiv).addClass($(elementToReplace).attr('class'));
		
		// Link used as the dropdown label
		mySelectedA = document.createElement('a');
		$(mySelectedA).attr({
			'href': '#',
			'tabindex': $(elementToReplace).attr('tabindex')
		});
		$(mySelectedA).addClass('arrow');
		$(mySelectedA).width($(elementToReplace).width());
		$(mySelectedA).click(function(){
			sweetDropDown.toggle(this);
			return false;
		});
		$(mySelectedA).focus(function(){
			sweetDropDown.selectedElement = $(this);
			sweetDropDown.initialColor = $(this).css('color');
			$(this).css('color','#000');
		});
		$(mySelectedA).blur(function(){
			$(this).css('color',sweetDropDown.initialColor);
		});
		
		$(mySelectedA).html(sweetDropDown.defaultText);
		
		// Input keeping the selected item value
		myHiddenInput = document.createElement('input');
		$(myHiddenInput).attr({
			'type' : 'hidden',
			'id' : $(elementToReplace).attr('id'),
			'name' : $(elementToReplace).attr('name'),
			'value' : $(elementToReplace).attr('')
		});

		// Building of the dropdown options.
		myUl = document.createElement('ul');
		$(myUl).css('opacity', sweetDropDown.opacity);

		elementToReplace.find('option').each(function(i){
			myLi = document.createElement('li');
			myA = document.createElement('a');
			$(myA).attr({ 'href' : '#', 'rel' : this.value, 'title' : $(this).text() });
			$(myA).click(function(){
				sweetDropDown.select(this);
				return false;
			});
			$(myA).text($(this).text());
			$(myLi).append(myA);
			$(myUl).append(myLi);
		});

		// Append all elements, then replace the old (ugly) dropdowns.
		$(myDiv).append(myHiddenInput); $(myDiv).append(mySelectedA); $(myDiv).append(myUl);
		$(myDiv).insertAfter($(elementToReplace));
		elementToReplace.remove();
		
		// Fix the dropdown width in IE
		$(myUl).width(parseFloat($(mySelectedA).css('width')) + 30);
	},

	toggle : function(caller){		
		// Close the previously opened dropdown and down their z-index
		sweetDropDown.close(caller);
		
		// Remove all the hover classes (used when selecting with arrows)
		$(caller).parent().find('ul li a').removeClass('hover');

		// Open the selected dropdown
		$(caller).parent().css('z-index',10000);
		$(caller).parent().find('ul:eq(0)').animate({'height': 'toggle'}, sweetDropDown.speed);
		sweetDropDown.openedDropdown = $(caller).parent().find('ul');
	},

	moveSelect : function(selectedItem, direction){
		// Open the dropDown if it's closed
		if(selectedItem.next().css('display') == "none"){
			sweetDropDown.currentHighlight = -1;
			$(selectedItem).parent().css('z-index',10000);
			$(selectedItem).next().animate({'height': 'show'}, sweetDropDown.speed);
			
			$(selectedItem).parent().find('ul li a').removeClass('hover');
			sweetDropDown.openedDropdown = $(selectedItem).next();
		}


		maxSelected = $(selectedItem).next().find('li').length;
		if(direction == 1 && sweetDropDown.currentHighlight < maxSelected){
			sweetDropDown.currentHighlight ++;
		}else if(direction == -1 && sweetDropDown.currentHighlight >= 0){
			sweetDropDown.currentHighlight --;
		}
		
		if(sweetDropDown.currentHighlight < maxSelected && sweetDropDown.currentHighlight >= 0){
			$(selectedItem).parent().find('ul li:eq('+ sweetDropDown.currentHighlight +')').find('a').addClass('hover');
			sweetDropDown.selectedItem = $(selectedItem).parent().find('ul li:eq('+ sweetDropDown.currentHighlight +')').find('a');
		
			if(direction == 1){ $(selectedItem).parent().find('ul li:lt('+ sweetDropDown.currentHighlight +')').find('a').removeClass('hover'); }
			if(direction == -1){ $(selectedItem).parent().find('ul li:gt('+ sweetDropDown.currentHighlight +')').find('a').removeClass('hover'); }
		}
	},

	select : function(caller){
		// Set the selected text
		$(caller).parent().parent().parent().find('a.arrow').each(function(){ $(this).text($(caller).text()) })

		// Change the hidden field value
		$(caller).parent().parent().parent().find('input[@type=hidden]').attr('value', $(caller).attr('rel'));

		// Close the drop down
		sweetDropDown.close(caller);
		
		sweetDropDown.openedDropdown = false;
	},
	close : function(caller){
		if(sweetDropDown.openedDropdown && (sweetDropDown.openedDropdown.html() != $(caller).parent().find('ul:eq(0)').html())){
			$(sweetDropDown.openedDropdown).parent().css('z-index',0);
			$(sweetDropDown.openedDropdown).slideUp(sweetDropDown.speed);
			sweetDropDown.openedDropdown = false;
		}
	},
	open : function(caller){
		sweetDropDown.currentHighlight = 0;
		
		// Open the selected dropdown
		$(caller).parent().css('z-index',10000);
		$(caller).parent().find('ul:eq(0)').animate({'height': 'show'}, sweetDropDown.speed);
		sweetDropDown.openedDropdown = $(caller).parent().find('ul');
	}
}