// JavaScript Document

$(document).ready(function()
{		

	// SET UP NEXT PREVIOUS PICTURES IN MODULE
	$(function() {
		// run the code in the markup!
		/*$('table pre code').not('#skip,#skip2').each(function() {
			eval($(this).text());
		});*/
		
		$('.module_item').cycle({
				fx:     'fade',
				speed:  'fast',
				timeout: 0,
				next:   '#next2',
				prev:   '#prev2'
			});
	});
	
	//SHOW HIDE ELEMENTS
	//hide all of the elements with clickable links
	$(".faq_body").hide();
	$(".review_body").hide();
	//$(".more_recently_viewed").hide();
	$("#landing_extra_content").hide();
	
	//toggle the componenet with class faq_body
	$("p#faq_body_more").click(function()
	{
		$(".faq_body").slideToggle(600);
	
		var str = $(this/*"p#faq_body_more"*/).text();
		
		if(str == 'view less') {     
				 $(this/*"p#faq_body_more"*/).text('view more');
		}else{
				$(this/*"p#faq_body_more"*/).text('view less');
		}
	});
  
	//toggle the componenet with class review_body
	$("p#review_body_more").click(function()
	{
		$(".review_body").slideToggle(600);
	
		var str = $(this/*"p#review_body_more"*/).text();
		
		if(str == 'view less') {     
				 $(this/*"p#review_body_more"*/).text('view more');
		}else{
				$(this/*"p#review_body_more"*/).text('view less');
		}
	});
  
	//toggle the landing page div
	$("p#landing_page_more").click(function()
	{
		$("#landing_extra_content").slideToggle(600);
		
		var str = $(this/*"p#landing_page_more"*/).text();
		
		if(str == 'view less') {     
				 $(this/*"p#landing_page_more"*/).text('view more');
		}else{
				$(this/*"p#landing_page_more"*/).text('view less');
		}
	});

	// SIDE MENU
     /*$(function() {
          $('#accordion').accordion({ active: -1, autoHeight: false, collapsible: true });
     });*/

	// SET UP THE SLIDING ACCESSORIES AND ADD A LINE
	$("#content_slider").slider({
		animate: true,
		change: handleSliderChange,
		slide: handleSliderSlide
	});

	function handleSliderChange(e, ui)
	{
	  var maxScroll = $("#content_scroll").attr("scrollWidth") -
					  $("#content_scroll").width();
	  $("#content_scroll").animate({scrollLeft: ui.value *
		 (maxScroll / 100) }, 1000);
	  /*$("#content_scroll").animate({scrollLeft: ui.value *
		 (maxScroll / 100) }, 1000);*/
	}

$.fn.getMaxScroll = function() {
	return $(this).attr("scrollWidth") - $(this).width();  
}

	function handleSliderSlide(e, ui)
	{
	  var maxScroll = $("#content_scroll").attr("scrollWidth") -
					  $("#content_scroll").width();
	  $("#content_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });
	  /*$("#content_scroll").attr({scrollLeft: ui.value * (maxScroll / 100) });*/
	}

	
	
	// 5 STAR RATING 

	var sMax;	// Isthe maximum number of stars
	var holder; // Is the holding pattern for clicked state
	var preSet; // Is the PreSet value onces a selection has been made
	var rated;
	
	// Rollover for image Stars //
	function rating(num){
		sMax = 0;	// Isthe maximum number of stars
		for(n=0; n<num.parentNode.childNodes.length; n++){
			if(num.parentNode.childNodes[n].nodeName == "A"){
				sMax++;	
			}
		}
		
		if(!rated){
			s = num.id.replace("_", ''); // Get the selected star
			a = 0;
			for(i=1; i<=sMax; i++){		
				if(i<=s){
					document.getElementById("_"+i).className = "on";
					document.getElementById("rateStatus").innerHTML = num.title;	
					holder = a+1;
					a++;
				}else{
					document.getElementById("_"+i).className = "";
				}
			}
		}
	}
	
	// For when you roll out of the the whole thing //
	function off(me){
		if(!rated){
			if(!preSet){	
				for(i=1; i<=sMax; i++){		
					document.getElementById("_"+i).className = "";
					document.getElementById("rateStatus").innerHTML = me.parentNode.title;
				}
			}else{
				rating(preSet);
				document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
			}
		}
	}
	
	// When you actually rate something //
	function rateIt(me){
		if(!rated){
			document.getElementById("rateStatus").innerHTML = document.getElementById("ratingSaved").innerHTML;
			preSet = me;
			rated=1;
			sendRate(me);
			rating(me);
		}
	}
	
	// Send the rating information somewhere using Ajax or something like that.
	function sendRate(sel){
		alert("Your rating was: "+sel.id);
		//document.getElementById("vote").style.display='none';
		//document.getElementById("voted").style.display='';
	}


});
