var opening, over;
opening = false;
over = false;
function jumpto(x){
	document.location.href = x
}


(function($) {
	/*$.fn.unselectable: function () {
        if (typeof $(this).onselectstart != 'undefined') {
            $(this).addEvent('selectstart', function () {
                return false;
            });
        } else if (typeof $(this).style.MozUserSelect != 'undefined') {
            $(this).setStyle('MozUserSelect', 'none');
        } else if (typeof $(this).style.WebkitUserSelect != 'undefined') {
            $(this).setStyle('WebkitUserSelect', 'none');
        } else if (typeof $(this).unselectable != 'undefined') {
            $(this).setProperty('unselectable', 'on');
        }
    }*/
	
	$.fn.comboReplace = function() {
		this.each(function(i,a){
			var optEls, cmb, sel, opts, onchg, arrow;
			onchg = $(a).attr('onchange');
			optEls = $(a).find('option');
			cmb = $('<div class="select"></div>');
			sel = $('<div class="sel"></div>');
			arrow = $('<div class="arrow"></div>');
			opts = $('<div class="opts"></div>');
			sel.appendTo(cmb);
			arrow.appendTo(cmb);
			opts.appendTo(cmb);
			cmb.appendTo($(a).parent());
			optEls.each(function(j,b){
				var flag, opt, img, tit;
				flag = './img/flags/' + $(b).attr('value').replace(/\//g,'') + '.png';
				if($(b).attr('value') != '#'){
				    img = ''
					//img = '<img src="' + flag + '" alt="' + tit + '" title="' + tit + '" />';
				}else{
					img = ''
				}
				tit = $(b).text();
				opt = $('<div class="option">' + img + '<span>' + tit + '</span></div>');
				opt.appendTo(opts);
				if($(b).attr('selected') == 'selected'){
					$(img + '<span>' + tit + '</span>').appendTo(sel);
				}
				$(opt).click(function(){
					$(this).parent().parent().find('.sel').html($(this).html());
					$(this).parent().parent().trigger('mouseleave');
					jumpto($(b).attr('value'));
				});
			});
			
			$(arrow).click(function(){
				if($(this).parent().find('.opts').height() > 0){
					$(this).parent().trigger('mouseleave');
					$(this).removeClass('activeArrow');
				}else{
					$(this).parent().trigger('mouseenter');
					$(this).addClass('activeArrow');
				}

			});

			$(cmb).mouseenter(function(){
				opening = true;
				over=true;
				var h, cant;
				cant = $(this).find('.opts .option').length;
				h = cant * $(this).find('.opts .option').height();
				$(this).find('.opts').animate({'height': h + 'px','border-width':'1px'},{duration:750,complete:function(){opening=false;if(!over){$(this).find('.opts').animate({'height':'0px','border-width':'0px'});$(this).find('.arrow').removeClass('activeArrow');}}.bind(this)});
				$(this).find('.arrow').addClass('activeArrow');
			});

			$(cmb).mouseleave(function(){
				if(!opening){
					$(this).find('.opts').animate({'height':'0px','border-width':'0px'});
					$(this).find('.arrow').removeClass('activeArrow');
				}
				over=false;
			});	
			
			$(a).remove();			
		});
	}
})(jQuery);

$(document).ready(function() { 
	$('select').comboReplace();
});
