var jq = jQuery.noConflict();

jq(document).ready(function() {

	jq('body :first-child').addClass('first-child');
	jq('body :last-child').addClass('last-child');
	jq('body :empty').addClass('empty');
	
	jq('#kontorInput img,div.selectItem li,div.selectItem li,#kontorInputHorizontal img,#kontorInputInside img,input[type=submit]').hover(
		function(){ jq(this).css('cursor','pointer'); },
		function(){ jq(this).css('cursor','default'); }
	);
	
	/*
	jq('.gridThumb').hover(
		function(){ jq(this).find('.prospectOverlay').fadeIn(200); },
		function(){ jq(this).find('.prospectOverlay').fadeOut(200); }
	);
	*/
	
	jq('input.selectInput').val("");
	
	jq('#kontorInput img,#kontorInputHorizontal img,#kontorInputInside img').toggle(
	    function () {
	    	jq('div.selectItem').slideDown(300);
	    	jq(this).attr({src:"./styles/images/upArrow.gif"});
	    },
	    function () {
	      	jq('div.selectItem').slideUp(300);
	      	jq(this).attr({src:"./styles/images/downArrow.gif"});
	    }
	);
	
    jq("input[type=text]").focus(function() {
        if(jq(this).val() == jq(this).attr('defaultValue')) {
            jq(this).val('');
        }
    }).blur(function() {
        if(jQuery.trim(jq(this).val()) === "") {
            jq(this).val(jq(this).attr('defaultValue'));
        }
    });
	
	/* DROPDOWN
	-------------------------------------*/
	createDropDown();
	
	jq(".dropdown dt").click(function(event) {
		event.preventDefault();
		var dropID = jq(this).closest("dl").attr("id");
		jq("#" + dropID).find("ul").toggle();
	  });

	  jq(document).bind('click', function(e) {
		var $clicked = jq(e.target);
		if (! $clicked.parents().hasClass("dropdown")) {
		  jq(".dropdown dd ul").hide();
                }
	  });


	jq(".dropdown dd ul a").click(function(event) {
		event.preventDefault();
		var dl = jq(this).closest("dl");
		var dropID = dl.attr("id");
		var sourceID = dl.attr("source");
		var text = jq(this).html();
		//var source = dl.prev();
		var source = jq("select[id='" + sourceID + "']");
		jq("#" + dropID + " dt a").html(text);
		jq("#" + dropID + " dd ul").hide();
		source.val(jq(this).find("span.value").html());
		var option = source.find("option[value='" + jq(this).find("span.value").html() + "']");
		source.selectedIndex = source.find("option").index(option);
		source.change();

		if(source.hasClass("absolute")) {
			jq("input[name='property_type']").attr('value', source.val());
		}
	});

	function createDropDown() {
		var selects = jq("select.selectlist");
		var idCounter = 1;
		selects.each(function() {
			var dropID = "dropdown_" + idCounter;
			var sourceID = "source_" + idCounter;
			var source = jq(this);
			source.attr('id', sourceID);
			var selected = source.find("option[selected]");
			var options = jq("option", source);

			if(source.hasClass("absolute")) {
				jq("#content").prepend('<dl id="' + dropID + '" source="' + sourceID + '" class="dropdown absolute"></dl>');
			}
			else {
				source.after('<dl id="' + dropID + '" source="' + sourceID + '" class="dropdown"></dl>');
			}
			jq("#" + dropID).append('<dt><a href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a></dt>');
			jq("#" + dropID).append('<dd><ul></ul></dd>');
			options.each(function() {
				if(jq(this).text() !== "") {
					jq("#" + dropID + " dd ul").append('<li><a href="#">' + jq(this).text() + '<span class="value">' + jq(this).val() + '</span></a></li>');
				}
			});
			idCounter++;
		});
	} 
});
