// Beware: jQuery is in nonConflict mode!!!

//alert('iphone.js included');
isMobileSafari = 0;
var isIPad = 0;

if (/iPhone/.test(navigator.userAgent) ||
	/iPod/.test(navigator.userAgent) ||
	/iPad/.test(navigator.userAgent)){
	isMobileSafari = 1;
	//document.write('<meta name="viewport" content="initial-scale = 1.0, user-scalable=yes" />');
	if (/iPad/.test(navigator.userAgent)){
		isIPad = 1;
		document.write('<link rel="stylesheet" media="all and (orientation:portrait)" href="http://www.greeneducationcuhk.net/portals/_default/skins/greenedu/ipad_portrait.css">');
		document.write('<link rel="stylesheet" media="all and (orientation:landscape)" href="http://www.greeneducationcuhk.net/portals/_default/skins/greenedu/ipad_landscape.css">');	
	} else {
		//document.write('<link rel="stylesheet" media="all and (orientation:portrait)" href="http://www.greeneducationcuhk.net/portals/_default/skins/greenedu/iphone_portrait.css">');
		//document.write('<link rel="stylesheet" media="all and (orientation:landscape)" href="http://www.greeneducationcuhk.net/portals/_default/skins/greenedu/iphone_landscape.css">');	
	}
}

function updateOrientation() {
	var orientation=window.orientation;
	switch(orientation)
	{
		case 0:
			alert('Potrait: ' + jQuery(document).width() + ',' + jQuery(window).width());
			break;	
		case 90:
			//jQuery('document').width('320px');
			alert('Landscape left: ' + jQuery(document).width() + ',' + jQuery(window).width());
			break;
		case -90:	
			alert('Landscape right: ' + jQuery(document).width() + ',' + jQuery(window).width());
			break;
		default:
			alert('Other orientation: ' + orientation + ',' + jQuery(document).width() + ',' + jQuery(window).width());
			break;
	}
}

function fixPopupMenu() {
	jQuery('#menuitem > li > ul > li').each(function(i) {
		var href = jQuery(this).find('a')[0].href;
		jQuery(this).mouseover(function(){
			window.location=href;
		});
	});
}

function fixLongLinks() {
	jQuery('a').each(function(i) {
		if (jQuery(this).children().length == 0) {
			var html = jQuery(this).html();
			//if (html.indexOf('http://') >= 0) {
				html = html.replace(/\//g, '/<wbr>');
				html = html.replace(/_/g, '_<wbr>');
				html = html.replace(/-/g, '-<wbr>');
				html = html.replace(/@/g, '@<wbr>');
				html = html.replace(/\./g, '.<wbr>');
				jQuery(this).html(html);
			//}
		}
	});
}

function fixLinkTargets() {
	jQuery('a').each(function(i) {
		jQuery(this).removeAttr('target');
	});
}

function fixMapLink() {
	jQuery('a').each(function(i) {
		var href = jQuery(this).attr('href');
		if (href && 
			(href.indexOf('tabid=57') >= 0 || href.indexOf('tabid/57') >= 0)) {
			href = "http://treedb.greeneducationcuhk.net/flashmap/map.html";
			jQuery(this).attr('href', href);
		}
	});
}

function fixPodcast() {
	jQuery('a').each(function(i) {
		var href = jQuery(this).attr('href');
		if (href && href.indexOf('itpc://') >= 0) {
			href = "http://treedb.greeneducationcuhk.net/flashmap/podcast.html";
			jQuery(this).attr('href', href);
		}
	});
}

function fixYouTube() {
	jQuery('a').each(function(i) {
		var href = jQuery(this).attr('href');
		if (href && href.indexOf('www.youtube.com') >= 0) {
			href = href.replace(/www\.youtube\.com/gi, 'm\.youtube\.com');
			//href = href.replace(/watch\?v=/i, 'embed/');
			jQuery(this).attr('href', href);
		}
	});
	
	jQuery('#youtube_normal').hide();
	jQuery('#youtube_mobile').show();
}

function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

jQuery(function() {
	if (isMobileSafari) {
		fixMapLink();
		fixPopupMenu();
		fixLongLinks();
		fixLinkTargets();
		fixYouTube();
		fixPodcast();

		//alert(jQuery(document).width() + ',' + jQuery(window).width());
		//jQuery('.c_contenttree').removeClass('c_contenttree').addClass('c_contenttree_iphone');
		//jQuery(document).width('320px');
		
		//window.onorientationchange=updateOrientation;	
	}
});

