var JUNE = {}
var June = {
    userCountry: undefined,
    siteCountry: undefined,
    init: function() {
        $("body").addClass("js");
        June.slideshows.init();
        June.links.init();

        if(google && google.loader && google.loader.ClientLocation) {
            this.userCountry = google.loader.ClientLocation.address.country_code;
        }
        if(this.userCountry !== undefined && this.userCountry != "SE" && this.siteId == 1) {
            June.alert.show();
        }
        
		June.sitemap = new June.Expander("June.sitemap", "toSitemap", "sitemap", "sitemap wideBox");
		June.sitemap.init();
		June.search = new June.Expander("June.search", "toSearch", "search", "searchFields wideBox");
		June.search.init();
		var subMenu;
		if(subMenu = document.getElementById("submenu")) {
			var subMenuHeight = subMenu.clientHeight;
			var contentArea = document.getElementById("content");
			var contentAreaHeight = contentArea.clientHeight;
			if (contentAreaHeight < subMenuHeight) {
				contentArea.style.height = subMenuHeight + "px";
			}
		}
	},
    alert: {
        markup: '<div id="alert" class="bubble type3 bubble-top" style="display: none;"><div><p>Hello! Are you sure you don\'t want the <a href="en">english version</a> of our web site?</p></div></div>',
        show: function() {
            $("#services").append(this.markup);
            var alertHeight = $("#alert").outerHeight();
            $("#services").animate({
                height: alertHeight + 52
            }, "normal", function() {
                $("#alert").fadeIn("normal");
            });
            //$("#services").slideDown("fast");  
        },
        hide: function() {

        }
    },
    links: {
        init: function() {
            $(".jLink")
            .click(function(event) {
                event.preventDefault();
                if (link = June.links.getSelectorLink(event.target, ".jLink")) {
                    document.location.href = link.href;
                }
            });
        },
        getSelectorLink: function(target, selector) {
            var $selectedElement = $(target).closest(selector);
            if ($selectedElement[0].tagName != "A") {
                return $selectedElement.find("a")[0];
            }
            else {
                return $selectedElement[0];
            }
        }
    },
    slideshows: {
        init: function() {
            // Nivo
            if($("#slider img").length > 1) {
                $("#slider").nivoSlider({
                    effect: "sliceDown",
                    slices: 20,
                    animSpeed: 1000,
                    pauseTime: 10000,
                    startSlide: 0,
                    directionNav: true,
                    directionNavHide: true,
                    controlNav: false,
                    controlNavThumbs: false,
                    keyboardNav: false,
                    pauseOnHover: true,
                    manualAdvance: false,
                    captionOpacity: 0.8,
                    beforeChange: function() { },
                    afterChange: function() { },
                    slideshowEnd: function() { }
                });
            }
            else {
                $("#slider img:first-child").show();
            }
           
            // Showcase
	   	    $(".interactiveSlider").showcase({
			    animation: { 
			    	interval: 7500,
                	stopOnHover: true,
                	easefunction: "swing",
                	speed: 600
                },
				css: { border: "solid 3px #121212", width: "450px", height: "266px" },
			    navigator: { 
			    	customNavigator: ".interactiveSliderNav",
			    	position: "top-left",
        			css: { width: "100px", padding:"0px", margin: "0px 0px 1px 1px" },
        			showNumber: true,
        			item: {
            			css: {
	            			height:"16px",
	                    	"line-height":"16px",
	                    	width:"16px",
	                    	"-moz-border-radius": "0px",
	                    	"-webkit-border-radius": "0px",
	                    	backgroundColor: "#383838",
	                    	borderColor:"#323232",
	                    	margin: "0px",
	                    	"text-align": "center",
	                    	"vertical-align": "middle"
	                    },
		            	cssHover: {
		                	backgroundColor: "#ababab",
		                	borderColor: "#ababab"
		                },
		            	cssClassSelected: "selected"
		            }
			    },
			    titleBar: { enabled: false },
                callback: function() {
                    $("#sc-slider").children("a").css({"display":"block"});
                }
			});
        }
    },
	ajax: {
		ajaxParamName: "ajax",
		ajaxParamValue: "true",
		get: function(url, id, script) {
	        if (url.indexOf("?") >= 0) {
	            url += "&";
	        } else {
	            url += "?";
	        }
	        url += "rnd=" + new Date().getTime();
	        if (this.ajaxParamName != null && this.ajaxParamName.length > 0) {
				url += "&" + this.ajaxParamName;
		        if (this.ajaxParamValue != null && this.ajaxParamValue.length > 0) {
					url += "=" + this.ajaxParamValue;
				}
			}
			try {
	            YAHOO.util.Connect.asyncRequest("GET", url, this._getCallBack(id, script));
	        } catch(e) {}
	    },
		post: function(form, id, script) {
	        if (this.ajaxParamName != null && this.ajaxParamName.length > 0) {
				if (!form.elements[this.ajaxParamName]) {
					var input = document.createElement("input");
					input.type = "hidden";
					input.name = this.ajaxParamName;
			        if (this.ajaxParamValue != null && this.ajaxParamValue.length > 0) {
						input.value = this.ajaxParamValue;
					}
					form.appendChild(input);
				}
			}
	        YAHOO.util.Connect.setForm(form);
	        try {
	            YAHOO.util.Connect.asyncRequest("POST", form.action, this._getCallBack(id, script));
	        } catch(e) {}
	    },
	    _successHandler: function(o) {
			if (o.responseText !== undefined) {
	            if (o.argument.id != null) {
	                var obj = document.getElementById(o.argument.id);
	                if (obj != null) {
	                    obj.innerHTML = o.responseText;
	                }
					if (o.argument.script) {
						eval(o.argument.script);
					}
	            }
	        }
	    },
	    _getCallBack: function(id, script) {
	        return {
	            success: this._successHandler,
	            argument: { id: id, script: script}
	        };
	    }
	},
	Expander: function(name, clickId, objId, objClasses) {
		this.isLoaded = false;
		this.isActive = false;
		this.isTweening = false;
		this.animIn = null;
		this.animOut = null;
		this.name = name;
		this.clickId = clickId;
		this.objId = objId;
		this.objClasses = objClasses;
	},
	expanders: Array(),
	expanderQueue: Array()
}
June.Expander.prototype.init = function () {
	var clickTarget = document.getElementById(this.clickId);
	if (clickTarget) {
		YAHOO.util.Event.on(this.clickId, "click", function(e) {
			if (!this.isTweening) {
				if (this.isLoaded) {
					if (this.isActive) {
						this.hide();
					} else {
						this.show();
					}
				} else {
					var obj = document.createElement("div");
					obj.id = this.objId;
					obj.className = this.objClasses;
					obj.style.visibility = "hidden";
					document.getElementById("services").appendChild(obj);
					June.ajax.get(clickTarget.href, this.objId, this.name + ".activate()");
				}
			}
		    YAHOO.util.Event.stopEvent(e);
	    }, this, true);
		June.expanders.push(this);
	}
	var obj = document.getElementById(this.objId);
	if (obj) {
		this.activate();
	}
}
June.Expander.prototype.activate = function() {
	var objHeight = document.getElementById(this.objId).clientHeight + 50;
    this.animOut = new YAHOO.util.Anim("services", { height: { to: objHeight } }, .5, YAHOO.util.Easing.easeOutStrong);
	this.animOut.onComplete.subscribe(function() { this.isTweening = false; this.isActive = true; document.getElementById("services").style.height = (document.getElementById(this.objId).clientHeight + 50) / 13 + "em"; }, this, true);
    this.animIn = new YAHOO.util.Anim("services", { height: { to: 17 } }, .5, YAHOO.util.Easing.easeOutStrong);			
	this.animIn.onComplete.subscribe(function() { document.getElementById(this.objId).style.display = "none"; this.isTweening = false; this.isActive = false; this.showNext(); }, this, true);
	this.isLoaded = true;
	this.show();
}
June.Expander.prototype.show = function() {
	for (var i = 0; i < June.expanders.length; i++) {
		if (June.expanders[i] != this) {
			if (June.expanders[i].isActive) {
				June.expanders[i].hide();
				June.expanderQueue.push(this);
				return;
			}
		}	
	}
	this.isTweening = true;
	var obj = document.getElementById(this.objId);
	obj.style.visibility = "visible";
	obj.style.display = "block";
	this.animOut.animate();
}
June.Expander.prototype.hide = function() {
	this.isTweening = true;
	this.animIn.animate();
}
June.Expander.prototype.showNext = function() {
	if (June.expanderQueue.length > 0) {
		June.expanderQueue.shift().show();
	}
}
//June.init();


function hideDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'none';
}

function showDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'block';
}

