/* ...................................................
	
	Common javascript functions.
		
................................................... */

$(document).ready(function(){
	
	if ($('.lnkBookmark').length > 0) {
		if((!$.browser.msie) || ($.browser.msie && $.browser.version!="6.0")) {
			$('.lnkBookmark').jFav();
		}
	}
	
	$('input').focus(function(){
		if ($(this).attr('title').length > 0 && $(this).val() == $(this).attr('title')) {
			$(this).val("");
		}
	});
	$('input').blur(function(){
		if ($(this).attr('title').length > 0 && $(this).val() == "") {
			$(this).val($(this).attr('title'));
		}
	});
	

	$('.newsPreview li').hover(function(){
		$(this).addClass('listingHover');
	}, function(){
		$(this).removeClass('listingHover');
	});
	
	
	/*	
	$('.newsPreview li').toggle(function() {
			$(this).find('.boxExp').show();
			$(this).find('.boxColl').hide();
			$(this).find('.lnkExpColl').toggleClass('show');
		}, function() {
			$(this).find('.boxExp').hide();
			$(this).find('.boxColl').show();
			$(this).find('.lnkExpColl').toggleClass('show');
	});
	*/
	
	$('.lnkExpColl').toggle(function() { 
			$(this).siblings('.boxExp').show(); 
			$(this).siblings('.boxColl').hide();
			$(this).toggleClass('show');
		}, function() { 
			$(this).siblings('.boxExp').hide();
			$(this).siblings('.boxColl').show();
			$(this).toggleClass('show');
		});

	$('.stationStatus li').hover(function(){
		if (!$(this).hasClass('listHeader')) {
			$(this).addClass('listingHover');
		}
	}, function(){
		if (!$(this).hasClass('listHeader')) {
			$(this).removeClass('listingHover');
		}
	});

	$('.block.disruptions li').hover(function(){
		if (!$(this).hasClass('listHeader')) {
			$(this).addClass('listingHover');
		}
	}, function(){
		if (!$(this).hasClass('listHeader')) {
			$(this).removeClass('listingHover');
		}
	});
	/*
	$('.serviceUpdates li').click(function(){
		window.location.href = $(this).find('.col1 a').attr('href');
	});	
	*/
	
	/* // [DISABLED]: ACCORDIAN EFFECT
		$(".serviceUpdates").accordion({
			event: 'mouseover',
			header: 'a.lnkInfo',
			active: 'selected',
			autoheight: false
		});
	//*/
	
	
	$('.viewport li').hover(function(){
		$(this).addClass('listingHover');
	}, function(){
		$(this).removeClass('listingHover');
	});
	
	$('.listing .moduleContent li').hover(function(){
		$(this).addClass('listingHover');
	}, function(){
		$(this).removeClass('listingHover');
	});
	/*
	$('.listing .moduelContent li').click(function(){
		window.location.href = $(this).find('.moreInfo a').attr('href');
	});	
	//*/
	
	/*
	// CALL CAROUSEL
	if ($('body').hasClass('home')) {
		$('#carousel .viewport').jcarousel();
	}
	*/
	var mousePosY;
	
	$().mousemove(function(e) {
		mousePosY = e.pageY;
	});

	// HOVER
	var infoBoxOpen = function(){
		$('.infoBox', this).show();
	}
	var infoBoxClose = function(){
		$('.infoBox', this).hide();
	}
	
	/*
	$('.serviceUpdates li').each(function(){
		//if there's an infoBox, hide it
		if($('.infoBox', this).length > 0){ 
			$('.infoBox', this).hide();
			$(this).hover(infoBoxOpen, infoBoxClose);
		}
	});
		
	//*/
	
	$('.plannedDisruptions li').each(function(){
		$('.infoBox', this).hide();
		$('.infoBox', this).addClass(($('.plannedDisruptions li').index(this) < $('.plannedDisruptions li').length-1)? 'below' : 'above');
		$(this).hover(infoBoxOpen, infoBoxClose);
	});
	
	$(".serviceUpdates li").each(function() {
		if ($('.infoBox', this).length > 0) {
			$('.infoBox', this).hide();
			$(this).hover(function(e){
				$('.infoBox', this).css({top: ($('.serviceUpdates li').index(this)*25) });
				$('.infoBox', this).show();
			}, infoBoxClose);
		}
	});

	$("body.home .serviceUpdates li").each(function() {
		if ($('.infoBox', this).length > 0) {
			$('.infoBox', this).hide();
			$('.infoBox', this).addClass(($('.serviceUpdates li').index(this) < $('.serviceUpdates li').length-2)? 'below' : 'above');
			$(this).hover(function(e){
				//$('.infoBox', this).css({top: ($('.serviceUpdates li').index(this)*25) });
				$('.infoBox', this).css({top: ($('.serviceUpdates li').index(this)*25) - ($('.infoBox', this).hasClass('above')? 70 : 0) });
				$('.infoBox', this).show();
			}, infoBoxClose);
		}
	});
    
    
    var tdHeight=0;
    var tableHeight=$("table.serviceUpdates").height();
    //alert("table height is "+tableHeight);
    
	$(".serviceUpdates td.col2").each(function() {
		
		tdHeight = tdHeight + 25;
		
		if ($('.infoBox', this).length > 0) {
		    
		    var detailBoxHeight = $('.infoBox', this).height();
			$('.infoBox', this).hide();
			
			if(detailBoxHeight > (tableHeight - tdHeight)){
			    var findTopMargin = (0 - (detailBoxHeight+5));
			    $('.infoBox', this).css({marginTop:findTopMargin+'px'});
			    $('.callOut', this).css({marginTop:detailBoxHeight+'px', backgroundPosition: "-299px 0"});
			}
			
			
			//var detailLineLength = parseInt($('.infoBox', this).find('.detail').text().length / 27);
			//var boxBottom = ($('.serviceUpdates td.col2').index(this) * 25) + (detailLineLength * 19);
			//$(this).siblings('.col1').html($(this).siblings('.col1').html() + ": <span title=\"" + boxBottom + "\">" + detailLineLength + "</span>");
			
			/*if (boxBottom > 420) {
				if (detailLineLength > 10 && $('.serviceUpdates td.col2').index(this) > 4 && $('.serviceUpdates td.col2').index(this) < $('.infoBox', this).find('.detail').html().length-5) {
					$('.infoBox', this).find('.detail').css({height: ((detailLineLength * 20)-20) + 'px', marginBottom: '20px'});
					$('.infoBox', this).find('.callOut').css({display: 'none'});
				}
				else if (detailLineLength > 6) {
					$('.infoBox', this).find('.detail').css({height: ((detailLineLength * 20)-20) + 'px', marginBottom: '20px'});
					$('.infoBox', this).find('.callOut').css({top: (detailLineLength * 20)+5 + 'px'});
				}
				$('.infoBox', this).addClass('above');
			}
			else {
				$('.infoBox', this).addClass('below');
			}*/
			
			
			
			
			$(this).hover(function(e){
				//$('.infoBox', this).css({paddingBottom: $('.infoBox', this).hasClass('above')? 80 : 0 });
				$('.infoBox', this).show();
			}, infoBoxClose);
		
		}
	});
	$(".serviceUpdates tr").hover(function() {
			if ($(".serviceUpdates tr").index(this) != 0) {
				$(this).addClass('listingHover');
			}
		}, function() {
			if ($(".serviceUpdates tr").index(this) != 0) {
				$(this).removeClass('listingHover');		
			}
	});

	$(".plannedDisruptions td.col2").each(function() {
		if ($('.infoBox', this).length > 0) {
			$('.infoBox', this).hide();
			$(this).hover(infoBoxOpen, infoBoxClose);
		}
	});
	$(".plannedDisruptions tr").hover(function() {
			if ($(".plannedDisruptions tr").index(this) != 0) {
				$(this).addClass('listingHover');
			}
		}, function() {
			if ($(".plannedDisruptions tr").index(this) != 0) {
				$(this).removeClass('listingHover');		
			}
	});

	if ($('.carousel').length > 0) {
		var autoscrolling = true;
		$('.carousel .wrapper li').hover(function() {
			$(this).addClass('listingHover');
		}, function() {
			$(this).removeClass('listingHover');
		});
		$('.carousel').infiniteCarousel().mouseover(function() {
			autoscrolling = false;
		}).mouseout(function() {
			autoscrolling = true;
		});
		setInterval(function() {
			if (autoscrolling) {
				$('.carousel').trigger('next');
			}
		}, 10000);
	}
});

