/*
 * jQuery replaceText - v1.1 - 11/21/2009
 * http://benalman.com/projects/jquery-replacetext-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){$.fn.replaceText=function(b,a,c){return this.each(function(){var f=this.firstChild,g,e,d=[];if(f){do{if(f.nodeType===3){g=f.nodeValue;e=g.replace(b,a);if(e!==g){if(!c&&/</.test(e)){$(f).before(e);d.push(f)}else{f.nodeValue=e}}}}while(f=f.nextSibling)}d.length&&$(d).remove()})}})(jQuery);
$(function() {

	setupZoom();
	if (navigator.userAgent.indexOf('iPhone') != -1) {
		setTimeout(hideURLbar, 0);
	}
	$(".caption a[href*='youtube.com']").addClass("youtube").addClass("social");
	$(".caption a[href*='flickr.com']").addClass("flickr").addClass("social");
	$(".caption a[href*='twitter.com']").addClass("twitter").addClass("social");
	$(".caption a[href*='facebook.com']").addClass("facebook").addClass("social");
	$(".caption a[href*='tumblr.com']").addClass("tumblr").addClass("social");
	$(".caption a[href*='digg.com']").addClass("digg").addClass("social");
	$(".caption a[href*='google.com']").addClass("google").addClass("social");
	$(".caption a[href*='friendfeed.com']").addClass("friendfeed").addClass("social");
	$(".caption a[href*='gowalla.com']").addClass("tumblr").addClass("social");
	$(".caption a[href*='lastfm.com']").addClass("lastfm").addClass("social");
	$(".caption a[href*='linkedin.com']").addClass("linkedin").addClass("social");
	$(".caption a img").parent("a").removeClass("social");
	$(".date .text").replaceText( /monday/gi, "mŒndag" );
	$(".date .text").replaceText( /tuesday/gi, "tisdag" );
	$(".date .text").replaceText( /wednesday/gi, "onsdag" );
	$(".date .text").replaceText( /thursday/gi, "torsdag" );
	$(".date .text").replaceText( /friday/gi, "fredag" );
	$(".date .text").replaceText( /saturday/gi, "lšrdag" );
	$(".date .text").replaceText( /sunday/gi, "sšndag" );

	$(".date .text").replaceText( /january/gi, "januari" );
	$(".date .text").replaceText( /february/gi, "februari" );
	$(".date .text").replaceText( /march/gi, "mars" );
	$(".date .text").replaceText( /april/gi, "april" );
	$(".date .text").replaceText( /may/gi, "maj" );
	$(".date .text").replaceText( /june/gi, "juni" );
	$(".date .text").replaceText( /july/gi, "juli" );
	$(".date .text").replaceText( /august/gi, "augusti" );
	$(".date .text").replaceText( /september/gi, "september" );
	$(".date .text").replaceText( /october/gi, "oktober" );
	$(".date .text").replaceText( /november/gi, "november" );
	$(".date .text").replaceText( /december/gi, "december" );
//	$(".date .text").replaceText( /january/gi, "jan" );
//	$(".date .text").replaceText( /february/gi, "feb" );
//	$(".date .text").replaceText( /march/gi, "mar" );
//	$(".date .text").replaceText( /april/gi, "apr" );
//	$(".date .text").replaceText( /may/gi, "maj" );
//	$(".date .text").replaceText( /june/gi, "jun" );
//	$(".date .text").replaceText( /july/gi, "jul" );
//	$(".date .text").replaceText( /august/gi, "aug" );
//	$(".date .text").replaceText( /september/gi, "sep" );
//	$(".date .text").replaceText( /october/gi, "okt" );
//	$(".date .text").replaceText( /november/gi, "nov" );
//	$(".date .text").replaceText( /december/gi, "dec" );
	$(".date").fadeIn();

	$("ul.social img").mouseenter(function(){
		var index = $("ul.social img").index(this);
		$("ul.social img:not(this))").fadeTo("fast",0.33)
		$(this).fadeTo("fast",1)
	})
	$("ul.social").mouseleave(function(){
		$("img",this).fadeTo("fast",1)
	})


	// search field nicer (more style in the future)
	$("#topSearch label:first").hide()
	var searchText = $("#topSearch label:first").text();
	$("#topSearch input:first").val(searchText);
	$("#topSearch input:first").focus(function(){
		$(this).addClass("focused");
		if($(this).val() == searchText){
			$(this).val("")
		}
	});
	$("#topSearch input:first").blur(function(){
		$(this).removeClass("focused");
		if(jQuery.trim($(this).val()) == ""){
			$(this).val(searchText)
		}
	});
	
	
	
	$(".mailForm label").each(function(){
		$(this).hide();
		var theText = $(this).text()
		if($(this).next().val() == ""){
			$(this).next().val(theText)
		}
		$(this).next().focus(function(){
			$(this).addClass("focused");
			if($(this).val() == theText){
				$(this).val("")
			}
			
		});
		$(this).next().blur(function(){
			$(this).removeClass("focused");
			if(jQuery.trim($(this).val()) == ""){
				$(this).val(theText)
			}
			
		});
		
	})
	
	var searchText = $("#topSearch label:first").text();
	$("#topSearch input:first").val(searchText);
	$("#topSearch input:first").focus(function(){
		$(this).addClass("focused");
		if($(this).val() == searchText){
			$(this).val("")
		}
	});
	$("#topSearch input:first").blur(function(){
		$(this).removeClass("focused");
		if(jQuery.trim($(this).val()) == ""){
			$(this).val(searchText)
		}
	});



	$(".post .date").each(function(){
		if($(this).parents(".post").next().children().hasClass("date")){
			$(this).parents(".post").css("min-height","34px")
		}
	});
	$(".photoAlbumsPhotos img").mouseover(function(){
		$(this).css({
			margin:"4px",
			width:"46px",
			height:"46px"
		})	
	})
	$(".photoAlbumsPhotos img").mouseout(function(){
		$(this).css({
			margin:"6px",
			width:"42px",
			height:"42px"
		})	
		$(this).css("margin","6px")	
		$(this).css("width","42px")	
		$(this).css("height","42px")	
	})
	$(".photoAlbumPhotos img").mouseover(function(){
		$(this).css("margin","4px")	
		$(this).css("width","73px")	
		$(this).css("height","73px")	
	})
	$(".photoAlbumPhotos img").mouseout(function(){
		$(this).css("margin","6px")	
		$(this).css("width","69px")	
		$(this).css("height","69px")	
	})
 	$("#sidebar").append('<img src="http://stil.janner.se/image/button.close.gif" alt="close" class="sidebarCloser">');

	$(".nav li:first a:eq(1),.sidebarCloser").click(function(){
		$("#sidebar").fadeToggle(400)
		if ($(".nav li:first a:eq(1)").text() != "6"){
			$(".nav li:first a:eq(1)").text("6");
		} else {
			$(".nav li:first a:eq(1)").text("5");	
		}
		return false;
	})

	var query = '?';
	$("a[href$='#disqus_thread']").each(function(i){
		query += 'url' + i + '=' + encodeURIComponent($(this).attr("href")) + '&';
	})
	$.getScript('http://disqus.com/forums/martinjanner/get_num_replies.js' + query );
	
	$.fn.fadeToggle = function(speed, easing, callback) {
		return this.animate({opacity: 'toggle'}, speed, easing, callback); 
	};
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
	$(window).keydown(function(e){
	    kkeys.push(e.keyCode);
	    	
	    if ( kkeys.toString().indexOf( konami ) >= 0 ) {
	    	$.getScript("http://www.cornify.com/js/cornify.js",function(){
	    		cornify_add();
	    	});
	    }
	}, true);
});

function hideURLbar() {
        window.scrollTo(0, 1);
}


