﻿$(function () {
	$("div.edition a").click(function () {
		$("div.edition a").removeClass("active");
		$(this).addClass("active");

		var $this = $(this);
		var item = $this.parent();
		var items = $("li", item.parent());
		var index = items.index(item);

		$("div.edition ul.info").hide();
		$("div.edition ul.info span").removeClass("active-isbn");
		var ul = $("div.edition ul.info.c" + index);
		$("div.edition ul.info.c" + index + " span").addClass("active-isbn");
		ul.show();
	});

	$("#maincontent .trial ul li a").click(function () {
		var $this = $(this);
		var type = $this.attr("id");
		var isbn = $("span.active-isbn").html().split(' ').join('');

		if (type == "smakprov") {
			window.open(SMAKPROV_URL + isbn);
			return false;
		}
		else if (type == "sample") {
			console.log(SAMPLE_URL + isbn);
		}
		else if (type == "tip") {
			console.log(type);
		}
		else if (type == "adlibris") {
			window.open(ADLIBRIS_URL + isbn);
			return false;
		}
		else if (type == "bokus") {
			window.open(BOKUS_URL + isbn);
			return false;
		}
		else if (type == "cdon") {
			window.open(CDON_URL + isbn);
			return false;
		}
		else if (type == "bokia") {
			window.open(BOOKIA_URL + isbn);
			return false;
		}
	});

	$("#maincontent div.news li").live("click", function () {
		location.href = $("a", $(this)).attr("href");
	});

	$("a[rel='external']").click(function () {
		window.open($(this).attr('href'));
		return false;
	});

	if ($("#fb-feed").length > 0) {

		var appid = "184878531594767";
		var appSecret = "efa56eb79c4092171bf025cfafb73b82";
		var accessToken = appid +"|"+appSecret;
		
		$.getJSON("https://graph.facebook.com/192214299958/feed" + "?access_token=" + accessToken, function (response) {
			if (response != null) {
				$("#fb-loader").hide();
				$.each(response.data, function (i, data) {
					$("<li><span class='who'>" + data.from.name + " <font class='when'>" + data.created_time.substring(0, 10) + "</font></span><div>" + cutToSize(data.message, 65) + "</div><a href='" + FACEBOOK_URL + "'></a></li>").appendTo("#fb-feed ul");
					if (i == 3) return false;
				});
			}
			else {
				$("<li>Error connecting to facebook</li>").appendTo("#fb-feed ul");
			}
		});
	}

	$("#fb-feed ul li").live("click", function () {
		window.open($("a", $(this)).attr("href"));
		return false;
	});

	$("#filters ul.authors li a").click(function () {
		$("#filters .loader").show();

		var $this = $(this);
		var sLetters = $this.attr("rel");

		$this.parent().parent().children().removeClass("active")
		$this.parent().addClass("active");

		$.getJSON(AUTHOR_SERVICE_URL, { letters: sLetters }, function (response) {
			if (response != null) {
				$("#filters .loader").hide();
				var UL = $("#maincontent .listing ul");
				UL.empty();
				$.each(response.data, function (i, obj) {
					$("<li><div class='listimage'><a href='" + obj.Url + "'><img src='" + obj.Img + "' alt=''/></a></div><h3>" + obj.Title + "</h3><p>" + obj.Text + "</p></li>").appendTo(UL);
				});
			}
		});
	});


	$("#filters ul.books li a").live("click", function () {
		$("#filters .loader").show();

		var $this = $(this);

		$this.parent().parent().children().removeClass("active")
		$this.parent().addClass("active");

		var item = $(".active", $("#filtertype"));
		var items = $("ul#filtertype li");
		var index = items.index(item);
		/* TEMPORARILY !!! */
		if (index == 5)
		{ index = 8; }
		/* TEMPORARILY !!! */

		var sortUl = $("ul#filtersort li");
		var sortItem = $(".active", $("#filtersort"));
		var sortIndex = sortUl.index(sortItem);

		var sLetters = $("#letterFilter li.active a").attr("rel");

		$.getJSON(BOOK_SERVICE_URL, { category: index, sort: sortIndex, letters: sLetters, bookpageid: BookPageId }, function (response) {
			if (response != null) {
				$("#filters .loader").hide();
				var UL = $("#books .listing ul");
				UL.empty();
				$.each(response.data, function (i, obj) {
					var slogan = "";
					if (obj.Slogan != null && obj.Slogan != "")
						slogan = "title='" + obj.Slogan + "' ";
					$("<li><a href='" + obj.Url + "'><img src='" + obj.Img + "' alt=''" + slogan + "/></a><div><h4>" + obj.Title + " - " + obj.Authors + "</h4></div></li>").appendTo(UL);
				});
				tooltip();
			}
			else {
				console.log("response is null");
			}
		});
	});

	$("#search input").focus(function () {
		$(this).attr("value", "");
	});

	$("#search input").example("Sök").keydown(function (e) {

		if (e.keyCode == 13) { // enter
			window.location = (SEARCH_URL + "?q=" + urlencode($(this).val()));
			return false;
		}
	});

	//books tooltip
	tooltip();

	//buy tooltip
	$("#buydiv").hover(function () {
		$(".tip", $(this)).show();
	},
        function () {
        	$(".tip", $(this)).hide();
        });

	//cycle tmp
	$('#items').cycle({
		fx: 'fade',
		timeout: 7000
	});

	$('#container #items img').click(function () {
		window.location = $(this).attr("longdesc");
		return false;
	});
});

function cutToSize(string, length){

    if (string.length > length) {
        return string.substring(0, length - 4) + " ...";
    }
    else {
        return string;
    }
}

var urlencode = function (str) {
    // return escape(str);
    str = str.replace("?", "%3F");
    str = str.replace("&", "%26");
    str = str.replace("#", "%23");
    str = str.replace(" ", "%20");
    return str;
}

function tooltip() {
    $("#books .listing ul li img[title]").tooltip({ offset: [35, 0] }).dynamic({ bottom: { direction: 'down', bounce: true} });
}


$('#highlightbooks ul').bookSlide();

function pageLoad(sender, args) {               // Reloads the tooltip when a page is partially reloaded through AJAX
    if (args.get_isPartialLoad()) {
        tooltip();
    }
}

$(".inputName").live("click", function () {
    $value = $(this).val();

    if ($value == "Namn")
        $(this).val("");
});

$(".inputName").live("blur", function () {
    $value = $(this).val();
    if ($value == "")
        $(this).val("Namn");
});

$(".inputEmail").live("click", function () {
    $value = $(this).val();

    if ($value == "Epost")
        $(this).val("");
});

$(".inputEmail").live("blur",function () {
    $value = $(this).val();
    if ($value == "")
        $(this).val("Epost");
});
