/*
 * 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() {
//		$("a[rel^='prettyPhoto']").prettyPhoto({
//			animationSpeed: 'normal', /* fast/slow/normal */
//			opacity: 0.80, /* Value between 0 and 1 */
//			showTitle: true, /* true/false */
//			allowresize: true, /* true/false */
//			default_width: 500,
//			default_height: 344,
//			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
//			theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
//			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
//			wmode: 'opaque', /* Set the flash wmode attribute */
//			autoplay: true, /* Automatically start videos: True/False */
//			modal: false, /* If set to true, only the close button will close the window */
//			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
//			callback: function(){}, /* Called when prettyPhoto is closed */
//			markup: '<div class="pp_pic_holder"> \
//						<div class="pp_top"> \
//							<div class="pp_left"></div> \
//							<div class="pp_middle"></div> \
//							<div class="pp_right"></div> \
//						</div> \
//						<div class="pp_content_container"> \
//							<div class="pp_left"> \
//							<div class="pp_right"> \
//								<div class="pp_content"> \
//									<div class="pp_fade"> \
//										<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
//										<div class="pp_loaderIcon"></div> \
//										<div class="pp_hoverContainer"> \
//											<a class="pp_next" href="#">next</a> \
//											<a class="pp_previous" href="#">previous</a> \
//										</div> \
//										<div id="pp_full_res"></div> \
//										<div class="pp_details clearfix"> \
//											<a class="pp_close" href="#">Close</a> \
//											<p class="pp_description"></p> \
//											<div class="pp_nav"> \
//												<a href="#" class="pp_arrow_previous">Previous</a> \
//												<p class="currentTextHolder">0/0</p> \
//												<a href="#" class="pp_arrow_next">Next</a> \
//											</div> \
//										</div> \
//									</div> \
//								</div> \
//							</div> \
//							</div> \
//						</div> \
//						<div class="pp_bottom"> \
//							<div class="pp_left"></div> \
//							<div class="pp_middle"></div> \
//							<div class="pp_right"></div> \
//						</div> \
//					</div> \
//					<div class="pp_overlay"></div> \
//					<div class="ppt"></div>',
//			image_markup: '<img id="fullResImage" src="" />',
//			flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
//			quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
//			iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
//			inline_markup: '<div class="pp_inline clearfix">{content}</div>'
//		});

	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[href*='apple.com']").addClass("apple").addClass("social");
	$(".caption a img").parent("a").removeClass("social");

	$("a[style='font-size: 85%;']").css("color","#c33");

	$(".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)
	});
	$("nav ul:not(.social) li li a").mouseenter(function(){
		$("img",this).fadeTo("fast",1)
	})
	$("nav ul:not(.social) li li a").mouseleave(function(){
		$("img",this).fadeTo("fast",0.5)
	})
	$("a.postLink").each(function(){
        var theHref = $(this).attr("href");
        var theLink = $(this);
        $.get(theHref, function(data){
            var theText = $(data).find("h1").text();
            var theDate = $(data).find("time:first").attr("datetime")
            theLink.html(theLink.text() + "<h2>" + theText + "</h2><p class='datum'>" + theDate + "</p>");
	  	})
	});
	$(".paginator a").each(function(){
        var theHref = $(this).attr("href");
        var theLink = $(this);
        $.get(theHref, function(data){
            var endText = $(data).find(".date:first time").attr("datetime");
            var startText = $(data).find(".date:last time").attr("datetime");
            theLink.attr("title", startText + " - " + endText);
	  	})
	
	});


	// 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);
}

