$(document).ready( function() {

	// Debug
	var d = function(data){
						if(window.console && typeof(window.console)=="object") {
							console.log(data);
						}
					}


	// IE6 PNG FIX
  if($.browser.msie && parseInt(jQuery.browser.version) <= 6) {
      //alert("IE6");
      $.ifixpng('/gfx/usynlig.gif');
      $('img[@src$=.png]').ifixpng();
  }
  
  // FRONT DROPDOWNS
  $("div.container-column-1 select, div.container-column-2 select").change(function(){

  	var $this 			  = $(this);
  	var itemtype		  = $this.attr("name").replace(/type-/, "");
  	var val   			  = $this.val();
 		var $nextselects  = $this.nextAll("select").hide(0).find("option").hide(0);
 		var $spinner		  = $this.next("p.spinner-blue");
 		var selectedtype  = $this.children(":selected").attr("id").replace('type-', '');

 		var basehref         = "/index.asp?id=";
 		var itemtype_article = "20";

  	if(val) {

 			$spinner.show(0);

  		if(!$nextselects.length || selectedtype==itemtype_article) {	// Last select or article, goto element
  		//if(!$nextselects.length) {	// Last select, goto element
    			document.location=basehref + val + "&open=" + val;
  		} else {
					//var $target = $this.next("select");		//.find("option").remove().end();
					var $target = $spinner.next("select");	//.find("option").remove().end();

					$target.empty().append($("<"+"option value=\"\">(Velg)<"+"/option>"));
					
					$.getJSON("/gen_json.asp", { id: val, itemtype: itemtype }, function(json){

						if (json.result != null) {

							//$target.append($("<"+"option value=\"\"><"+"/option>"));

							$.each(json.result, function(i,item) {
								var cat_id    = item[0];
								var cat_title = item[1];
								var cat_type  = item[2];
								var option    = $("<"+"option id=\"type-" + cat_type + "\" value=\"" + cat_id + "\">" + cat_title + "<"+"/option>");

								$target.append(option);
								$target.show();
							});
						
						} else {
							document.location=basehref + val;
						}

					});
					
				$spinner.hide(500);
      }
  	}

  });
  
  
  // CATALOG IMAGES
  $("div.category div.image img").load(function(){
  	var $i = $(this);
  	var h  = parseInt($i.height());
    if(h < 116) {
      $i.css("padding-top", parseInt((116-h)/2)+"px");
    }
  });

  $("#template_catalog_articlegroup img.right").load(function(){
  	var $i = $(this);
  	var h  = parseInt($i.height());
  	var w  = parseInt($i.width());

    if(h < 116) {
      $i.css("padding-bottom", parseInt((116-h)+5)+"px");
    } else if(h > 116) {
    	$i.css("height", 116+"px");
    }

    if(w < 141) {
      $i.css("padding-left" , parseInt(((141-w)/2)+5)+"px");
      $i.css("padding-right", parseInt(((141-w)/2)+6)+"px");
    //} else if(w > 141) {
    //	$i.css("width", 141+"px");
    }
    

  });

	if($.browser.msie && parseInt(jQuery.browser.version) <= 6) {
	  $("div.category div.image img").load(function(){
	    var $i = $(this);
	    if(parseInt($i.height()) > 116) {
	      $i.css("height", "116px");
	    }
	  });
	}


	// TARGET BLANK ON ANCHORS
	$("a[rel=external]").attr("target", "_blank");
	
	
	// SEARCH
	$("#searchbox").submit(function(){ 
		$(this).find("input[type=text]:first").addClass("working");
	});

	
}); 