/* SNOOPING AROUND IN THE CODE, ARE WE?
*
* Javascript authored by Tobias, Håkon and Rolf at Powow AS
* Find us procrastinating at twitter.com/powow
* Or contact us through http://powow.no
*
* Thanks to jQuery for being nice to us
*/

$(document).ready(function(){
  $("#login-form-container #username-input").blur(notify_user_if_user_best);
	
  function notify_user_if_user_best(e) {
    if ( e.currentTarget.value == "best" ) {
      alert("Kjære besøkende!\n\nFor å øke sikkerhetsnivået har vi gjort endringer i våre påloggingsrutiner. Om du har vært registrert hos oss tidligere kan du lage et passord via linken 'Glemt passord?' eller opprette en ny bruker. Ditt brukernavn er epostadressen du har registrert hos oss.");
    }
  }

	/* MAKE SCALER IMAGE BG IMAGE */
  /*
	if($.browser.msie){
	  bgimg = $(".scaler img").attr("src");
	  $(".scaler").remove();
	  $("body").css("background","url("+bgimg+") no-repeat fixed center center #000");
	}
	*/
	
	/* Make linked images absolutely gorgeous */
	$("#picturebank a img, #bbpictures-nav a img, button#login, a#new-user, button#register-user").hover(
		function () {
        	$(this).stop(true,true).fadeTo("slow", 0.7);
      	}, 
      	function () {
        	$(this).stop(true,true).fadeTo("fast", 1);
      	}
	);
	

	
	/* Show wine numbers controller */	
	showPol = function(){
		$("table#wine-table").find(".vsd").hide().end().find(".pol").show().end().find("#pol").attr("checked","checked");
	}
	showVSD = function(){
		$("table#wine-table").find(".pol").hide().end().find(".vsd").show().end().find("#vsd").attr("checked","checked");
	}
	$("table#wine-table input#vsd").click(function(){showVSD();});
	$("table#wine-table input#pol").click(function(){showPol();});
	if($("table#wine-table tr#show-type-controller input").is(":checked")){
		if($(this).find(":checked").attr("id")=="pol"){
			showPol();
		} else {
			showVSD();
		}
	} else {
		showPol();
	}
	

});

$(window).ready(function(){
	
	/* Make stuff high enough, dynamically, relative to position of #content */
	$("#content:not('.push'), #producer-month, .fullheight").css("minHeight", ($(window).height() - $("#content").offset().top)+"px");

/*	if ($(window).height() < 600){
		$("body#story-month div#content").css("height", "600px");
	} */

	$(window).resize(function(){
		$("#content:not('.push), #producer-month, .fullheight").css("minHeight", ($(window).height() - $("#content").offset().top)+"px")

	/*	if ($(window).height() < 600){
			$("body#story-month div#content").css("height", "600px");
		}else{
			$("body#story-month div#content").css("height", "100%");
		} */

	});

	/* Make news and press list items the same height - apply article height to image div */
	$("#news-list .news-list-item").each(function(){
		var w4h = $(this).find(".w4").height();
		$(this).find(".w2").css("height", w4h + "px");
	});
	
	// Make news and press stories items the same height 
	if($("#content").is(":has('#story-wine-picture')")){
		$("#story-wine-picture img").load(function(){
		var storyText = $("#story-text");
		var storyWinePicture = $("#story-wine-picture");
		var mh = Math.max(storyText.height(),storyWinePicture.height());
		storyText.css("height", mh + "px");
		storyWinePicture.css("height", mh + "px");			
		});
	}
	
	/* Make menu items behave, width-wise */
	$("#subnav li a, #subsubnav li a").each(function(){$(this).css("width", $(this).width())});

	/* Make proper use of checkboxes in wine table */
	$("#wine-table tr td input:checkbox").bind("click", function() {
		var el = this
		if($(this).attr('checked'))
			$("#wine-list").load('/cart_items/new/', {id : $(el).attr('value')});
		else
			$("#wine-list").load('/cart_items/destroy/', {id : $(el).attr('value')});
	});
	
	/* Add wine on wine page */
	$("#add-wine").bind("click", function() {
			$("#wine-list").load(this.href);
			return false;
	});
	
	var scale_method = "fill";
	
	if($("#story-month, #producer-month-body, #gallery-body").length > 0){
		$(".scaler").css("top", "92px");
		scale_method = "fit";
	}
	$(".scaler img").cjObjectScaler({method: scale_method, fade: 500});
	
	$(".story_gallery_image").click(function(e) {
		e.preventDefault();
	   e.stopPropagation();
	//$('.bg_img').attr('src', $(e.target).parent().attr('href'));
	  $('.scaler img').attr('src', $(e.target).parent().attr('href'));
	$(".scaler img").cjObjectScaler({method: scale_method, fade: 500})
		return false;
	});
	
	var timer;
	$(window).resize(function() {
		clearTimeout(timer);
		timer = setTimeout(function() {
			$(".scaler img").cjObjectScaler({method: scale_method})
		}, 80);
	})
	
});

