$(document).ready(function(){
	
	// The select element to be replaced:
	var select = $('select.makeMeFancy');

	// Auto select dropdown value based on
	// language already selected.
	var pathNameUrl = window.location.pathname;
	if (pathNameUrl.toLowerCase().indexOf("chinese") > 0) {
		select.val(1);
	} else if (pathNameUrl.toLowerCase().indexOf("french") > 0) {
		select.val(2);
	} else if (pathNameUrl.toLowerCase().indexOf("portuguese") > 0) {
		select.val(3);
	} else if (pathNameUrl.toLowerCase().indexOf("spanish") > 0) {
		select.val(4);
	} else {
		select.val(0);
	}

	var selectBoxContainer = $('<div>',{
		width		: select.outerWidth(),
		className	: 'tzSelect',
		html		: '<div class="selectBox"></div>'
	});

	var dropDown = $('<ul>',{className:'dropDown'});
	var selectBox = selectBoxContainer.find('.selectBox');
	
	// Looping though the options of the original select element
	
	select.find('option').each(function(i){
		var option = $(this);
		
		if(i==select.attr('selectedIndex')){
		    selectBox.html('<img style="width: 16px; height: 16px; margin-right: 5px; position:relative; top: 2px;" src="' + option.data('icon') + '" />' + option.text());
		}
		
		// As of jQuery 1.4.3 we can access HTML5 
		// data attributes with the data() method.
		
		if(option.data('skip')){
			return true;
		}
		
		// Creating a dropdown item according to the
		// data-icon and data-html-text HTML5 attributes:
		
		var li = $('<li>',{
			html: '<img src="'+option.data('icon')+'" /><span>' + option.data('html-text')+'</span>'
		});
				
		li.click(function(){

		    selectBox.html('<img style="width: 16px; height: 16px; margin-right: 5px; position:relative; top: 2px;" src="' + option.data('icon') + '" />' + option.text());
			dropDown.trigger('hide');
			
			// When a click occurs, we are also reflecting
			// the change on the original select element:
			select.val(option.val());

			var language = selectBox.text();
			if (language == "English") language = "";
			document.location.href = '/' + language;
			
			return false;
		});
		
		dropDown.append(li);
	});
	
	selectBoxContainer.append(dropDown.hide());
	select.hide().after(selectBoxContainer);
	
	// Binding custom show and hide events on the dropDown:
	
	dropDown.bind('show',function() {
		
		if(dropDown.is(':animated')){
			return false;
		}
		
		selectBox.addClass('expanded');
		dropDown.show();
		
	}).bind('hide',function(){
		
		if(dropDown.is(':animated')) {
			return false;
		}
		
		selectBox.removeClass('expanded');
		dropDown.hide();
		
	}).bind('toggle',function(){
		if(selectBox.hasClass('expanded')) {
			dropDown.trigger('hide');
			
			//window.location.href='';
			//alert(option.val());
		} else {
			dropDown.trigger('show');
		}
	});
	
	selectBox.click(function() {
		//dropDown.trigger('toggle');
		alert('Coming Soon!');
		return false;
	});

	// If we click anywhere on the page, while the
	// dropdown is shown, it is going to be hidden:
	$(document).click(function(){
		dropDown.trigger('hide');
	});
});
