/**
 * jQuery jqGalScroll Plugin
 * Examples and documentation at: http://benjaminsterling.com/jquery-jqgalscroll-photo-gallery/
 *
 * @author: Benjamin Sterling
 * @version: 2.1
 * @copyright (c) 2007 Benjamin Sterling, KenzoMedia
 * @extendThanks Koesmanto Bong http://www.koesbong.com/
 *		Koes put a fire under my butt to improve this plugin
 *		and when I took too long he took what I had and added
 *		the horizontal scroll and in turn I ripped it from his
 *		hands and made it better :)
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *   
 * @requires jQuery v1.2.1 or later
 * @optional jQuery Easing v1.2
 *
 * @name jqGalScroll
 * @example $('ul').jqGalScroll();
 * 
 * @Semantic requirements:
 * 				The structure fairly simple; the structure should consist
 * 				of a ul > li > img structure.
 * 
 * 	<ul>
 *		<li><img src="common/img/dsc_0003.thumbnail.JPG"/></li>
 *		<li><img src="common/img/dsc_0012.thumbnail.JPG"/></li>
 *	</ul>
 *
 * @param String ease
 *					refer to http://gsgd.co.uk/sandbox/jquery.easing.php for values
 * 
 * @example $('#gallery').jqGalScroll({speed:1000});
 
 * @param String speed
 * 					fast, slow, 1000, ext..
 * 
 * @example $('#gallery').jqGalScroll({speed:1000});
 * 
 * @param String height
 * 					the default height of your wrapper
 * 
 * @example $('#gallery').jqGalScroll({height:490});
 * 
 * @param String titleOpacity
 * 					the opacity of your title bar (if present)
 * 
 * @example $('#gallery').jqGalScroll({titleOpacity:.70});
 * 
 * @param String direction 
 *					vertical horizontal diagonal
 * 
 * @example $('#gallery').jqGalScroll({direction:'vertical'});
 * 			
 */
 
/*var infotext = new Array();
infotext[0] = "T-Handle Set, 7 pc Metric";
infotext[1] = "Rack for T-Handle Set";
infotext[2] = "Tire Iron, Spoon Type";
infotext[3] = "T-Handle 1/4 Drive, 8.5\" long";
infotext[4] = "Motion Pro Tire Pressure Gauge";
infotext[5] = "Folding Sag Scale";
infotext[6] = "Mini-Pro T-Handle Set";
infotext[7] = "T-Handle, Ball-End Hex, Dual Drive, Metric Set";
infotext[8] = "ForkTru - Fork Alignement Tool";
infotext[9] = "T6 Tool Pack";
infotext[10] = "T6 T-Handle Holster";
infotext[11] = "MP Tech Glove Large";*/

var infolink = new Array();
infolink[0] = "http://motionpro.com/motorcycle/partno/08-0113/";
infolink[1] = "http://motionpro.com/motorcycle/partno/08-0114/";
infolink[2] = "http://motionpro.com/motorcycle/partno/08-0115/";
infolink[3] = "http://motionpro.com/motorcycle/partno/08-0158/";
infolink[4] = "http://motionpro.com/motorcycle/partno/08-0258/";
infolink[5] = "http://motionpro.com/motorcycle/partno/08-0336/";
infolink[6] = "http://motionpro.com/motorcycle/partno/08-0340/";
infolink[7] = "http://motionpro.com/motorcycle/partno/08-0383/";
infolink[8] = "http://motionpro.com/motorcycle/partno/08-0412/";
infolink[9] = "http://motionpro.com/motorcycle/partno/21-0001/";
infolink[10] = "http://motionpro.com/motorcycle/partno/21-0003/";
infolink[11] = "http://motionpro.com/motorcycle/partno/21-0008/";

var stop_safety = 0;

(function($) {
	$.fn.jqGalScroll = function(options){
		return this.each(function(i){
			var el = this
			el.curImage = 0;
			el.jqthis = $(this).css({position:'relative'});
			el.jqchildren = el.jqthis.children();
			el.opts = $.extend({}, jqGalScroll, options);
			el.index = i;
			el.totalChildren = el.jqchildren.size();
			var width,height;
			
			switch(el.opts.direction){
				case 'horizontal':
					width = el.totalChildren *el.opts.width;
					height = el.opts.height;
					break;
				case 'vertical':
					width = el.opts.width;
					height = el.totalChildren *el.opts.height;
					break;
				default:
					width = el.totalChildren *el.opts.width;
					height = el.totalChildren *el.opts.height;
					break;
			};
			
			el.container = $('<div id="jqGS'+i+'" class="jqGSContainer">').css({position:'relative'});
			el.ImgContainer = $('<div class="jqGSImgContainer" style="height:'+el.opts.height+'px;position:relative;overflow:hidden">')
								.css({height:el.opts.height,width:el.opts.width,position:'relative',overflow:'hidden'});
			el.jqthis.css({height:height,width:width});
			
			el.jqthis.wrap(el.container);
			el.jqthis.wrap(el.ImgContainer); 
			el.pagination = $('<div class="jqGSPagination">'); 
			el.jqthis.parent().parent().append(el.pagination);
			
			// var pag_wrapper = $('<div align="center" class="jqGSPagWrapper">').appendTo(el.pagination.parent());
			// var jqul = $('<ul>').appendTo(pag_wrapper);
			var jqul = $('<ul>').appendTo(el.pagination);
			var pos = {x:0,y:0};

			var current = 0;
			
			el.jqchildren
			.each(function(j){
				var selected = '';
				if(j == 0) {
					selected = 'selected';
					this_or_that = 'this';
				} else {
					this_or_that = 'that';
				}
				
				// var $a = $('<a href="#'+(j)+'" class="'+selected+'">'+(j+1)+'</a>').click(function(){
				var $a = $('<a href="#'+(j)+'" class="'+selected+'">&nbsp;</a>').click(function(){
					if (stop_safety == 1) {
						stop_safety = 0;
					} else {
						stop_cycle();
					}

					var href = this.index;//href.replace(/^.*#/, '');
					el.pagination.find('.selected').removeClass('selected');
					$(this).addClass('selected');
					var params = {};
					if( el.opts.direction == 'diagonal'){
						params = {right:(el.opts.width*href),bottom:(el.opts.height*href)}
					}
					else if( el.opts.direction == 'vertical'){
						params = {bottom:(el.opts.height*href)}
					}
					else if( el.opts.direction == 'horizontal'){
						params = {right:(el.opts.width*href)}
					};

					el.jqthis.stop().animate(params,el.opts.speed, el.opts.ease);

					index = href;
					
					var get_current = $(this).attr('href');
					current = get_current.replace(/^.*#/, "");
					current = parseInt(current);
					
					// console.log('get_current = ' + get_current);
					// console.log('current = ' + current);

					var gallery_infobar_id = "#gallery_infobar_text_holder" + current;
					var gallery_infobar_text = $(gallery_infobar_id).html();
					$("#gallery_infobar_text").html(gallery_infobar_text);

					var gallery_infobar_link_id = "#gallery_infobar_link_holder" + current;
					var gallery_infobar_link = $(gallery_infobar_link_id).html();
					$("#gallery_infobar_button_a").attr('href', gallery_infobar_link);
					// console.log('gallery_infobar_id = ' + gallery_infobar_id);
					// console.log('gallery_infobar_text = ' + gallery_infobar_text);
					
					return false;
				});

				var n = $a.get(0);

				n.index = j;

				$('<li>').appendTo(jqul).append($a);

				if( el.opts.direction == 'diagonal'){
					pos.x = j * el.opts.width;
					pos.y = j * el.opts.height;
				}
				else if( el.opts.direction == 'horizontal'){
					pos.x = j * el.opts.width;
				}
				else if( el.opts.direction == 'vertical'){
					pos.y = j * el.opts.height;
				};

				var jqchild = $(this).css({height:el.opts.height,width:el.opts.width,position:'absolute',left:pos.x, top:pos.y});

				var jqimg = jqchild.find('img').hide()
				
				if(jqimg.parent().is('a')){
					var p = jqimg.parent();
					jqimg.get(0).linkHref = p.attr('href');
					p.remove();
					jqimg.appendTo(jqchild);
				};

				/*jqimg.click(function(){
					var next = n.index + 1;
					if((n.index + 1) == el.totalChildren ){
						el.pagination.find('[href$=#0]').click();
					}
					else{
						el.pagination.find('[href$=#'+next+']').click();
					}
				});*/

				var $loader = $('<div class="jqGSLoader">').appendTo(jqchild);
				var $titleHolder = $('<div class="jqGSTitle">').appendTo(jqchild).css({opacity:el.opts.titleOpacity}).hide();
				var image = new Image();
				image.onload = function(){
					image.onload = null;
					$loader.fadeOut();
					jqimg.css({marginLeft:-image.width*.5,marginTop:-image.height*.5,position:'absolute',left:'50%',top:'50%'}).fadeIn();
					var alt = jqimg.attr('alt');
					if(typeof alt != 'undefined'){
						$titleHolder.text(alt).fadeIn();
					}
				};
				image.src = jqimg.attr('src');
			});

			var gallery_infobar_text = $("#gallery_infobar_text_holder" + current).html();
			$("#gallery_infobar_text").html(gallery_infobar_text);

			var gallery_infobar_link_id = "#gallery_infobar_link_holder" + current;
			var gallery_infobar_link = $(gallery_infobar_link_id).html();
			$("#gallery_infobar_button_a").attr('href', gallery_infobar_link);
			// console.log('gallery_infobar_text = ' + gallery_infobar_text);
			
			// $(jqul).prepend('<img id="gallery_carousel_prev" src="images/global/navigator/prev.png" />');
			// $(jqul).append('<img id="gallery_carousel_next" src="images/global/navigator/next.png" />');
			$(el.pagination).prepend('<img id="gallery_carousel_prev" src="images/global/navigator/prev.gif" />');
			$(el.pagination).append('<img id="gallery_carousel_next" src="images/global/navigator/next.gif" />');
					// console.log("test here");
			$("#gallery_carousel_next").click(function(){
				if (stop_safety == 0) stop_cycle();
				var next = current + 1;
				if(next <= (el.totalChildren - 1) ){
					el.pagination.find('[href$=#'+next+']').click();
					current = next;
				} else {
					$.fn.pause = function(duration) {
					    $(this).animate({ dummy: 1 }, duration);
					    return this;
					};
/*
					$("#gallery_carousel").fadeOut("slow", function () {
						el.pagination.find('[href$=#0]').click();
						$("#gallery_carousel").pause(1000).fadeIn("slow");
				    });
					*/
					stop_cycle();
					var offpage_r = $("#gallery_carousel").parent().width() + $("#gallery_carousel").width();
					$("#gallery_carousel").animate({right: offpage_r}, 400, null, function(){
						var offpage_l =  0 - $("#gallery_carousel").parent().width();
						$("#gallery_carousel").animate({right: offpage_l}, 0);
						$("#gallery_carousel").animate({right: 0}, 400, null, function(){
							el.pagination.find('[href$=#0]').click();
							cycle = setInterval('do_cycle()', 5000);	
						});
					});
					//

					// $("#gallery_carousel").css('opacity','.5');
					// $("#gallery_carousel").css('right','0px');
					// var timeout = setTimeout($("#gallery_carousel").css('opacity','1'),1000);
					current = 0;
				}
			});
			$("#gallery_carousel_prev").click(function(){
				if (stop_safety == 1) {
					stop_safety = 0;
				} else {
					stop_cycle();
				}
				var prev = current - 1;
				if(prev >= 0 ){
					el.pagination.find('[href$=#'+prev+']').click();
					current = prev;
				} else {
					el.pagination.find('[href$=#'+ (el.totalChildren - 1) +']').click();
					current = el.totalChildren - 1;
				}
			});
			
					
			
		}); // end : this.each(function()
		
	};  // end : $.fn.jqGalScroll
	jqGalScroll = {
		ease: null,
		speed:0,
		height: 500,
		width: 500,
		titleOpacity : .60,
		direction : 'horizontal' // vertical horizontal diagonal
	};
})(jQuery);