searchValue = "Search"; 
fixedBar = true; 

var fancyboxArgs = {
    type: "image",
    padding: 0,
	mouseWheel: true,
	autoPlay: true,
    overlayColor: "#000",
    overlayOpacity: 0.85,
    titlePosition: "outside",
    titleFormat: function (a) {
        return a
    }
};

var IOS = (navigator.platform == "iPad" || navigator.platform == "iPhone" || navigator.platform == "iPod") ? true : false;
var fixedBar = (typeof fixedBar === "undefined") ? false : true;








$(function() {
	/* header */
    var q = $("#header");
    if (IOS) { fixedBar = false }
	
    if (fixedBar) {
        var n = false,
            m = false;
        var l = 750;
        var o = false;
        var k = true;
        var p = $(window).scrollTop();
        if (p > l) {
            o = true;
            k = false;
            q.removeClass("pinned").addClass("fixed").css({
                top: "0px"
            }).find(".shadow").show()
        }
    }
	
    $(window).scroll(function () {

        if (fixedBar) {
            p = $(window).scrollTop();

            if (p > l) {

                if (!o) {
                    if (m !== false) {
                        clearTimeout(m);
                        clearTimeout(n)
                    }
                    
                    o = true;
                    k = false;
                    m = setTimeout(function () {
                        q.stop(true, true).removeClass("pinned").addClass("fixed").css({
                            top: "-50px"
                        }).animate({
                            top: "0px"
                        }, 300).find(".shadow").show()
                    }, 100)
                }
            } else {
                if (p < l) {
                    if (!k) {
                        if (n !== false) {
                            clearTimeout(m);
                            clearTimeout(n)
                        }
                        o = false;
                        k = true;
                        n = setTimeout(function () {
                            q.stop(true, true).animate({
                                top: "-60px"
                            }, 300, function () {
                                $(this).hide().css({
                                    top: "30px"
                                }).find(".shadow").hide().end().removeClass("fixed").addClass("pinned").fadeIn("fast")
                            })
                        }, 100)
                    }
                }
            }
        }
    });
	
	/* Share */
    $(".share-btn").live("click", function (u) {
        u.preventDefault();
        $(".share-box:not(this)").hide();
        var t = $(this).attr("data-permalink");
        var w = $(this).next(".share-box");
        var v = w.find(".like-button");
        var s = w.find(".plusone-btn");
        w.show().find(".shortlink").focus();
        if (s.length > 0) {
            var r = $(this).closest(".post").attr("id");
            gapi.plusone.go(r)
        }
        if (v.length > 0) {
            v.replaceWith('<iframe class="facebook-like-button" src="http://www.facebook.com/plugins/like.php?href=' + t + '&amp;layout=button_count&amp;show_faces=false&amp;width=90&amp;action=like&amp;colorscheme=light&amp;height=20" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:90px; height:20px;" allowTransparency="true"></iframe>')
        }
    });
    $(document).mousedown(function (r) {
        if (!$(r.target).parents(".share-box").get(0)) {
            $(".share-box").hide()
        }
    });
	
	
	/* search */
    if (searchValue !== "") {
        $("#search-form").find("input").val(searchValue).focus(function () {
            if ($(this).val() == searchValue) {
                $(this).val("")
            }
        }).blur(function () {
            if ($(this).val() == "") {
                $(this).val(searchValue)
            }
        })
    }
	
	/* jcarousel */
	$.easing['easeInOutQuart'] = function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
			return -c/2 * ((t-=2)*t*t*t - 2) + b;
	};



	$('.jcarousel').bind('jcarouselitemfullyvisiblein', function(carousel) {
		
		
		//var new_height = $(this).find('.jcarousel-item-first').height();		
		//alert( $(this).height());
		
	});


	$('.jcarousel').bind('jcarouselinitend', function(carousel) {
		var height = $(this).find('.attachment-full_image_format').attr('height');		
		var width = $(this).find('.attachment-full_image_format').attr('width');				
		var factor = 700/width;
		var new_height = factor * height;
		
		$(this).animate({
		    height: new_height+"px"
		  }, 500, function() {
		    // Animation complete.
		  });
		  
		
		
		if ($(this).find(".caption").html().trim()) {

		} else {
			return;						
		}

		$(this).find(".caption").animate({
		    height: "40px"
		  }, 500, function() {
		    // Animation complete.
		  });

	    // "this" refers to the root element
	    // "carousel" is the jCarousel instance
	});

	$('.jcarousel')
	.jcarousel({
        animation: {
            duration: 1000,
            easing: 'easeInOutQuart'
        },
        wrap: 'circular'
	});
	
	

	
	$('.jcarousel').bind('jcarouselanimate', function(carousel) {
	    // "this" refers to the root element
	    // "carousel" is the jCarousel instance
	});
	
	$('.jcarousel').bind('jcarouselanimateend', function(carousel) {

  		var new_height = $(this).find('.jcarousel-item-target').height();		
  		$(this).animate({
  		    height: new_height+"px"
  		  }, 500, function() {
  		    // Animation complete.
  		  });
		
		if ($(this).find(".caption").html().trim()) {

		} else {
			return;						
		}

		$(this).find(".caption").animate({
		    height: "40px"
		  }, 250, function() {
		    // Animation complete.
		  });
		
	    // "this" refers to the root element
	    // "carousel" is the jCarousel instance
	});
	
	
	
	
	
	$('.navi .next').click(function() {

		//carousel.next();
    var div = $(this).parent("div");
    var parent_div = div.parent("div");
    var carousel_object = parent_div.find(".jcarousel");
    var carousel2 = carousel_object.data('jcarousel');


		$(parent_div).find(".caption").animate({
		    height: "0px"
		  }, 250, function() {
		    // Animation complete.
			carousel2.scroll('+=1');
		  });
		


		//carousel2.next();
		return false;
	});
	
	$('.navi .prev').click(function() {
    var div = $(this).parent("div");
    var parent_div = div.parent("div");
    var carousel_object = parent_div.find(".jcarousel");
    var carousel2 = carousel_object.data('jcarousel');

		$(parent_div).find(".caption").animate({
		    height: "0px"
		  }, 250, function() {
		    // Animation complete.
			carousel2.scroll('-=1');
		  });


		//carousel2.next();
		return false;
	});
	
	
	
	
	/* photobuttons */
    $(".photo-panel").live("mouseenter", function () {
        $(this).find(".photo-btns").stop(true, true).fadeIn(150)
    }).live("mouseleave", function () {
        $(this).find(".photo-btns").stop(true, true).fadeOut(150)
    });
    $(".type-photoset").find(".photo-panel").find("a.lightbox").live("click", function () {
        $(this).parent().prev("a:has(img)").click();
        return false
    });
	
	
	/* Fancybox */
	$("a.lightbox").fancybox(fancyboxArgs);
	
  var panel = $(document).find(".photo-panel");
  var image = panel.find("img");					// bilden
  var btn = panel.find(".lightbox");			// knappen
  var link = image.parent("a");					// bildlänken
  
  if (link.length > 0) {
    if (link.attr("href") == btn.attr("href")) {
      link.click(function () {
        var panel = $(this).parent(".photo-panel");
        var btn = panel.find(".lightbox");
        btn.click();
        return false;	
      });
    }
  }
	
  /* Posts */
  var posts = $(".post:not(.loaded)");
	
    posts.each(function () {
        var current_post = $(this);
        var r = current_post.attr("id").split("-")[1];
        if (current_post.hasClass("type-quote")) {
            var quote = current_post.find(".quote-text");
            var begin_quote = quote.find(":first-child");
            var end_quote = quote.find(":last-child");
            var quote_begin = '<span class="ldquo">&ldquo;</span>';
            var quote_end = '<span class="rdquo">&rdquo;</span>';
            if ((begin_quote.length > 0) && begin_quote.is("p")) {
                begin_quote.prepend(quote_begin)
            } else {
                quote.prepend(quote_begin)
            }
            if ((end_quote.length > 0) && (/<\/[a-zA-Z]+>$/.test(quote.html()))) {
                end_quote.append(quote_end)
            } else {
                quote.append(quote_end)
            }
        }
		
        current_post.addClass("loaded")
    });
	
	
	
});
