$(document).ready(function(){

	//Clear search field on focus; re-populate on blur and if empty
	$('.searchField').focus(function(){
		if ($(this).val() == 'Rechercher') {
			$(this).val('');
		}
		
		$(this).blur(function(){
			if ($(this).val() == '') {
				$(this).val('Rechercher');
			}
		});
	});

	//Auto open sidenav dropdown
	var path_name = location.pathname;
	var base_name = path_name.lastIndexOf('/')+1;
	var this_page = path_name.substring(base_name);

	if (this_page) {
		var $this = $('#' + this_page.replace(/(\.jsp)/, ''));
				
		if ($this.hasClass('levelOne')) {
			$this.next().show().parent().addClass('open');
		}
		else if ($this.hasClass('levelTwo')) {
			$this.css('text-decoration', 'underline').addClass('currentCard').parents('ul').show().parent().addClass('open');
			if ($this.children('.levelThree')) {
				$this.next().show();
			}
		}
		else if ($this.hasClass('levelThree')) {
			$this.css('text-decoration', 'underline').parents('ul').show().parents('li:eq(2)').addClass('open');
		}
	}
	
	//Animate sitemap container
	$('#revealSitemap').click(function(){
		$('#sitemapContent').slideToggle(500);
		if ($(this).hasClass('open')) {
			$(this).removeClass('open');
		}
		else {
			$(this).addClass('open');
		}
	});

	//Remove card from DOM if deselected in compare box
	/*$('.top5CheckBox input').each(function(){
		if ($(this).attr('checked') == false) {
			$(this).parents('.top5CompareBoxes').remove();
		}
	});*/
		
	//Add to compare box
	$('.compareBtn').click(function(){	
		
		//Function searches to see if element exists in array
		Array.prototype.exists = function(o) {
			for (var i = 0; i < this.length; i++) {
				if (this[i] === o) {
					return true;
				}
			}
			return false;
		};
		
		var idListCookie = $.cookie('idListfr'); //Read in cookies 
		var idListCookie2 = '';
		var id = new Array();

		if (idListCookie) {
			idListCookie2 = idListCookie.replace(/\"/g, ''); //Take out quotes	
			id = idListCookie2.split('==='); //Parse cookie to get card id
		}
		
		var thisId = $(this).attr('id'); //Get selected card id
				
		//If selected card id not in cookie
		if (idListCookie === null || !(id.exists(thisId))) {
			var html = '';
			var name = $('.currentCard').text();
			var imgPath = $(this).parents('#cardContainer').find('.cardImage').attr('src');
			var img = imgPath.replace(/template_c\/cardimages/, 'recommender/cards');
						
			//If less than 5 items in comparison box, add to box
			if ($('.top5CompareBoxes').size() < 5) {
			
				idListCookie = idListCookie2 + thisId + '==='; //Add card id to cookie array
				$.cookie('idListfr', '"' + idListCookie + '"'); //Set new cookie
				
				html += '<div class="top5CompareBoxes">';
				html += '<div class="top5CheckBox"><input type="checkbox" class="styled" value="' + thisId + '" name="cat" checked="checked"/></div>';
				html += '<div class="top5CardImage"><img src="' + img + '"/></div>' + '<div class="cName">' + name + '</div>';
				html += '</div>';
				
				$('.top5CompareBtn').before(html);
				
				//Re-initialize custom checkboxes
				Custom.init();
			}
			else {
				alert('You have already selected 5 cards to compare.  Please remove a card if you would like to add another card.');
			}
		}
		else if (id.exists(thisId)) {
			alert('You have already added this card.');
		}
	});

	// Switch language using JS, only needed for card recommender step 2 and 3
	var getURL = window.location.toString();
	var switchURL = getURL.replace('francais', 'english');

	$('#langSwitch').attr('href', switchURL);
		
	//Intialize side nav dropdowns
	//$('#sideNav a.levelOne').sideNavDD();
	
	// IE fix to be able to use indexOf for arrays
	if (!Array.indexOf) {
		Array.prototype.indexOf = function (obj, start) {
			for (var i = (start || 0); i < this.length; i++) {
				if (this[i] == obj) {
					return i;
				}
			}
		}
	}	
		
});

function citi_pop(URL,facename,w,h,scroll,t,l,s,m,r,d){
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	
	settings='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',toolbar='+t+',location='+l+',status='+l+',menubar='+l+',resizable='+l+',dependent='+l;
	
	win = window.open(URL,facename,settings);
		
	if(parseInt(navigator.appVersion) >= 4){
		win.window.focus();
	} 
}