/*
	Depreciated
	has to be deleted
	
	You can find the real search page manager in /common/javascript/search-results.js
*/

var searchResultsPage = new Class({
	initialize:function(){
/*
To activate/deactivate product search and/or news search, set the following var to true/false
*/
		this.productSearchActive = true;
		this.newsSearchActive = false;
/******************************************/
		this.newSearch = true;
		this.productsComplete = false;
		this.newsComplete = false;
		this.total_results = 0;
		this.productShadowed = false;
		this.newsShadowed = false;
		this.stateCookie = Cookie.read("GLOBAL_SEARCH_STATE",{path:'/'});
		if(!this.stateCookie){
			this.newSearch = true;
			this.stateCookie = "products/1/1";
			Cookie.write("GLOBAL_SEARCH_STATE",this.stateCookie,{path:'/'});
		}
		
		Cookie.write('REQUEST_HISTORY_URI', Window.getCleanUrl(),{path:'/'});
		cookie_array = new Array();
		cookie_array =  this.stateCookie.split('/');
		this.current_tab = cookie_array[0];
		this.products_current_range = cookie_array[1];
		this.news_current_range = cookie_array[2];
		$('products-tab').getElement('a').addEvent('click', function(){
			$('products-center').setStyle('display', 'block');
			$('products-tab').getElement('a').addClass('selected');
			$('news-center').setStyle('display', 'none');
			$('news-tab').getElement('a').removeClass('selected');
			Cookie.write("GLOBAL_SEARCH_STATE","products/"+this.productsGrid.getRangeStart()+"/"+this.newsGrid.getRangeStart(),{path:'/'});
			return false;
		}.bind(this));
		$('news-tab').getElement('a').addEvent('click', function(){
			$('news-center').setStyle('display', 'block');
			$('news-tab').getElement('a').addClass('selected');
			$('products-center').setStyle('display', 'none');
			$('products-tab').getElement('a').removeClass('selected');
			Cookie.write("GLOBAL_SEARCH_STATE","news/"+this.productsGrid.getRangeStart()+"/"+this.newsGrid.getRangeStart(),{path:'/'});
			if(!this.newsShadowed){
				$$('.newsPreviewBoxes .newsPreviewBox').addShadow();
				this.newsShadowed = true;
			}
			return false;
		}.bind(this));
		this.productsGrid = new SearchResultProducts(this.products_current_range,this.newSearch);
		this.newsGrid = new SearchResultNews(this.news_current_range,this.newSearch);
		window.addEvent('SearchResultProductsComplete', this.completeProductsResults.bind(this));
		window.addEvent('SearchResultNewsComplete',this.completeNewsResults.bind(this));
		if(this.productSearchActive)
			this.productsGrid.loadSearch();
		else
			window.fireEvent('SearchResultProductsComplete');
//		if(this.newsSearchActive)
//			this.newsGrid.loadSearch();
//		else
//			window.fireEvent('SearchResultNewsComplete');


	},
	displayPage: function(){
		$('results-table').setStyle('display','none');
		if(this.total_results == 0)
			$('no-result').setStyle('display','block');
		else{
			$('results-container').setStyle('display','block');
			if(this.productsGrid.total_items == 0){
				$('products-tab').setStyle('display','none');
				$('news-tab').getElement('a').fireEvent('click');
			}
			else{
				$('results-table').setStyle('display','block');
				$$('.productPreviewBox').addShadow();
				this.productShadowed = true;
			}
			if(this.newsGrid.total_items == 0){
				$('news-tab').setStyle('display','none');
			}
			else{
				$('results-table').setStyle('display','block');
				if(this.current_tab=="news"){
					$('news-tab').getElement('a').fireEvent('click');
				}
				else{
					if(!this.productShadowed){
						$$('.productPreviewBox').addShadow();
						this.productShadowed = true;
					}
				}
			}
		}
		$$('.topResultsProductPreviewBox').addShadow();
		$$('.topResultsNews .newsPreviewBox').addShadow();
	},
	completeProductsResults: function(){
		this.productsComplete = true;
		this.total_results += this.productsGrid.total_items+this.productsGrid.total_top_results;
		if(this.newsComplete) this.displayPage();

//		var tagCommander = new TagCommander();
//		tagCommander.sendTag({'search_keywords': this.productsGrid.query, 'search_page_number': this.productsGrid.currentPage, 'search_results_number': this.productsGrid.total_items+this.productsGrid.total_top_results});
		
	},
	completeNewsResults: function(){
		this.newsComplete = true;
		this.total_results += this.newsGrid.total_items+this.newsGrid.total_top_results;
		if(this.productsComplete) this.displayPage();
		//alert('test');
	}
});

var SearchResultProducts = new Class({
	initialize: function(range,newSearch){
		this.total_top_results = 0;
		this.viewMode = 'onmodel';
		//this.itemPerPage = 12;
		this.query = getURLParam('levisQuery');
		this.itemPerPage = getURLParam('rangeLength') || 12;
		this.newSearch = getURLParam('newSearch') || newSearch;
		this.currentPage = Math.ceil((getURLParam('rangeStart') || range.toInt())/ this.itemPerPage) || 1;
		this.total_items = totalItems;
		this.total_pages = Math.ceil(this.total_items / this.itemPerPage);
		this.logLocation = "search-results SearchResultProducts";
	},
	loadSearch: function(){
		logging.info(this.logLocation,"Write cookie 'PARAM_LEVIS_QUERY' : "+escape(this.query));
		logging.info(this.logLocation,"Write cookie 'SEARCH_PARAM_RANGE_LENGTH' : "+this.itemPerPage);
		logging.info(this.logLocation,"Write cookie 'SEARCH_PARAM_RANGE_START' : " +this.getRangeStart());
		
		Cookie.write('PARAM_LEVIS_QUERY', escape(this.query),{path:'/'});
		Cookie.write('SEARCH_PARAM_RANGE_LENGTH', this.itemPerPage,{path:'/'});
		Cookie.write('SEARCH_PARAM_RANGE_START', this.getRangeStart(),{path:'/'});
		levisShop.clearProductFinderLastSearch();
		if (!this.newSearch) {
			var jsonRequest = new Request.JSON({
				url: this.getServiceUrl(),
				noCache: true,
				onComplete: function(search){
					this.total_items = search.METADATA.TOTAL_ITEMS;
					this.total_pages = Math.ceil(this.total_items / this.itemPerPage);
					this.products = search.OTHER_RESULTS;
					this.topResults = search.TOP_RESULTS;
//					this.setTopResults();
					this.setProducts();
					this.setPaging();
					window.fireEvent('SearchResultProductsComplete');
					this.newSearch = false;
				}.bind(this)
			}).get();
		}
		this.setPagingActions();
	},
	getRangeStart: function(){
		return (this.currentPage * this.itemPerPage) - this.itemPerPage + 1;
	},
	getServiceUrl: function(){
		return "/"+ locale +"/shop/services/SearchProductsFreeText?rangeStart="+ this.getRangeStart() +"&rangeLength=" + this.itemPerPage+"&levisQuery="+escape(this.query)+"&newSearch="+this.newSearch+'&'+ $time();
	},
	setTopResults: function(){
		this.topBoxObjects = new Array();
		$$('.topResultsProductPreviewBox').each(function(object){
			box = new ProductPreviewBox(object);
			this.topBoxObjects.push(box);
		},this);
		this.topBoxObjects.each(function(topBox,index){
			var topBoxElement = $$('div.topResultsProductPreviewBox')[index];
			if(typeof(this.topResults[index])!="undefined"){
				this.total_top_results++;
				topBox.box.getParent('td').setStyle('display','');
				topBox.update(this.topResults[index]);
				topBoxElement.getElements('.typeface-js').each(function(item){
					reintinilazeTypeFace(item);
				});
				topBox.show({'productImageSide': 'front', 'productImageView': this.viewMode});
			}
		},this);
	},
	setProducts: function(){
		boxObjects.each(function(boxObject, index){
			var boxElement = $$('div.productPreviewBox')[index];
			if (!this.products[index]) {
				boxElement.getParent('div').setStyle('display', 'none');
			} else {
				var interval = (50 - index) * Math.random() * 20;
				boxElement.getParent('div').setStyle('display','');
				var timer = setTimeout(function() {
					boxObject.hide(function () {
						boxObject.update(this.products[index]);
						boxElement.getElements('.typeface-js').each(function(item){
							reintinilazeTypeFace(item);
						});
						boxObject.show({'productImageSide': 'front', 'productImageView': this.viewMode});
					}.bind(this));
				}.bind(this), interval);
			}
		}.bind(this));
		this.setPageTag();
	},
	setOtherProducts: function(){
		logging.info(this.logLocation,"Write cookie 'PARAM_LEVIS_QUERY' : "+escape(this.query));
		logging.info(this.logLocation,"Write cookie 'SEARCH_PARAM_RANGE_LENGTH' : "+this.itemPerPage);
		logging.info(this.logLocation,"Write cookie 'SEARCH_PARAM_RANGE_START' : " +this.getRangeStart());
        Cookie.write('PARAM_LEVIS_QUERY', escape(this.query),{path:'/'});
		Cookie.write('SEARCH_PARAM_RANGE_LENGTH', this.itemPerPage,{path:'/'});
		Cookie.write('SEARCH_PARAM_RANGE_START', this.getRangeStart(),{path:'/'});
		levisShop.clearProductFinderLastSearch();
		
		var jsonRequest = new Request.JSON({url: this.getServiceUrl(), onComplete: function(search){
			this.products = search.OTHER_RESULTS;
			this.setProducts();
		}.bind(this)}).get();
	},
	setPaging: function(){
		var pagingHtml = '';
        var showAll = 5;
		var pagingLength = 4;
		if (this.total_items > this.itemPerPage) {
			//display previous button
            if (this.currentPage > 1) {
                pagingHtml += '<a href="#" style="font-size:16px" class="prev"><img src="/ecom/common/images/shop/arrows/arrow-search-previous-page.gif" alt="" /></a>';
            }
            if(this.total_pages <= showAll) {     
                for (var i = 1; i <= this.total_pages; i++) {
                    if (i == this.currentPage) {
                        pagingHtml += '<span class="pages-number">'+ i +'</span>';
                    } else {
                        pagingHtml += '<a href="#" class="pages-number">'+ i +'</a>';
                    }
                }
            } else {


                if (this.total_pages <= pagingLength) {
                    var startPage = 1;
                    var endPage = this.total_pages;
                } else if (this.total_pages <= this.currentPage) {
                    var startPage = this.total_pages - pagingLength + 1;
                    var endPage = this.total_pages;
                } else {
                    var startPage = (this.currentPage%pagingLength == 0) ? this.currentPage - pagingLength + 1 : this.currentPage - this.currentPage%pagingLength + 1;
                    var endPage = Math.min(startPage + pagingLength - 1,this.total_pages);
                }
                
                var selectedSetted = false;
                
                //Add first page
                var pageNumber = 1;
                pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
 
                //three dots
                if (1 < (this.currentPage-2)) {
                    pagingHtml += '<span class="pages-number" style="color:#404040">...</span>';
                } 
                
                //page 2 before selected
                pageNumber = (this.currentPage-2);
                if (pageNumber > this.total_pages -3) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }
                
                //page 1 before selected
                pageNumber = (this.currentPage-1);
                if (pageNumber > 1) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }
                
                //selected page (if not yet printed)
                pageNumber = this.currentPage;
                if (!selectedSetted) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                } 

                //page after 1 selected
                pageNumber = (this.currentPage+1);
                if (pageNumber < this.total_pages) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }

                //page after 2 selected
                pageNumber = (this.currentPage+2);
                if (pageNumber < 4) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }
                
                //three dots
                if (this.total_pages > (this.currentPage+2)) {
                    pagingHtml += '<span class="pages-number" style="color:#404040">...</span>';
                }

                //Add last page
                pageNumber = this.total_pages;
                if (this.total_pages > 1) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }
            }

            //display next button
			if (this.currentPage < this.total_pages) {
				pagingHtml += '<a href="#" style="font-size:16px" class="next"><img src="/ecom/common/images/shop/arrows/arrow-search-next-page.gif" alt="" /></a>';
			}
		}
		
		/*
		var pagingHtml = '';
		var pagingLength = 5;
		if (this.total_items > this.itemPerPage) {
			if (this.total_pages <= pagingLength) {
				var startPage = 1;
				var endPage = this.total_pages;
			} else if (this.currentPage <= Math.floor(pagingLength / 2)) {
				var startPage = 1;
				var endPage = pagingLength;
			} else if (this.total_pages <= this.currentPage + Math.floor(pagingLength / 2)) {
				var startPage = this.total_pages - pagingLength + 1;
				var endPage = this.total_pages;
			} else {
				var startPage = this.currentPage - Math.floor(pagingLength / 2);
				var endPage = this.currentPage + Math.ceil(pagingLength / 2) - 1;
			}
			if (this.currentPage > 1) {
				pagingHtml += '<a href="#" class="prev"><img src="/images/shop/arrows/arrow-search-previous-page.gif" alt="" /></a>';
			}
			for (var i = startPage; i <= endPage; i++) {
				if (i == this.currentPage) {
					pagingHtml += '<span class="pages-number">'+ i +'</span>';
				} else {
					pagingHtml += '<a href="#" class="pages-number">'+ i +'</a>';
				}
			}
			if (this.currentPage < this.total_pages) {
				pagingHtml += '<a href="#" class="next"><img src="/images/shop/arrows/arrow-search-next-page.gif" alt="" /></a>';
			}
		}
		*/
		$('products-center').getElement('span.pages-block').set('html', pagingHtml);
		this.setPagingActions();
	},
	setPagingActions: function(){
		stateCookie = Cookie.read("GLOBAL_SEARCH_STATE",{path:'/'});
		cookie_array = new Array();
		cookie_array =  stateCookie.split('/');
		current_tab = cookie_array[0];
		products_current_range = cookie_array[1];
		news_current_range = cookie_array[2];
		if ($('products-center').getElement('span.pages-block a.prev')) {
			$('products-center').getElement('span.pages-block a.prev').addEvent('click', function(){
				this.currentPage--;
				this.newSearch = false;
				this.setOtherProducts();
				this.setPaging();
				Cookie.write("GLOBAL_SEARCH_STATE",current_tab+"/"+this.getRangeStart()+"/"+this.currentPage,{path:'/'});
				return false;
			}.bind(this));
		}
		if ($('products-center').getElements('span.pages-block a.pages-number')) {
			$('products-center').getElements('span.pages-block a.pages-number').addEvent('click', function(e){
				this.currentPage = parseInt(e.target.get('text'));
				this.newSearch = false;
				this.setOtherProducts();
				this.setPaging();
				Cookie.write("GLOBAL_SEARCH_STATE",current_tab+"/"+this.getRangeStart()+"/"+this.currentPage,{path:'/'});
				return false;
			}.bind(this));
		}
		if ($('products-center').getElement('span.pages-block a.next')) {
			$('products-center').getElement('span.pages-block a.next').addEvent('click', function(){
				this.currentPage++;
				this.newSearch = false;
				this.setOtherProducts();
				this.setPaging();
				Cookie.write("GLOBAL_SEARCH_STATE",current_tab+"/"+this.getRangeStart()+"/"+this.currentPage,{path:'/'});
				return false;
			}.bind(this));
		}
	},
	setPageTag: function() {
		var tag = new Tag();
		var eVar16 = (this.total_items == 0) ? 'zero': this.total_items;
		var events = (this.total_items == 0) ? 'event1,event2': 'event1';
		tag.set({'events': events, 'pageName': 'shop search results page '+ this.currentPage, 'channel': 'shop', 'prop1': 'shop:search', 'prop10': locale, 'eVar15': escape(this.query), 'eVar16': eVar16});
		tag.send();
		
//		var tagCommander = new TagCommander();
//		tagCommander.sendTag({'search_page_number': this.currentPage});
	}
});
//**********************************************************
// Set all the results News stuff
//**********************************************************

var SearchResultNews = new Class({
	initialize: function(range,newSearch){
		this.itemPerPage = 6;
		this.currentPage = Math.ceil(range/ this.itemPerPage) || 1;
		this.query = getURLParam('levisQuery');
		this.newSearch = newSearch;
		this.total_top_results = 0;
		this.total_items = 0;
	},
	loadSearch: function(){
		var jsonRequest = new Request.JSON({
			url: this.getServiceUrl(),
			noCache: true,
			onComplete: function(search){
				this.total_items = search.METADATA.TOTAL_ITEMS;
				this.total_pages = Math.ceil(this.total_items / this.itemPerPage);
				this.news = search.OTHER_RESULTS;
				this.topResults = search.TOP_RESULTS;
				this.setTopResult();
				this.setNews();
				this.setPaging();
				window.fireEvent('SearchResultNewsComplete');
				this.newSearch = false;
			}.bind(this)
		}).get();
	},
	getServiceUrl: function(){
		return "/cmsservices/SearchNewsitemsFreeText?localeCode=" + locale + "&rangeStart="+ this.getRangeStart() +"&rangeLength=" + this.itemPerPage+"&levisQuery="+escape(this.query)+"&newSearch="+this.newSearch+'&'+ $time();
	},
	getRangeStart: function(){
		return (this.currentPage * this.itemPerPage) - this.itemPerPage + 1;
	},
	setTopResult: function(){
		topBox = new NewsPreviewBox($$('.topResultsNews .newsPreviewBox'));
		if(typeof(this.topResults[0])!="undefined"){
			this.total_top_results++;
			topBox.news.getParent('td').setStyle('display','');
			topBox.update(this.topResults[0]);
		}
	},
	setNews: function(){
		//newsObjects.shuffle();
		newsObjects.each(function(boxObject, index){
			var boxElement = $$('.newsPreviewBoxes .newsPreviewBox')[index];
			if (!this.news[index]) {
				boxElement.getParent('td').setStyle('display', 'none');
			} else {
				//var interval = index * 200;
				var interval = (50 - index) * Math.random() * 20;
				boxElement.getParent('td').setStyle('display','');
				var timer = setTimeout(function() {
					boxObject.hide(function () {
						boxObject.update(this.news[index]);
						boxElement.getElements('.typeface-js').each(function(item){
							reintinilazeTypeFace(item);
						});
						boxObject.show();
					}.bind(this));
				}.bind(this), interval);
			}
		}.bind(this));
		//this.setPageTag();
	},
	setOtherNews: function(){
		var jsonRequest = new Request.JSON({url: this.getServiceUrl(), onComplete: function(search){
			this.news = search.OTHER_RESULTS;
			this.setNews();
		}.bind(this)}).get();
	},
	setPaging: function(){

		var pagingHtml = '';
        var showAll = 5;
		var pagingLength = 4;
		if (this.total_items > this.itemPerPage) {
			//display previous button
            if (this.currentPage > 1) {
                pagingHtml += '<a href="#" style="font-size:16px" class="prev"><img src="/ecom/common/images/shop/arrows/arrow-search-previous-page.gif" alt="" /></a>';
            }
            if(this.total_pages <= showAll) {     
                for (var i = 1; i <= this.total_pages; i++) {
                    if (i == this.currentPage) {
                        pagingHtml += '<span class="pages-number">'+ i +'</span>';
                    } else {
                        pagingHtml += '<a href="#" class="pages-number">'+ i +'</a>';
                    }
                }
            } else {


                if (this.total_pages <= pagingLength) {
                    var startPage = 1;
                    var endPage = this.total_pages;
                } else if (this.total_pages <= this.currentPage) {
                    var startPage = this.total_pages - pagingLength + 1;
                    var endPage = this.total_pages;
                } else {
                    var startPage = (this.currentPage%pagingLength == 0) ? this.currentPage - pagingLength + 1 : this.currentPage - this.currentPage%pagingLength + 1;
                    var endPage = Math.min(startPage + pagingLength - 1,this.total_pages);
                }
                
                var selectedSetted = false;
                
                //Add first page
                var pageNumber = 1;
                pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
 
                //three dots
                if (1 < (this.currentPage-2)) {
                    pagingHtml += '<span class="pages-number" style="color:#404040">...</span>';
                } 
                
                //page 2 before selected
                pageNumber = (this.currentPage-2);
                if (pageNumber > this.total_pages -3) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }
                
                //page 1 before selected
                pageNumber = (this.currentPage-1);
                if (pageNumber > 1) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }
                
                //selected page (if not yet printed)
                pageNumber = this.currentPage;
                if (!selectedSetted) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                } 

                //page after 1 selected
                pageNumber = (this.currentPage+1);
                if (pageNumber < this.total_pages) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }

                //page after 2 selected
                pageNumber = (this.currentPage+2);
                if (pageNumber < 4) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }
                
                //three dots
                if (this.total_pages > (this.currentPage+2)) {
                    pagingHtml += '<span class="pages-number" style="color:#404040">...</span>';
                }

                //Add last page
                pageNumber = this.total_pages;
                if (this.total_pages > 1) {
                	pagingHtml += addPageNumber(selectedSetted, pageNumber, this.currentPage);
                	selectedSetted = isPageNumberSelected(pageNumber,this.currentPage) || selectedSetted;
                }
            }

            //display next button
			if (this.currentPage < this.total_pages) {
				pagingHtml += '<a href="#" style="font-size:16px" class="next"><img src="/ecom/common/images/shop/arrows/arrow-search-next-page.gif" alt="" /></a>';
			}
		}
		
		$('news-center').getElement('span.pages-block').set('html', pagingHtml);
		this.setPagingActions();
		
		
		
		
		/*
		
		var pagingHtml = '';
		var pagingLength = 5;
		if (this.total_items > this.itemPerPage) {
			if (this.total_pages <= pagingLength) {
				var startPage = 1;
				var endPage = this.total_pages;
			} else if (this.currentPage <= Math.floor(pagingLength / 2)) {
				var startPage = 1;
				var endPage = pagingLength;
			} else if (this.total_pages <= this.currentPage + Math.floor(pagingLength / 2)) {
				var startPage = this.total_pages - pagingLength + 1;
				var endPage = this.total_pages;
			} else {
				var startPage = this.currentPage - Math.floor(pagingLength / 2);
				var endPage = this.currentPage + Math.ceil(pagingLength / 2) - 1;
			}
			if (this.currentPage > 1) {
				pagingHtml += '<a href="#" class="prev"><img src="/images/shop/arrows/arrow-search-previous-page.gif" alt="" /></a>';
			}
			for (var i = startPage; i <= endPage; i++) {
				if (i == this.currentPage) {
					pagingHtml += '<span class="pages-number">'+ i +'</span>';
				} else {
					pagingHtml += '<a href="#" class="pages-number">'+ i +'</a>';
				}
			}
			if (this.currentPage < this.total_pages) {
				pagingHtml += '<a href="#" class="next"><img src="/images/shop/arrows/arrow-search-next-page.gif" alt="" /></a>';
			}
		}
*/
	},
	setPagingActions: function(){
		stateCookie = Cookie.read("GLOBAL_SEARCH_STATE",{path:'/'});
		cookie_array = new Array();
		cookie_array =  stateCookie.split('/');
		current_tab = cookie_array[0];
		products_current_range = cookie_array[1];
		news_current_range = cookie_array[2];
		if ($('news-center').getElement('span.pages-block a.prev')) {
			$('news-center').getElement('span.pages-block a.prev').addEvent('click', function(){
				this.currentPage--;
				this.newSearch = false;
				this.setOtherNews();
				this.setPaging();
				Cookie.write("GLOBAL_SEARCH_STATE",current_tab+"/"+this.currentPage+"/"+this.getRangeStart(),{path:'/'});
				return false;
			}.bind(this));
		}
		if ($('news-center').getElements('span.pages-block a.pages-number')) {
			$('news-center').getElements('span.pages-block a.pages-number').addEvent('click', function(e){
				this.currentPage = parseInt(e.target.get('text'));
				this.newSearch = false;
				this.setOtherNews();
				this.setPaging();
				Cookie.write("GLOBAL_SEARCH_STATE",current_tab+"/"+this.currentPage+"/"+this.getRangeStart(),{path:'/'});
				return false;
			}.bind(this));
		}
		if ($('news-center').getElement('span.pages-block a.next')) {
			$('news-center').getElement('span.pages-block a.next').addEvent('click', function(){
				this.currentPage++;
				this.newSearch = false;
				this.setOtherNews();
				this.setPaging();
				Cookie.write("GLOBAL_SEARCH_STATE",current_tab+"/"+this.currentPage+"/"+this.getRangeStart(),{path:'/'});
				return false;
			}.bind(this));
		}
	}/*,
	setPageTag: function() {
		var tag = new Tag();
		tag.set({'events': 'event1', 'pageName': 'shop search results page '+ this.currentPage, 'channel': 'shop', 'prop1': 'shop:search', 'prop10': locale, 'eVar15': escape(this.query), 'eVar16': this.total_items});
		tag.send();
	}*/
});
var NewsPreviewBox = new Class({
	initialize: function(object){
		this.news = object;
		this.image = this.news.getElement('.newsImage img');
		this.imageLink = this.news.getElement('.newsImage a');
		this.title = this.news.getElement('.newsDetails h2 a');
		this.text = this.news.getElement('.newsText');
		this.link = this.news.getElement('.newsLink a');
		//this.news.setStyles({opacity: 0.9});
		this.morphDuration = 500;
		this.news.addEvents({
			'mouseenter': function(){
				this.rollover();
			}.bind(this),
			'mouseleave': function(){
				this.rollout();
			}.bind(this)
		});
	},
	rollover: function(){
		this.news.addClass('newsPreviewBoxOver');
		//this.news.setStyles({opacity: 1});
	},
	rollout: function(){
		this.news.removeClass('newsPreviewBoxOver');
		//this.news.morph({opacity: 0.9});
	},
	show: function(options){
		this.news.getParent('td').set('morph', {duration:this.morphDuration});
		this.news.getParent('td').morph({opacity: 1});
	},
	hide: function(callback){
		if (!callback) {
			var options = {duration:this.morphDuration};
		} else {
			var options = {duration:this.morphDuration, onComplete: callback};
		}
		this.news.getParent('td').set('morph', options);
		this.news.getParent('td').morph({opacity: 0});
	},
	update:function(news){
		this.image.setProperty('src', news.IMAGE);
		this.title.set('html',news.TITLE);
		this.text.set('html',news.TEXT);
		this.link.setProperty('href',news.LINK);
		this.imageLink.setProperty('href',news.LINK);
	}
});

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

newsObjects = new Array();
window.addEvent('domready', function(){

//	$$('.topResultsProductPreviewBox').addShadow();
//	$$('.productPreviewBox').addShadow();
//	$$('.newsPreviewBox').addShadow();
	$('news-center').setStyle('display', 'none');

	$('query').set('html',getURLParam('levisQuery'));
	new searchResultsPage();
	$$('.newsPreviewBoxes .newsPreviewBox').each(function(object, index){
		var box = new NewsPreviewBox(object);
		newsObjects.push(box);
	});
});
Array.implement({
	shuffle: function() {
		for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
		return this;
	}
});

function addPageNumber(selectedSetted, pageNumber, currentPage) {
	if (pageNumber == currentPage) {
		if (!selectedSetted) {
			return '<span class="pages-number">'+ pageNumber +'</span>';
		}
    } else {
        return '<a href="#" class="pages-number">'+ pageNumber +'</a>';
    }
	return '';
}

function isPageNumberSelected(pageNumber, currentPage) {
    return currentPage == pageNumber;
}
