// GLOBAL VARIABLE 
var ecobox;
var defaultSearchBox = 'mot-recherche...';
var numberSpecialBoxes = 3;

/*
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
 * @desc Create a cookie with all available options.
 */
jQuery.noConflict();
(function($) { 
	$(function() {
    	$(document).ready(function(){
			//init the app
			ecobox = new ecobox();
			
			//get the last session preferences in order to retablish the box
			if($.cookie('hiddenBoxes') != null){
				ecobox.hiddenBoxes = $.cookie('hiddenBoxes');
			}
			//add the event listener to each box
			for(var index = 1; index <= numberSpecialBoxes; index++){
				//true when the slider was up the last visit
				if(ecobox.hiddenBoxes.search('contentBoxText'+index) != -1){
					$("#contentBoxText"+index).slideUp("slow");
					var sliderImg = $('#contentBoxSlider'+index);
					$(sliderImg).bind('click',ecobox.slideDown);
					
					//change the image source (icon collapse)
					var newSrc = $(sliderImg).attr('src').replace('hideBox', 'dropdown');
					$(sliderImg).attr('src',newSrc);
				}
				else{
					$('#contentBoxSlider'+index).bind('click',ecobox.slideUp);
				}
			}
			$('#contentBoxSlider4').bind('click',ecobox.slideDown);
			ecobox.attachEventContentBoxes();
			//add misc events
			$('#ecobox_searchTxt').focus(ecobox.clearSearchBox).blur(ecobox.fillSearchBox).keypress(ecobox.searchTxt);
			$('#ecobox_synopsis,#ecobox_keyword,#ecobox_author').change(ecobox.search);
			$('#ecobox_searchTypeBySynopsis').bind('click',{section:'synopsis'},ecobox.showSection);
			$('#ecobox_searchTypeByArticles').bind('click',{section:'articles'},ecobox.showSection);
			$('#ecobox_searchIcon').click(ecobox.searchTxtIcon)
			ecobox.makeNavigable();
		});
		
		
		function ecobox(){
			this.hiddenBoxes = '';
			this.url = 'index.php?type='+boxAjaxType;
			
			/* attach the event to all content boxes (except the special on in the left column) */
			this.attachEventContentBoxes = function(){
				for(var index = numberSpecialBoxes + 2; $("#contentBoxText"+index).length != 0; index++){
					$('#contentBoxSlider'+index).bind('click',ecobox.slideUp);
				}
			}
			
			/* handle the event when the user click on the icon search Text */
			this.searchTxtIcon = function(){
				if($('#ecobox_searchTxt').val() != '' && $('#ecobox_searchTxt').val() != defaultSearchBox){
					if($('#ecobox_searchIcon').attr('src').search('search.png') != -1){
						$('#ecobox_searchIcon').attr('src',ecoboxPathRessources + 'img/search-cancel.png');
					}
					else{
						$('#ecobox_searchIcon').attr('src',ecoboxPathRessources + 'img/search.png');
						$('#ecobox_searchTxt').val(defaultSearchBox);
					}
					ecobox.search();
				}
			}
			
			/* handle the event when the user is typing a search and pressing "enter" */
			this.searchTxt = function(event){
				if(event.keyCode == 13){
					if($('#ecobox_searchTxt').val() != ''){
						$('#ecobox_searchIcon').attr('src',ecoboxPathRessources + 'img/search-cancel.png');
					}
					else{
						$('#ecobox_searchIcon').attr('src',ecoboxPathRessources + 'img/search.png');
						$('#ecobox_searchTxt').val(defaultSearchBox);
					}
					ecobox.search();
					return false;	
				}
			}
			//show synopsis or articles (drop down menu : one contains the list of the issue, the other the list of the keywords + authors)
			this.showSection = function(event){
				switch(event.data.section){
					case 'synopsis' :
						$('#ecobox_synopsisSection').removeClass('hidden');
						$('#ecobox_articlesSection').addClass('hidden');
						break;
					case 'articles':
						$('#ecobox_synopsisSection').addClass('hidden');
						$('#ecobox_articlesSection').removeClass('hidden');
						break;
				}
				ecobox.search();
			}
			
			//send the form
			this.search = function(){
				var searchType = '&ecobox%5BsearchType%5D='+$('#contentBoxText1 [@name=ecobox_searchType][@checked]').val();
				if($('#ecobox_searchTxt').val() == defaultSearchBox){
					var request = $('#searchForm select').serialize();
				}
				else{
					var request = $('#searchForm input[@type=text], #searchForm select').serialize()
				}
				$.get(ecobox.url,request+searchType,ecobox.callBackSearch);
				
				$('#contentColumns').addClass('hidden');
				$('#ecobox_loading').removeClass('hidden');
				return false;
			}
			
			this.showLoading = function(){
				$('#ecobox_loading').removeClass('hidden');
			}
			
			this.callBackSearch = function(data){
				$('#ecobox_loading').addClass('hidden');
				var cols = data.split('<!-- section-delimiter -->');
				
				$('#contentColumns').empty().append(cols[0]);
				if(typeof(cols[1]) == 'string'){
					$('#ecobox_synopsisSummary').empty().append(cols[1]);	
				}
				
				//attach some events
				ecobox.makeNavigable();
				ecobox.attachEventContentBoxes();
				
				//reshow the 2 colomns
				$('#contentColumns').removeClass('hidden').css({display: 'block'}); // added .css({display: 'block'}) to fix a display bug 13.10.10
				$('#ecobox_loading').addClass('hidden');
			}
			
			this.makeNavigable = function(){
				//make the resultbrowser intelligent with javascript
				$('#ecobox_browser a.ecobox_offset').click(ecobox.navigateTo);
				$('#ecobox_browser a.ecobox_previousJump,#ecobox_browser a.ecobox_nextJump').click(ecobox.jumpNavigate)
				$('#ecobox_browser select').change(ecobox.navigateTo);				
			}
			
			/* handle a jump 1 - 2 - 3 > */
			this.jumpNavigate = function(){
				if(this.className == 'ecobox_nextJump'){
					var positionLast = $('#ecobox_browser a').length - 2;
					var offset = $('#ecobox_browser a:eq('+positionLast+')').html() - 0 + resultsPerView;
					if(offset > maxOffset){
						offset = maxOffset;
					}
				}
				else{
					var offset = $('#ecobox_browser a:eq(1)').html() - resultsPerView;
					if(offset <= 0){
						offset = 1;
					}
				}
				ecobox.navigateTo(offset-0);
				return false;
			}
			// navigate to the right page
			this.navigateTo = function(offset){
				if(typeof(offset) != 'number'){
					if($(this)[0].nodeName == 'A'){
						var offset = $(this).html();
					}
					else{
						var offset = 1;
					}
				}
				var numBoxes = $('#ecobox_browser select').val();
				$.get(ecobox.url,{'ecobox[offset]':offset-1,'ecobox[numBoxes]':numBoxes}, ecobox.callBackSearch);
				
				//Change to 0 because of IE7. Last value was 200
				$('#contentColumns').fadeOut(0,ecobox.showLoading); //cosmetic
				return false;
			}
			
			this.clearSearchBox = function(){
				if($(this).val() == defaultSearchBox){
					$(this).val('');
				}
			}
			
			this.fillSearchBox = function(){
				if($(this).val() == ''){
					$(this).val(defaultSearchBox);
				}
			}
			
			//slide up the boxes (collapse)
			this.slideUp = function(){
				var id = this.id.replace('contentBoxSlider', "") - 0;
				
				// slide up the box
				$("#contentBoxText"+id).slideUp("slow");
				
				this.src = this.src.replace('hideBox', 'dropdown');
				$(this).unbind('click',ecobox.slideUp).bind('click',ecobox.slideDown);
				//memorize only special boxes in column right
				if(id <= numberSpecialBoxes + 1 ){
					ecobox.hiddenBoxes += 'contentBoxText'+id+',';
					$.cookie('hiddenBoxes',ecobox.hiddenBoxes,{expires: 40});
				}
			}
			
			//slide down the boxes (expand)
			this.slideDown = function(){
				var id = this.id.replace('contentBoxSlider', "") - 0;
				$("#contentBoxText" + id).slideDown("slow");
				this.src = this.src.replace('dropdown', 'hideBox');
				$(this).unbind('click',ecobox.slideDown).bind('click',ecobox.slideUp);
				//memorize only the boxes in column right
				if(id <= numberSpecialBoxes + 1 ){
					ecobox.hiddenBoxes = ecobox.hiddenBoxes.replace('contentBoxText'+id+',', "");
					$.cookie('hiddenBoxes',ecobox.hiddenBoxes,{expires: 40});
				}
			}
			
			//data contains unecessary html, extract only the body
			this.extractResponse = function(data){
				var pos1 = data.search(/<body>/gi);
				var pos2 = data.length - pos1 - 21; //21 == </body></html>
				return data.substr(pos1+6,pos2);
			}
		}
	});
})(jQuery);

function echo(data){
	try{
		console.log(data)
	}
	catch(e){}
}
