
(function($){
    $(document).ready(function(){
        $('.langswitch.jsswitch').each(function(){
            var btns = $('<div></div>');
            $(btns).addClass('buttons');
            $(this).prepend(btns);
	        $(this).find('.langbox').each(function(){
	            var btn = $('<div></div>');
	            btn.addClass('button');
	            
	            var rel = $(this).attr('class');
	            btn.attr("rel",rel)
	            var matchLang = rel.match(/langbox_([a-z]+)/);
	            if(matchLang != null){
	                btn.attr("style","background-image: url('/img/icons/flags/"+matchLang[1]+".png');");
	            }
	            
	            $(btns).append(btn);
	            btn.click(function(){
	                $(this).parent().find('.active').removeClass('active');
	                $(this).addClass('active');
	                $(this).parent().parent().find('.langbox').hide();
	                
	                var findClass = $(this).attr('rel');
	                $(this).parent().parent().find('.langbox').each(function(){
	                    if($(this).attr('class')==findClass){
	                        $(this).show();
	                    }
	                });
	            });
	        });	        
	        $(this).find('.langbox').hide();
	        $(this).find('.langbox').first().show();
	        $(this).find('.buttons .button').first().addClass('active');
	        $(btns).append($('<div class="clearer"></div>'));
	    });
    });
	
})(jQuery);
