/**
 * @depend jquery-1.2.6.js
 * @depend swfobject.js
 */

jQuery.noConflict();

function IXD(headerType) {
  var that = this;
  this.headerType = headerType;
    
  this.init = function() {
    tableClickability();
    initEmails();
    initNewsletterForm();
    initUrlField();

    if (headerType) {
      adjustWrapper(jQuery("#header"));
    }
  };
  
  var adjustWrapper = function( wrapper ) {
    switch( this.headerType ) {
    case "banner":
      wrapper.css({
	background: '#fff',
        height: '300px',
        width: '982px',
        padding: '0'
      });
      
      // append corner to flash container
      wrapper.append("<span>&nbsp;</span>");
      break;
    case "coverflow_purple":
      wrapper.css({
	background : '#9d92cc url(/design/ixd_no/images/coverflow_bg_purple.jpg) 50% 53px no-repeat',
        height : '300px'
      });
      
      // append corner to flash container
      wrapper.append('<span>&nbsp;</span>');
      break;
    case "coverflow_grey":
      wrapper.css({
	background : '#fff url(/design/ixd_no/images/coverflow_bg_grey.jpg) 50% 53px no-repeat',
        height : '285px'
      });
      
      // append corner to flash container
      wrapper.append('<span>&nbsp;</span>');
      break;
    default:
      // handled by css
      break;
    }
  };
  
  var tableClickability = function() {
    jQuery("tr a").each(function(i) {
      var row = jQuery(this).parent().parent();

      if (!row.is("tr")) {
	return;
      }

      if (row.find("a").length == 1) {
	row.css({ cursor: 'pointer' });
      }
    });

    jQuery("tr").click(function() {
      if (jQuery(this).find("a").length == 1) {
	var destination = jQuery(this).find("a")[0].href;

	if (destination) {
	  window.location = destination;
	}
      }
    });
  };

  var initEmails = function() {
    if (!document.getElementById || !document.createElement) {
      return;
    }

    var links = document.getElementsByTagName('a');

    for (var i = 0, link = null, email = null, em = null; (link = links[i]); i++) {
      if (link.rel == 'email') {
    	em = link.innerHTML.trim().split(' a ');
    	email = em[0] + '@' + em[1];
    	link.href = 'mailto:' + email;
    	link.innerHTML = email;
      }
    }
  };

  var initNewsletterForm = function() {
    var input = jQuery("#newsletter input[type!=submit]");
    var label = jQuery("#newsletter label");
    
    input.attr("value", label.text());
    label.hide();
    
    input.focus(function() {
      if (input.val() == label.text()) {
	input.val("");
      }
    });
    
    input.blur(function() {
      if (input.attr("value") === "") {
	input.attr("value", label.text());
      }
    });
  };

  var initUrlField = function() {
    var input = jQuery(".promo .url");
    var label = jQuery(".promo .urllabel");
    
    input.attr("value", label.text());
    label.hide();
    
    input.focus(function() {
      if (input.val() == label.text()) {
	input.val("");
      }
    });
    
    input.blur(function() {
      if (input.attr("value") === "") {
	input.attr("value", label.text());
      }
    });
  };
}

IXD.prototype.popups = {
  orderCourse : { 
    url : "bestilling_kurs.html",
    filter : ".wrapper"
  }
};

String.prototype.trim = function() {
  return this.replace(/^\s*|\s*$/g, "");
};

// takes a DOM element and returns a serialized XML string of the element and all siblings
function serializeToXML(el) {
  var xmlStr = "";
  if (el.nodeType == 1) {
     // element node 
    xmlStr += '<';
    xmlStr += el.nodeName.toLowerCase();

    // attributes
    if (el.attributes.length > 0) {
      for (var i = 0; i < el.attributes.length; i++) {
	// filter out some IE madness (they give you all attributes, not just the user ones)
	if (el.attributes[i].nodeValue !== null && el.attributes[i].nodeValue !== '' && el.attributes[i].nodeName != 'contentEditable') {
	  xmlStr +=  ' '+el.attributes[i].nodeName+'=\''+el.attributes[i].nodeValue+'\'';
	}
      }
    }

    if (el.childNodes.length > 0) {
      xmlStr += '>';

      jQuery(el.childNodes).each(function(i){
	xmlStr += serializeToXML(this);
      });

      xmlStr += '</'+el.nodeName.toLowerCase()+'>';
    } else {
      xmlStr += '/>';
    }
  } else if (el.nodeType == 3) {
    // text node
    xmlStr += el.nodeValue;
  } else {
    // other node
    //alert('other node type');
  }

  return xmlStr;
}

jQuery(document).ready(function() {
  var ixd = window.ixd || new IXD(null);

  if (ixd) {
    ixd.init();
  }
});

if (typeof window.ixdff !== "undefined") {
  var bannerXML = serializeToXML(jQuery("#flash")[0]);
  swfobject.embedSWF(ixdff.flash,
                     "flash",
		     ixdff.width,
		     ixdff.height,
		     "9",
		     ixdff.expressInstall,
		     { cssurl: ixdff.cssUrl,
		       xmldata: bannerXML },
		     { wmode: 'transparent' },
		     { style : 'outline:none' });

  var headerType = 'banner';
  window.ixd = new IXD(headerType);
}

if (typeof window.MooFlow !== "undefined") {
  var headerType = 'coverflow_grey';
  var ixd = new IXD(headerType);
  $("header").setStyle("padding", "0");

  var myMooFlowPage = {
    start: function() {
      var mf = new MooFlow($('MooFlow'), {
		 bgColor: 'transparent',
		 startIndex: 3,
		 heightRatio: 0.3,
		 factor: 100,
		 reflection: 0.2,

		 onClickView: function(obj) {
		   var img = new Element('img', {
			       src: obj.src,
			       title: obj.title,
			       alt: obj.alt,
			       styles: obj.coords
			     }).setStyles({
			       position: 'absolute',
			       border: 'none'
			     });

		   var link = new Element('a', {
				'class': 'remooz-element',
				'href': obj.href,
				'title': obj.title + ' - '+ obj.alt,
				'styles': { 'border':'none' }
			      });

		   $(document.body).adopt(link.adopt(img));

		   var remooz = new ReMooz(link, {
				  centered: true,
				  resizeFactor: 0.8,
				  origin: link.getElement('img'),

				  onCloseEnd: function() {
				    link.destroy();
				  }
				});

		   remooz.open();
		 }
      });
    }
  };

  window.addEvent('domready', myMooFlowPage.start);
}

(function() {
   var pageTracker = _gat._getTracker("UA-10280319-1");
   pageTracker._initData();
   pageTracker._trackPageview();
})();
