/**
Written by Tizian Schmidlin, inspired (and mostly copied the detection part) from:
http://www.hand-interactive.com/resources/detect-mobile-javascript.htm
*/

/** Mobilephone detection */
var deviceIphone = "iphone";
var deviceIpod = "ipod";
var deviceS60 = "series60";
var deviceSymbian = "symbian";
var engineWebKit = "webkit";
var deviceAndroid = "android";
var devicePalm = "palm";
var deviceBB = "blackberry";
var deviceWinMob = "windows ce";

//Initialize our user agent string to lower case.
var uagent = navigator.userAgent.toLowerCase();

// Detect the browsers

function detectMobileBrowsers() {
   // Avoid javascript errors
   try {
   // Detect android phones
   if (uagent.search(deviceAndroid) > -1)
   {
     if (uagent.search(engineWebKit) > -1)
        return true;
   }else{
   	if(uagent.search(engineWebKit) > -1)
   	return true;
   }
   
   // Detect iPhone
   if (uagent.search(deviceIphone) > -1)
      return true;

   // Detect iPod
    if (uagent.search(deviceIpod) > -1)
      return true;
   
   // Detect Nokia phones
   if (uagent.search(engineWebKit) > -1)
   {
     if ((uagent.search(deviceS60) > -1 || 
          uagent.search(deviceSymbian) > -1))
        return true;
   }

   // Windows Mobile
   if (uagent.search(deviceWinMob) > -1)
      return true;
   
   // Blackberry
   if (uagent.search(deviceBB) > -1)
      return true;
   // Palm
   if (uagent.search(devicePalm) > -1)
      return true;
   
   // If none of them is recognized, assume it is not a mobile browser
   return false;
   }catch(e){
      return false;
   }
}	

/** Initialisation */

function initQuickNavForMobile() {

	if(detectMobileBrowsers()) {
		// Avoid Javascript errors so that other application do not crash if this messes up.
		try {
			var quickLinks = $$('#quickNav li span');
			quickLinks.each(function(el) {
				var tmp = $(el).innerHTML;
				var a = new Element('a', { href: '#' }).update(tmp);
				a.observe('click', function() {
					
					var hadClass = false;
					
					if(this.up().up().hasClassName('openNavi')) {
						hadClass = true;
					}
					
					this.up().up().up().childElements().each(function(child) {
						child.removeClassName('openNavi');
					});
					if(!this.up().up().hasClassName('openNavi') && !hadClass) {
						this.up().up().addClassName('openNavi');
					}else{
						this.up().up().removeClassName('openNavi');
					}
					
					return false;
				});
				$(el).update( a );
			});
		}catch(e){}
	}
}
Event.observe(document, 'dom:loaded', initQuickNavForMobile);

