/*
 * jqModal - Minimalist Modaling with jQuery
 *   (http://dev.iceburg.net/jquery/jqModal/)
 *
 * Copyright (c) 2007,2008 Brice Burgess <bhb@iceburg.net>
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 * 
 * $Version: 03/01/2009 +r14
 */
(function($) {
$.fn.jqm=function(o){
var p={
overlay: 50,
overlayClass: 'jqmOverlay',
closeClass: 'jqmClose',
trigger: '.jqModal',
ajax: F,
ajaxText: '',
target: F,
modal: F,
toTop: F,
onShow: F,
onHide: F,
onLoad: F
};
return this.each(function(){if(this._jqm)return H[this._jqm].c=$.extend({},H[this._jqm].c,o);s++;this._jqm=s;
H[s]={c:$.extend(p,$.jqm.params,o),a:F,w:$(this).addClass('jqmID'+s),s:s};
if(p.trigger)$(this).jqmAddTrigger(p.trigger);
});};

$.fn.jqmAddClose=function(e){return hs(this,e,'close');};
$.fn.jqmAddTrigger=function(e){return hs(this,e,'jqmShow');};
$.fn.jqmShow=function(t){return this.each(function(){t=t||window.event;$.jqm.open(this._jqm,t);});};
$.fn.jqmHide=function(t){return this.each(function(){t=t||window.event;$.jqm.close(this._jqm,t)});};

$.jqm = {
hash:{},
open:function(s,t){var h=H[s],c=h.c,cc='.'+c.closeClass,z=(parseInt(h.w.css('z-index'))),z=(z>0)?z:3000,o=$('<div></div>').css({height:'100%',width:'100%',position:'fixed',left:0,top:0,'z-index':z-1,opacity:c.overlay/100});if(h.a)return F;h.t=t;h.a=true;h.w.css('z-index',z);
 if(c.modal) {if(!A[0])L('bind');A.push(s);}
 else if(c.overlay > 0)h.w.jqmAddClose(o);
 else o=F;

 h.o=(o)?o.addClass(c.overlayClass).prependTo('body'):F;
 if(ie6){$('html,body').css({height:'100%',width:'100%'});if(o){o=o.css({position:'absolute'})[0];for(var y in {Top:1,Left:1})o.style.setExpression(y.toLowerCase(),"(_=(document.documentElement.scroll"+y+" || document.body.scroll"+y+"))+'px'");}}

 if(c.ajax) {var r=c.target||h.w,u=c.ajax,r=(typeof r == 'string')?$(r,h.w):$(r),u=(u.substr(0,1) == '@')?$(t).attr(u.substring(1)):u;
  r.html(c.ajaxText).load(u,function(){if(c.onLoad)c.onLoad.call(this,h);if(cc)h.w.jqmAddClose($(cc,h.w));e(h);});}
 else if(cc)h.w.jqmAddClose($(cc,h.w));

 if(c.toTop&&h.o)h.w.before('<span id="jqmP'+h.w[0]._jqm+'"></span>').insertAfter(h.o);	
 (c.onShow)?c.onShow(h):h.w.show();e(h);return F;
},
close:function(s){var h=H[s];if(!h.a)return F;h.a=F;
 if(A[0]){A.pop();if(!A[0])L('unbind');}
 if(h.c.toTop&&h.o)$('#jqmP'+h.w[0]._jqm).after(h.w).remove();
 if(h.c.onHide)h.c.onHide(h);else{h.w.hide();if(h.o)h.o.remove();} return F;
},
params:{}};
var s=0,H=$.jqm.hash,A=[],ie6=$.browser.msie&&($.browser.version == "6.0"),F=false,
i=$('<iframe src="javascript:false;document.write(\'\');" class="jqm"></iframe>').css({opacity:0}),
e=function(h){if(ie6)if(h.o)h.o.html('<p style="width:100%;height:100%"/>').prepend(i);else if(!$('iframe.jqm',h.w)[0])h.w.prepend(i); f(h);},
f=function(h){try{$(':input:visible',h.w)[0].focus();}catch(_){}},
L=function(t){$()[t]("keypress",m)[t]("keydown",m)[t]("mousedown",m);},
m=function(e){var h=H[A[A.length-1]],r=(!$(e.target).parents('.jqmID'+h.s)[0]);if(r)f(h);return !r;},
hs=function(w,t,c){return w.each(function(){var s=this._jqm;$(t).each(function() {
 if(!this[c]){this[c]=[];$(this).click(function(){for(var i in {jqmShow:1,jqmHide:1})for(var s in this[i])if(H[this[i][s]])H[this[i][s]].w[i](this);return F;});}this[c].push(s);});});};
})(jQuery);

/*
Javascript code supporting ajax functionalities.
*/

var NotesCookie = false;
var PrintCartCookie = false;
var LinkCartCookie  = false;
var dldInProgress = false;
var basepath = '/';
var notesUrl = "";
var printUrl = "";
var LinksUrl = "";
var mailerUrl = "";
var feedbackUrl = "";
var downloadsUrl = "";
var exitUrl = "";
var options = false;
var inputDefaults = {fromName: 'Your name',toEmail: 'Contact\'s email',toName: 'Contact name',message: 'Your message',zoom_query: 'Search by keyword',frmCode: 'Validation code'};

function establishPgId()
{
	var contentClass = $('body').attr("class");
	var rg = new RegExp("pg\\d+");
	var res = rg.exec(contentClass);
	contentClass = res;
	return res;
}		


function isInCart(){
		var value =  PrintCartCookie;
		return (value != null && value.indexOf(establishPgId())>-1)
}
	
	
function setCartPageStatus(){
    if (isInCart()){
			$('ul#features > li > a.add').addClass("inactive");
			$('ul#features > li > a.add').click(function(){return false;});
			var currnode = $('ul#features > li:first').html();
	}
}


function getPageFromLink(link){			
		var pg = $(link).attr('href');
		var rg = new RegExp("pg\\d+");
		var res = rg.exec(pg);
		return res;
}
function displayPopup(){
	$('body').append('<div id="dialog" class="jqmWindow"><div class="border_opt"><a class="close" id="closeX" title="Close window" href="#">x</a><div class="content"></div><div id="splash"><h3>Processing...</h3><img src="'+basepath+'images/loading.gif" /></div></div></div>');
 	$('#dialog').jqm({onShow: showEvent, modal: true}).jqmShow();
	$('div.jqmOverlay').click(function(){ClosePopup();});
	$('.close').click(function(){ClosePopup();});
}
function showEvent(hash){
		SetSplashStatus(true);
		hash.w.show();
		$.get(options['url'],options['params'],function (data){
				loadEvent(data);
			}
	);
}
function loadEvent(data){
	$('#dialog  div.content').html(data);	
	SetSplashStatus(false);
	if (options['CancelAction']){
			$(options['CancelAction']).click(function(){
				ClosePopup();
				return false;
			});
			$(options['CancelAction']).show();
	}		
	if (options['DoAction']) {
		$(options['DoAction']).click(submitEvent);
	}
	
	if (options['loadCallback']) {
		options['loadCallback']();
	}
	
	var ifocus = function() {
				if($(this).attr('id') != '' && inputDefaults[$(this).attr('id')] != '' && $(this).val() == inputDefaults[$(this).attr('id')]) {
				  $(this).val('');
				}
		    };
			
	var iblur = function() {
				if($(this).val().length == 0 && $(this).attr('id') != '' && inputDefaults[$(this).attr('id')] != '') {
				  $(this).val(inputDefaults[$(this).attr('id')]);
				}
		    };

	$('input[@type="text"],textarea').focus(ifocus);
	$('input[@type="text"],textarea').blur(iblur);
}

function submitEvent(data){
	  SetSplashStatus(true);
	  var fields = $('#dialog  div.content').find(":input");
	  var inputs = getAjaxFormFields(fields);
	  $.get(options['url'],inputs,function (data){
	  		loadEvent(data);
	  		if (options['submitCallback'] != null){
	  			options['submitCallback']();
				}
	  	});
		return false;
}
function ClosePopup(){
	$('#dialog').jqmHide();
	$('#dialog').remove();
}

function getAjaxFormFields(fields){
	var inputs = new Object();
	inputs['ajax']=1;
	for (i=0;i<fields.length;i++){
	  	if (fields[i].name != '') {
			if ((fields[i].type == 'radio' || fields[i].type == 'checkbox')){
				if (fields[i].checked) inputs[fields[i].name]= fields[i].value;
			}
			else
				inputs[fields[i].name]= fields[i].value;
		}
	}
	return inputs;
}

function SetSplashStatus(visible){
	if (visible){
		$('#splash').show();
		$('#dialog  div.content').hide();
		if (options['windowClass']) $('#dialog').removeClass(options['windowClass']);		
	}
	else{
		$('#splash').hide();
		$('#dialog  div.content').show();
		if (options['windowClass']) $('#dialog').addClass(options['windowClass']);
	}
}


$(document).ready(function() {
			var x = document.location.href.replace('http://','');
			var y = $('li.home > a').attr('href');
			
			basepath = x.substring(x.indexOf('/'),x.lastIndexOf('/')+1)+y.replace('index.html','');
			//define urls
			printUrl = basepath+'scripts/print.asp';
			mailerUrl = basepath+'scripts/email.asp';
			exitUrl = basepath+'scripts/exit.asp';
			feedbackUrl = basepath+'scripts/feedback.asp';
			$('#print_all').click(function (){
				$('input.'+$(this).attr('class')).attr('checked',$(this).attr('checked'));						     
			});
		   $('#delete_all').click(function (){
				$('input.'+$(this).attr('class')).attr('checked',$(this).attr('checked'));						     
			});
		   $('#email_all').click(function (){
				$('input.'+$(this).attr('class')).attr('checked',$(this).attr('checked'));						     
			});
		    
			$('a.printButton').click(function() {
				window.print();
		  });
			
		  PrintCartCookie = $.ajax({type: 'GET',url: printUrl,async: false,data: {ajax: 1, ajxcmd: 'getcookie'},dataType: 'text'}).responseText
		  
		  $('a.email').click(function(){
				options = {url: mailerUrl,params: {ajax:1,pg:getPageFromLink('a.email')},CancelAction: '#CancelSend',DoAction: '#ActionSend', windowClass: 'email'};
				displayPopup();
				return false;		
		   });  	
		
		  $('a.feedback').click(function(){
				options = {url: feedbackUrl,params: {ajax:1},CancelAction: '#CancelSend',DoAction: '#ActionSend',windowClass: 'feedback'};
				displayPopup();				
				return false;		
		   });  	
		
		
		   $('a.external').click(function (){
						var surl = (this.href.indexOf('url=')>0) ? this.href.substring(this.href.indexOf('url=')+4) : this.href;
						options = {url: exitUrl,params: {url:surl},CancelAction: '#CancelExit',windowClass: 'exit_page'};
						displayPopup();
						return false;
			 });	
			$('input.sa01').click(function (){
				$('input.sa02').attr('checked','');	
				//return false;
				
			 });
			$('input.sa02').click(function (){
				$('input.sa01').attr('checked','');
//return false;				
			 });			 
			setCartPageStatus();		
			$("table tbody tr").mouseover(function(){
				  $(this).addClass("rowhover");
				}).mouseout(function(){
				  $(this).removeClass("rowhover");
				});
				
			$('a.popup').click(function(){
				window.open($(this).attr('href'));
		        return false;		
		   });  
		   
		   var ifocus = function() {
				if($(this).attr('id') != '' && inputDefaults[$(this).attr('id')] != '' && $(this).val() == inputDefaults[$(this).attr('id')]) {
				  $(this).val('');
				}
		    };
			
		   var iblur = function() {
				if($(this).val().length == 0 && $(this).attr('id') != '' && inputDefaults[$(this).attr('id')] != '') {
				  $(this).val(inputDefaults[$(this).attr('id')]);
				}
		    };

		   $('input, text').focus(ifocus);
		   $('input, text').blur(iblur);
		   $('p.closetab').show();
		   
		   // section tabs header switchers
		   
		   $('li a#profile_1').click(function(){
				$('img#lead').attr("src","../../images/business_overview/how_we_operate.jpg");	
				$('img#lead').attr("alt","A Transmission linesman working on a high voltage tower");			
				$('img#lead').attr("title","A Transmission linesman working on a high voltage tower");			
			}); 
		   
		    $('li a#profile_2').click(function(){
				$('img#lead').attr("src","../../images/business_overview/bo_gas_dist.jpg");	
				$('img#lead').attr("alt","UK Gas Distribution crew working in a residential street");	
				$('img#lead').attr("title","UK Gas Distribution crew working in a residential street");		
		    }); 
			
			$('li a#profile_3').click(function(){
				$('img#lead').attr("src","../../images/business_overview/bo_ed&g.jpg");
				$('img#lead').attr("alt","Electricity Distribution & Generation crew working on line repairs after a storm");
				$('img#lead').attr("title","Electricity Distribution & Generation crew working on line repairs after a storm");
			}); 
			 
			$('li a#profile_4').click(function(){
				$('img#lead').attr("src","../../images/business_overview/bo_non_reg.jpg");
				$('img#lead').attr("alt","OnStream employee with branded vehicle");
				$('img#lead').attr("title","OnStream employee with branded vehicle");
		    }); 
		    $('.where_we_operate li a#profile_1').click(function(){
				$('img#lead').attr("src","../../images/business_overview/how_we_op_alt_pic.jpg");	
				$('img#lead').attr("alt","National Grid branding on a hard hat");		
				$('img#lead').attr("title","National Grid branding on a hard hat");		
		    }); 
		    $('.where_we_operate li a#profile_2').click(function(){
				$('img#lead').attr("src","../../images/business_overview/how_we_op_alt_pic.jpg");
				$('img#lead').attr("alt","National Grid branding on a high visibility jacket");			
				$('img#lead').attr("title","National Grid branding on a high visibility jacket");			
		    }); 
		    $('.where_we_operate li a#profile_3').click(function(){
				$('img#lead').attr("src","../../images/business_overview/how_we_op_alt_pic.jpg");
				$('img#lead').attr("alt","National Grid branding on a high visibility jacket");			
				$('img#lead').attr("title","National Grid branding on a high visibility jacket");			
		    }); 
		    $('.where_we_operate li a#profile_4').click(function(){
				$('img#lead').attr("src","../../images/business_overview/how_we_op_alt_pic.jpg");
				$('img#lead').attr("alt","National Grid branding on a high visibility jacket");
				$('img#lead').attr("title","National Grid branding on a high visibility jacket");						
		    }); 
		    
		    $(".homepage_mod").hover(
			  function () {
				src = $(this).attr('src');
				$(this).attr("style","background:#fff url(images/homepage/module_imgs.jpg) 900px top;");
			  }, 
			  function () {
				$(this).removeAttr('style')
			  }
			);
			
			$("#ceos_review.homepage_mod").hover(
			  function () {
				src = $(this).attr('src');
				$(this).attr("style","background:#fff url(images/homepage/module_imgs.jpg) -1200px top;");
			  }, 
			  function () {
				$(this).removeAttr('style')
			  }
			);
			
			$("#homepage_vision.homepage_mod").hover(
			  function () {
				src = $(this).attr('src');
				$(this).attr("style","background:#fff url(images/homepage/module_imgs.jpg) right top;");
			  }, 
			  function () {
				$(this).removeAttr('style')
			  }
			);
			
			
			
		   
});