/**
 * @author Sonic
 */

var _timeout = 750;
var _subnav = '';
var _region = '';
var _current ='';

var _xpos = '';
var _ypos = '';

// Standard jQuery header
;(function($) {
$(document).ready(function() {
	
		
		//default
	 var _loggedin = false;
	 var _lname = '';
	 var _access  = '';
	 
	 $('.tracklink').bind('click keypress',function(event){
	 	var fixedlink = $(this).attr('name');
	 	pageTracker._trackPageview("/"+ fixedlink.replace(/_/gi, "/"));
	 });
	 
	 $("a[href^='http://my.airtricity.com/']").bind('click keypress',function(event){
		if($(this).hasClass('tracklink')==false){
			if(pageTracker){
			
			
				var page = document.location;
				var tmp = page.toString();
				var addy = tmp.split('/');
				
				
				section  = addy[(addy.length-1)];
				if(section==""){
					section  =addy[(addy.length-2)]
				}
			
				var fixedLink = this.href;
				//fixedLink = fixedLink.replace(/http?:\/\/(.*)/,"$1");
				fixedLink = '/' + fixedLink.substring(25,28) + '/'  + section + '/';
				pageTracker._trackPageview(fixedLink);
				
			  }
          }
          
	});
	
	$("a[href^='https://my.airtricity.com/']").bind('click keypress',function(event){
		if($(this).hasClass('tracklink')==false){
			if(pageTracker){
			
			
				var page = document.location;
				var tmp = page.toString();
				var addy = tmp.split('/');
				
				
				section  = addy[(addy.length-1)];
				if(section==""){
					section  =addy[(addy.length-2)]
				}
			
				var fixedLink = this.href;
				//fixedLink = fixedLink.replace(/http?:\/\/(.*)/,"$1");
				fixedLink = '/' + fixedLink.substring(26,29) + '/'  + section + '/';
				pageTracker._trackPageview(fixedLink);
			  }
          }
          
	});
	 
	 
	  $('.logout').live("click", function(){
  
              $(document.body).append('<iframe id="killsession" height="0" width="0" onload="if (document.location.href != \'about:blank\') parent.location.reload()"></iframe>');
    		  $('iframe#killsession').attr('src', 'https://my.airtricity.com/ews/logout.jsp?nt='+$(this).attr('name'));
              Set_Cookie( 'oss_LastAccess', '19700316T133221', '1', '/', 'airtricity.com' );
            
       });
  
		
	  if (ingetCookie('oss_LastAccess')) {
	
			_access  =	ingetCookie('oss_LastAccess');
	
		  var y = _access.substring(0,4);
		  var m = _access.substring(4,6);
		  var dd = _access.substring(6,8);
		  var h = _access.substring(9,11);
		  var i = _access.substring(11,13);
		  var s = _access.substring(13,15);
		  
		  var d = new Date(parseInt(y),parseInt(m),parseInt(dd),parseInt(h),parseInt(i),parseInt(s));
		  d.setMonth((m-1),dd);
		  var n = new Date();
	
		  
		  var diff = Math.ceil((n.getTime()-d.getTime())/(1000*60));
		  
		  //older than 5 minutes
		  if(diff < 6){
		  	_loggedin = true;
		  }

	  	_lname = ingetCookie('oss_Surname');
	  
	  }
	  
	  //user has cookie
	  if(_lname != ""){

		  
		  if(_loggedin==true){
		  	
		  	loc = document.location.toString();
		  	
		  	section = "roidom";
		  	
		  	if(loc.indexOf('roi-commercial')>-1){
		  		section = "roicomm"
		  	}
		  	if(loc.indexOf('ni-commercial')>-1){
		  		section = "nicomm"
		  	}
		  	
		  	$('#loginstatus').html('<a href="#" class="logout" name="'+section+'">Log Out</a>');
			 
		  }else{
		  	
		  	$('#loginstatus').html('<a href="http://my.airtricity.com/ews/welcome.jsp">Log In</a>');
			
		  }
	  }
		
		
		
	
		/*
		//doesn't work in ie7
		$('area[shape="circle"]').hover(
			function(){
			
				$(document).mousemove(function(e){
      				_xpos = e.pageX ;
      				_ypos = e.pageY;
   				});
			
				el = $(this).attr('href');
				
				
				
				$(el).css("left", _xpos + "px");
				$(el).css("top", _ypos+ "px");
				
				$(el).show();
				
			},
			function(){
				el = $(this).attr('href');
				$(el).hide();
			}
		);
		*/
		$('area').hover(
			function(){
			if($(this).attr('shape')=='circle' || $(this).attr('SHAPE')=='circle' || $(this).attr('shape')=='CIRCLE'  || $(this).attr('SHAPE')=='CIRCLE' ){
				$(document).mousemove(function(e){
      				_xpos = e.pageX ;
      				_ypos = e.pageY;
   				});
			
				el = $(this).attr('href');
				
				
				
				$(el).css("left", _xpos + "px");
				$(el).css("top", _ypos+ "px");
				
				$(el).show();
				}
				
			},
			function(){
			if($(this).attr('shape')=='circle' || $(this).attr('SHAPE')=='circle' || $(this).attr('shape')=='CIRCLE'  || $(this).attr('SHAPE')=='CIRCLE' ){
				el = $(this).attr('href');
				$(el).hide();
				}
			}
		);
		$('area').click(function(){
			if($(this).attr('shape')=='circle' || $(this).attr('SHAPE')=='circle' || $(this).attr('shape')=='CIRCLE'  || $(this).attr('SHAPE')=='CIRCLE' ){
				return false;
			}
		});
		
        
        $('a[rel="lightbox"]').fancybox(
          {
          'width': 760,
          'height': 800,
          'autoScale': false,
          'type': 'iframe',
          'showNavArrows': false
          }
        );
        
        $('a[rel="lightbox"][href*=.jpg]').fancybox(
          {
          'autoScale': false
          }
        );
        
        
	var _loginpos = "up";
	
	//setup all to have pointer if js enabled
	//$("#login_radio").css("cursor", "pointer");
	//if($('#lhs_col a:last')){
        $('#lhs_col a:last').css({'border': '0'});
        //}

	$('.tab').hover(function(){
		$(this).css({'background-position':'0 -26px'});
	},function(){
		$(this).css({'background-position':'0 0'});
	});
	
	$('.region_item').hover(function(){
		$(this).css({"background":"#f4f4f4"});
	},function(){
		$(this).css({"background":"#ffffff"});
	});
	
	//remember me radio
	$('#login_radio').hover(
		function(){
			if (_loginpos == "up") {
				$(this).css({
					'background-position': '0 -12px'
				});
			}
			
			$('#login_radio .text').css({'border-bottom':'1px dotted #666666'});
		
		},
		function(){
			if(_loginpos=="up"){
				$(this).css({
					"background-position":'0 0'
				});
			}
			$('#login_radio .text').css({'border-bottom':'0'});
		}
	);
	
	
	$('#login_user').focus(function(){
		if($(this).val()=="Username"){
			$(this).val('');
		}
	});
	
	$('#login_user').blur(function(){
		if($(this).val()==""){
			$(this).val('Username');
		}
	});
	
	$('#sitesearch').focus(function(){
		if($(this).val()=="SEARCH SITE"){
			$(this).val('');
		}
	});
	
	$('#sitesearch').blur(function(){
		if($(this).val()==""){
			$(this).val('SEARCH SITE');
		}
	});
	
	$('#login_password').focus(function(){
		$(this).css({"background-position":"0 -16px"});
	});
	
	$('#login_password').blur(function(){
		if($(this).val()==""){
			$(this).css({"background-position":"0 0px"});
		}
	});
	
	$('#select_region').click(function(){
		clearTimeout(_region);
		if($('#region_drop').is(':visible')==true){
			$('#region_drop').hide();
		}else{
			$('#region_drop').slideDown('fast');
			 _region =  window.setTimeout(function(){
				$('#region_drop').hide();
			 },(_timeout+2000));
		}
		
	});
	
	$('#login_radio').click(function(){
		if(_loginpos=="up"){
			_loginpos ="down";
			$(this).css({'background-position':'0 -12px'});
			$('#rememberme').attr("checked","checked");
		}else{
			_loginpos ="up";
			$(this).css({'background-position':'0 0px'});
			$('#rememberme').attr("checked","");
		}
	});
	
	
	//clear after timeout
	function removeTemp(el){
		
		//ie6 fix?
		if(jQuery.browser.msie){
			$('#'+el+'_menu').stop().fadeOut(100);
		}else{
			$('#'+el+'_menu').stop().fadeOut(100);
		}
	
		$(".nav_item[id='"+el+"_item']").css({'background-position':'0 0'});
		$(".nav_item[id='"+el+"_item']").find('.nav_icon').css({'background':'url(/themes/airtricity/img/plus_icon.png) right center no-repeat'});
	
		_current = '';
	}
		
	//menu rollovers
	$(".nav_item").hover(function(){
			
			//reset all rollovers
			$('.nav_item').css({'background-position':'0 0'});
			$('.nav_icon').css({'background':'url(/themes/airtricity/img/plus_icon.png) right center no-repeat'});
		
			 var tmp = $(this).attr('id');
			 tmp2 = tmp.split('_');	
			 var el = tmp2[0];		  
			 if(_current != el){ 
			 	$('.submenu').stop(true,true).hide();
			 }
			 
			 $(this).css({'background-position':'0 -39px'});
			 $(this).find('.nav_icon').css({'background':'url(/themes/airtricity/img/arrow_icon.png) right center no-repeat'});
			 
			 clearTimeout(_subnav);
			  
			 if (_current != el) {
			 	
				var _height = 0;
				
                                if ($('#' + el + '_menu .submenu_detail ul li a').length > 0) {
				//calculate height - overcomes diffuclty with absolute position and height problems 
				$('#' + el + '_menu .submenu_detail ul li a').each(function(){
					_height = _height + 24;
				});
				$('#' + el + '_menu').height(_height + 12);
				
				
				//realigns top blue line
				offset = $('#' + el + '_item a').width();
				$('#' + el + '_menu').css({"background-position": (offset-2) + "px 0px"});
				
                                if (jQuery.browser.msie) {
				  if(parseInt(jQuery.browser.version)==7 || parseInt(jQuery.browser.version) == 6){
				  	//ie7 fix
                                        var position = $('#' + el + '_item a').position();
					$('#' + el + '_menu').css({"left": position.left + "px"});
					$('#' + el + '_menu').css({"top": "194px"});
					$('#' + el + '_menu').css({"margin-left": "auto"});
                                        
                                        if(parseInt(jQuery.browser.version) == 6){
                                          $('#' + el + '_menu').css({"top": "180px"});
                                        }
				  }
				}
                                
                                
				//slidedown
				$('#' + el + '_menu').slideDown(180);
                                }
				
			  }
			  _current = el;
			  
		    },
			function() {
                        var tmp = $(this).attr('id');
			 tmp2 = tmp.split('_');	
			 var el = tmp2[0];	
			  _subnav =  window.setTimeout(function(){
				removeTemp(el);
			 },_timeout);
	});
		
	$('.submenu').hover(
		function(){
			clearTimeout(_subnav);				
			$(this).show();
			var el = $(this).attr('id').split('_');
		},
		function(){
                        var el = $(this).attr('id').split('_');
			_subnav =  window.setTimeout(function(){
			removeTemp(el[0]);
		  	},_timeout);
			
		}
	);
	
	
// Standard jQuery footer
})
})(jQuery);


function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function inDelete_Cookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


	function ingetCookie(name) {
	  var dc = document.cookie;
	  var prefix = name + "=";
	  var begin = dc.indexOf("; " + prefix);
	  if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	  } else
	    begin += 2;
	  var end = document.cookie.indexOf(";", begin);
	  if (end == -1)
	    end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	}
