/*	Initialization Scripts for GE Capital website
		Use with jQuery 1.2.3 (http://www.jquery.com) and Cufon-YUI (http://cufon.shoqolate.com).
		Author: Kyle Crouse (kyle.crouse@frogdesign.com)
*/


//	Inspira text replacement
Cufon.replace('#nav-primary > li > a',{fontFamily:'GE Inspira Medium SC',hover:true});
Cufon.replace('h2',{fontFamily:'GE Inspira Medium'});
Cufon.replace('.introduction p',{fontFamily:'GE Inspira'});
Cufon.replace('.button',{fontFamily:'GE Inspira Medium SC'});


//	Dropdown Navigation Component
var GEC_NavTree = [
	{	id: 'nav-home',
		name: 'Home',
		url: 'index.html',
		children: null
	},
	{	id: 'nav-products-services',
		name: 'Products Services',
		url: 'products-services/index.html',
		children: [
			{ name: '<b>Commercial Lending &amp; Leasing</b>', url: '#' },
			{ name: 'Equipment Finance', url: '../products-services/equipment-finance.html' },
			{ name: 'Healthcare Financial Services', url: '../products-services/healthcare-financial-services.html' },
			{ name: 'Corporate Aircraft Services', url: '../products-services/corporate-aircraft-services.html' },
			{ name: 'Asset-based Lending', url: '../products-services/abl.html' },
			{ name: '<b>Consumer Finance</b>', url: '#' },
			{ name: 'Credit Card Management Services', url: '../products-services/credit-card-management-services.html' },
			{ name: '<b>Equity</b>', url: '../products-services/equity.html' },
			{ name: '<b>Non-performing Loan</b>', url: '../products-services/npl.html' },
			{ name: '<b>Commercial Distribution Finance</b>', url: '../products-services/cdf.html' }
		]
	},	
	{	id: 'nav-careers',
		name: 'Careers',
		url: 'http://www.ge.com/careers/index.html',
		children: null
	},
	{	id: 'nav-news',
		name: 'News',
		url: 'http://www.gereports.com/',
		children:null 
		//[
			//{ name: 'News &amp; Media', url: '../news/news-and-media.html' },
			//{ name: 'GE Reports', url: '../news/ge-reports.html' }
		//]
	},
	{	id: 'nav-faqs',
		name: 'FAQs',
		url: '../faqs/faqs.html',
		children: null
	},
	{	id: 'nav-contact',
		name: 'Contact Us',
		url: '../contact/contact-us.html',
		children: null
	},
	{	id: 'nav-worldwide',
		name: 'Worldwide Locations',
		url: 'http://www.gecapital.com/worldwide/index.html',
		children: null
	}
];

var GEC_Nav = function(item) {
	this.name = item.name;
	this.url = item.url;
	this.children = item.children;
	this.el = $('#'+item.id);
	this.subNav = null;
	this.init();
};
GEC_Nav.prototype.init = function() {

	//insert current section in to secondary nav tree
	if (this.el.hasClass('current')) {
		if ($('#nav-secondary').length>0) $('#nav-secondary').prepend('<li'+((document.location.href.indexOf(this.url)>0)?' class="current"':'')+'><a href="'+this.url+'">'+this.name+'</a></li>');
		else this.el.append('<ul id="nav-secondary"><li class="current"><a href="'+this.url+'">'+this.name+'</a></li></ul>');
	}
	
	if (this.children) {

		//create dropdown sub-navigation items
		this.subNav = $('<ul class="nav-dropdown"></ul>');
		var self = this;
		$.each(this.children,function(){
			self.subNav.append('<li><a href="'+this.url+'">'+this.name+'</a></li>');
		});
		
		//attach event listeners for opening/closing dropdowns
		this.el
			.bind('mouseleave blur',{self:this},this.hide)
			.find('> a')
			.bind('mouseenter focus',{self:this},this.show);
	}
};
GEC_Nav.prototype.show = function(e) {
	$(e.data.self.el)
		.append(e.data.self.subNav)
		.find('> a')
		.addClass('hovered');
};
GEC_Nav.prototype.hide = function(e) {
	$(e.data.self.el).find('> a').removeClass('hovered');
	$(e.data.self.subNav).remove();
	Cufon.refresh('#' + e.data.self.el.id + ' > a');
};


//	Newsfeeds
var GEC_Newsfeed = function(feed,limit,el) {
	if ( swfobject.getFlashPlayerVersion().major < 6 ) return;
	GEC_Newsfeed.el = el;
	GEC_Newsfeed.feed = feed;
	GEC_Newsfeed.items = [];
	GEC_Newsfeed.limit = limit;
	GEC_Newsfeed.page = 1;
	GEC_Newsfeed.pageLength = 20;
	GEC_Newsfeed.pages = null;
	GEC_Newsfeed.init();
};
GEC_Newsfeed.init = function() {
	$('h3',GEC_Newsfeed.el).after('<dl><img alt="loading progress spinner" id="spinner" src="assets/styles/images/loading.gif" style="border: 0; display: block; margin: 0 auto; padding: 20px 0;"/></dl><div id="rss_tool"></div>');
	GEC_Newsfeed.writeSWF();
};
GEC_Newsfeed.writeSWF = function() {
	var flashvars = {feedUrl:GEC_Newsfeed.feed};
	var params = {wmode:'transparent'};
	var attributes = {style:'left: 0px; position: absolute;'};
	swfobject.embedSWF("assets/scripts/rss_tool.swf", "rss_tool", "1", "1", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
};
GEC_Newsfeed.process = function(data) {
	var end = (GEC_Newsfeed.limit!=null)?Math.min(data.length,GEC_Newsfeed.limit):data.length;
	for (var i = 0; i < end; i++) {
		var item = data[i];
		var title = item.title.value;
		var date = item.date.value;
		date = date.substring(0,date.length-12);
		var link = item.link.value;
		var html = '<dt>'+date+'</dt><dd><a href="'+link+'" title="'+title+'">'+title+'</a></dd>';
		GEC_Newsfeed.items.push(html);
	}
	$('#spinner').remove();
	if (GEC_Newsfeed.items.length > GEC_Newsfeed.pageLength) GEC_Newsfeed.createPagination();
	GEC_Newsfeed.showPage(1);
};
GEC_Newsfeed.showPage = function(p) {
	var dl = $('dl',GEC_Newsfeed.el).html('');
	for (var i = (p*GEC_Newsfeed.pageLength)-GEC_Newsfeed.pageLength; i < p*GEC_Newsfeed.pageLength && i < GEC_Newsfeed.items.length; i++) {
		$(dl)
			.append(GEC_Newsfeed.items[i])
			.find('dd:last-child a').click(function(){ window.open(this.href,'press_release',''); return false; });
	}
	GEC_Newsfeed.page = p; 
	if (GEC_Newsfeed.items.length > GEC_Newsfeed.pageLength) { $('h3 a',GEC_Newsfeed.el).text('Press Releases '+((p*GEC_Newsfeed.pageLength)-(GEC_Newsfeed.pageLength-1))+'–'+(i)); GEC_Newsfeed.updatePagination(); }
};
GEC_Newsfeed.createPagination = function() {
	GEC_Newsfeed.pages = (GEC_Newsfeed.items.length%GEC_Newsfeed.pageLength==0)?GEC_Newsfeed.items.length/GEC_Newsfeed.pageLength:GEC_Newsfeed.items.length/GEC_Newsfeed.pageLength+1;
	$('dl',GEC_Newsfeed.el).after('<p id="pagination"></p>');
};
GEC_Newsfeed.updatePagination = function() {
	var start = Math.max(1,GEC_Newsfeed.page-4), end = start + 8, self = this;
	$('#pagination').html('');
	if (start > 1) { $('#pagination').append( $('<a href="#" style="margin-right: 5px;">&#171;</a>').click(function(){ self.showPage(self.page-1); return false; }) ); }
	for (var p = start; p <= end && p <= GEC_Newsfeed.pages; p++) { $('#pagination').append( (p==GEC_Newsfeed.page)? '<strong style="margin-right: 5px;">'+p+'</strong>' : $('<a href="#" style="margin-right: 5px;">'+p+'</a>').click(function(){ self.showPage(parseInt(this.innerHTML)); return false; }) ); }
	if (GEC_Newsfeed.pages > p) { $('#pagination').append( $('<a href="#" style="margin-right: 5px;">&#187;</a>').click(function(){ self.showPage(p+1); return false; }) ); }
};
var getRSSData = function(data){GEC_Newsfeed.process(data);};


//	Account Settings for AddThis
var addthis_pub = "GECapital";

//	Document initialization functions
var docInitFn = function(){
	//external links
	$('a[href^=http:]').click(function(){ window.open(this.href,'window',''); return false; });

	//navigation
	$.each(GEC_NavTree,function(){ new GEC_Nav(this); });
	
	//directory blinds
	$('.directory h3').each(function(){
		$(this)
			.wrapInner('<a href="javascript:void(0);"></a>')
			.bind('click focus blur',function(e){
				$(this).toggleClass('closed').siblings('.section').toggle();
				return false;
			})
			.toggleClass('closed')
			.siblings('.section').toggle();
	});

	//directory blinds deep linking by index #
	var targetRegExp = /#(.+)$/;
	var targets = targetRegExp.exec(window.location);
	if((targets && targets[1]) && (!isNaN(targets[1]))){
		var index = targets[1];
		$($('.directory h3')[index]).toggleClass('closed').siblings('.section').toggle();
	}

	//print links
	$('a.asset.print').click(function(){ window.print(); return false; });
	
	//share links
	$('a.asset.share')
		.bind('mouseenter focus',function(){return addthis_open(this, '', '[URL]', '[TITLE]');})
		.bind('mouseleave blur',function(){addthis_close();})
		.click(function(){return addthis_sendto();});
	
	//to prevent image flicker in IE 6
	if (/MSIE/i.test(navigator.userAgent)) { try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {} }
};
//bind docInitFn to domReady unless IE, where it is called at end of each doc using conditional comments
if (/MSIE/i.test(navigator.userAgent)==false) $(document).ready(docInitFn);