
/*
jQuery(document).ready(function() {
    if(jQuery("#movFlashCore").size()>0){
        jQuery(".gBodyContainer").css("background-image","none");
    }
});
*/

jQuery(document).ready(function() {jQuery("#newsEventsWidget").css("display","block");});
jQuery(document).ready(initWidget);


function initWidget(){
	initSelectors();
	initLinks();
}

function initSelectors(){
	
	jQuery('#newsVehicleFilter li a').bind('click', showModel);
	jQuery('#newsYearFilter li a').bind('click', showYear);
	
	jQuery('#emptyNewsItems').hide();
}

function hideAllNewsItems(){
	jQuery('.article').each(function (e){
		jQuery(this).hide()
	})
	
	jQuery('#emptyNewsItems').hide();
}

function showAllNewsItems(){
	jQuery('.article').each(function (e){
		jQuery(this).show()
	})
}

function showSelectedNewsItems(year, modelName){
	if((year == "all")&& (modelName == "all")){
		showAllNewsItems();
		checkEmptyNewsItems('.article');
	}else if(modelName == "all"){
		hideAllNewsItems();
		// show only what we want
		jQuery('.'+year).each(function (e){
			jQuery(this).show();
		})
		checkEmptyNewsItems('.'+year);
	}else if(year == "all"){
		hideAllNewsItems();
		// show only what we want
		jQuery('.'+modelName).each(function (e){
			jQuery(this).show();
		})
		checkEmptyNewsItems('.'+modelName);
	}else{
		hideAllNewsItems();
		// show only what we want
		jQuery('.'+modelName+'.'+year).each(function (e){
			jQuery(this).show();
		})
		checkEmptyNewsItems('.'+modelName+'.'+year);
	}
}

function checkEmptyNewsItems(selectors){
	jQuery('#emptyNewsItems').hide();
	if(jQuery(selectors).length < 1){
		jQuery('#emptyNewsItems').show();
	}
}

function showModel(e){ 
	var modelName = e.target.innerHTML.toLowerCase().replace(/ /g, '-');
	var year = jQuery('#newsYearFilter .active').get(0).innerHTML.toLowerCase();
	
	jQuery('#newsVehicleFilter .active').removeClass('active'); // reset old
	jQuery(this).addClass('active'); // assign new
	
	showSelectedNewsItems(year, modelName);
}

function showYear(e){
	var year = e.target.innerHTML.toLowerCase();
	var modelName = jQuery('#newsVehicleFilter .active').get(0).innerHTML.toLowerCase().replace(/ /g, '-');
		
	jQuery('#newsYearFilter .active').removeClass('active'); // reset old
	jQuery(this).addClass('active'); // assign new
	
	showSelectedNewsItems(year, modelName);
}

// because to filter, we need to know what the active params are
function initLinks(){
	jQuery('.widgetLink').each(function (){
		jQuery(this).bind('click', getWidgetLink);
	})
	
	jQuery('.listLink').each(function (){
		jQuery(this).bind('click', getListLink)
	})
}

function getWidgetLink(e){ // no params
	var vehicle = jQuery('#newsVehicleFilter .active').get(0).innerHTML.toLowerCase().replace(/ /g, '-');
	var year = jQuery('#newsYearFilter .active').get(0).innerHTML.toLowerCase();
	var newsID = jQuery(this).parents('.article').get(0).id;
	var link = document.location.href.replace("#","") + '?articleID='+newsID+'&v='+vehicle+'&y='+year;
	//alert(link);
	document.location.href = link;
}

function getListLink(e){ // the params are already filled
	var origHref = document.location.href;
	var baseHref = origHref.substring(0, origHref.indexOf("?"));
	var vehicle = origHref.substring(origHref.indexOf("v=")+2, origHref.lastIndexOf("&y"))
	var year = origHref.substring(origHref.indexOf("y=")+2, origHref.length);
	var newsID = jQuery(this).parents('li').get(0).id;
	link = baseHref + '?articleID='+newsID+'&v='+vehicle+'&y='+year;
	//alert(link);
	document.location.href = link;
}

// used for when we click on an article from the list or the widget
function isolateArticle(articleID){
	jQuery('.article').each(function (e){
		jQuery(this).hide()
	})
	
	var arTitle = jQuery("#"+articleID+" .newsTitle").text();

	crmEvent2(arTitle);
	
	jQuery('#'+articleID).show();
	jQuery('li.'+articleID).addClass('active');
}

// Taken from old - updated for release 258 05/19/2010 
function emailNewsArticle(){
	 var emailTemplate="mailto:?Subject=An Interesting Article Recommended for You at InfinitiUSA.com&body=Hello. I thought you\'d be interested in reading this article at InfinitiUSA.com: %0D%0A%0D%0A{0} %0D%0Ahttp://www.infinitiUSA.com/about/news-events.html?articleID={1}%26v=all%26y=all%26Site=TAF%26Area=About_Infiniti%26Creative={2} %0D%0A%0D%0AIf the above link does not work, copy and paste it into your browser.";
	 var theHeadline=jQuery(".newsTitle").get(0).innerHTML;
	 var theId = jQuery('.article').get(0).id;
	 
	 jQuery('.article').each(function(){
		if(this.style.display=="block"){
			theHeadline = jQuery(this).children('.articleContent').children('.newsTitle').eq(0).text();
			theId = this.id;
		}
	 })
	
		theHeadline = theHeadline.replace(/\"/gi, '');
		
	 document.location = emailTemplate.format(theHeadline,theId,"News");
	 //currentArticle.emailTaggingFunction(currentArticle.headline);//alert4(currentArticle.emailTaggingFunction)
}



