jQuery(document).ready(function($) {
  $('a[rel*=facebox]').facebox() 
})

var timer = new Array();

$(document).ready(function() {
	$("a.page, a.pageLink, a.pageButton").click(function() {
		if ($(this).hasClass("form")) {
			document.location.href = "?loadpage=" + $(this).attr("href").replace("oid=", "");
			return false;
		}
		
		if ($(this).attr("target") != "_blank") {
			showPage($(this).attr("href"));
			return false;
		}
	});
	
	$("div.fadedPopup img.close").click(function() {
		$("div.fader").hide();
		$("div.fadedPopup").hide();
		
		$("div.fadedPopup div.popupContent").html("");
	});
	$("div.popupLogin img.close").click(function() {
		$("div.fader").hide();
		$("div.popupLogin").hide();
	});
	
	$('#newsScroller').jScrollPane({scrollbarWidth:12});
	
	$("div.search form").submit(function() {
		// Load content
		var dataParams = "";		
		dataParams += "subject=" + $("select[name=subject]").val();
		dataParams += "&region=" + $("select[name=region]").val();
		dataParams += "&search=" + $("input[name=search]").val();
		dataParams += "&type=" + $("input[name=type]").val();
		ajaxHTML("/newscontent.jsp", dataParams, function(html) {
			$("div#newsScroller").html(html);
		});
		return false;
	});
	
	$("div.box div.resources").jCarouselLite({
		btnNext: "img.resources.next",
		btnPrev: "img.resources.prev",
		visible: 5
	});

	$("div.box.resources div.top select").change(function() {		
		// Load content
		var dataParams = "";		
		dataParams += "feeling=" + $("select[name=feeling]").val();
		dataParams += "&type=" + $("select[name=type]").val();
		ajaxHTML("/resources_content.jsp", dataParams, function(html) {
			$("div.box div.resources").remove();
			$("img.resources.prev").after(html);
			
			if ($("div.box div.resources ul li").length > 5) {
				$("div.box div.resources").jCarouselLite({
					btnNext: "img.resources.next",
					btnPrev: "img.resources.prev",
					visible: 5
				});
				$("img.resources.prev").css("visibility", "visible");
				$("img.resources.next").css("visibility", "visible");
			} else {
				$("img.resources.prev").css("visibility", "hidden");
				$("img.resources.next").css("visibility", "hidden");
			}
		});
	});
	
	$("div.ideaSearch select").change(function() {		
		// Load content
		var dataParams = "";		
		dataParams += "month=" + $("select[name=month]").val();
		dataParams += "&type=" + $("select[name=type]").val();
		ajaxHTML("/ideas_content.jsp", dataParams, function(html) {
			$("div.ideas").html(html);
		});
		return false;
	});
	
	$("div.search.devotion select").change(function() {		
		// Load content
		var dataParams = "";		
		dataParams += "subject=" + $("select[name=subject]").val();
		ajaxHTML("/devotionContent.jsp", dataParams, function(html) {
			$("div#newsScroller").html(html);
		});
		return false;
	});
	
	$("div.mailPray form").submit(function() {
		ajaxXMLFromForm($(this), function() {

		}, function(err, err1) {
			$("div.mailPray form").hide();
			$("div.mailPray #sendOk").show();
		});
		
		return false;
	});
	
	$("div.shareButtons div.bubbleMail form").submit(function() {
		ajaxXMLFromForm($(this), function() {

		}, function(err, err1) {
			$("div.shareButtons div.bubbleMail form").hide();
			$("div.shareButtons div.bubbleMail #sendOk").show();
		});
		
		return false;
	});
	
	$("div.shareButtons a.close").click(function() {
		$(this).parent().hide();
		return false;
	});
	
	$("a.notLoggedIn").click(function() {
		showPopupLogin();
		return false;
	});
	
	$("a.comment").click(function() {
		$("div.writeComment").slideDown(200);
		return false;
	});
	
	$("a.share").click(function() {
		window.open($(this).attr("href") + $(this).attr("id"));
		return false;
	});
	
	$("div.shareButtons img.share").click(function() {
		$("div.shareButtons div.bubbleMail").hide();
		$("div.shareButtons div.bubbleShare").fadeIn(200);
	});
	$("div.shareButtons img.mail").click(function() {
		$("div.shareButtons div.bubbleShare").hide();
		$("div.shareButtons div.bubbleMail").fadeIn(200);
	});
	
	$("div.comment strong a").click(function() {
		var link = $(this);
		ajaxHTML($(this).attr("href"), "", function(html) {
			link.html("Anm&auml;lan skickad");
		});
		
		return false;
	});
	
	if ($("div.slideshow").length) {
		$("div.slideshow").each(function() {
			$("div.slide:first", $(this)).show();
			timer[$(this).attr('id')] = setTimeout("showNextImage('" + $(this).attr('id') + "');", 5000);
			var slider = $(this);
			$("div.menu div", slider).click(function() {
				clearTimeout(timer[slider.attr("id")]);
				$("div.slide:visible", slider).fadeOut(500);
				$("div.slide:eq(" + $("div.menu div", slider).index(this) + ")", slider).fadeIn(500);
				timer[slider.attr("id")] = setTimeout("showNextImage('" + slider.attr('id') + "')", 5000);
			});
		});
	}
	
	$("map[name=ukmap] area").mouseover(function() {
		$("img[usemap=#ukmap]").attr("src", "http://newgen.blindindesign.com/wp-content/themes/newgen/images/team_ukmap_" + $(this).attr("id") + ".png");
	});
	$("map[name=ukmap] area").mouseout(function() {
		$("img[usemap=#ukmap]").attr("src", "http://newgen.blindindesign.com/wp-content/themes/newgen/images/team_ukmap.png");
	});
	
	$("div.box div.tand form").submit(function() {
		ajaxXMLFromForm($(this), function() {
		}, function(err, err1) {
			$("div.box div.tand img").css("display", "block");
			$("div.box div.tand form").hide();
			$("div.box div.tand span.success").show();
			$("div.box.ngpluslights em").text(parseInt($("div.box.ngpluslights em").text()) + 1);
			setTimeout("$('div.box div.tand').fadeOut(200)", 4000);
		});
		
		return false;
	});
	
	$("div.box div.tand form input").focus(function() {
		$(this).val("");
	});
	
	$("div.box.ngpluslights").click(function() {
		$("div.box div.tand img").hide();
		$("div.box div.tand span.success").hide();
		//$("div.box div.tand form input").val("Namn");
		$("div.box div.tand form").show();
		$("div.box div.tand").show();
	});
	
	$("div.box a#openBag").click(function() {
		showPageWithUrl("/shop/checkout1.jsp");
		return false;
	});
	
	$("div#productScroller").jScrollPane({scrollbarWidth:12});
});

function showNextImage(slideshowId) {
	$("div.slide:visible", $("div.slideshow#" + slideshowId)).fadeOut(500);
	
	if ($("div.slide:visible", $("div.slideshow#" + slideshowId)).next("div.slide").length) {
		$("div.slide:visible", $("div.slideshow#" + slideshowId)).next().fadeIn(500);
	} else {
		$("div.slide:first", $("div.slideshow#" + slideshowId)).fadeIn(500);
	}
	timer[slideshowId] = setTimeout("showNextImage('" + slideshowId + "');", 5000);
}

function showPage(oid, skipContent) {
	// Make fader layer cover all page
	$("div.fader").height($(document).height());
	$("div.fader").width($(document).width());
	
	// Hide and reset share layers
	$("div.shareButtons div.bubbleShare").hide();
	$("div.shareButtons div.bubbleMail").hide();
	$("div.shareButtons div.bubbleMail form").get(0).reset();
	$("div.shareButtons div.bubbleMail form").show();
	$("div.shareButtons div.bubbleMail #sendOk").hide();
	
	// Show fader layer and popup
	$("div.fader").show();
	$("div.fadedPopup").show();
		
	$.scrollTo("div.fadedPopup");
	
	$("div.fadedPopup div.shareButtons").show();
	$("div.fadedPopup div.shareButtons a.print").attr("href", "/print.jsp?" + oid);
	$("div.fadedPopup div.shareButtons a.share").attr("id", oid.replace("oid=", ""));
	$("div.fadedPopup div.shareButtons div.bubbleMail form input[name=poid]").val(oid.replace("oid=", ""));
	
	if (skipContent == undefined) {
		// Load content
		ajaxHTML("/page_content.jsp", oid, function(html) {
			$("div.fadedPopup div.popupContent").html(html);
			initPopup();
		});
	}
	
	return false;
}

function showPageWithUrl(url) {
	// Make fader layer cover all page
	$("div.fader").height($(document).height());
	$("div.fader").width($(document).width());
	
	// Hide and reset share layers
	$("div.shareButtons div.bubbleShare").hide();
	$("div.shareButtons div.bubbleMail").hide();
	$("div.shareButtons div.bubbleMail form").get(0).reset();
	$("div.shareButtons div.bubbleMail form").show();
	$("div.shareButtons div.bubbleMail #sendOk").hide();
	
	// Show fader layer and popup
	$("div.fader").show();
	$("div.fadedPopup").show();
		
	$.scrollTo("div.fadedPopup");
	
	$("div.fadedPopup div.shareButtons").hide();
	
	// Load content
	ajaxHTML(url, "", function(html) {
		$("div.fadedPopup div.popupContent").html(html);
		initPopup();
	});
	
	return false;
}

function showPopupLogin() {
	// Make fader layer cover all page
	$("div.fader").height($(document).height());
	$("div.fader").width($(document).width());
	
	// Show fader layer and popup
	$("div.fader").show();
	$("div.popupLogin").show();
		
	$.scrollTo("div.popupLogin");
}

function ajaxHTML(urlPost, dataPost, successFunc, errorFunc) {
	$.ajax({
	      url: urlPost,
	      type: "POST",
	      data: dataPost,
	      dataType: "html",
	      success: successFunc,
	      error: errorFunc
	   }
	);
}

function ajaxXMLFromForm(form, successFunc, errorFunc) {
	var url = form.attr("action");
	var data = "";
	
	$("input[type=text], input[type=password], input[type=hidden]", form).each(function() {
		var name = escape(utf8Encode($(this).attr("name")));
		var value = escape(utf8Encode($(this).val()));
		
		if (name != "") {
			data += name + "=" + value + "&";
		}
	});
	
	$("input[type=checkbox]", form).each(function() {
		var name = $(this).attr("name");
		var value = $(this).attr("checked");
		
		if (name != "") {
			data += name + "=" + value + "&";
		}
	});
	
	$("input[type=radio]", form).each(function() {
		var name = $(this).attr("name");
		if (data.indexOf(name) < 0) {
			var value = escape(utf8Encode($("input[name=" + name + "]:checked").val()));
	
			if (name != "") {
				data += name + "=" + value + "&";
			}
		}
	});
	
	$("textarea", form).each(function() {
		var name = escape(utf8Encode($(this).attr("name")));
		var value = escape(utf8Encode($(this).val()));

		if (name != "") {
			data += name + "=" + value + "&";
		}
	});	
	
	$("select", form).each(function() {
		var name = $(this).attr("name");
		var value = escape(utf8Encode($(this).val()));

		if (name != "") {
			data += name + "=" + value + "&";
		}
	});
	
	ajaxXML(url, data, successFunc, errorFunc);
}

function ajaxXML(urlPost, dataPost, successFunc, errorFunc) {
	$.ajax({
	      url: urlPost,
	      type: "POST",
	      data: dataPost,
	      dataType: "xml",
	      success: successFunc,
	      error: errorFunc
	   }
	);
}

function utf8Encode(string) {
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}

var currentPage = 0;
function initPopup() {
	$("div.fadedPopup img.next").click(function() {
		$("div.fadedPopup ul:eq(" + currentPage + ")").hide();
		//$("div.fadedPopup ul:eq(" + (++currentPage) + ")").fadeIn(300);
		$("div.fadedPopup ul:eq(" + (++currentPage) + ")").show();
		if (currentPage == $("div.fadedPopup ul").length - 1) {
			$("div.fadedPopup img.next").hide();
		} else {
			$("div.fadedPopup img.next").show();
		}
		
		if (currentPage == 0) {
			$("div.fadedPopup img.prev").hide();
		} else {
			$("div.fadedPopup img.prev").show();
		}
	});
	
	$("div.fadedPopup img.prev").click(function() {
		$("div.fadedPopup ul:eq(" + currentPage + ")").hide();
		//$("div.fadedPopup ul:eq(" + (--currentPage) + ")").fadeIn(300);
		$("div.fadedPopup ul:eq(" + (--currentPage) + ")").show();
	
		if (currentPage == $("div.fadedPopup ul").length - 1) {
			$("div.fadedPopup img.next").hide();
		} else {
			$("div.fadedPopup img.next").show();
		}
	
		if (currentPage == 0) {
			$("div.fadedPopup img.prev").hide();
		} else {
			$("div.fadedPopup img.prev").show();
		}
	});
	
}
	

