﻿$(document).ready(function () {

//SMARTSEL

	$("select[class^=SmartSel]").each(function () {
		var id = $(this).attr("name");
		$(this).before("<div id=\"SmartSel_id_" + id + "\" class=\"SmartSel_drop\"></div>");
		$(this).children("option").each(function(index) {
			var t = $(this).text();
			var v = $(this).val();
			var sltd = $(this).attr('selected') ? ' sel' : '';
			var sub = $(this).hasClass('sub') ? ' SmartSel_opt_sub' : '';
			if (t || v) {
				$("#SmartSel_id_"+id).append("<div id=\"SmartSel_opt_" + id + "_" + index + "\" class=\"SmartSel_opt" + sltd + sub + "\">" + t + "</div>");
			}
		});
	});
	$("select[class^=SmartSel]").change(function () {
		var id = $(this).attr("name");
		var s='';
		$(this).children("option").each(function(index) {
			if ( $(this).attr('selected') ) {
				s += $(this).text() + ", ";
				$(this).parent().siblings(".SmartSel_drop").children("#SmartSel_opt_"+id+"_"+index).addClass("sel");
			} else {
				$(this).parent().siblings(".SmartSel_drop").children("#SmartSel_opt_"+id+"_"+index).removeClass("sel");
			}
		});
		s = s.replace(/,\s*$/, "");
		if (s.length > 40) {
			s = s.substring(0,40) + '...';
		}
		$(this).siblings("div.SmartSel_field:first").children("span:first").text(s);
	});
	$("[id^='SmartSel_opt']").click(function () {
		var o = $(this);
		var re = /(\d+)$/;
		var index = re.exec(o.attr('id'))[0];
		var sel = o.parents(".SmartSel_box:first").children("select[class^=SmartSel]:first");
		if (!sel.attr('multiple')) {
			sel.children("option").removeAttr('selected');
			o.parents(".SmartSel_drop").children().removeClass("sel");
		}
		if (sel.children("option:eq("+index+")").attr('selected') && sel.attr('multiple')) {
			sel.children("option:eq("+index+")").removeAttr('selected');
			o.removeClass("sel");
		} else {
			sel.children("option:eq("+index+")").attr('selected', '1');
		}
		sel.trigger('change');
		if (!sel.attr('multiple')) {
			o.parent(".SmartSel_drop").css('display','none');
		}
	});
	$("select[class^=SmartSel]").css("display", "none");
//	$("select[class^=SmartSel]").trigger('change');
	$(".SmartSel_field").click(function() {
		$(this).parent(".SmartSel_box").children(".SmartSel_drop").slideToggle();
	});

//SMARTRANGE
	
	var R = ['beg', 'end'];
	for (var r in R) {
		$("select.SmartRange_"+R[r]).each(function () {
			$(this).children("option").each(function () {
				var t = $(this).text();
				var v = $(this).val();
				var sltd = $(this).attr('selected') ? ' sel' : '';
				$(this).parents(".SmartRange_box:first").find(".SmartRange_"+R[r]+"_box").append("<div id=\"SmartRange_opt_" + R[r] + "_" + v + "\" class=\"smartrange_ SmartRange_opt_" + R[r] + sltd + "\">" + t + "</div>");
			});
		});
	}
	$("select[class^=SmartRange]").change(function () {
		var b='';
		var e='';
		var from_word = $(this).parents(".SmartRange_box:first").find(".SmartRange_beg_box").children(".smartrange_title").text();
		var to_word = $(this).parents(".SmartRange_box:first").find(".SmartRange_end_box").children(".smartrange_title").text();
		var from_;
		var to_;
		var p = $(this).parents(".SmartRange_box:first");
		p.find("select.SmartRange_beg option:selected").each(function () {
			from_ = parseInt($(this).val());
			b = from_ ? from_word + ' ' + $(this).text() : '';
		});
		p.find("select.SmartRange_end option:selected").each(function () {
			to_ = parseInt($(this).val());
			e = to_ ? to_word + ' ' + $(this).text() : '';
		});
		if (from_) {
			p.find("select.SmartRange_end option").each(function () {
				var v = parseInt($(this).val());
				if (v && v < from_) {
					$(this).attr('disabled', '1');
				} else {
					$(this).removeAttr('disabled');
				}
				if ((v <= to_ && v >= from_) || (v >= from_ && !to_)) {
					$(this).addClass('cur');
				} else {
					$(this).removeClass('cur');
				}
			});
		} else {
			p.find("select.SmartRange_end option").removeAttr('disabled');
			p.find("select.SmartRange_end option").removeClass('cur');
		}
		if (to_) {
			p.find("select.SmartRange_beg option").each(function () {
				var v = parseInt($(this).val());
				if (v && v > to_) {
					$(this).attr('disabled', '1');
				} else {
					$(this).removeAttr('disabled');
				}
				if ((v <= to_ && v >= from_) || (v <= to_ && !from_)) {
					$(this).addClass('cur');
				} else {
					$(this).removeClass('cur');
				}
			});
		} else {
			p.find("select.SmartRange_beg option").removeAttr('disabled');
			p.find("select.SmartRange_beg option").removeClass('cur');
		}
		$("select[class^=SmartRange] option").each(function () {
			var d = $(this).parent().attr('class').substring(11);
			var o = $(this).parents(".SmartRange_box:first").find("#SmartRange_opt_"+d+"_"+$(this).val());
			if ($(this).attr('disabled')) {
				o.addClass('dis');
			} else {
				o.removeClass('dis');
			}
			if ($(this).hasClass('cur')) {
				o.addClass('cur');
			} else {
				o.removeClass('cur');
			}
		});
		var s = b + ' ' + e;
		$(this).siblings("div.SmartRange_field:first").children("span:first").text(s);
	});
	$("[id^='SmartRange_opt']").click(function () {
		if (!$(this).hasClass('dis')) {
			var o = $(this);
			var v = o.attr('id').substring(19);
			var d = o.attr('id').substring(15,18);
			var sel = o.parents(".SmartRange_box:first").children("select[class^=SmartRange_"+d+"]:first");
			sel.children("option").removeAttr('selected');
			o.parent(".SmartRange_"+d+"_box").children(".SmartRange_opt_"+d).removeClass('sel');
			o.addClass('sel');
			sel.children("[value='"+v+"']").attr('selected', '1');
			sel.trigger('change');
		}
	});
	$("select[class^=SmartRange]").css("display", "none");
//	$("select[class^=SmartRange]").trigger('change');
	$(".SmartRange_field").click(function() {
		$(this).parent(".SmartRange_box").children(".SmartRange_drop").slideToggle();
	});

//SMARTHREF
	$(".SmartHref_field").click(function() {
		$(this).parent(".SmartHref_box").children(".SmartHref_drop").slideToggle();
	});


//ALLSMARTS
	
	$(document).click(function() {
		$(".SmartRange_drop,.SmartSel_drop,.SmartHref_drop").css("display", "none");
	});
	$(".SmartRange_box,.SmartSel_box,.SmartHref_box").click(function(event){
		event.stopPropagation();
	});

	
});

