function jsAPI(){
	this.lastNum='';
	this.lastTopic='';
	this.homePage = function(){
		newWindow = window.open('http://www.nissanusa.com','newWindow','');
		newWindow.focus();
	};
	this.termsOfUse = function(){
		newWindow = window.open('http://www.nissanusa.com/contactnissan','newWindow','');
		newWindow.focus();
	};
	this.filterChanged = function(whatFilter) {
		if (jQuery("#all").length > 0){
			jQuery("#all").hide();
		}
		page=1;
		jQuery('#tweets').html("");
		if (whatFilter == 'all topics'){
			whatFilter = '';
		}
		globalCategory = whatFilter;
		loadqadb(page,"", whatFilter);
	};
	this.carSequenceFinished = function() {
		//show some ui elements
		jQuery('#menu').fadeIn(200);
		if (window.location.href.indexOf('#/news') < 0){
			jQuery('#bottom').fadeIn(500);
			jQuery("#question_text").focus();
		} 
		jQuery.cookie('LeafAnimationSeen', 'yes');
	};
	this.liveChat = function(type) {
		try{
			crmEvent35({
				'pageType': (type == 'HTML' ? 'HTML':'Flash')
			});
		}catch(e){}
		var e = window.open('http://www.nnainfo.com/SightMaxAgentInterface/PreChatSurvey.aspx?accountID=1&siteID=1&queueID=2',
	        'chatWindow','width=490,height=460,resizable=0,scrollbars=no,menubar=no,status=no');
	};
	this.gallery = function(type) {
		centerPopup(); loadPopup();	
	};
	this.showVideo = function(video) {
		// make sure value from flash is a string
		video = '' + video;
		
		// determine what video to show
		// variables are left global so they are accessible when flash player is embedded
		if(video === 'details'){
			videoFile = '/ev/media/videos/Leaf_NOSLATE_110609small.flv';
			imageFile = '/ev/media/images/nissan-ev/video/leaf_noslate_110609small_poster.jpg';
		} else if(video === 'vision'){
			videoFile = '/ev/media/videos/main_intro.flv';
			imageFile = '/ev/media/images/nissan-ev/video/main_intro_poster.jpg';
		} else if(video === 'battery'){
			videoFile = '/ev/media/videos/batteries.flv';
			imageFile = '/ev/media/images/nissan-ev/video/batteries_poster.jpg';
		} else if(video === 'charging'){
			videoFile = '/ev/media/videos/charging.flv';
			imageFile = '/ev/media/images/nissan-ev/video/charging_poster.jpg';
		} else{
			// break out and do nothing if video is not in the list above
			return -1;
		}
		
		// create popup html and link if first call
		var videoPopup = jQuery('#video-popup');
		var videoPopupTrigger = jQuery('#video-popup-trigger');
		
		if(videoPopup.length === 0){
			videoPopupTrigger = jQuery('<a id="video-popup-trigger" href="#video-popup"></a>');
			videoPopup = jQuery('<div id="video-popup"></div>');
			
			jQuery('body').append(videoPopupTrigger);
			jQuery('body').append(videoPopup);
						
			videoPopupTrigger.fancybox({
				overlayOpacity: 0.9,
				'hideOnContentClick': false,
				callbackOnShow: function(){
					// embed the flash viedo player after showing fancybox
					jQuery('#fancy_div').append('<div id="video-popup-flash"></div>');
					var flashvars = { lang: 'en', videoFile: videoFile, imageFile: imageFile, playerWidth: '640', playerHeight: '360' };
					var params = { allowscriptaccess: 'always', menu: 'false', allowFullScreen:'true', wmode: 'transparent', scale:'noscale', salign:'tl', bgcolor:'0x000000' };
					var attributes = { id: 'popupvideo' };
					swfobject.embedSWF('/ev/media/swf/nissan/videoplayer.swf', 'video-popup-flash', '640', '360', '9.0.45', null, flashvars, params, attributes);
				}
			});
		}
		
		// triger the fancybox popup to open
		videoPopupTrigger.trigger('click');
	};
	this.tourPage = function(formObj) {
		var location = 'tour.jsp';
		
		if(formObj[0]) {
			var zipCode = formObj[0].zipcode.toLowerCase();
			
			if(zipCode != "" && zipCode != "zip code") {
				location = location + '?zipCode='+formObj[0].zipcode;
			} else {
				location = location + '?zipCode=';
			}
		}
		
		window.location = location;
	};
	this.carNavigationClicked = function(section,type){
		try{
			if (section=="details"){
				crmEvent42({'pageType':(type=='HTML'?'HTML':'Flash')});	
			}
		}catch(e){}
	};
	this.videoFinished = function(){
		var overlay = '';
		
		// tour map video tracking
		if(typeof(ev) !== 'undefined' && typeof(ev.maps) !== 'undefined'){
			try{
				var videoId = jQuery('#fancy_div').children()[0].id.substring(8);
				crmEvent28({'num':videoId});
			}catch(e){}
		}
		// everything else
		else{
			try{
				// car details video
				if(SWFAddress.getValue().indexOf('details') > -1){
					overlay = 'car';
					crmEvent44();
				}
				// vision video
				else if(SWFAddress.getValue().indexOf('vision') > -1){
					overlay = 'vision';
					crmEvent51();
				}
				// battery video
				else if(SWFAddress.getValue().indexOf('battery') > -1){
					overlay = 'battery';
					crmEvent62();
				}
				// charging video
				else if(SWFAddress.getValue().indexOf('charging') > -1){
					overlay = 'charging';
					crmEvent72();
				}
			}catch(e){}
			
			// show an overlay if set
			if(overlay !== ''){
				jQuery.ajax({
					url: 'includes/' + overlay + '-video-overlay.jsp',
					type: "GET",
					success: function(overlay) {
						jQuery("#fancy_div").append(overlay);
						
						// replay button
						jQuery('#video-popup-overlay .replay').click(function(e){
							e.preventDefault();
							jQuery('#video-popup-overlay').remove();
							thisMovie('popupvideo').startVideo();
						});
					}
				});
			}
		}
	};
	this.videoFirstStart = function(){
		// tour map video tracking
		if(typeof(ev) !== 'undefined' && typeof(ev.maps) !== 'undefined'){
			try{
				var videoId = jQuery('#fancy_div').children()[0].id.substring(8);
				crmEvent27({'num':videoId});
			}catch(e){}
		}
		// everything else
		else{
			try{
				// car details video
				if(SWFAddress.getValue().indexOf('details') > -1){
					crmEvent43();
				}
				// vision video
				else if(SWFAddress.getValue().indexOf('vision') > -1){
					crmEvent50();
				}
				// battery video
				else if(SWFAddress.getValue().indexOf('battery') > -1){
					crmEvent61();
				}
				// charging video
				else if(SWFAddress.getValue().indexOf('charging') > -1){
					crmEvent71();
				}
			}catch(e){}
		}
	};
	this.videoStart = function (){
		// tour map video tracking
		if(typeof(ev) !== 'undefined' && typeof(ev.maps) !== 'undefined'){
			try{
				var videoId = jQuery('#fancy_div').children()[0].id.substring(8);
				crmEvent27({'num':videoId});
			}catch(e){}
		}
		// everything else
		else{
			try{
				// car details video
				if(SWFAddress.getValue().indexOf('details') > -1){
					crmEvent43();
				}
				// vision video
				else if(SWFAddress.getValue().indexOf('vision') > -1){
					crmEvent50();
				}
				// battery video
				else if(SWFAddress.getValue().indexOf('battery') > -1){
					crmEvent61();
				}
				// charging video
				else if(SWFAddress.getValue().indexOf('charging') > -1){
					crmEvent71();
				}
			}catch(e){}
		}
	};
	this.sectionChanged = function(section, type){
		try{
			if (section=="vision"){
				crmEvent52({'pageType':type});
			} else if (section=="car"){
				crmEvent42({'pageType':type});
			} else if (section=="battery"){
				crmEvent60({'pageType':type});
			} else if (section == "charging"){
				crmEvent70({'pageType':type});
			}
		}catch(e){}
	};
	this.newsLoaded = function(){
		try{
			crmEvent80();
		}catch(e){}
	};
	this.newsClicked = function(num, article){
		try{
			crmEvent81({
				'num':num,
				'article': article,
				'pageType': 'HTML'
			});
		}catch(e){}
	};
	this.questionSubmitted = function(){
		try{
			crmEvent90({'pageType':'HTML'});
		}catch(e){}
	};
	this.microAnswers = function(num,topic){
		try{
			if (!(num=='1' && topic=='No_topic')){
				if (!(topic == this.lastTopic && num == this.lastNum)){	
				crmEvent95({'num':num,'topic':topic, 'pageType':'HTML'});
				this.lastTopic = topic;
				this.lastNum = num;
				}
			}
		}catch(e){}
	};
	this.communityIconClicked = function(){ 
		try{
			crmEvent30();
		}catch(e){}
	};
	this.emailFriendClicked = function(){
		try{
			crmEvent31();
		}catch(e){}
	};
	this.loadTopQuestions = function(){
		loadqadb(1,"","","",1);
		setFilter("top questions");
	};
	this.featuredAnswer = function(num, q){
		try{
			crmEvent96({'num':num,'question':q.replace(/ /g,"_")});
		}catch(e){}
	};
};
document.jsAPI = new jsAPI();
