
// Simple JavaScript Rotating Banner Using jQuery
// www.mclelun.com
// Script used in the portfolio with pagination

/* homepage gallery  */

// set the defaults
var jqb_vCurrent = 0;
var jqb_vTotal = 0;
var jqb_vDuration = 6500; // time between change in slides
var jqb_intInterval = 0;
var jqb_vGo = 1;
var jqb_vIsPause = false;
var jqb_tmp = 20;
var jqb_slide = 1;
var newlink = 1
jQuery(document).ready(function() {	
								
	jqb_vTotal = $(".jqb_slides").children().size() -1; //calculate the number of slides
	
	// the 3 lines below are for additional text areas and previous button. Not currently being used 
	$(".jqb_info").text($(".jqb_slide").attr("title"));	
	$("#btn_prev").css('visibility','hidden')
	
	jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);
			
	$("#jqb_object").find(".jqb_slide").each(function(i) { 												  
		jqb_tmp = ((i - 1)*781) - ((jqb_vCurrent -1)*781); //the value 781 refers to the width of the image for this slider
		$(this).animate({"left": jqb_tmp+"px"}, 500); // 500 refers to the animation interval
	});
	
	//for catalogue page slider
	$("#jqb_object2").find(".jqb_slide").each(function(i) { 												  
		jqb_tmp = ((i - 1)*603) - ((jqb_vCurrent -1)*603); //the value 603 refers to the width of the image for this slider
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	$("#jqb_object3").find(".jqb_slide").each(function(i) { 												  
		jqb_tmp = ((i - 1)*812) - ((jqb_vCurrent -1)*812); //the value 603 refers to the width of the image for this slider
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	if($(".jqb_slide").length > 1){ // set up the pagination (1,2,3 ) buttons 
			pagination = $('<div class="pagination"><ul/></div>');
			$('.jqb_btn_next').after(pagination);
	
			$(".jqb_slide").each(function(i){ // loop through the slides and create a number button for each
			var count = 1+i;
			var links = '<li><a href="#">'+count+'</a></li>';
			$('ul',pagination).append(links)
		});
	}
	
	$(".pagination a").first().addClass('selected') // set the first mumber to selected by default
	
	$(".pagination a").click(function(evt) { // when a pagination button is clicked...
		evt.preventDefault(); // stops the page aligning to the top 
		clearInterval(jqb_intInterval);
		jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);
		var me = $(this);	
		var current = $('.selected')	
	
		if(me.hasClass('selected')){return false} // do nothing if it is already selected
					
		$(".pagination a").removeClass('selected'); // otherwise remove the selected class from the previously select number and add it to the one that has been clicked
		 me.addClass('selected');
		
		jumpTo(me.text()); // jump to relevant slide
		})
	
	
	// Currently hidden. Gives the option of pause and play buttons and adds removes their css styles
	$("#btn_pauseplay").click(function() {
		if(jqb_vIsPause){
			jqb_fnChange();
			jqb_vIsPause = false;
			$("#btn_pauseplay").removeClass("jqb_btn_play");
			$("#btn_pauseplay").addClass("jqb_btn_pause");
		} else {
			clearInterval(jqb_intInterval);
			jqb_vIsPause = true;
			$("#btn_pauseplay").removeClass("jqb_btn_pause");
			$("#btn_pauseplay").addClass("jqb_btn_play");
		}
	});
	
	// Currently hidden. moves to the next or previous slide using next and previous buttons
	$("#btn_prev").click(function() {		
		jqb_vGo = -1;
		jqb_fnLoop();	
	});
		
	$("#btn_next").click(function() {		
		jqb_vGo = 1;
		jqb_fnLoop();
	});
});


// Sets the variables when using next/ previous buttons and then move to the next slide
function jqb_fnChange(){
	clearInterval(jqb_intInterval);
	jqb_intInterval = setInterval(jqb_fnLoop, jqb_vDuration);
	jqb_fnLoop();
}

function refreshButtons(){ // Set the visibility of the next & prev buttons

	if(jqb_vCurrent == jqb_vTotal){
			$("#btn_next").css('visibility','hidden')
		}else{
			$("#btn_next").css('visibility','')
		}
		if(jqb_vCurrent == 0){
			$("#btn_prev").css('visibility','hidden')
		}else{
			$("#btn_prev").css('visibility','')
		}
		
}

function jumpTo(idx){ // jump to a different slide using the pagination
	jqb_vCurrent=idx-1;
	$("#jqb_object").find(".jqb_slide").each(function(i) { 	
		jqb_tmp = ((i - 1)*781) - ((jqb_vCurrent -1)*781);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});
	
	$("#jqb_object2").find(".jqb_slide").each(function(i) { 	
		jqb_tmp = ((i - 1)*603) - ((jqb_vCurrent -1)*603);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});

	$("#jqb_object3").find(".jqb_slide").each(function(i) { 	
		jqb_tmp = ((i - 1)*812) - ((jqb_vCurrent -1)*812);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
	});

// make sure the correct next/previous buttons are displaying. Currently not being used
	refreshButtons();
}

function jqb_fnLoop(){
	// Auto play. Removing this will mean the slides will only move using the pagination or next previous buttons
	if(jqb_vGo == 1){
		jqb_vCurrent == jqb_vTotal ? jqb_vCurrent = 0 : jqb_vCurrent++; // short hand for if (?)/else (:)
	} else {
		jqb_vCurrent == 0 ? jqb_vCurrent = jqb_vTotal : jqb_vCurrent--;
	}

	
	$(".pagination a").removeClass('selected'); // Remove selected from the current pagination button
		
	$(".pagination a").each(function(idx){ 
		if(idx == jqb_vCurrent){$(this).addClass('selected')}// Addv selected class to the newly clicked pagination button
	});
	
		// make sure the correct next/previous buttons are displaying. Currently not being used
		refreshButtons();
		
	$("#jqb_object").find(".jqb_slide").each(function(i) { 
	//slide
		jqb_tmp = ((i - 1)*781) - ((jqb_vCurrent -1)*781);
		$(this).animate({"left": jqb_tmp+"px"}, 500); // Animation interval
		
		// Fade
		/*if(i == jqb_vCurrent){
			$(".jqb_info").text($(this).attr("title"));
			$(this).fadeIn("slow");
		} else {
			$(this).fadeOut("slow");
		}*/
		
		
		// Add titles
		/*if(i == jqb_vCurrent){
			jqb_title = $(this).attr("title");
			$(".jqb_info").animate({ opacity: 'hide', "left": "-50px"}, 250,function(){
				$(".jqb_info").text(jqb_title).animate({ opacity: 'show', "left": "0px"}, 500);
			});
		} */
	});
	
//for catalogue page slider
	$("#jqb_object2").find(".jqb_slide").each(function(i) { 
	
	//slide
		jqb_tmp = ((i - 1)*603) - ((jqb_vCurrent -1)*603);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
		
		// Fade
		/*if(i == jqb_vCurrent){
			$(".jqb_info").text($(this).attr("title"));
			$(this).fadeIn("slow");
		} else {
			$(this).fadeOut("slow");
		}*/
		
		
		// Add titles
		/*if(i == jqb_vCurrent){
			jqb_title = $(this).attr("title");
			$(".jqb_info").animate({ opacity: 'hide', "left": "-50px"}, 250,function(){
				$(".jqb_info").text(jqb_title).animate({ opacity: 'show', "left": "0px"}, 500);
			});
		} */
	});
	
	$("#jqb_object3").find(".jqb_slide").each(function(i) { 
	
	//slide
		jqb_tmp = ((i - 1)*812) - ((jqb_vCurrent -1)*812);
		$(this).animate({"left": jqb_tmp+"px"}, 500);
		
		// Fade
		/*if(i == jqb_vCurrent){
			$(".jqb_info").text($(this).attr("title"));
			$(this).fadeIn("slow");
		} else {
			$(this).fadeOut("slow");
		}*/
		
		
		// Add titles
		/*if(i == jqb_vCurrent){
			jqb_title = $(this).attr("title");
			$(".jqb_info").animate({ opacity: 'hide', "left": "-50px"}, 250,function(){
				$(".jqb_info").text(jqb_title).animate({ opacity: 'show', "left": "0px"}, 500);
			});
		} */
	});
	
}
