
var AWARD_ENTRY_URL_SEGMENT_YEAR = 0;
var AWARD_ENTRY_URL_SEGMENT_TITLE = 2;
var AWARD_ENTRY_URL_SEGMENT_ID = 3;
var AWARD_EVENTITEM_URL_SEGMENT_ID = 2;

/**
* Called from ExternalInterface to show the page overlay
* e.g.s
* /2008/entry/ad-title/123
* /year/2008/
* /connect/members/become-a-member
* /learn/something/or/other
*/
function Award_ShowContent(path){
  //alert(">>showContent("+path);
  path = new String(path);

  // Detect type of page
//     /connect/events
  
  if(path.substr(5,7) == '/entry/'){
    sSegments = path.substr(1);
    aSegments = sSegments.split('/');
    
    //alert("entry URL segments: "+aSegments.length+" ["+aSegments[0]+", "+aSegments[1]+", "+aSegments[2]+", "+aSegments[3]+", "+aSegments[4]+"]");
    sPage = 'award_overlay.php?a='+Math.floor(aSegments[AWARD_ENTRY_URL_SEGMENT_ID]);
    //alert(sPage);
  }
  else if ( path.substr(0,16) == '/connect/events/' ) {
    sSegments = path.substr(1);
    aSegments = sSegments.split('/');

	sPage = 'news-events-detail.php?nid='+Math.floor(aSegments[AWARD_EVENTITEM_URL_SEGMENT_ID]);
  }
  else if ( path == '/connect/events' ) {
	sPage = 'news-events-listings2.php';
  }
  else {
    // Fallback to page URL
    sPage = 'page_overlay.php?up='+path;
  }
  
  // Load the content and animate in
  $("#page-centrer")
    .show();
  $("#page-holder")
    .show();
  //alert("loading "+sPage);  
  $("#page-content")
    .css("opacity", "0.0")
    .load(sPage, {}, function(){
      //alert("loaded");  
	    initialiseScrollPanes();
			$("#page-content")
			  .animate({opacity: 0.95}, {queue:false, duration:600, easing:"easeInOutQuad", complete: function(){} })
    });

    //<? ($g_oPage->cp_id == 10)? "fnFillMembers(5, 'MemCol_');" : '' ?>
}

/**
* Opposite of showContent, note SwfAddress will automatically notify the flash which animates out.
*/
function Award_HideContent(){
	$("#page-holder").hide();
	$("#page-content").html('');
	$(".video_iframes").remove();
	document.location.href = "/#";
}
/**
 * Maps the escape key to close the content window
 */
$(window.document).keyup(function (e) {
	if (e == null) { // ie
	  keycode = event.keyCode;
	} 
	else { // mozilla
	  keycode = e.which;
	} 
	if( keycode == 27 ) {
    	Award_HideContent();
    }
	/* Debug Code
	 *
	if ( window.console ) {
		console.log( keycode );
	}
	 */
});


function Award_Entry_Player( sIdent ) {
	var sVideo = $("#"+sIdent).attr( 'alt' );
	
	var flashvars = {};
		flashvars.width             = "530";
		flashvars.height            = "300";
		flashvars.file              = sVideo;
		flashvars.skin              = "/flash/flvplayer_skin.swf";
		flashvars.type              = "rtmp";
	var params = {};
		params.allowscriptaccess    = "always";
		params.allowfullscreen      = "true";
	var attributes = {};
	swfobject.embedSWF("/flash/flvplayer.swf", sIdent, "530", "300", "7.0.0", "/js/swfobject/expressInstall.swf", flashvars, params, attributes);
}
// TODO // Add some looping functionality to replace all instances of the words with the colour.
function HTagsToImages() {
	$("h1").each(function(){
		var iPos = $(this).text().toLowerCase().indexOf('gold');
		var iStrLength = 4;
		var sStyle = '_gold';
		if ( iPos == -1 ) {
			  iPos = $(this).text().toLowerCase().indexOf('silver');
			  iStrLength = 6;
			  sStyle = '_silver';
		}
		if ( iPos == -1 ) {
			  iPos = $(this).text().toLowerCase().indexOf('bronze');
			  iStrLength = 6;
			  sStyle = '_bronze';
		}
		if ( iPos >= 0 ) {
			var normalPrefixText    = $(this).text().substr( 0 ,iPos );
			var colourText          = $(this).text().substr( iPos,iStrLength );
			var normalPostfixText   = $(this).text().substr( iPos+iStrLength );
			var html = ''; 
			if ( normalPrefixText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(normalPrefixText)+"&s=h1\" alt=\""+normalPrefixText+"\" \/>";
			}
			if ( colourText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(colourText)+"&s=h1_"+sStyle+"\" alt=\""+colourText+"\" \/>";
			}
			if ( normalPostfixText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(normalPostfixText)+"&s=h1\" alt=\""+normalPostfixText+"\" \/>";
			} 
			$(this).html( html );
		}
		else {
			$(this).html( "<img src=\"/image.php?txt="+escape($(this).text())+"&s=h1\" alt=\""+$(this).text()+"\" \/>" );
		}
	});
	$("h2").each(function(){
		var iPos = $(this).text().toLowerCase().indexOf('gold');
		var iStrLength = 4;
		var sStyle = '_gold';
		if ( iPos == -1 ) {
			  iPos = $(this).text().toLowerCase().indexOf('silver');
			  iStrLength = 6;
			  sStyle = '_silver';
		}
		if ( iPos == -1 ) {
			  iPos = $(this).text().toLowerCase().indexOf('bronze');
			  iStrLength = 6;
			  sStyle = '_bronze';
		}
		if ( iPos >= 0 ) {
			var normalPrefixText    = $(this).text().substr( 0 ,iPos );
			var colourText          = $(this).text().substr( iPos,iStrLength );
			var normalPostfixText   = $(this).text().substr( iPos+iStrLength );
			var html = ''; 
			if ( normalPrefixText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(normalPrefixText)+"&s=h2\" alt=\""+normalPrefixText+"\" \/>";
			}
			if ( colourText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(colourText)+"&s=h2_"+sStyle+"\" alt=\""+colourText+"\" \/>";
			}
			if ( normalPostfixText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(normalPostfixText)+"&s=h2\" alt=\""+normalPostfixText+"\" \/>";
			} 
			$(this).html( html );
		}
		else {
			$(this).html( "<img src=\"/image.php?txt="+escape($(this).text())+"&s=h2\" alt=\""+$(this).text()+"\" \/>" );
		}
	});
	$("h3").each(function(){
		var iPos = $(this).text().toLowerCase().indexOf('gold');
		var iStrLength = 4;
		var sStyle = '_gold';
		if ( iPos == -1 ) {
			  iPos = $(this).text().toLowerCase().indexOf('silver');
			  iStrLength = 6;
			  sStyle = '_silver';
		}
		if ( iPos == -1 ) {
			  iPos = $(this).text().toLowerCase().indexOf('bronze');
			  iStrLength = 6;
			  sStyle = '_bronze';
		}
		if ( iPos >= 0 ) {
			var normalPrefixText    = $(this).text().substr( 0 ,iPos );
			var colourText          = $(this).text().substr( iPos,iStrLength );
			var normalPostfixText   = $(this).text().substr( iPos+iStrLength );
			var html = ''; 
			if ( normalPrefixText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(normalPrefixText)+"&s=h3\" alt=\""+normalPrefixText+"\" \/>";
			}
			if ( colourText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(colourText)+"&s=h3_"+sStyle+"\" alt=\""+colourText+"\" \/>";
			}
			if ( normalPostfixText != '' ) {
				html += "<img src=\"/image.php?txt="+escape(normalPostfixText)+"&s=h3\" alt=\""+normalPostfixText+"\" \/>";
			} 
			$(this).html( html );
		}
		else {
			$(this).html( "<img src=\"/image.php?txt="+escape($(this).text())+"&s=h3\" alt=\""+$(this).text()+"\" \/>" );
		}
	});
}

function Award_FormFieldnames() {
	$("form input,form textarea").each(function( i ) {
	   if ( $(this).attr( "title" ) && $(this).attr( "title" ) != "" && $(this).attr( "value" ) == "" ) {
	     if ( $(this).attr( 'class' ).indexOf( 'required' ) > -1 ) {
	       $(this).attr( "style" , "background: #fffff0 url( \'/image.php?txt="+$(this).attr( "title" )+"&sz=10&c=CCCCCC&b=FFFFF0&f=ARIAL\' ) no-repeat left" );
	     }
	     else {
	       $(this).attr( "style" , "background: #FFFFFF url( \'/image.php?txt="+$(this).attr( "title" )+"&sz=10&c=CCCCCC&b=FFFFFF&f=ARIAL\' ) no-repeat left" );
	     }
	   }
	});
	$("form input,form textarea").bind('keyup',function(){
	  if ( $(this).attr( 'value' ) != '' ) {
	    $(this).attr( "style" , '' );
	  }
	  else if ( $(this).attr( 'class' ).indexOf( 'required' ) == -1 ) {
	    $(this).attr( "style" , "background: #FFFFFF url( \'/image.php?txt="+$(this).attr( "title" )+"&sz=10&c=CCCCCC&b=FFFFFF&f=ARIAL\' ) no-repeat left" );
	  }
	  else{
	    $(this).attr( "style" , "background: #fffff0 url( \'/image.php?txt="+$(this).attr( "title" )+"&sz=10&c=CCCCCC&b=fffff0&f=ARIAL\' ) no-repeat left" );
	  }
	});
}

function Award_FooterOpen( sIdent ) {
	// TODO Fix this hack so that it works a little better or leave as is.
  var iAreaLeftPos = $(".FooterNav").offset();
  var iLeftPos = $("#"+sIdent).parent().get(0);
  iLeftPos = $(iLeftPos).offset();
  var iLeft = 0;
  if ( iLeftPos.left > iAreaLeftPos.left ) {
    iLeft = -(iLeftPos.left - iAreaLeftPos.left);
  }
  $(".FooterNav .open").attr( 'class', 'FooterNav closed' );
  $("#"+sIdent).parents("ul").attr( 'class' , 'FooterNav open' );
  $("#"+sIdent).css('position' , 'absolute').attr( 'class' , 'FooterNav open' ).css( 'left' , iLeft+'px' );
}

/**
* e.g. ResizeFlash({nHeight:nClientHeight});
* or more specifically oResult = ExternalInterface.call('Award_ResizeFlash', {nHeight:nClientHeight});
*/
function Award_ResizeFlash(nWidth,nHeight) {
  //var nWidth = arguments[0].nWidth ? arguments[0].nWidth : null;
  //var nHeight = arguments[0].nHeight ? arguments[0].nHeight : null;
  //
  var divFlash = document.getElementById("pagewrapper");
  //divFlash.style.margin = "0 auto";
  //divFlash.style.position = "relative";
  //if (nWidth) divFlash.style.width = nWidth+"px";
  if (nHeight) divFlash.style.height = nHeight+"px";
  //
  if (nHeight && nWidth) {
    var sResponse = "'jsResizeFlash' resized '"+divFlash.id+"' to width='"+nWidth+"' and height='"+nHeight+"'.";
  } else if (nWidth && !nHeight) {
    var sResponse = "'jsResizeFlash' resized '"+divFlash.id+"' to width='"+nWidth+"'.";
  } else if (!nWidth && nHeight) {
    var sResponse = "'jsResizeFlash' resized '"+divFlash.id+"' to height='"+nHeight+"'.";                             
  } else {
    var sResponse = "'jsResizeFlash' did not resize anything (no values passed).";
  }
  return(sResponse);
}
function FFMacFix(){
	console.log( "FFMacFix();" );
	$("h2").click();
	$("div#inner-content").click();
	$("div#content").click();
	$("div#scrollpane").click();
	$("div#pagewrapper").click();
	$("div#page-content").click();
	$("div#page-content").click();
	$("div#page-holder").click();
	$("div#page-centerer").click();
	$("div#pagewrapper").click();
	$("body").click();
	$("html").click();
	
}