
//set sharing prefs
var	addthis_config = {
	username: 'gecitizenship',
	ui_cobrand: 'GE Citizenship',
	services_compact : 'favorites, digg, twitter, google, facebook, delicious, myspace, linkedin, messenger',
	data_track_clickback: true
};

(function($) {
	
	/* CONSTANTS */
	var FLASH_VERSION_SUPPORTED = '9';

	/*
	* GLOBAL ENHANCEMENTS
	*/
	$(function() {

		Cufon.replace('h2.page-title span.title, p.aside-pullquote-attribution, p.standard-pullquote-attribution, p.hanging-image-pullquote-attribution, h3.stories-landing-header, p.stories-precis, p.metrics-intro, h4.citizenship-report-primary-heading, p.citizenship-report-subtitle', {fontFamily: 'GE Inspira Book', forceHitArea: true});
		Cufon.replace('p.intro, .article h3.article-subhead, div.video-player h4, blockquote, p.factoid-copy', {fontFamily: 'GE Inspira Book', forceHitArea: true});

		//cufon fixes for IE
		var isCufonReady = function() {
			return $html.hasClass('cufon-ready');
		}
		var doCufonFix = function() {
			if(isCufonReady()) {
				$('div.stories-large-inline-image-container').css('float', 'none').css('overflow', 'hidden');	
				$('div.stories-large-inline-image-adjuster').css('zoom', '1');					
				$('div.hanging-image-pullquote-contents blockquote').css('height', 'auto');					
				$('div.hanging-image-pullquote-contents').css('zoom', '1');	
				$('div.standard-pullquote blockquote').css('height', 'auto');					
				$('div.standard-pullquote').css('zoom', '1');					
				$('div.aside-pullquote blockquote').css('height', 'auto');					
				$('h4.primary-header').css('zoom', '1');
				$('div.insite-related-teaser').css('zoom', '1');
				$('div.pullquote-wrap').css('zoom', '1');
				return;	
				$('div.standard-pullquote blockquote').css('zoom', '1');					

			} 
			setTimeout(doCufonFix,1);
			setTimeout(function() { $('div.standard-pullquote').css('zoom', '1'); }, 2000);
		}
		doCufonFix();
		
		if(CitiUtil.isIE6()) {
			var cufs = [ 'h2.page-title span.title', 'p.aside-pullquote-attribution', 'p.standard-pullquote-attribution', 'p.hanging-image-pullquote-attribution', 'h3.stories-landing-header', 'p.stories-precis', 'p.metrics-intro', 'h4.citizenship-report-primary-heading', 'p.citizenship-report-subtitle', 'p.intro', '.article h3.article-subhead', 'div.video-player h4', 'blockquote', 'p.factoid-copy']
			for (var i=0; i<cufs.length; i++) {
				var $el = $(cufs[i]);
				if($el.length) {
					var h = $el.height();
					$el.css('height',h);
				}
			};
		}	


    $(function() {
		if ($('body').hasClass('story-article-short')){
			var controlslist = $('div.controls').html();
			$('div.head-content-alt div.controls').hide();     
			$('<div class="controls">' + controlslist + '<a href="#" class="print-link">Print</a></div>').appendTo('div.head-content-alt');
			$('div.head-content-alt div.controls').css('zoom', '1').css('visibility', 'visible');
        } else {
			var controls = $('div.controls').html();     
			$('<div class="controls">' + controls + '<a href="#" class="print-link">Print</a></div>').appendTo('div#main-content');
			$('div#main-content div.controls').css('zoom', '1').css('visibility', 'visible').css('top', '410px').css('right', '0').css('height', '100px');
			$('div.head-content div.controls').css('display', 'none');
        }

		if(CitiUtil.isIE6()) {
			$('div#main-content div.controls').css('right', '95px');
			$('div.head-content div.controls').css('right', '0px');
			$('div.head-content-alt div.controls').css('right', '0px');
		}	

       	if ($('body').hasClass('tag, error404, archive')){
       		$('div.controls').hide();
       	}
		
    });
		
		/* activate print link */
		$('a.print-link').click(function() {
			window.print();
		});
		
		/* open external urls in new window */
		$('a.external, a.external-link, a.download-transcript, a.download').attr('target', '_blank');
    
		/* adds label text as value of text input  */
		$('input.autolabel').each(function() {
			var $this = $(this);
			$this
				.data('label',$('label[for="' + $this.attr('id') + '"]').html())
				.val($this.data('label'))
				.focus(function() {
					if ($this.val() === $this.data('label')) {
						$this.val('');
					}
				})
				.blur(function() {
					if ($this.val() === '') {
						$this.val($this.data('label'));
					}
				});
		});
		  
		/* redirect site search */
		$('#site-search').submit(function() {
			var action = (CitiUtil.isBlog() || CitiUtil.isSecure()) ? 'http://www.gecitizenship.com/search.html' : CitiUtil.getBasePath() + 'search.html',
				$form = $(this),
				query = $form.serialize();
				window.location.href = action + '?' + query;
			return false;
		});

		/* enable navigation for ie6 */
		if (CitiUtil.isIE6()) {
			$('#site-navigation ul > li').hover(
				function() {
					$(this).addClass('hover_');
					$('li.hover_ div').css('display', 'block');				
				},
				function() {
					$('li.hover_ div').css('display', 'none');								
					$(this).removeClass('hover_');
				}
			);		
		}



/*
		//make stories abstract a link on listing pages
		$('.featured-secondary-content').each(function() {
			var $this = $(this)
				$this.wrapInner($this.prev().find('a').clone(true).empty());
		});
*/


	//end global enhancements
	});		
	
	
	/*
	* CUSTOM ONLOAD EVENTS
	*/
	$(function() {

		/* set up any slide shows that are on this page */
		if ($(".slideshow").length) {
			$(".slideshow").slideshow();
		}

		$('ul.sections-secondary').hide();
		$("a.show").click(function() {
		    $(this).prev("ul.sections-secondary").slideToggle(600);
		    $(this).toggleClass("fewer");
		    $(this).text($(this).text() == 'Show More' ? 'Show Fewer' : 'Show More');
		    return false;
		});

		/* hide accordions */
		$('div.subsection-content').hide();
		//check for hash (keep it open)
		if(window.location.hash && window.location.hash !== '#') {
			$(window.location.hash).find('div.subsection-content').show().prev().addClass('active');
		}
		$("h4.subsection-head").click(function() {
		    $(this).next("div.subsection-content").slideToggle(400);
		    $(this).toggleClass("active");
		    return false;
		});

/*
		$('ul#sorted-results .story-listing-container').hover(
			function() {
				$(this).parents('.story-listing').addClass('story-hover_');
			},
			function() {
				$(this).parents('.story-listing').removeClass('story-hover_');
			}
		);	

*/
		$('div.filter-navigation li').hover(
			function() {
				$(this).addClass('hover_');
			},
			function() {
				$(this).removeClass('hover_');
			}
		);			

		//load media.js if video and audio elements exist
/*
		if($('a.play-audio, a.view-video').length) {
			var _js = CitiUtil.getFilesPath() + 'scripts/media.js';
			CitiUtil.loadJS(_js)
		}
*/
				
	//end custom onload events
	});
	

	/* UTIL VARS */
	var _isIE = !$.support.htmlSerialize,
		_userAgent = navigator.userAgent,
		_browserVersion = parseInt($.browser.version, 10),
		_filesPath = null,
		$html = $('html'),
		CitiUtil = {
			isSecure: function() { 
				return window.location.protocol === 'https:'; 
			},
			isBlog: function() { 
				return window.location.hostname.indexOf('geblogs') !== -1; 
			},
			isIE: function() { 
				return _isIE; 
			},
			isIE6: function() { 
				return this.isIE() && _browserVersion === 6; 
			},
			hasFlash: function() { 
				return typeof swfobject !== 'undefined' && swfobject.hasFlashPlayerVersion(FLASH_VERSION_SUPPORTED);
			},
			isIPhone: function() {
				return _userAgent.match(/iPhone/i) || _userAgent.match(/iPod/i) ? true : false
			},
			isIPad: function() {
				return _userAgent.match(/iPad/i) ? true : false
			},
			isAndroid: function(){
				return _userAgent.match(/Android/i) ? true : false
			},
			isMobile: function(){
				return this.isAndroid() || this.isIPhone() || this.isIPad();
			},
			getFilesPath: function(){
				//determine path based on link path
				if(_filesPath === null) {
					var linkPath = $('link[media*=screen]').eq(0).attr('href');
					_filesPath = linkPath.substring(0,linkPath.indexOf('style'));
				}
				return _filesPath;
			},
			getBasePath: function(){
				return this.isBlog() ? $('link[rel=index]').eq(0).attr('href') + '/' : this.getFilesPath().replace('_files/','');
			},
			getQueryParam: function(parameter, query){
				//uses window.location.search if !query
				var p = escape(unescape(parameter)),
					regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i"),
					match = regex.exec(query ? query : window.location.search);
				return match !== null ? match[1] : null;
			},			
			loadJS: function () {
				for (var i = 0; i < arguments.length; i++){
					$('<script />').attr({ 'type': 'text/javascript', 'src': arguments[i] }).appendTo('head');
				}
			},
			loadCSS: function () {
				for (var i = 0; i < arguments.length; i++){
					$('<link />').attr({ 'type': 'text/css', 'rel': 'stylesheet', 'href': arguments[i] }).appendTo('head');
				}
			},
			embedFlash: function(containerId, swf, width, height, flashvars, params, attributes) {
				if(!this.hasFlash()) { return; }
				else {
					var fvars = flashvars || {},
						fparams = params || {},
						fattrs = attributes || {};
					fparams.wmode = 'transparent';
					var flashID = 'flash-' + Math.floor(Math.random()*100000),
						flashContainer = $('<div class="flash_"></div>').append('<div id="' + flashID + '" />');
					$("#" + containerId)
						.addClass('containsFlash_')
						.wrapInner('<div class="noflash_"></div>')
						.prepend(flashContainer);
					swfobject.embedSWF(swf, flashID, width, height, FLASH_VERSION_SUPPORTED, null, fvars, fparams, fattrs);
				}
			},
			pngfix: function(selectors) {
				/* call this outside of $(document).ready() */
				if(typeof DD_belatedPNG == "undefined") {
					return;
				}
				else {
					$(window).load(function() {
						DD_belatedPNG.fix(selectors);
					});
				}
			}
			
	/* end Util	*/
	};
	/* expose globally */
	window.CitiUtil = CitiUtil;
	
	/* add JS and FLASH flags */
	$html.addClass('js-enabled');
	if(CitiUtil.hasFlash()) {
		$html.addClass('flash-enabled');
	}
	
	/* IE helpers */
	if(CitiUtil.isIE6()) {
		document.createElement("abbr");	
	    /* prevents background-image flicker in IE 6
			(http://support.microsoft.com/?scid=kb;en-us;823727&spid=2073&sid=global) */
		try { document.execCommand("BackgroundImageCache",false,true); } 
		catch(e) { }	    
	}	


})(jQuery);
