$(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('idList'); //Read in cookies 
		var idListCookie2 = '';

		if (idListCookie) {
			var idListCookie2 = idListCookie.replace(/\"/g, ''); //Take out quotes	
			var 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/, 'reccomender/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('idList', 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>' + name;
				html += '</div>';
				
				$('.top5CompareBtn').before(html);
				
				//Re-initialize custom checkboxes
				Custom.init();
			}
		}
	});
	
	$('#offerHelp').click(function(){
		citi_pop();	
	});	
	
	function citi_pop(URL,facename,w,h,scroll,t,l,s,m,r,d){
		var winl = (screen.width-620)/2;
		var wint = (screen.height-620)/2;
	
		settings='height=620, width=620, scrollbars=yes';
		
		win = window.open('../../homepage/english/pop_access.jsp', 'pop_01' ,settings);
	
		win.window.focus();
	}

	// 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;
				}
			}
		}
	}
		
});

