var geocoder;
var map;
var infoWindow;

window.addEvent('domready', function(){

	//call multiBox
	var initMultiBox = new multiBox({
		mbClass: '.mb',//class you need to add links that you want to trigger multiBox with (remember and update CSS files)
		container: $(document.body),//where to inject multiBox
		descClassName: 'multiBoxDesc',//the class name of the description divs
		path: '',//path to mp3 and flv players
		useOverlay: true,//use a semi-transparent background. default: false;
		maxSize: {w:600, h:400},//max dimensions (width,height) - set to null to disable resizing
		addDownload: false,//do you want the files to be downloadable?
		addRollover: false,//add rollover fade to each multibox link
		addOverlayIcon: false,//adds overlay icons to images within multibox links
		addChain: false,//cycle through all images fading them out then in
		recalcTop: true,//subtract the height of controls panel from top position
		addTips: false,//adds MooTools built in 'Tips' class to each element (see: http://mootools.net/docs/Plugins/Tips)
		autoOpen: 0//to auto open a multiBox element on page load change to (1, 2, or 3 etc)
	});

	/* generic code to open all links with rel=_blank in new window, to pass validation in xhtml strict. */
	$$('a').each(function(link,i){
		if(link.get('rel')=='_blank') link.setProperty('target','_blank');
	});	
	/* end link code */

	/* Standard function to give mCCWrapper the remaining window height if avoidElements is defined */
	//autosize_mCCWrapper();

	/* Standard function to mark all last listitems with class=last in the current document - default enabled */
	markLastLis();
	
	/* Standard function to mark left/right sibling on list - enable with .mark_siblings on list */
	markSiblings();
	
	// Do google map
	if($(document.body).hasClass('do_map'))
	{
	  // initialize map
	  var latlng = new google.maps.LatLng(59.171976787829, 10.215396881103516); // center point for the map
	  var mapOptions = {
	    zoom: 15,
	    center: latlng,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	  };
	  map = new google.maps.Map(document.getElementById("mapC"), mapOptions);
	  geocoder = new google.maps.Geocoder();
  	infoWindow = new google.maps.InfoWindow();
    var address = 'Hunsrødveien 27 3241 Sandefjord, Norway';
    var business =  'Hurricane';
    codeAddress(business, address, 0);
	}

	var noobGallery = $('noobGallery');
 	if(noobGallery != null){
		
		var items_array = [];
		var img_items = $$('#noobGallery img');
		if(img_items != null && img_items.length > 0)
		{
			for(var i=0; i<Math.ceil(img_items.length/4); i++)
			{
				items_array[i] = i;
			}
		}
		
		var ns4 = new noobSlide({
			box : $('noobGallery'),
			items : items_array,
			startItem : 0,
			size : 239,
			onWalk : function(currentItem, currentHandle){
			
			},
			addButtons : {
				previous : $('previous'),
				next : $('next')
			}
		});
	
	}	
	
	
	// actions for fylke-oversikt
	Array.each($$('#fylker area'),function(el,i){
		el.addEvent('click',function(e){
			e.stop();
			Array.each($$('#fylke option'),function(option,j){
				option.selected = false;
				if(option.get('value')==el.get('href'))
					option.selected = true;
			});
			load_fylke_list();
		});
	});
	
	// hook up category flap link
	Array.each($$('.categoryC li'),function(el,i){
		el.addEvent('click',function(e){
			document.location.href = el.getFirst('a').get('href');
		})
	});
	
	// Setup flaps
	if($(document.body).hasClass('do_flaps'))
	{
		// add actions to flaps
		Array.each($$('.flaps li a'),function(el,i){
			el.addEvent('click',function(e){
				e.stop();
				enable_flap(el.get('href'));
			});
		});
		// enable first flap visible on init
		enable_flap(0);
	}
	
	/* Init tooltips */
	var tooltips = new Tips($$('.dotips'), {
		offset: {'x': 10, 'y': 10},
		fixed: false,
		className: 'tool-tip'
	});
	
});

window.addEvent('resize', function(){
	/* Standard function to give mCCWrapper the remaining window height if avoidElements is defined */
	//autosize_mCCWrapper();
});

function codeAddress(name, address, addressIndex) {
  if (geocoder) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        nameIndex = addressIndex + 1; // offset
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location,
          title: nameIndex+': '+name
        });
        var html = '<div id="infoWindow">'+
		    	'<div id="bodyContent">'+
						'<img src="/assets/_gfx/hurricane_logo.gif" width="191" height="46" alt=""/>'+
						'<p>'+address+'</p>'+
					'</div>'+
					'</div>';
//        google.maps.event.addListener(marker, 'click', function() {
          infoWindow.setContent(html);
          infoWindow.open(map,marker);
//        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
}

function load_fylke_list()
{
	var x = $('fylke').getSelected().get('value');
	if(x==0)
	{
		$('fylke_image').set('src','/assets/_gfx/fylker/default.gif');
		$('fylke_list').set('html','');
	}
	else
	{
		$('fylke_image').set('src','/assets/_gfx/fylker/'+x+'.gif');
		
		var lang = $('language').get('value');
		
		var req = new Request.HTML({
			url:'/' + lang + 'fylke',
			update: $('fylke_list'),
			onSuccess: function(e){
				$$('a').each(function(link,i){
					if(link.get('rel')=='_blank') link.setProperty('target','_blank');
				});					
			}
		}).post('fylke='+x);		
	}
}

function enable_flap(index)
{
	var rel_content;
	Array.each($$('.flaps li'),function(el,i){
		el.removeClass('active');
		if(i==index){
			el.addClass('active');
 			rel_content = el.getFirst('a').get('rel');
		}
	});
	if($('flap_contents') != null)
	{
		Array.each($('flap_contents').getChildren('li'),function(el,i){
			el.addClass('hidden');
			if(el.get('id')==rel_content)
				el.removeClass('hidden');
		});
	}
}

function change_language(value)
{
	var pathname = window.location.pathname;
	
	if(pathname.substr(0, 3) == '/no')
		pathname = pathname.substr(4, pathname.length);
	if(pathname.substr(0, 3) == '/en')
		pathname = pathname.substr(4, pathname.length);
	if(pathname.substr(0, 3) == '/de')
		pathname = pathname.substr(4, pathname.length);
	
	if(pathname.substr(0, 1) == '/')
		pathname = pathname.substr(1, pathname.length);
	
	var urlstring = 'http://' + window.location.host + '/' + value + '/' + pathname;
	
	window.location = urlstring;
	
}
