var siteUrl = "http://www.webmed.ru/";
var searchPhrase = "Поиск";
var nodeAttrs = {};

$(function() {
	$("input:button, input:submit").button();
	$("#collapse").button({
		icons: {
        	primary: "ui-icon-triangle-1-e"
    	}
	});
	$("#collapse").click(function() {
		if($("#collapsible:hidden").length > 0) {
			$(this).button( "option", "icons", {primary:'ui-icon-triangle-1-s'} );
		} else {
			$(this).button( "option", "icons", {primary:'ui-icon-triangle-1-e'} );
		}
		$('#collapsible').slideToggle('slow');
	});
	
	// --------
	
	nodeAttrs.read();
	if(nodeAttrs.val.branch !== undefined || 
			nodeAttrs.val.date !== undefined || 
			nodeAttrs.val.after !== undefined || 
			nodeAttrs.val.before !== undefined){
		$('#collapsible').show();
	}
	
	$("#searchText").focus(function(){
		if($(this).val() == searchPhrase){
			$(this).val("");
		}
	});
	
	$("#searchText").focusout(function(){
		if($(this).val() == ""){
			$(this).val(searchPhrase);
		}
	});
	
	$("#searchSubmit").click(function(){
		if($("#searchText").val() == searchPhrase)
		{
			return false;
		}
	});
	
	$("#calendarFilter").tabs();
	
	$.datepicker.setDefaults( $.datepicker.regional["ru"] );

	$('.datepicker').datepicker({
		inline: true,
		onSelect: function(dateText, inst) {
			var name = $(this).attr("id");
			if(name == "date"){
				delete nodeAttrs.val.after;
				delete nodeAttrs.val.before;
				nodeAttrs.val[name] = dateText;
			} else {
				delete nodeAttrs.val.date;
				nodeAttrs.val[name] = dateText;
			}
		}
	});
	
	$("#confSearch").click(function(){
		delete nodeAttrs.val.page;
		nodeAttrs.go();
	});

	$("input[name='region']").change(function(){
		nodeAttrs.val.region = $(this).val();
	});
	
	$("input[name='branch']").change(function(){
		nodeAttrs.val.branch = $(this).val();
	});
	
	var arr = ["date", "after", "before"];
	
	for(var i = 0; i < arr.length; i++){
		if(nodeAttrs.val[arr[i]] !== undefined){
			$("#" + arr[i]).datepicker("setDate", nodeAttrs.val[arr[i]]);
		}
	}
});


nodeAttrs.go = function(){
	var attrsStr = "";
	var arr = [];
	for(var i in nodeAttrs.val){
		arr.push(i + "=" + nodeAttrs.val[i]);
	}
	attrsStr = arr.join(";");/*
	var curr = decodeURI(location.href);
	if(curr.lastIndexOf("[") != -1){
		var before = curr.substring(0, curr.lastIndexOf("["));
		var after = curr.substring(curr.lastIndexOf("]"), curr.length);
		var newUrl = before + "[" + attrsStr + after;
	} else {
		curr = curr.substring(0, curr.length - 1);
		var newUrl = curr + "[" + attrsStr + "]/";
	}*/
	var newUrl = siteUrl + "konferencii[" + attrsStr + "]/";
	location.href = newUrl;
};

nodeAttrs.read = function(){
	nodeAttrs.val = {};
	var pathNodes = decodeURI(location.href).split("/");
	var lastNode = pathNodes[(pathNodes.length - 1)];
	if(lastNode == ""){
		lastNode = pathNodes[(pathNodes.length - 2)];
	}
	if(lastNode.indexOf("[") != -1){
		var attrs = lastNode.substring((lastNode.indexOf("[") + 1), (lastNode.length - 1));
		attrs = attrs.split(";");
		for(var i = 0; i < attrs.length; i++){
			var a = attrs[i].split("=");
			if(a.length == 2){
				nodeAttrs.val[a[0]] = a[1];
			}
		}
	}
};
