/*
  moopop: unobtrusive javascript popups via late binding using mootools 1.11
  
  copyright (c) 2007 by gonchuki - http://blog.gonchuki.com
  
  version:	1.0
  released: August 12, 2007
  
  This work is licensed under a Creative Commons Attribution-Share Alike 3.0 License.
    http://creativecommons.org/licenses/by-sa/3.0/
*/

/*
  Basic usage:
    add a rel attribute to your <a> tags to look like this:
      <a href="http://blog.gonchuki.com" rel="popup">foobar</a>
      or
      <a href="http://blog.gonchuki.com" rel="popup[600, 400]">foobar</a>
      or
      <a href="http://blog.gonchuki.com" rel="popup[600, 400, string, string]">foobar</a>
      
    where:
      "popup" is the default string token to match against so the popup behavior
              can be attached.
      "[600, 400, string]" is the (optional) size of the newly created window - and attribute.
*/

var moopop = 
{
  width: 0,
  height: 0,
  /*
    Function: captureByRel
      standard capturing method, it's autorun onDomReady and you can manually use it
      to capture a different set of popup windows.
      
    Syntax:
      moopop.captureByRel(value, element);
      
    Arguments:
      value - The partial string to match against the rel attribute of your links.
      element - [optional] a DOM element to restrict which links should be processed.
  */
  captureByRel: function(attrVal, parent) 
  {
  	this.capture((parent || document).getElements('a[rel^=' + attrVal + ']'));
  },
  
  /*
    Function: capture
      multipurpose function allowing for different methods of capturing the popups.
      
    Syntax:
      moopop.capture(obj, width, height);
      
    Arguments:
      obj - (mixed) can be either a DOM element, an Array of elements or a className.
      width - [optional] (integer) default width for popups without a given size, if
              specified you must also specify the height.
      height - [optional] (integer) default height for popups without a given size.
  */
  capture: function(el, width, height) 
  {
    if ($defined(width) && $defined(height)) {
      this.width = width;
      this.height = height;
    }
    
    switch ($type(el)) {
      case 'element':
        this.add_pop_to(el);
        break;
      case 'string':
      case 'array':
        $$(el).each( function(el) {
          this.add_pop_to(el);
        }, this);
        break;
    }
    
    this.width = null;
    this.height = null;
  },
  
  /*
    Function: add_pop_to
      Primarily used internally but you can also use it to manually attach the popup
      behavior to a single DOM element.
      
    Syntax:
      moopop.add_pop_to(element);
      
    Arguments:
      element - a DOM element to process.
  */
  add_pop_to: function(el) 
  {
    el.addEvent('click', function(e){ new Event(e).stop(); this.popup(el); }.bind(this));    
    var rel = el.getAttribute('rel');
    var match = rel.match(/.*\[(.*)]/);    
    if (match) {
		var parts = match[1].split(/,\s*/);
	    //var size = el.getAttribute('rel').match(/\[(\d+),\s*(\d+),\s*([a-zA-Z0-9%]+)?,\s*([a-zA-Z0-9%]+)?(,\s*[a-zA-Z0-9%]+)?\]/) || ['', this.width, this.height];
	    
		// added centering of the popup window
		var left = (screen.width - parts[0]) / 2;
		var top = (screen.height - parts[1]) / 2;
		el.setAttribute('popupprops', 'width=' + parts[0] + ', height=' + parts[1] + ', left=' + left + ', top=' + top);
		if (parts[4]){
			el.href = el.href + "?page=" + parts[2] + "&title=" + parts[3] + "&w=" + parts[0] + "&h=" + parts[1] + "&teaser=" + parts[4];
		}
		else {
			el.href = el.href + "?page=" + parts[2] + "&title=" + parts[3] + "&w=" + parts[0] + "&h=" + parts[1];
		}	
	}
  },
  
  /*
    Function: popup
      Triggers the popup behavior on a given link. Used internally but you can also use it to
      force a given unprocessed link to open in a new window.
      
    Syntax:
      moopop.popup(element);
      
    Arguments:
      element - a DOM element to process.
  */
  popup: function(el) 
  {
    window.open(el.href, '', el.getAttribute('popupprops') || '');
  }
};

var AccordionMenu = 
{	
	applyAccordions: function()
	{	
	  	var stretchers = $$('ul.atStart');
	  	var toggles = $$('li.atStart');
	  	var accordion = new Accordion(
	  		toggles, stretchers, { opacity: true, display: -1 }, 
	  		$('accordion'));
	  	
	  	AccordionMenu.checkHash(accordion, toggles, stretchers);
	  	
	},
  			
	checkHash: function(accordion, toggles, stretchers)
	{
		var found = false;
		toggles.each(function(ul, i){
			if (window.location.hash === '#' + ul.title) {
				accordion.display(i);
				found = true;
			}
		});
		return found;
	}
};

var Quichlinks = 
{
	startGame: function()
	{
		$('quickstart').addEvent('change', function(e){			
			var array = $('quickstart').value;
			array = array.split(","); 
			var popurl = array[0];
			var w = array[1];
			var h = array[2];
			var popup = array[3];
			
			if (w && h) {			
				var left = (screen.width - w) / 2;
				var top = (screen.height - h) / 2;	
				fullpopurl =  "?page=" + popurl + "&w=" + w + "&h=" + h;
				window.open(popup + fullpopurl, '', 'width=' + w + ', height=' + h + ', left=' + left + ', top=' + top);
			}			
		});
	}
};

/* Not in use
var SendTilVen =
{
	sendtilven: function()
	{
		var el = $('SendTilVen').addEvent('click', function(e){
			var spilFullUrl = document.location.href.split('?');
			if (spilFullUrl[1] != null) {
				var urlvars = spilFullUrl[1].split('&');
				for (var i=0;i<urlvars.length;i++) {
					var urlvar = urlvars[i].split('=');
					this[urlvar[0]]=urlvar[1];
				}
			}
			var url = document.location.href.split('/').slice(0, -2).join('/');
			url = url + '/sendtilven/index.asp&title=' + urlvar[0];
			var w = "640";
			var h = "560";
			var left = (screen.width - w) / 2;
			var top = (screen.height - h) / 2;
			window.open(url, '', 'width=' + w + ', height=' + h, + ', left=' + left + ', top=' + top || '');
		});
	}
};
*/

 // font resize functions for article/articleList functions bar
//////////////////////////////////////////////////////////////////////////
function fontSizer (inc) {
	if (document.getElementById) {
		if (!this.container) this.container = $$("div.article");
		if (!this.size) this.size = 1;
		
		if ((inc<0 && this.size>0) || (inc>0 && this.size<4)) {
			var prevClass = "articleFontSize" + this.size;
			this.size += inc;
			var newClass = "articleFontSize" + this.size;
			this.container.removeClass(prevClass).addClass(newClass);
			//DR.CMS.Components.Columns.set();
			//DR.CMS.Layout.ResizeDiv.set();
		}
	}
	else {
		var displayChar = (inc>0) ? '+' : '-';
		alert('Det er desv' + String.fromCharCode(230) + 'rre ikke muligt at ' + String.fromCharCode(230) + 'ndre fontst' + String.fromCharCode(248) + 'rrelse i Deres browser vha. javascript.\nBrug istedet den indbyggede funktionalitet i Deres browser ved at trykke "Ctlr" og "' + displayChar + '" (Macintosh: "' + String.fromCharCode(198) + 'ble" og "' + displayChar + '")');
	}
}

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

	// Accordion function
	AccordionMenu.applyAccordions();
	
	// Popup med link og rel
	moopop.captureByRel('popup');
	
	// Send til en ven Popup med link og rel
	moopop.captureByRel('sendtilven');
	
	// Quick links
	Quichlinks.startGame();
	
	fontSizer();
	
	spotFinish();
	
});





