// Vantage Scripts

$.noConflict();

jQuery(document).ready(function($) {

	// Navigation
	$("#header li").hover(function(event){
      $(this).addClass("over");
      $(this).parent('li').addClass("over");
	}, function(){
      $(this).removeClass("over");
	});
	
	// Navigation icons
    var ext = jQuery('ul.AspNet-Menu a').filter(function() { return this.hostname && this.hostname !== location.hostname; });
    var links = jQuery("ul.AspNet-Menu li a");

    ext.after('<span class="external"></span>');

    jQuery("ul.AspNet-Menu > li:has('ul')").each(function(){
		jQuery(this).find("a:first").after('<span class="down-arrow"></span>');
    });

    jQuery("ul.AspNet-Menu ul.AspNet-Menu > li:has('ul')").each(function(){
		jQuery(this).find("span.down-arrow").remove();
		jQuery(this).find("a:first").after('<span class="side-arrow"></span>');
    });

    links.hover(function(){
		jQuery(this).next('span').addClass('hover')
    }, function(){
		jQuery(this).next('span').removeClass('hover')
    });
	
	// GA Cross site links
	$("a[href^='http://']").each(function(){
		var url = $(this).attr("href");
		$(this).attr("onclick", "_gaq.push(['_link', '" + url + "']); return false;");
	});

	// Remove empty <p> tags
	$('p').filter(function() {
        return $.trim($(this).text()) === ''
    }).remove();	
	
	
	// Print stylesheet switcher
	$('#printable-button').show().toggle(function() {
		//$("link[rel=stylesheet]").attr({href : "/CMSPages/GetCSS.aspx?stylesheetname=vantagePrint"});
		$('head').append('<link rel="stylesheet" href="/CMSPages/GetCSS.aspx?stylesheetname=vantagePrint" media="screen" type="text/css" />');
		$(this).removeClass('viewprint').addClass('viewnonprint').text('View normal version');
		$('#content').removeClass().addClass('clear prepend-2 span-20 append-2 last');
		$('#actionbar').removeClass().addClass('clear prepend-2 span-20 append-2 last');
		$('#header').removeClass().addClass('clear prepend-2 span-20 append-2 last');
		return false;
	}, function() {
		//$("link[rel=stylesheet]").attr({href : "/CMSPages/GetCSS.aspx?stylesheetname=vantage"});
		//$("link[media=print]").attr({href : "/CMSPages/GetCSS.aspx?stylesheetname=vantagePrint"});
		
		$('link[media="screen"]').remove();
		
		//$('head').append('<link rel="stylesheet" href="/CMSPages/GetCSS.aspx?stylesheetname=blueprint" class="blueprintcss" type="text/css" />');
		
		// IE doesn't always notice the presence of new stylesheets
		//if ($.browser.msie) { $('.blueprintcss').clone().appendTo('head');}

		$(this).removeClass('viewnonprint').addClass('viewprint').text('View printable version');
		$('#content').removeClass().addClass('prepend-1 span-11 append-1');
		$('#actionbar').removeClass().addClass('clear span-17');
		$('#header').removeClass().addClass('span-24 last');
		return false;
	});

	$('#print-button').click(function() {
		window.print();
		return false;
	});
	
	$("ul#ticker").liScroll({travelocity: 0.05});

});


/*!
 * liScroll 1.0
 * Examples and documentation at: 
 * http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html
 * 2007-2010 Gian Carlo Mingati
 * Version: 1.0.2 (30-MARCH-2009)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires:
 * jQuery v1.2.x or later
 * 
 */


jQuery.fn.liScroll = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");								
				var containerWidth = $strip.parent().parent().width();	//a.k.a. 'mask' width 	
				$strip.find("li").each(function(i){
				stripWidth += jQuery(this, i).outerWidth(true); // thanks to Michael Haszprunar
				});
				$strip.width(stripWidth);			
				var totalTravel = stripWidth+containerWidth;
				var defTiming = totalTravel/settings.travelocity;	// thanks to Scott Waye		
				function scrollnews(spazio, tempo){
				$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
				}
				scrollnews(totalTravel, defTiming);				
				$strip.hover(function(){
				jQuery(this).stop();
				},
				function(){
				var offset = jQuery(this).offset();
				var residualSpace = offset.left + stripWidth;
				var residualTime = residualSpace/settings.travelocity;
				scrollnews(residualSpace, residualTime);
				});			
		});	
};
