var HolviksAppDelegate = Class.create({
	initialize: function() {
		
		$('content').appear({duration: .7,delay:.8});
		this.content = $("textbox");
		this.portfolioTilt();
		this.runFlir();
		this.ajaxify();	
		this.bigTextFields();
		this.coolMenu();
		if(location.hash!=""){
			this.hashLoad();
		};
		
		
		Event.observe($("holviks-email"), 'click', function(e){
        	//window.location = element.href;

        	
        	Event.stop(e);
        	
        	var info = "person=" + $("holviks-email").down('a').href.split("#")[1];
			Modalbox.show('/global/specialpages/test.php', {params: info,title: 'HOLVIKS REKLAMBYR', afterLoad: function(){
				FLIR.replace($('popinforub'), new FLIRStyle({ cFont:'gillsanslight' }) );
				
			}});        
		
		});
	
		
	},
	initStaffLinks:function()
	{
		
		$("staff").select(".personal").each(function(elm)
		{
			if($(elm).down('a'))
				$(elm).down('a').bigtargetstaff();			
		});
		
	},
	runFlir: function(one, two)
	{
		FLIR.init( { path: '/global/scripts/facelift/' } );
		FLIR.replace(['h1','h2','h3'], new FLIRStyle({cFont:'gillsanslight', mode:'wrap'}) );
		FLIR.replace('.ing', new FLIRStyle({cFont:'gillsanslight',mode:'wrap'}) );
		FLIR.replace('div#langBox', new FLIRStyle({cFont:'gillsanslight'}) );
	},	
	bigTextFields: function()
	{
		if($("staff"))
		{
			this.initStaffLinks();
		}
		else
		{
		
		$("textbox").select(".textblock").each(function(elm, i){
				if($(elm).down('a'))
				{
					//$(elm).down('a').bigtarget();
				}
			
		}.bind(this));
		
		}
	},

	portfolioTilt: function() { 
		//var c = 0;
		var block=$('textbox').select('.floatRight');
		var image=$('textbox').select('.projimage');
		image.each(function(elm, i){
			var degree = Math.ceil((2*Math.random())-6)
			var path=image[i].readAttribute('src');
			roPhoto= new Photo().rotate(degree).resize({width:200}).load(document.location.hostname+path).dropShadow();
			//block[i].appendChild(roPhoto.fetch());
		});
		
	},
	
	ajaxify: function() {
		var self = this;
		$("textbox").select("a").each(function(elm, i){
				var b = $(elm);
				if($(b))
				{
					if(!b.hasClassName("websitelink"))
						Event.observe(b, 'click', this.linkSelected.bindAsEventListener(self, elm));
				}
			
		}.bind(this));
	},
	
	linkSelected: function(e, elm)
	{
		Event.stop(e);
		var url = elm.readAttribute('href');
		var rel = elm.href.split(document.location.hostname);
        elm.setAttribute("rel",rel[1])
		SWFAddress.setValue(elm.rel);
		this.ajaxLoad(url);
		
	},
	
	hashLoad: function()
	{
		var url = location.hash.split('#');
		this.isDefined(url);			
	},
	
	isDefined: function(url)
	{
		if(url[1]===undefined){
			this.chkMenClass('/hem/');
			this.ajaxLoad('/hem/');
		}else{
			this.chkMenClass(url[1]);
			this.ajaxLoad(url[1])
		}
	},
	
	ajaxLoad: function(url)
	{
		this.content.hide();
		var self = this;
		this.therequest = new Ajax.Request(url,{
			method: "get",
			onSuccess: function(transport){
            	self._processData(transport)
            	}
		});
	},
	coolMenu: function()
	{
	
		var self = this;
		
		$("globalnav").select("li").each(function(elm, i){
				$(elm).hide().appear({duration:(i+1)/3});
				/*$$('div li a').each(function(elm){
					elm.observe('click',function(evnt){
					})
				})*/
				
				var b = $(elm).down('a');
				
				
				if($(b))
				{
					if($(elm).id != "holviks-email")
						Event.observe(b, 'click', this.menuSelected.bindAsEventListener(self, elm));
				}
			
		}.bind(this));
	},
	
	chkMenClass: function(url)
	{
		var u = url.split("/");
		u="/"+u[1]+"/";
		$("globalnav").select('li').each(function(aa){
			aa.removeClassName('selected');
		});
		$("globalnav").select('li').each(function(aa){
			if($(aa.down('a')))
			{
			if(aa.down('a').rel==u){
				aa.addClassName('selected');
			}
			}
		});
	},
	
	menuSelected: function(e, elm)
	{
		
		Event.stop(e);
		
		$("globalnav").select('li').each(function(aa){
			aa.removeClassName('selected');
		});
		
		elm.addClassName('loading');
		//elm.addClassName('selected');
		
		
		var url = elm.down('a').readAttribute('href');
		var rel = url.split(document.location.hostname);
		if(rel[1]==undefined){
		SWFAddress.setValue('/hem/');
		}
		else{
		SWFAddress.setValue(rel[1]);
		}		
		var url = elm.down('a').readAttribute('href');
		this.content.hide();
		var self = this;
		this.therequest = new Ajax.Request(url,{
			method: "get",
			onSuccess: function(transport){
				elm.removeClassName('loading');
				elm.addClassName('selected');
            	self._processData(transport);
            	}
		});
		
	},
	
	_processData: function(DataRequest, ResponseHeader){
		this.hUrl=location.hash;
		var text=DataRequest.responseText.split('<div id="textbox">');
		var title = text[0].split('<title>');
		title=title[1].split('</title>');
		SWFAddress.setTitle(title[0]);
		text=text[1].split('<!--/textbox-->');
		
		this.content.update(text[0]);
		this.content.show();
		this.runFlir();
		this.content.hide();
		this.content.appear();
		//this.content.wrap('div');
		//this.content.appear({ duration: 1.0 });
		//Effect.SlideDown($(this.content),{scaleX:true, scaleY:false});
		
		this.portfolioTilt();
		this.bigTextFields();
		this.ajaxify();	
		hUrl=location.hash;
	}

});


document.observe('dom:loaded', function() {
	var holviksapp = new HolviksAppDelegate();
});
