var TalentumHR = function() {
    var DOMReady = function() {
        TalentumHR.slideshowMypageOffer.DOMReady();
    };
    return {
        init: function() {
            Talentum.addOnDOMReady(DOMReady);

            /* IE6 fixes */
            if (document.attachEvent) {
                document.createElement("abbr");
            }

            /* Init modules */
            Talentum.addAnchors.init([
                { element: ".teaser", include: ["IMG"] },
            	{ element: ".packages li", include: ["P", "IMG"] },
            	{ element: ".service-teaser", include: ["P", "IMG"] }
            ]);
            Talentum.mediaplayer.init();
            Talentum.print.init();
            Talentum.slideshow.init();
            Talentum.toggle.init();
            Talentum.removeEmptyAdtechAds.init();
            TalentumHR.recommend.init();
            TalentumHR.toggleIframes.init();
            Talentum.zoom.init();
            Talentum.tooltip.init();
            Talentum.poll.init();
        }
    };
}();



TalentumHR.toggleIframes = function() {
	var buttonClass = "toggle-iframe";
	var easingTime = 300;
	return {
		init: function() {
			Talentum.eventHandler.register("click", "a." + buttonClass, function() {
				this.blur();
				var iframes = $("iframe");
				for (var i = 0, ln = iframes.length; i < ln; i++) {
					if (!$(iframes[i].parentNode.parentNode).hasClass("sil")) {
						$(iframes[i]).setStyle("display", "none");				
					}
				}
				var targetIframeId = Talentum.getClassNameValue(this, buttonClass);
				var targetIframe = $$(targetIframeId);
				targetIframe.setStyle(DOMEffects.getOpacityRule(0));
				targetIframe.setStyle("display", "block");
				targetIframe.fadeIn();
				return true;
			});
		}
	};
}();


TalentumHR.recommend = function() {
	var triggerClass = "toggle-recommend";
	var recommendId = "recommend";

	var DOMReady = function() {
		var recommend = $$(recommendId);
		if (recommend != null) {
			document.body.insertBefore(recommend, document.body.firstChild);
			recommend.setStyle({
				position: "absolute",
				display: "none",
				"z-index": 10000
			});

			var form = $(recommend.cssSelect("form")[0]);
			var fieldset = recommend.cssSelect("fieldset");
			fieldset.removeClass("display-none");
			$$("recommend-from").value = Talentum.userSettings.getPreference("name");
			fieldset.create("a", {href: "", className: "close"}, true, "Avbryt");
			form = new Talentum.AJAXForm(form);
			form.setBeforeSubmissionHandler(function() {
				if (!Talentum.form.validate.call(form)) {
					return false;
				} else {
					this.cssSelect(".alert-box").remove();
				}
				Talentum.userSettings.setPreference("name", $$("recommend-from").value);
			});
			
			form.setResponseHandler(function(data) {
				if (data && data.length > 0) {
					var notice = form.create("span", {className: "notice"}, true);
					notice.setStyle("display", "none");
					fieldset.addClass("display-none");
					notice.setStyle(DOMEffects.getOpacityRule(0));
					notice.setStyle("display", "block");
					data = unescape(data).replace("toggle-recommend", "close");
					notice.innerHTML = data;
					notice.fadeIn();
				}
			});
		}
	};

	return {
		init: function() {
			Talentum.addOnDOMReady(DOMReady);

			Talentum.eventHandler.register("click", "." + triggerClass, function() {
				var recommend = $$("recommend");
				if (recommend) {
					TalentumHR.cover.show();
					recommend.setStyle(DOMEffects.getOpacityRule(0));
					recommend.setStyle("display", "block");
					var width = Talentum.getActualWidth(recommend);
					var height = Talentum.getActualHeight(recommend);
					var pageWidth = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
					var pageHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
					var pageTop = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
					recommend.setStyle({
						left: (pageWidth / 2 - width / 2) + "px",
						top: (pageTop + pageHeight / (1 + Math.sqrt(5) / 2)) - height + "px"
					});
					recommend.fadeIn({duration: 250, callback: function() {
						$$("recommend-to").focus();
					}});
				}
			});
			
			Talentum.eventHandler.register("click", "#" + recommendId + " .close", function() {
				TalentumHR.cover.hide();
				$$(recommendId).hide({duration: 250, callback: function() {
					this.cssSelect("fieldset").setStyle("display", "block");
					this.cssSelect(".alert-box, .notice").remove();
					this.cssSelect(".has-error").removeClass("has-error");
					$$("recommend-to").value = "";
				}});
			});
		}
	};
}();


TalentumHR.cover = function() {
	return {
		resize: function() {
			$$("page-cover").setStyle({
				width: Math.max(Talentum.getActualWidth($$("page")), window.innerWidth ? window.innerWidth : document.documentElement.clientWidth) + "px",
				height: Math.max(Talentum.getActualHeight($("html")[0]), Talentum.getActualHeight($("body")[0]), window.innerHeight ? window.innerHeight : document.documentElement.clientHeight) + "px"
			});
		},
		show: function() {
			var cover = $$("page-cover");
			$("body").setStyle("overflow", "hidden");
			if (!cover) {
				cover = $(document.body).create("div", {id: "page-cover"}, true);
				cover.setStyle({
					position: "absolute",
					left: "0",
					top: "0",
					"z-index": 9999
				});
			}
			TalentumHR.cover.resize();
			cover.setStyle("display", "block");
			$(window).addEvent("resize", TalentumHR.cover.resize);
		},
		hide: function() {
			$("body").setStyle("overflow", "auto");
			$$("page-cover").setStyle("display", "none");
			$(window).removeEvent("resize", TalentumHR.cover.resize);
		}
	};
}();

TalentumHR.slideshowMypageOffer = function() {
    var idPrefix = "slideshow";
    var listSuffix = "-list";
    var navigationSuffix = "-nav";
    var no = 0;
    var duration = 500;
    var autoplayInterval = 5000;
    var elmPos;
    var elmHeight;
    
    var scrollTo = function(id, itemNo) {
               var el = $$(id);
               if (itemNo == el.noOfItems) {
                          itemNo = 0;
               } else if (itemNo < 0) {
                          itemNo = el.noOfItems - 1;
               }
               el.itemNo = itemNo;
               $$(id + listSuffix).animate({"margin-left": -el.width * el.itemNo}, {duration: duration, easing: O.easing.expoOut});
    };
    
    var previous = function(id) {
               scrollTo(id, $$(id).itemNo - 1);
    };
    
    var next = function(id) {
               scrollTo(id, $$(id).itemNo + 1);
    };
    
    var init = function(el, ul, lis, img) {
               var width = O.getActualWidth(el);
               el.width = width;
               ul.style.width = width * el.noOfItems + "px";
               for (var i = 0; i < el.noOfItems; i++) {
                          lis[i].style.display = "block";
                          lis[i].style.width = width + "px";
               }
               var nav = el.create("div", {className: "navigation", id: el.id + navigationSuffix}, true);
               nav.create("a", {className: "previous", href: ""}, true, "Föregående");
               nav.create("a", {className: "next", href: ""}, true, "Nästa");
               el.nav = nav;
               O.eventHandler.register("click", "#" + el.id + " .next", function() {
                          next(el.id);
               });
               O.eventHandler.register("click", "#" + el.id + " .previous", function() {
                          previous(el.id);
               });
               if (el.autoplay) {
                          nav.setStyle(DOMEffects.getOpacityRule(0));
                          TalentumHR.slideshowMypageOffer.autoplayStart(el.id);
                          O.eventHandler.register("mouseout", "#" + el.id, function() {
                                     el.startTimer = setTimeout("TalentumHR.slideshowMypageOffer.autoplayStart('" + el.id + "', true)", 1000);
                          });
                          O.eventHandler.register("mouseover", "#" + el.id, function() {
                                     clearTimeout(el.startTimer);
                                     TalentumHR.slideshowMypageOffer.autoplayStop(el.id);
                          });
               }
    }
    
    return {
               DOMReady: function() {
               
                          
                          $(".slideshow").each(function() {
                                     var el = $(this);
                                     var lis = this.elmsByTag("li");
                                     var noOfItems = lis.length;
                                     el.id = idPrefix + no++;
                                     el.itemNo = 0;
                                     el.noOfItems = noOfItems;
                                     el.autoplay = el.hasClass("autoplay");
                                     el.style.overflow = "hidden";
                                     lis[0].style.display = "block";
                                     if (noOfItems > 1) {
                                               elmPos = O.getPosition(el);
                                               elmHeight = O.getActualHeight(el);
                                               var ul = lis[0].parentNode;
                                               ul.id = el.id + listSuffix;
                                               init(el, ul, lis);
                                     }
                          });
               },
               
               autoplayStart: function(id, hide) {
                          var el = $$(id);
                          if (hide) {
                                     el.nav.hide( { duration: 500 } );           
                          }
                          el.autoplayTimer = setInterval("TalentumHR.slideshowMypageOffer.autoplay('" + id + "')", autoplayInterval);
               },
               
               autoplayStop: function(id) {
                          var el = $$(id);
                          if (el.autoplayTimer) {
                                     clearInterval(el.autoplayTimer);
                                     el.autoplayTimer = null;
                                     el.nav.fadeIn( { duration: 500 } );
                          }
               },
               
               autoplay: function(id) {
                          var viewPortHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
                          var scrollTop = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop
                          if(viewPortHeight+scrollTop > elmPos.y && scrollTop < (elmPos.y+elmHeight)) {
                                     next(id);
                          }
               }
    }
}();


TalentumHR.init();
