/*
Sony Style Reviews Utils page Javascript
AUTHOR: Jonathan Cheung
===================================================================================================================================================================
// !  * Registration page javascript logic.
* 	PRE-CONDITIONS:
*
* 	Required JS files: prototype.js, validation.js, ss_core_omniture.js
*
// ===================================================================================================================================================================
*/

/*
*	On Load we do the following things to setup the registration form.
*
*
*/

var ReviewsTabUtils = {

	/*
		PRECONDITION: None
		POST CONDITION:

		*NOTE Reset all the header fonts within the reviews areas to use the correct font
	*/
	sfrWithinReviews: function(){
		sIFR.replace(AvantGardeMedium, {
		  selector: '.sfrSortHeader',
		  css: ['.sIFR-root { color: #333333; font-size: 11px;}'],
		  wmode: 'transparent',
		  offsetTop: 3,
		  preventWrap:true,
		  forceSingleLine: true
		});
		sIFR.replace(AvantGardeMedium, {
		  selector: '.sfrReviewDetailsHeader',
		  css: ['.sIFR-root { color: #333333; font-size: 16px; }'],
		  wmode: 'transparent',
		  offsetTop: 5,
		  preventWrap:true,
		  forceSingleLine: true
		});
		sIFR.replace(AvantGardeMedium, {
		  selector: '.noReviewHeader',
		  css: ['.sIFR-root { color: #333333; font-size: 32px; }'],
		  wmode: 'transparent',
		  offsetTop: 5,
		  preventWrap:true,
		  forceSingleLine: true
		});
	},

	/*
	*
	*	NOTE: 	Should call this page whenver new content
	*			This function resets all the buttons/rollover action on the page.
	*/
	setupReviewTabInteractions: function(){
		/*
		* Add rollover for the "Was this helpful" popups
		*
		*
		*/
		$$('.reviewDetailPopup').each(function(element){
			element.hide();

			Event.observe(element.parentNode, 'mouseover', function(e){
				element.show();
			});

			Event.observe(element.parentNode, 'mouseout', function(e){
				if (Position.within(Event.element(e), Event.pointerX(e), Event.pointerY(e))  == false){
					element.hide();
				}
			});
		});

		/*
		* Add rollover for the Ratings Snapshots
		*
		*
		*/


		$$('a.popupIconButton').each(function(e){


			Event.observe($(e), 'mouseover', function(e){
				$($(this).readAttribute('href').substring(1)).addClassName('show');
				if (Prototype.Browser.IE)
					$$('select').each(function(e){e.hide()});
			});
			Event.observe($(e), 'mouseout', function(e){
				if (
					(Position.within(Event.element(e), Event.pointerX(e), Event.pointerY(e))  == false)
				){
					$($(this).readAttribute('href').substring(1)).removeClassName('show');
				if (Prototype.Browser.IE)
					$$('select').each(function(e){e.show()});
			}
		});
		})
	},


	/*
	*	Pagination Functions
	*
	*
	*
	*	First functrion sets up the pagination using a selector
	*/

	setupPagination:function(selector){
		$$(selector).each(function(element){
			/*
				first We setup an Attribute to store the current location of the
			*/
			ReviewsTabUtils.paginationClick(-1, element)
			/*
				for each of the <href> we setup the Event listeners
			*/
			$(element).select(".paginationPageNumber").each(function(element,index){
				element.pageNumber = index;
				Event.observe($(element), 'click', function(e){
					var thisPaginationElement = Event.element(e);
					ReviewsTabUtils.paginationClick(thisPaginationElement.pageNumber, thisPaginationElement.parentNode.parentNode);
					//Stops from anchoring
					Event.stop(e);
				})
			})
			//Set left Arrow Button
			$(element).select(".leftPagination").each(function(element,index){
				Event.observe(element, 'click', function(e){
					var pageArrow = Event.element(e);
					ReviewsTabUtils.paginationClick(pageArrow.parentNode.parentNode.currentNumber-1, pageArrow.parentNode.parentNode)
					//Stops from anchoring
					Event.stop(e);
				})
			})
			$(element).select(".rightPagination").each(function(element,index){
				Event.observe(element, 'click', function(e){
					var pageArrow = Event.element(e);
					ReviewsTabUtils.paginationClick(pageArrow.parentNode.parentNode.currentNumber+1, pageArrow.parentNode.parentNode)
					//Stops from anchoring
					Event.stop(e);
				})
			})


		});
	},
	// Delta number controls how many of the page numbers should display
	// Has to be an odd number
	paginationDeltaNumber: 1,
	//Function to call when pagination is clicked
	paginationClick: function(number, listElement){
		/*
			Every pagination click happens here
		*/

            	var execute_ajax=true;
            	if(number==-1)
                {
                	execute_ajax=false;
                  	number=0;
                }


		// If the number is within range
		if (listElement.currentNumber != number &&
			number <$(listElement).select(".paginationPageNumber").length &&
			number >=0)
		{
			/* Store the number for this */
			listElement.currentNumber = number;
			if ($(listElement).select(".paginationPageNumber").length > (this.paginationDeltaNumber+1) *3)
			{
				//Go through and hide the elements we don't want
				listElement.select(".paginationNumberItem").each(function(element,index){
					//Remove the left and right trailing dots.
					$(element).removeClassName('moreNumbersLeft');
					$(element).removeClassName('moreNumbersRight');
					//Remove active Classname
					$(element).removeClassName('active');

					if(index==listElement.currentNumber-ReviewsTabUtils.paginationDeltaNumber){
						element.show();
						if (index > ReviewsTabUtils.paginationDeltaNumber)
							$(element).addClassName('moreNumbersLeft');
					}else if (index == listElement.currentNumber+ReviewsTabUtils.paginationDeltaNumber){
						element.show();
						if (index < listElement.select(".paginationNumberItem").length - ReviewsTabUtils.paginationDeltaNumber-1)
							$(element).addClassName('moreNumbersRight');
					}else if (index == listElement.currentNumber){
						element.show();
						$(element).addClassName('active');
					}else if (index > listElement.currentNumber-ReviewsTabUtils.paginationDeltaNumber && index < listElement.currentNumber+ReviewsTabUtils.paginationDeltaNumber){
						element.show();
					}else{
						element.hide();
					}

					if (index == 0 ) element.show();
					if (index == $(listElement).select(".paginationPageNumber").length-1 ) element.show();

				})
			}else{
				listElement.select(".paginationNumberItem").each(function(element,index){
					element.removeClassName('active');
					if (index == listElement.currentNumber)element.addClassName('active');
				});
			}
		}//End within range check

		/*
		*	Insert pagination AJAX calls here.
		*
		*/



		if(execute_ajax)
		{
			submitReviewsSearch(number);
		}


	}//End paginationClick function



}


