$(document).ready(function(){
	var valuesheight = 200;
	var offset = $(this).offset();
	
	

	
	$('.f_loop .input_select_wrapper').each(function(index) {
	

	
	if ($(this).find('select').attr('id') != '') {
		var input_select_id = $(this).find('select').attr('id');
	}
	if ($(this).find('select').hasClass('f_select_small')) {
		var input_select = ' input_select_small';
		var input_select_inner = ' input_select_inner_small';
		var input_select_active = ' input_select_active_small';
		var input_select_btn = ' input_select_btn_small';
		var input_select_values = ' input_select_values_small';
	} else {
		var input_select = '';
		var input_select_inner = '';
		var input_select_active = '';
		var input_select_btn = '';
		var input_select_values = '';
	}
	/*
	$(this).children().map(function(){
		var values = $(this).val();
	});
	*/
	
	var firstval = $(this).find('select option:selected').html();
	//alert($(this).attr('style'));
	
	//alert(this+" option:selected");
	
	
	
	var foo = [];
	$('.f_loop .input_select_wrapper select option').each(function(i, selected){
		foo[i] = '<div rel="'+$(selected).val()+'">'+$(selected).text()+'</div>';
	});

	$(this).find('select').hide();
	$(this).find('select').parent().prepend('<div id="'+input_select_id+'" class="input_select'+input_select+'"><div class="input_select_inner'+input_select_inner+'"><div class="input_select_active'+input_select_active+'"><div class="input_select_active_inner">'+firstval+'</div></div><div class="input_select_btn'+input_select_btn+'">&nbsp;</div><div class="f_clear"></div></div></div>');
	$(this).find('select').parent().append('<div class="input_select_values'+input_select_values+'"><div class="input_select_values_inner"></div></div>');
	
	var selectwidth = $(this).find('select').width();
	$(this).find('.input_select_values').css('width', selectwidth);
	
	var selectwidthbtn = $(this).find('.input_select_btn').width();
	var selectwidthactive = selectwidth-selectwidthbtn;
	$(this).find('.input_select_active').css('width', selectwidthactive);
	
	
	
	/*
	$('.f_loop .input_select_wrapper select option').each(function(i, option) {
		$('.input_select_values').append('<div rel="'+option.value+'">'+option.text+'</div>');
	});
	*/
	$(this).find('select option').each(function(i, option) {
		if ($(option).attr('style') != undefined) {
			var styleattr = 'lang="'+$(option).attr('style')+'" ';
		} else {
			var styleattr = '';
		}
		$(this).parent().parent().find('.input_select_values_inner').append('<div '+styleattr+'rel="'+option.value+'">'+option.text+'</div>');
	});
		
	$('.input_select_values_inner div').mouseover(function(){
		$(this).addClass('input_select_values_active');
	});
		
	$('.input_select_values_inner div').mouseleave(function(){
		$(this).removeClass('input_select_values_active');
	});

	if ($(this).find('.input_select_values').height() > valuesheight) {
		$(this).find('.input_select_values').css('height', valuesheight+'px');
	}
	});
	
	$('.input_select_values_inner div').click(function(){
		$(this).parent().parent().parent().find('.input_select_values').slideToggle("fast");
		$(this).parent().parent().parent().find('.input_select_values').toggleClass("input_select_values_open");
		$(this).parent().parent().parent().find('.input_select_active_inner').html($(this).html());
			
		var thisvalue = $(this).attr('rel');

		$(this).parent().parent().parent().find('select option[value="'+thisvalue+'"]').attr('selected', 'selected');
		
		$(this).parent().parent().parent().find('select').change();
		
	});
	

	
	$('.input_select_inner').click(function(){
		$(this).parent().parent().find('.input_select_values').slideToggle("fast");
		$(this).parent().parent().find('.input_select_values').toggleClass("input_select_values_open");
		//var thisvalue2 = $(this).parent().parent().find('select option:selected').html();
	});
	
	
	
	// Checkbox
	$('.f_loop input:checkbox').each(function(index) {
		$(this).css('display', 'none');
		$(this).parent().css('padding', '0px');
		$(this).parent().css('background', 'none');
		$(this).parent().append('<div class="label_checkbox"></div>');
		if ($(this).attr('checked') == 'checked' || $(this).attr('checked') == true) {
			$(this).parent().find('.label_checkbox').addClass('label_checkbox_active');
		}
		$(this).parent().find('.label_checkbox').click(function(){
			value = $(this).parent().find('input').attr('value');
			$(this).toggleClass('label_checkbox_active');
			var aktvalue = $(this).parent().find('input').attr('checked');
			if (aktvalue == true) {
				var newvalue = false;
			}
			if (aktvalue == false) {
				var newvalue = true;
			}
			if ( $.browser.msie || $.browser.opera ) {
				$(this).parent().find('input').attr('checked', newvalue);
			} else {
				$(this).parent().find('input').attr('value', value);
			}
		});
	});
	
	// Radio
	$('.f_loop input:radio').each(function(index) {
		$(this).parent().wrap('<div class="label_wrapper"></div>');
		$(this).css('display', 'none');
		$(this).parent().css('padding', '0px');
		$(this).parent().css('background', 'none');
		$(this).parent().append('<div class="label_radio"></div>');
		if ($(this).attr('checked') == 'checked' || $(this).attr('checked') == true) {
			$(this).parent().find('.label_radio').addClass('label_radio_active');
			$(this).parent().parent().addClass('label_wrapper_active');
		}
		$(this).parent().find('.label_radio').click(function(){
			thisname = $(this).parent().find('input').attr('name');
			thisid = $(this).parent().find('input').attr('id');
			//alert(thisid);
			$('input[name="'+thisname+'"]').parent().find('.label_radio').removeClass('label_radio_active');
			$('input[name="'+thisname+'"]').parent().parent().removeClass('label_wrapper_active');
			$(this).toggleClass('label_radio_active');
			$(this).parent().parent().toggleClass('label_wrapper_active');
			//$(this).parent().find('input[name="'+thisname+'"]').attr('value', '');
			$(this).parent().find('input').attr('checked', ':checked');
		});

	});
	
	// File
	$('.f_loop input[type=file]').each(function(index) {
		//alert($(this).val());
		//$(this).css('visibility', 'hidden');
		//$(this).css('position', 'absolute');
		$(this).addClass('inputfile');
		$(this).wrap('<div class="input_file"></div>');
		$(this).parent().prepend('<div class="input_file_txt"><div class="input_file_txt_inner"></div></div>');
		$(this).parent().prepend('<div class="input_file_btn"></div>');
		$(this).parent().prepend('<div class="clear"></div>');
		var widthbtn = $(this).parent().find('.input_file_btn').width();
		var widthinputfile = $(this).width();
		var widthinputfilenew = widthinputfile-widthbtn+6;
		$(this).parent().find('.input_file_txt').css('width', widthinputfilenew);
		$(this).parent().find('.input_file_txt_inner').html($(this).parent().find('input[type=file]').val());
		
		
		if ( $.browser.msie || $.browser.opera ) {
			
			$(this).parent().find('input[type=file]').change(function() {
				$(this).parent().find('.input_file_txt_inner').html($(this).parent().find('input[type=file]').val());
			});
			
			
			
		} else if ($.browser.chrome) {
			$(this).parent().find('input[type=file]').choose(function() {
				$(this).parent().find('.input_file_txt_inner').html($(this).parent().find('input[type=file]').val());
			});
		} else {
			$(this).parent().find('input[type=file]').change(function() {
				$(this).parent().find('.input_file_txt_inner').html($(this).parent().find('input[type=file]').val());
			});
		}
	});
});
