
var SplitList = new Class({
   initialize: function(h1, h2, content1, content2){
      this.h1 = h1;
      this.h2 = h2;
      this.content1 = content1;
      this.content2 = content2;
      this.h1.addEvent('click', this.click.bind(this));
      this.h2.addEvent('click', this.click.bind(this));
   },
   click: function(e){
      this.h1.toggleClass('selected');
      this.h2.toggleClass('selected');
      this.content1.toggleClass('selected');
      this.content2.toggleClass('selected');
      e.stop();
   }
});

function getBanners()
{
	var containers = $$('.banner_container');
	idList = [];
	idString = null;
	for (var i = 0; i < containers.length; i++){
		idList[i] = containers[i].getAttribute('zone');
		idString = idString == null ? idList[i] : idString + ',' + idList[i];
	}
	request = new Request.JSON({
		url: '/banner/' + idString,
		onComplete: function(result){
			for (var i = 0; i < result.length; i++){
				var code = result[i].html;
				var zone = result[i].zone;
				if (code != '') {
					container = $('zone' + zone);
					container.set('html', code);
					container.setStyle('display', 'block');
					//this.containers[i].set('html', code);
					//this.containers[i].setStyle('display', 'block');
				}
			}
		}.bind({ containers: containers })
	}).get({});
}

function initializePage()
{
	getBanners();
   $('searchText').addEvent('keydown', function(e)	{
      if (e.key == 'enter') {
         search();
         e.stop();
      }
   });
   var searchText2 = $('searchText2');
   if (searchText2 != null) {
	   searchText2.addEvent('keydown', function(e)	{
	      if (e.key == 'enter') {
	         search2();
	         e.stop();
	      }
	   });
   }
	var wndSize = window.getSize();
	if ($('position_T') != null) {
	   var topCoords = $('position_T').getCoordinates(true);
	   var overlay = $('topOverlay');
	   var content= $('topContent');
	   if (content != null) {
	      var contentCoords = content.getCoordinates(true);
	      var top = topCoords.bottom - contentCoords.height + 'px'
		   overlay.setStyles({
		   	left: '50%',
		   	top: top,
	         height: contentCoords.height - 16 + 'px',
		   	'marginLeft': -wndSize.x / 2 + topCoords.left
		   });
		   content.setStyles({
		   	left: '50%',
		   	top: top,
		   	'marginLeft': -wndSize.x / 2 + topCoords.left
		   });
		
		   var fadeInOverlay = new Fx.Tween(overlay);
		   fadeInOverlay.start('opacity', 0.0, 0.7);
		   var fadeInContent = new Fx.Tween(content);
		   fadeInContent.start('opacity', 0.0, 1.0);
		}
	}
	$$('.splitList').each(function(el){
		var headings = el.getElements('h2');
		var left = el.getElement('.left');
		var right = el.getElement('.right');
		if (headings.length == 2)
			new SplitList(headings[0], headings[1], left, right);
	});
}

window.addEvent('load', function(){
   initializePage();
});

function search()
{
	window.location.href = '/finn/' + $('searchText').value;
}

function search2()
{
	window.location.href = '/finn/' + $('searchText2').value;
}

function insertPlayer(autoPlay, splashImage, scaleSplash, video, width, height)
{
	omagVideoClip = video;
	var maxWidth = 1000;
	var maxHeight = 634;
	var xScale = maxWidth / width;
	var yScale = maxHeight / height;
	var scale = Math.min(xScale, yScale);
	var width = Math.round(width * scale);
	var height = Math.round(height * scale);
	//var height = Math.round(height / width * 664);
	window.addEvent('load', function(){
		var playerContainer = new Element('a', {
			id: 'omagVideoContainer',
			href: 'http://www.omag.no/video/' + escape(omagVideoClip),
			styles: {
				position: 'relative',
            top: (maxHeight - height) / 2 + 3 + 'px',
            left: (maxWidth - width) / 2 + 3 + 'px',
				width: width + 'px',
				height: height + 'px',
				display: 'block'
			}
		});
		//alert($('omagVideoPlayer'));
		if (splashImage.indexOf('/bilder') == -1)
			splashImage = 'video/' + splashImage;
		playerContainer.inject($('videoArea'));
		$f("omagVideoContainer", "/media/js/flowplayer-3.2.5.swf", {
			// don't start automcatically
			clip: {
				//autoPlay: false,
				autoBuffering: true,
				baseUrl: 'http://www.omag.no/'
			},
			playlist: [
				{ url: splashImage },
				{ url: 'video/' + video, autoPlay: false },
				{ url: splashImage }
	      ],
			// disable default controls
			plugins: {controls: null}
		}).controls("flowplayerControls", {});
        //controls("flowplayerControls", {duration: 25});
	});
}

