var ajaxAnywhereDefaultloadingMessage = "Loading...";

function popUpImage(id, width, height, title){
    showImage(id, title);
}

function popUpOrigImage(id, width, height, title){
    var content = "<html><head><title>" + title + "</title><style type=\"text/css\">body{margin: 0; padding: 0;}</style></head><body><img style=\"cursor: pointer\" onclick=\"window.close()\" src=\"/ferd/image.do?orig_id=" + id + "&w="+width+"\" border=\"0\"></body></html>";
    var popupimage = window.open("", "popupimage", "width=" + width + ",height=" + height + ",status=no,resizable=yes,scrollbars=0,top=200,left=400");
    popupimage.focus();
    popupimage.opener = self;
    var popupDoc = popupimage.document;
    popupDoc.innerHTML = "";
    popupDoc.write (content);
    popupimage.resizeTo(width,height+2);
    var pWidth = popupDoc.documentElement.offsetWidth;
    var pHeight = popupDoc.documentElement.offsetHeight;
    popupDoc.close();
    if ( width > pWidth || height > pHeight ){
	popupimage.resizeTo(2*width-pWidth+4,2*height-pHeight+5);
    }
}

function openMap() {
	var url = document.location.pathname + "?page=49";
	var popup = window.open(url, "map", "resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no,width=430,height=720" );
	popup.focus();
}
function Openme(newin) {
    var flyout=window.open(newin,"flyout","resizable=yes,scrollbars=yes,location=no,menubar=no,toolbar=no,width=420,height=400");
	flyout.focus();
}

function showPrintPage(){
	var url = document.location + "&printpage=true"
    var width = 700;
    var height = 600;
    var printPage = window.open(url, "printpage", "width=" + width + ",height=" + height + ",toolbar=1,menubar=1,status=no,resizable=yes,scrollbars=1,top=100,left=100");
    printPage.focus();
	printPage.onload = printPage.print;
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages(lang) {
	if (document.images) {
		invest_over = newImage("docs/images/menub/"+lang+"/invest-over.jpg");
		eiendom_over = newImage("docs/images/menub/"+lang+"/eiendom-over.jpg");
		venture_over = newImage("docs/images/menub/"+lang+"/venture-over.jpg");
		konsern_over = newImage("docs/images/menub/"+lang+"/konsern-over.jpg");
		principal_over = newImage("docs/images/menub/"+lang+"/principal-over.jpg");
		equity_over = newImage("docs/images/menub/"+lang+"/equity-over.jpg");
		elopak_over = newImage("docs/images/menub/"+lang+"/elopak-over.jpg");
		preloadFlag = true;
	}
}

function clearSearchBox(el){
    if(el.value == "Search:" || el.value == "Søk:"){
        el.oldValue = el.value;
        el.value = "";
        el.onblur = function(){
            if(this.value == "" && this.oldValue){
                this.value = this.oldValue;
            }
        }
    }
}

function placeElementCentered(element) {
    element.style.display = "";
    var documentHeight = document.documentElement.offsetHeight ? document.documentElement.offsetHeight:document.body.offsetHeight;
    var documentWidth =  document.documentElement.offsetWidth ? document.documentElement.offsetWidth:document.body.offsetWidth;
    var popupWidth =     element.offsetWidth;
    var popupHeight =    element.offsetHeight;
    var scrollX = document.body.scrollLeft ? document.body.scrollLeft:document.documentElement.scrollLeft;
    var scrollY = document.body.scrollTop ? document.body.scrollTop:document.documentElement.scrollTop;
    var x = Math.floor((documentWidth/2) - (popupWidth/2));
    var y = Math.floor((documentHeight/2) - (popupHeight/2));
    y = y + scrollY < 0 ? 0:y + scrollY;
    x = x + scrollX < 0 ? 0:x + scrollX;
  
    element.style.top = y +  "px";
    element.style.left = x + "px";
}



function showImage(image, title){
	var ic = $("imageContainer");

    //get imageContainer
    if(!ic){
        ic = document.createElement("div");
        ic.style.position = "absolute";
        ic.className = "imageContainer";
        ic.id = "imageContainer";
        ic.style.display = "none";
        document.body.appendChild(ic);
    }

    if(ic){
	    ic.style.display  = "";
        ic.innerHTML = "";
        var a = document.createElement("a");
        a.onclick = function(){hideImage()};
        a.innerHTML = "[ x ]";
        ic.appendChild(a);
        var img = document.createElement("img");
        if(parseInt(image) > 0){
            img.src = "/image.do?id=" + image;
        }else{
            img.src = image;
        }
        img.alt = title;
        img.onload = function(){placeElementCentered(ic);};
        img.onclick = function(){hideImage()};
        ic.appendChild(img);
    }
}

function hideImage(){
	var ic = $("imageContainer");
    if(ic){
        ic.style.display = "none";
    }
}

function log(txt){
    if(window.console){
        window.console.log(txt);
    }
}

function getAjaxForMap(map, async, callback){
    var url = "show.do";

	map = $H(map);
	if(!map.get("page")) {
		map.set("page", "186");
    }

    if(!map.get("appmode")) {
		map.set("appmode", "/tipFriend");
    }

   url += "?" + map.toQueryString();

	var myAjax;
	if(async){
		myAjax = getFreeAjax();
	}else{
		myAjax = ajaxAnywhere;
	}
	if(callback == undefined){
		myAjax.onAfterResponseProcessing =  function(){}
	}else{
		myAjax.onAfterResponseProcessing =  function(){
			//log("jeg skal kjøre callbacken");
			eval(callback);
		}
	 }
	myAjax.getAJAX(url);
}

var ajaxmap = new Array();

function getFreeAjax(){
   // log("getFreeAjax: size: " + ajaxmap.length);
    for(var i=0;i<ajaxmap.length;i++){
        if(ajaxmap[i].req.readyState == 0 || ajaxmap[i].req.readyState == 4) {
            //log("fant en brukt en: " + ajaxmap[i]);
            return ajaxmap[i];
        }
    }
    //we didn't return any thing. Lets make a new one
    var newId = ajaxmap.length;
    ajaxmap[newId] =  new AjaxAnywhere();
    ajaxmap[newId].bindById();
    //log("laget en ny: " + newId);
    return ajaxmap[newId];
}

function submitAJAX(formName, ajaxAnywhereInstance){
	var _localAjaxAnywhere = (ajaxAnywhereInstance != undefined ? ajaxAnywhereInstance : ajaxAnywhere);
    _localAjaxAnywhere.formName = formName;
    _localAjaxAnywhere.submitAJAX();
    return false;
}


function getZone(zoneName){
	zoneName = "aazone." + zoneName;
	if($(zoneName)){
		return $(zoneName);
	} else {
		var zone = document.createElement("div");
		zone.style.display = "inline";
		zone.id = zoneName;
		document.body.appendChild(zone);
		return zone;
	}
}




/*thisIsFerd*/
var thisIsFerdBackgrounds = [
	"files/ferd/gfx/thisIsFerd/image1.jpg",
	"files/ferd/gfx/thisIsFerd/image2.jpg",
	"files/ferd/gfx/thisIsFerd/image3.jpg",
	"files/ferd/gfx/thisIsFerd/image4.jpg",
	"files/ferd/gfx/thisIsFerd/image5.jpg",
	"files/ferd/gfx/thisIsFerd/image6.jpg"
];

var fadeInCounter = 1;
var thisIsFerdChangeTime = 10000;

function fadeOutThisIsFerd() {
	var front = document.getElementById("thisIsFerdFront");
	var back = document.getElementById("thisIsFerdBack");
	if(front.style.opacity > 0) {
		var opacity = front.style.opacity*1 - 0.1;
		setOpacity(front, opacity);
		setTimeout("fadeOutThisIsFerd()", 200);
	} else {
		fadeInCounter++;		
		front.style.backgroundImage = back.style.backgroundImage;
		setOpacity(front, 1);
		if(fadeInCounter >= thisIsFerdBackgrounds.size()) {
			fadeInCounter = 0;
		}
		back.style.backgroundImage = "url(" + thisIsFerdBackgrounds[fadeInCounter] + ")";
		setTimeout("fadeOutThisIsFerd()", thisIsFerdChangeTime);
	}
}

function setOpacity(el, value) {
	el.style.opacity = value;
	el.style.filter = 'alpha(opacity=' + value*100 + ')';
}

var imageArray = new Array();
function startThisIsFerd() {
	thisIsFerdBackgrounds.each( function (src) {
		var img = new Image();
		img.src = src;
		imageArray[imageArray.length] = img;
	});
	var front = document.getElementById("thisIsFerdFront");
	front.style.opacity = 1;
	setTimeout("fadeOutThisIsFerd()", thisIsFerdChangeTime);
}

function openAsPopup(event, el) {
    if(!el || !el.href) return;
    Event.stop(event);
    var win = window.open(el.href, "ferd_popup", "status=no,toolbar=no,menubar=no,location=no");
    win.focus();
}


var HistoryList = {
	current : undefined,

	setAsCurrent : function(el) {
		el = $(el);
		this.selectOnlyThis(el);
		this.updateDetail(el.up().down("div.historyListItemDetail").innerHTML);
		this.updateListHeight();
	},

	selectOnlyThis : function(el) {
		el = $(el);
		this.deselectCurrent();

		el.addClassName("historyListItemSelected");
		this.current = el;
	},

	deselectCurrent : function() {
		if(this.current) {
			this.current.removeClassName("historyListItemSelected");
			this.current = undefined;
		}
	},

	updateListHeight : function() {
		$("historyListScroller").style.height = $("historyListItemContainer").up("td.middleCenter").offsetHeight + "px";
	},

	updateDetail : function(content) {
		$("historyListItemContainer").update(content);
	},

	setMainAsCurrent : function() {
		this.deselectCurrent();
		this.updateDetail($("historyListMainArticle").down("div.historyListItemDetail").innerHTML);
	}

};


	var RealEstateShow = (function(){

		function getLargeImageContainer() {
			return $("realEstateImages").down(".imageLarge");
		}

		function getLargeImageForNr(imageNr) {
			return getLargeImageContainer().down(".image"+imageNr);
		}

		function getActiveLargeImage() {
			return getLargeImageContainer().down(".activeImage");
		}

		function switchToNewLargeImage(newImage) {
			var activeImage = getActiveLargeImage();

			if(newImage && newImage != activeImage ) {
				activeImage.removeClassName("activeImage");
				newImage.addClassName(("activeImage"));
			}
		}

		function showCorrectImage(imageNr) {
			switchToNewLargeImage(getLargeImageForNr(imageNr));
		}


		function displayNextImageInList() {
			var activeImage = getActiveLargeImage();
			switchToNewLargeImage(activeImage.next("img") || activeImage.up().down("img"));
		}

		return {
			displayImage : function(imageNr) {
				showCorrectImage(imageNr);
			},

			showNextImage : function() {
				displayNextImageInList();
			}
		}
	})();


var OrderProspect = (function(){

	var getResponse = function (params) {
		getOrderZone();
		params = $H(params);
		params.set("appname", "ferd");
		params.set("appmode", "/orderProspect");
		getAjaxForMap(params);
	};

	var getOrderZone = function() {
		return getZone("orderProspect");
	};

	return {
		openDialog : function (articleId, localeCode) {
			getResponse({ action : "open", articleId : articleId, localeCode : localeCode});
		},

		closeDialog : function() {
			Element.update(getOrderZone(), "");
		},

		send : function (el) {
			el.onclick = function() {
				return false;
			}
		}

	}
})();


var ConferenceRegistration = (function(){

	var getResponse = function (params) {
		getThisZone();
		params = $H(params);
		params.set("appname", "ferd");
		params.set("appmode", "/conferenceRegistration");
		getAjaxForMap(params);
	};

	var getThisZone = function() {
		return getZone("conferenceRegistration");
	};

	return {
		openDialog : function (registrationType) {
			getResponse({ action : "open", registrationType : registrationType });
		},

		closeDialog : function() {
			Element.update(getThisZone(), "");
		},

		send : function (el) {
			el.onclick = function() {
				return false;
			}
		}

	}
})();

