/**
 * @author Sonic
 */

var _timeout = 750;
var _subnav = '';
var _region = '';
var _current ='';

var _xpos = '';
var _ypos = '';

// Standard jQuery header
;(function($) {
$(document).ready(function() {

	
		$('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();
			}
		);
        
        $('a[rel="lightbox"]').fancybox(
          {
          'width': 760,
          'height': 800,
          'autoScale': false,
          'type': 'iframe'
          }
        );
        
        $('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'});
        //}
        
        $('.mouseovertip').hover(function(){
    		rel = $(this).attr('rel');
    		$('#'+rel).show();
    	},function(){
    		rel = $(this).attr('hide');
    		$('#'+rel).hide();
    	});
        

	$('.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 Airtricity"){
			$(this).val('');
		}
	});
	
	$('#sitesearch').blur(function(){
		if($(this).val()==""){
			$(this).val('Search Airtricity');
		}
	});
	
	$('#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"});
				  }
				}
                                
                                
				//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);
