/**
* @fileoverview Javascript only for frontpages 
*/

Event.observe(document, "dom:loaded", function(){
	if(Eniro.Page.is_frontpage){
		/* Set up togglers for Promo Boxes */
		switch(Eniro.Page.tab()){
			case "web":
				if($("linkguide-and-news")){
					var linkguide_and_news = new Eniro.Classes.Toggler("linkguide-and-news", {
						"prefix":"promo-",
						"hide":"Hide linkguide and news".t(),
						"show":"Show linkguide and news".t(),
						"set_cookie":true
					});
				}
				if($("news")){
					var news = new Eniro.Classes.Toggler("eniro-news", {
						"prefix":"promo-",
						"hide":"Hide news".t(),
						"show":"Show news".t(),
						"set_cookie":true
					});
				}
				if($("linkguide")){
					var linkguide = new Eniro.Classes.Toggler("linkguide", {
						"prefix":"promo-",
						"hide":"Hide linkguide".t(),
						"show":"Show linkguide".t(),
						"set_cookie":true
					});
				}
				break;
			case "company":
				var news = new Eniro.Classes.Toggler("eniro-news", {
					"prefix":"promo-",
					"hide":"Hide news".t(),
					"show":"Show news".t(),
					"set_cookie":true
				});
				var branches = new Eniro.Classes.Toggler("branches", {
					"prefix":"promo-",
					"hide":"Hide branches".t(),
					"show":"Show branches".t(),
					"set_cookie":true
				});
				break;
			case "person":
				var news = new Eniro.Classes.Toggler("eniro-news", {
					"prefix":"promo-",
					"hide":"Hide news".t(),
					"show":"Show news".t(),
					"set_cookie":true
				});
				break;
			case "muni":
				var news = new Eniro.Classes.Toggler("eniro-news", {
					"prefix":"promo-",
					"hide":"Hide news".t(),
					"show":"Show news".t(),
					"set_cookie":true
				});
				break;
			case "news":

			case "video":
				/*
				= RSS FEED AJAX HANDLER
				*/
				var RSSAjax = Class.create({ 
				  initialize: function(url, element, time) {
				    if (!(this.target = $(element) || $('RSSContainer')) || !url) return;
				    this.url = url;
				    this.timeout = null;
				    this.time = time;
				    this.fetch(time);
				  },
				  updateProducts: function(r){
				    var response = r.responseText;
				    this.target.innerHTML = response;
				    this.last_response = response;
				  },
				  fetch: function(time){
				    new Ajax.Request(this.url + "&random=" + Math.round(100000*Math.random()), {
				      method:'get',
				      onSuccess:this.updateProducts.bind(this)
				    });
				    this.timeout = setTimeout(this.fetch.bind(this), this.time);
				  }
				});

				/*
				= VIDEO SEARCH LOAD EVENTS, BUBBLARE.se
				*/
				new RSSAjax('/query?what=bubrss&numItems=5','video-bubblare-container',60000);
				
		}
	}
});

/*****************************************************************************
 ************************ Improvements of supersearch ************************
 *****************************************************************************/

/**
* Cookie
* Encapsulates functinality for patching a javascript cookie
*/
var CookiePatch = {
	/**
	* Patch a cookie with the supplied name
	* @param  {String}  name  Identifies the cookie
	* @return  cookie value or NULL if not found
	* @type  String
	*/
	patch: function(name){
		var cookie;
		if ((cookie = Cookie.get(name))) {
			cookie = unescape(cookie).replace(/Â¤undefined/g, "").replace(/Â/g, "");
			
			Cookie.set(name, cookie, {
				"QMFormat":true, 
				"domain": Eniro.Page.main_domain(),
				"expires": 365
			});
		}
		return true;
	}
}

Event.observe(document, "dom:loaded", function(){
	var cookiePatch = CookiePatch.patch("egoSettings");
});

/**
* AddressBook
* Adds a pop-up to mycontacts link
* Counts entries in address book and updates ( ) next to the link
*/
function contactsAmount() {
	var pbCookie = Cookie.get("PhoneBook");
	if (pbCookie) {
		pbCookie = pbCookie.split("£");
		return Math.max(pbCookie.length - 1, 0);
	}
	else {
		return "0";
	}
}

Event.observe(document, "dom:loaded", function(){
	var mycontactlist = $$(".mycontactlist");
	addressBook = false;
	if (mycontactlist) {
		mycontactlist.each(function(e){
			e.observe("click", function(v) {
			var url = e.href;
			v.stop();
			addressBook =  window.open(url, 'addressbook', 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=670,height=600,top=0,left=0', false);
		}, false);
	});
	}
	var contactsamount = $$(".contacts_amount");
	if (contactsamount) {
		contactsamount.each(function (e) {
		e.update(contactsAmount());
		});
	}
});

document.observe("dom:loaded", function() {
	if (typeof(secondaryNav) === "function" && Eniro.Page._body().id === 'web') {
		var secNav = new secondaryNav("secondary-nav");
		secNav.findLinks();
	}
});
