/**
 * Highlight active menu selection
 */
Event.observe(window, 'load', function() {
	var menuLinks = $$('#navbar > li > a');
	var url = window.location.href;
	var path = window.location.href.replace('http://' + window.location.hostname, '');
	var matchType = 'partial'; // or exact
	var selected = false;

	if(path.match(/^\/?$/i)){// homepage
		matchType = 'exact';
	}else if(path.match(/^\/help-info\/contact\/$/i)){ // contact
		matchType = 'exact';
	}
	
	
	for(var i=0, link; link = menuLinks[i];i++){
		if(matchType == 'exact' && link.href.toLowerCase() == window.location.href.toLowerCase()){
			selected = true;
		}else if(matchType == 'partial'){
			var re = new RegExp("^"+link.href, "i");
			var linkPath = link.href.replace('http://' + window.location.hostname, '');
			if(matchType == 'partial' && linkPath.match(/^\/.+/i) && window.location.href.match(re)){
				selected = true;
			}
		}
		if(selected){
			$(link.parentNode).addClassName('selected');
			break;
		}
	}
	
});


function isValidPostcode(p) {
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}


function getDomainFromUrl(url) {
    var leadSlashes = url.indexOf('//');
    var domainStart = leadSlashes+2;
    var withoutResource = url.substring(domainStart, url.length);
    var nextSlash = withoutResource.indexOf('/');
    var domain = withoutResource.substring(0, nextSlash);
    if (domain.indexOf(':') > -1){
        var portColon = domain.indexOf(':');
        domain = domain.substring(0, portColon);
    }
    return domain;
}

var mouseX = 0;
var mouseY = 0;
var recaptureDisplayed = false;

function launchRecapture(){
    // Only launch if the mouse moves north
    // Don't launch if the referring page is on the same domain as the user may just want to go back
    // Don't fire again while still executing the function from previous mouse move.'


    
    var referringDomain = getDomainFromUrl(document.referrer);

    if(!referringDomain || referringDomain != window.location.hostname){


        Event.observe(document, 'mousemove', function(event) {
           //alert(event.clientX + ' - ' + event.clientY);

            if(recaptureDisplayed == true){
                cancelMouseMoveObserver();
            }else if(event.clientY < mouseY){
                recaptureDisplayed = true;
                loadRecapture();
                myLightWindow.activateWindow({
                    href: window.location.href + '#recapture',
                    width: 890,
                    height: 450
                });
                var iframes = $('lightwindow_contents').select('iframe');
                iframes[0].setStyle({
                  display: 'block',
                  visibility: 'visible'
                });

                //recaptureDisplayed = true;
                cancelMouseMoveObserver();

            }else{ // We don't want this observer hanging around as its a bit hungry so make sure it gets removed
                var t = setTimeout('cancelMouseMoveObserver()', 1000);
            }
            mouseY = event.clientY;
       });
    }
}

function cancelMouseMoveObserver(){
    //alert('cancelling observer');
    Event.stopObserving(document, 'mousemove');
}



function loadRecapture(){
   iframe = document.createElement('iframe');
   Element.writeAttribute(iframe, 'src', 'http://mediaserver.hthealth.com/www/delivery/afr.php?zoneid=31&amp;cb=INSERT_RANDOM_NUMBER_HERE');
   Element.writeAttribute(iframe, 'style', 'border:none;width:890px;height:450px;');
   Element.writeAttribute(iframe, 'class', 'recaptureIFrame');
   Element.writeAttribute(iframe, 'scrolling', 'no');
   Element.writeAttribute(iframe, 'frameborder', '0');
   Element.writeAttribute(iframe, 'border', '0');
   $('recapture').insert(iframe);



}

        //Event.observe(window, 'load', function(event) {
        //    var int=self.setInterval("btn_effects()",2000);
        //});
        //function btn_effects(){
        //    btn_glow();
        //   btn_shake();
        //}
        //function btn_glow(){
        //    var glowElts = $('content').select('.btnGlow');
        //    for(var i = 0; i < glowElts.length; i++){
        //        new Effect.Opacity($(glowElts[i]), { queue : 'end',  from: 1.0, to: 0.65, duration: 0.25 });
        //        new Effect.Opacity($(glowElts[i]), { queue : 'end',  from: 0.65, to: 1, duration: 0.25 });
        //    }
       // }
       // function btn_shake(){
        //    var glowElts = $('content').select('.btnShake');
        //    for(var i = 0; i < glowElts.length; i++){
        //        new Effect.Shake($(glowElts[i]), {duration : 0.25, distance: 3});
         //   }
        //}





