// General Scripts for Africa Nomad
//
// slide menu code by xn*au webdesign
var slideMenu = new Class({
	tabs : [],
	initialize: function(selector) {
		// the selector is assumed to be the mouse over menu element li, 
		// with the submenu in a nested ul
		this.tabs = $$(selector);
		this.tabs.each( function(tab) {
			var dropdown = tab.getElement('ul');
			if (null != dropdown) {
				var height = dropdown.getSize().y;
				tab.addEvent('mouseenter', this.unRoll.pass([dropdown,height], this) );
				//tab.addEvent('mouseleave', this.rollUp.pass(dropdown, this) );
				tab.addEvent('mouseleave', this.rollUp.pass(dropdown, this) );
			}
		  },   this );
	},

	unRoll: function(e,height) {
				//alert("element: "+e+" size y: "+height);
				e.setStyle( 'opacity', 0);
				e.set('morph', {
					duration: 200,
					transition: Fx.Transitions.Sine.easeOut
					}).morph({ 
						height : (height-1) + 'px',
						opacity : 1,
						visibility : 'visible'
						});
	},

	rollUp: function(e) {
				(function() {
				e.set('morph', {
					duration: 200,
					transition: Fx.Transitions.Sine.easeIn
					}).morph({
						height : '0px',
						opacity : 0
						})
							 }).delay(200,this);
	}

});
///////////////////////////
// ROLL-OVER GRID
//get the image id of the top-left key image
function getInitialID() {
	var grid_images = $$('#grid-area img');
	return grid_images[0].get('id');
}
// roll-over grid code by xn*au webdesign
var detailGrid = new Class({
	Implements: [ Options, Events ],
	options: {
		duration: 50,
		transition: 'quad',
		key_element: 'div#grid-area div img',
		detail_element: 'div#detail-area>div',
		container: 'pagewrapper',
		select_border_color: '#ff0000',
		border_color: '#33CC33',
		fps: 24,
		initial: 'key1'
	},
	keys : [],
	initialize: function(options) {
		this.setOptions(options);
		this.hold = false;
		$(this.options.container).setStyle('height',this.findHeight());
		this.keys = $$(this.options.key_element);// grab all the keys
		this.keys.removeProperty('alt');// remove the alt attributes to stop annoying tooltips
		document.addEvent('click', this.clearAll.bind(this));// set the click-outside-clear event
		this.keys.each( function(key) { // add an event to each key...
			//  this fingers the target element-- which is the detail div of the same number
			var detail = $(key.get('id').replace('key','detail'));  
			if (null != detail) {
				detail.setStyle('opacity',0);
				key.addEvent('mouseenter', this.showIt.pass([detail,key.getParent('div')], this) ); 
				key.addEvent('mouseleave', this.hideIt.pass([detail,key.getParent('div')], this) );
				key.addEvent('click',this.unHold.pass([detail,key.getParent('div')], this) );
			}
		  },   this );
		$(this.options.initial).fireEvent('mouseenter');//set the initial detail pane
		this.init = true;
	},
		showIt: function(e,p) {
		if (this.init) { // clear the initial detail pane the first time in
			$(this.options.initial).fireEvent('mouseleave');
			this.init = false;
		}
		if (!this.hold) {
				e.get('morph').pause();
				e.set('morph', {
					duration: this.options.duration,
					transition: this.options.transition + ':in',
					fps: this.options.fps
					}).morph({ 
						opacity: 1
						});
				p.set('morph', {
					duration: this.options.duration,
					transition: this.options.transition + ':in'
					  }).morph({
						  'border-color':this.options.select_border_color
					  });
					  
		}
	},
	hideIt: function(e,p) {
		if (!this.hold) {
				e.get('morph').pause();
				e.set('morph', {
					duration: this.options.duration,
					transition: this.options.transition + ':out',
					fps: this.options.fps
					}).morph({
						opacity: 0
						});
				p.set('morph', {
					duration: this.options.duration,
					transition: this.options.transition + ':out'
					  }).morph({
						  'border-color':this.options.border_color
					  });
				
		}
	},
	clearAll: function(ev) { // this function clears all the holds and visibile detail panels
		ev = new Event(ev);
		if (strpos(ev.target.id,'key')===false) {  //  i.e.  we've clicked outside one of the keys
			this.setDefault();
			this.hold = false;	
		}
		
	},
	unHold: function(e,p) {  //  this function clears the hold and displays a new detail panel
		if(this.hold) {
			this.setDefault();
			this.hold = false;
			this.showIt(e,p);
		} else {
			this.hold = true;
		}
	},
	setDefault: function() {
			$$(this.options.detail_element).setStyle('opacity',0);
			var key = $$(this.options.key_element);
			key.getParent('div').setStyle('border-color',this.options.border_color);
	},
	findHeight: function() {
		var maxheight = $('left').getSize().y;// left column height is the minimum height
		var height = 0;
		$$(this.options.detail_element).each( function(detail) {
																	  height = detail.getSize().y;
																	  if (height > maxheight) {
																		  maxheight = height;
																	  }
																	  });
		return maxheight;
	}
	
});

//////////////////////////////
// GENERAL FUNCTIONS
//get the image id of the top-left key image
function getInitialID() {
	$grid_images = $$('#grid-area img');
	return $grid_images[0].get('id');
}
//
function strpos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = (haystack+'').indexOf( needle, offset ); 
    return i===-1 ? false : i;
}
//
//
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);}
//
var mp3div = new Class({
	Implements: Options,
	options: {
		divID: 'player',
		title: '',
		movie: null,
		height: 16,
		width: 560,
		auto: false,
		volume: 100,
		kiosk: true
	},
	initialize: function(options) {
		this.setOptions(options);
		var embedCode = '\n<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="'+this.options.width+'" height="'+this.options.height+'">';
		embedCode += '\n<param name="src" value="'+this.options.movie+'" /><param name="controller" value="true" /><param name="autoplay" value="'+this.options.auto+'" /><param name="volume" value="'+this.options.volume+'" /><param name="kioskmode" value="'+this.options.kiosk+'" />';
		embedCode += '\n<!--[if !IE]>--><object type="audio/x-mpeg" data="'+this.options.movie+'" width="'+this.options.width+'" height="'+this.options.height+'"><param name="autoplay" value="'+this.options.auto+'" /><param name="volume" value="'+this.options.volume+'" /><param name="controller" value="true" /><param name="kioskmode" value="'+this.options.kiosk+'" /></object>\n<!--<![endif]-->';
		embedCode += '\n</object>';
		$(this.options.divID).innerHTML = embedCode;
	}
});
// BUY BUTTON CODE
function replaceLink(el) {
	var itemName = el.getProperty('name');
	var price = el.getProperty('text');
	var userID = "63303066";
	var server = "http://ww4.aitsafe.com/cf/add.cfm?";
	var getString = server.concat("userid=",userID,"&productpr=",escape(itemName),":",escape(price.replace(/\$/,"")));
	var buyButton = new Element('a',{
										 href:getString,
										 "class":"buybutton",
										 text:price,
										 name:itemName
										 }).replaces(el);
}