(function (window) {
	"use strict";

	var $ = window.jQuery,
		arrowsAdded = false,
		imagesReady = false,
		facebookReady = false,
		view; //Object

	view = {
		equalHeight : {
			threeCols : {
				setHeight : function (callback) {
					$(".socialPage .threeCols").css({
						height : function () {
							
							var that = this,
								t = $(that);

							if (that.style.height) {
								that.style.height = "";
							}

							$.runWhenTrue(function () {
								return (that.style && parseInt(that.style.height, 10));
							}, function () {
								t.addClass("hasHeight");

								if (callback) {
									callback();
								}
							});

							return that.offsetHeight + "px"; 
						}
					});
				},
				init : function () {
					$.runWhenTrue(function () {
						return (imagesReady && arrowsAdded && facebookReady);
					}, function () {
						view.equalHeight.threeCols.setHeight();
					});
				}
			}
		},
		ajax : {
			
			feeds : {

				facebook : {
					
					hasFacebookModule : $("div.module.facebook").length ? true : false,

					fbWallId : "nordiclighthotel",
					fbAccesToken: "206158599425293|7809823973348bcf8cd72f6d.1-100000221135225|BW9n2eoyL7EYvJs7GEmv61NbBFk",

					initFacebookFeed : {

						addFBWallCss : function addFBWallCss() {
							$("head").append("<link href=\"/Facebook/jquery.neosmart.fb.wall.css\" rel=\"stylesheet\" type=\"text/css\" />");
						},

						addFBWallScript : function addFBWallScript() {
							$("body").append("<script src=\"/Facebook/jquery.neosmart.fb.wall.js\" type=\"text/javascript\" />");
						},

						addFBWallUl : function addFBWallUl() {
							$("div.module.facebook h2").after("<ul class=\"faceBookWall\" />");
						},

						addFBFeed : function addFBFeed() {
							$("ul.faceBookWall").fbWall({ 
								id: view.ajax.feeds.facebook.fbWallId, 
								accessToken: view.ajax.feeds.facebook.fbAccesToken,
								showGuestEntries : true,
								showComments : true
							});
						},

						setFaceBookReady : function setFaceBookReady() {
							$.runWhenTrue(function () {
								return $("ul.faceBookWall li").length;
							}, function () {
								facebookReady = true;
							});
						}
					},

					init : function initFacebook() {
						if (view.ajax.feeds.facebook.hasFacebookModule) {
							 $.runAll.apply(view.ajax.feeds.facebook, ["initFacebookFeed"]);
						} else {
							facebookReady = true;
						}
					}
				},

				rssBox : {
					
					hasRssBox : $("div.rssBox").length ? true : false,

					setRssBoxUlHeight : function setRssBoxUlHeight() {
						$(".rssBox > h2 + ul").css({
							height : function () {
								return $(this).height() + "px";
							}
						}).addClass("hasHeight");
					},

					initRssBox : {

						setYouTubeBoxUlHeight : function setYouTubeBoxUlHeight() {
							$(".youTube.rssBox ul").css({
								height : function () {
									var that = this,
										t = $(that),
										liHeight = t.find("li").height();

									t.css("height", liHeight + "px");
								}
							});
						},

						// check if the facebook wall has been loaded, the arrows has been added and that all images are loaded then set the height of all rssBoxes
						checkFacebookReady : function checkFacebookReady() {
							$.runWhenTrue(function () {
								return (imagesReady && arrowsAdded && facebookReady);
							}, function () {
								view.ajax.feeds.rssBox.setRssBoxUlHeight();
							}, 100, 1000);
						}	

					},

					init : function initRssBox() {
						if (view.ajax.feeds.rssBox.hasRssBox) {
							$.runAll.apply(view.ajax.feeds.rssBox, ["initRssBox"]);
						}
					}

				},

				addFeedArrows : {
					
					init : function () {
						
						var iv,
							scrollUL = function scrollUl(dir) {
								var that = this,
									t = $(that),
									ul = t.closest("p").prev("ul"),
									ulElem = ul.get(0),
									scrollHeight = 20, //ulElem.offsetHeight,
									scroll = function () {
										
										var st = ulElem.scrollTop;

										if (dir === "down") {
											ul.get(0).scrollTop = parseInt(st + scrollHeight, 10);
										} else {
											ul.get(0).scrollTop = parseInt(st - scrollHeight, 10);
										}
									};

								iv = setInterval(scroll, scrollHeight);
							};

						$(".socialPage .module:not(.noArrows)").append($.create({
							tagName : "p",
							className : "feedArrows",
							append : [{
								tagName : "a",
								href : "/",
								click : function (e) {
									e.preventDefault();
								},
								mousedown : function () {
									scrollUL.apply(this, ["up"]);
								},
								mouseup : function () {
									clearInterval(iv);
								}
							}, {
								tagName : "a",
								href : "/",
								click : function (e) {
									e.preventDefault();
								},
								mousedown : function () {
									scrollUL.apply(this, ["down"]);
								},
								mouseup : function () {
									clearInterval(iv);
								}
							}, {
								className : "cb" 
							}]
						}));

						$.runWhenTrue(function () {
							return $(".socialPage .module p:last-child > a").length;
						}, function () {
							arrowsAdded = true;
						});
					}
				}
			},

			tripadvisor : {
				init : function () {

					$("div.module.tripadvisor ul").delegate("li, a", "click", function (e) {
						
						if (e.preventDefault) {
							e.preventDefault();
						}

						if (e.stopPropagation) {
							e.stopPropagation();
						}

						var that = this,
							t = $(that),
							href = that.href || t.find("a:first").attr("href"),
							ul = t.closest("ul"),
							phDiv = $.create({
								className : "phDiv"
							}),
							addHideLink = function addHideLink() {
								phDiv.appendChild($.create({
									tagName : "a",
									href : "/",
									innerHTML : "D\u00f6lj",
									click : function (e) {
										e.preventDefault();

										$(phDiv).remove();

										ul.show();
									}
								}));
							};

						ul.addClass("ajaxLoader").before(phDiv);

						$(phDiv).load(href + " #specificReview", function () {
							ul.hide().removeClass("ajaxLoader");
							addHideLink();
							view.equalHeight.threeCols.setHeight();
						});

					});
				}
			}
		},
		init : {
			imagesReady : function () {

				var imgs = $("img");

				imgs.each(function () {
					
					var that = this,
						count = 0,
						stopCount = 3,
						lw = that.offsetWidth,
						lh = that.offsetHeight;
						
					$.runWhenTrue(function () {
						
						var w = that.offsetWidth,
							h = that.offsetHeight;

						if (w && h && (w === lw && lh === h)) {
							count += 1;
						} else {
							count = 0;
						}

						lw = that.offsetWidth;
						lh = that.offsetHeight;

						return (count === stopCount);

					}, function () {
						imagesReady = true;
					}, 100, 100);

				});

			},
			relExternal : function () {
				
				// Check if any link (without rel="external") is an external one and open it in a new window
				$(".socialPage a:not([rel~=external])").each(function () {

					var that = this,
						t = $(that),
						domain = new RegExp("^(http:\/\/" + document.domain.toString() + "|\/)", "ig"),
						href = that.href,
						isExternal = href ? !(domain).test(href) : false;

					if (isExternal) {
						t.click(function (e) {
							e.preventDefault();
							window.open(this.href);
						});
					}
				});

				$("a[rel~=external]").click(function (e) {
					e.preventDefault();
					window.open(this.href);
				});
			},
			ie78 : function () {
				var ua = window.navigator.userAgent;

				if ((/MSIE\s[78]/).test(ua)) {

					$("body").addClass("ie" + ua.match(/MSIE\s[78]/gi).toString().replace(/\D/g, ""));
				}
			}
		}
	};

	$.runInit(view);

}(window));
