/*  */
$(
  function(){
    $('input:text').hint();
  }
)

/**
 Plugins
*/
jQuery.fn.debug = function() {
  return this.each(function(){
    alert(this);
  });
};
jQuery.log = function(message) {
  if(window.console) {
     console.debug(message);
  } else {
     alert(message);
  }
};

jQuery.fn.hint = function () {
  return this.each(function (){
    // get jQuery version of 'this'
    var t = jQuery(this);
    //var t = $(this);
    // get it once since it won't change
    var title = t.attr('title');
    // only apply logic if the element has the attribute
    if (title) {

      // on focus, set value to blank if current value
      // matches title attr
      t.focus(function (){
        if (t.val() == title) {
          t.val('');
          t.removeClass('blur');
        }
      });

      // on blur, set value to title attr if text is blank
      t.blur(function (){
        if (t.val() == '') {
          t.val(title);
          t.addClass('blur');
        }
      });

      // clear the pre-defined text when form is submitted
      t.parents('form:first()').submit(function(){
          if (t.val() == title) {
              t.val('');
              t.removeClass('blur');
          }
      });

      // now change all inputs to title
      t.blur();
    }
  });
}



/**
	Dropdown Basics
*/

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function select_dropdown_item(selectid, value)
{

  if ( selectid == "category" ){

    if ( value == "Kategória" ){

      $( "#subcategorydiv" ).hide();

    }

  }

  $("#"+selectid).children().removeAttr("selected").each(function() {

    if ($(this).html().replace( "&amp;", "" ) == value.replace( "&", "" )) {

			$(this).attr("selected","selected");

		}

	})

	$(".selid_"+selectid).find("div").html(value);
	$("#dropdown").css({"display": "none"});


  if ( selectid == "category" ){

    $( "#subcategorydiv" ).show();

    //Reverse hack
    if ( value == "Kategória" ){

      $( "#subcategorydiv" ).hide();

    }
    else {

      loadNextSelect( selectid, "Alkategória", "subcategory" );

    }

    loadNextSelect( selectid, "Márka", "brand" );
    loadNextSelect( selectid, "Hol", "company" );

  }

  if ( selectid == "subcategory" ){

    //Reverse hack
    if ( value == "Alkategória" ){

      selID ="category";

    }
    else {

      selID =selectid;

    }

    loadNextSelect( selID, "Márka", "brand" );
    loadNextSelect( selID, "Hol", "company" );

  }

}

function loadNextSelect( selID, initText, target ){

  $("#"+selID).children().each(function() {

    if ( $(this).attr("selected") ) {

      $.ajax({
        type: "POST",
        url: "/ajax/productsearchform/",
        data: "section=true&target="+ target +"&id="+$(this).val(),
        success: function( msg ){

          var catData = eval( msg );
          var opt  ='<option value="">'+initText+'</option>';

          for ( idx in catData ) {

            opt +='<option value="'+catData[idx].id+'">'+catData[idx].name+'</option>';

          }

          $("#"+target).html(opt);
          $("#"+target).next().children().text(initText);
        }
      });

    }
  });

}

function attach_dropdown_behaviors()
{

	//global click, closes all dropdowns if the event target is not a dropdown itself.
	//in this case the corresponting dropdown click event will close the others.
	$(".hiddenselect select").css({"display": "none"});
	$(document).click(function(event) {
		if (event.target.parentNode.className.indexOf("dropdown") == -1) {
			$("#dropdown").css({"display": "none"});
		}
	});

	$("div.dropdown div").toggle(
  	function() {

  		var o = this.parentNode;
  		var jo = $(this.parentNode);

  		var sel = "";
  		jo.siblings().each(function() {
  			if ($(this).is("select")) {
  				sel = $(this);
  			}
  		})

  		var w = jo.width();
  		var ddul = $("#dropdown ul");

  		var optstr = "";
  		ddul.html("");
  		sel.find("option").each(function() {
  			if ($(this).attr("value").indexOf("http://") != 0) {
  				var selid = sel.attr("id");
  				var content = $(this).html();
  				optstr += "<li onclick='select_dropdown_item(\""+selid+"\", \""+content+"\")'><a href='javascript:;'>"+content+"</a></li>";
  			} else {
  				optstr += "<li><a href='"+$(this).attr("value")+"'>"+$(this).html()+"</a></li>";
  			}

  		});
  		ddul.html(optstr);
  		$("#dropdown").css({"display": "block"});

  		var pos = findPos(o);
  		var containerpos = findPos(document.getElementById("container"));
  		if ($.browser.msie) {
  			var top = pos[1]+11
  		} else {
  			var top = pos[1]+21
    		if (o.className == 'bordered dropdown selid_fullcatalog'){
    		    top += 10; w = 190;}
    		if (o.className == 'bordered dropdown selid_partofcatalog'){
    		    top += 10; w = 190;}
    		if (o.className == 'bordered dropdown selid_catalogoffers'){
    		    top += 10; w = 190;}
    		/*if (o.className == 'bordered dropdown selid_listing_catalogsearch_what'){
    		    top += 10; w = 190;}
    		if (o.className == 'bordered dropdown selid_listing_catalogsearch_where'){
    		    top += 10; w = 190;}
    		if (o.className == 'bordered dropdown selid_listing_productsearch_what'){
    		    top += 10; w = 220;}
    		if (o.className == 'bordered dropdown selid_listing_productsearch_where'){
    		    top += 10; w = 220;}*/
  		}

  		$("#dropdown").css({
  			"left": pos[0]+5-containerpos[0],
  			"top": top,
  			"width": w
  		});

  	}
  	,
  	function() {
      $("#dropdown").css({"display": "none"});
    }
	);

}
