horus.script.load('dom', 'xmlhttp', 'popover', 'iterator', 'counter');

horus.cspot.eventdata={};
horus.cspot.events={};

horus.cspot.eventfields=
  [ 'name', 'description', 'location', 'web', 'phone', 'email', 'dates' ];


horus.cspot.dateselector=
  function ( chunkid ) {
    var data=horus.cspot.eventdata[chunkid];

    if (!data.selector) {
      data.selector=horus.getElement('dateselector'+chunkid);
      data.selectorbox=horus.getElement('dateselectorbox'+chunkid);
      data.selyear=data.year;
      data.selmonth=data.month;
    }

    return data;
  };


horus.cspot.toggleselector=
  function ( chunkid ) {
    var data=horus.cspot.dateselector(chunkid);
    var hide=data.selector.style.visibility=='visible';

    if (hide)
      horus.popover.hide(data.selector);
    else
      horus.popover(data.selector, { reference: data.selectorbox });

    data.selectorbox.src='/common/resources/'+(hide ? 'open' : 'close')+'box.gif';

    return;
    var hide=data.selector.style.display=='';
    horus.visible(data.selector, !hide);
    data.selectorbox.src='/common/resources/'+(hide ? 'open' : 'close')+'box.gif';
  };


horus.cspot.monthsel=
  function ( chunkid, m ) {
    var data=horus.cspot.dateselector(chunkid);

    if (m!=data.selmonth) {
      horus.setClass('m'+data.selmonth+chunkid);
      horus.setClass('m'+m+chunkid, 'selected');
      data.selmonth=m;
    }
  };


horus.cspot.yearsel=
  function ( chunkid, y ) {
    var data=horus.cspot.dateselector(chunkid);

    if (y!=data.selyear) {
      horus.setClass('y'+data.selyear+chunkid);
      horus.setClass('y'+y+chunkid, 'selected');
      data.selyear=y;
    }
  };


horus.cspot.gotomonth=
  function ( chunkid, sel ) {
    var data=horus.cspot.dateselector(chunkid);
    horus.cspot.toggleselector(chunkid);

    if (sel==2) {
      var today=new Date;
      horus.cspot.monthsel(chunkid, today.getMonth()+1);
      horus.cspot.yearsel(chunkid, today.getFullYear());
    }

    if (data.selmonth!=data.month || data.selyear!=data.year)
      if (sel>0)
	horus.linkTo('?date='+data.selyear+'-'+data.selmonth);
      else {
	horus.cspot.monthsel(chunkid, data.month);
	horus.cspot.yearsel(chunkid, data.year);
      }

    return false;
  };


horus.cspot.eventview=
  function ( control ) {
    var id=horus.toId(control);
    var chunkid=horus.toId(control, 1);
    var data=horus.cspot.events;
    var theEvent=data[id];

    if (!theEvent) {
      var response=horus.ajax('/action/event.cfm', 'getevent', { id: id });

      if (response) {
	var fieldlist=horus.cspot.eventfields.iterator();
	var fields={};
	theEvent={};

	while (fieldlist.hasNext()) {
	  var field=fieldlist.next();
	  fields[field]=response.getAttribute(null, field);
	}

	if (fields.description)
	  if (/</.test(fields.description)) {
	    var text=horus.replaceHTML(null, fields.description);

	    if (text.nextSibling) {
	      fields.description=[ 'div', null ];
	      do { fields.description.push(text); text=text.nextSibling } while (text);
	    } else
	      fields.description=text;

	  } else
	    fields.description=[ 'p', null, fields.description ];

	if (fields.description)
	  fields.description=/</.test(fields.description) ?
	    horus.replaceHTML(null, fields.description) :
	    [ 'p', null, fields.description ];

	if (fields.location)
	  fields.location=
	    [ 'p', null, [ 'span', '.highlight', 'Location: ' ], fields.location ];

	if (fields.email)
	  fields.email=
	    [ 'p', null,
	      [ 'span', '.highlight', 'E: ' ],
	      [ 'a', { href: 'mailto:'+fields.email }, fields.email ] ];

	if (fields.phone)
	  fields.phone=[ 'p', null, [ 'span', '.highlight', 'T: ' ], fields.phone ];

	if (fields.web)
	  fields.web=
	    [ 'p', null,
	      [ 'span', '.highlight', 'W: ' ],
	      [ 'a', { href: fields.web, target: '_new' }, fields.web ] ];

	var booking=response.selectNodes('horus/booking', true);

	if (!booking.isEmpty()) {
	  fields.booking=[ 'div', '.purchase' ];
	  theEvent.booking=[];

	  while (booking.hasNext()) {
	    var nextbooking=booking.next();
	    var bookingid=response.getAttribute(nextbooking, 'id', 0);
	    var name=response.getAttribute(nextbooking, 'name', '');
	    var description=response.getAttribute(nextbooking, 'description', '');
	    var price=response.getAttribute(nextbooking, 'price', 0);
	    var subject=encodeURIComponent(name+' - '+description);
	    theEvent.booking.push(bookingid);

	    fields.booking.push
	      ([ 'div', '.item wrapper',
		 [ 'div', { classname: 'padded column', width: '25%' }, name ],
		 [ 'div', { classname: 'padded column', width: '30%' }, description ],
		 [ 'div', { classname: 'padded column', width: '15%' },
		   price ? price.toCurrency() : 'P.O.A.' ],
		 [ 'div', { classname: 'padded column', width: '30%' },
		   [ 'div', { id: 'buylink0event'+bookingid, classname: 'buylink' },
		     [ 'div', '.basket',
		       [ 'a',
			 { id: 'booked'+bookingid,
			   href: '/basket',
			   classname: 'basketlink inbasket' },
			 'booked' ],
		       horus.counter.create
		         (null, 'places'+bookingid, 0,
			  { id: 'places0event'+bookingid,
			    arrows: 'move-16x16.0.png', height: 16, width: 16,
			    callback: horus.cspot.quantity }),
		       [ 'a',
			 { classname: 'remove', onclick: horus.cspot.removefrombasket },
			 'cancel' ] ],
		     [ 'div', '.available',
		       [ 'a', { onclick: horus.cspot.addtobasket }, 'book' ] ],
		     [ 'div', '.enquire',
		       [ 'a', { href: '/contact?subject='+subject }, 'enquire' ] ],
		     [ 'div', '.reserved', 'reserved' ],
		     [ 'div', '.sold', 'sold out' ] ] ] ]);

	  }

	  theEvent.booking=theEvent.booking.iterator();
	}

        theEvent.popover=horus.insertChild
	  ([ 'div', '.event rounded popover left buttons',
	     [ 'form', { name: 'event'+id },
	       [ 'button',
                 { classname: 'action closebox',
		   type: 'button',
		   onclick: horus.popover.hide },
		 [ 'img', { src: '/common/resources/closebutton.png', alt: 'close' } ] ],
	       [ 'h2', '.titlebar', fields.name ],
	       [ 'div', '.content',
		 [ 'p', '.highlight', fields.dates ],
		 fields.description, fields.location,
		 fields.email, fields.phone, fields.web,
		 fields.booking ] ] ]);

	data[id]=theEvent;
      }
    }

    horus.popover
      (theEvent.popover, { reference: control, focus: false, moveable: true });

    if (theEvent.booking) {
      theEvent.booking.reset();

      while (theEvent.booking.hasNext())
	horus.cspot.refreshbasket('event', theEvent.booking.next());

    }
  };

