// JAVASCRIPT STYLE SHEET
$('<link rel="stylesheet" href="http://www.bulletcreative.com/wp-content/themes/bullet-creative/javascript.css" type="text/css" media="screen">').appendTo('head');



// INITALISE SOME BIT N BOBS
var previousitemno = 1; //initalise the previousitemno to 1 - the first spread
var flashon = 0;



// Carousel setup

	/*stepcarousel.setup({ 
		galleryid: 'mygallery', //id of carousel DIV
		beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
		panelclass: 'panel', //class of panel DIVs each holding content
		autostep: {enable:false, moveby:1, pause:3000},
		panelbehavior: {speed:500, wraparound:true, persist:false},
		defaultbuttons: {enable: false, moveby: 1, leftnav: ['http://i34.tinypic.com/317e0s5.gif', -5, 80], rightnav: ['http://i38.tinypic.com/33o7di8.gif', -20, 80]},
		statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
		contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
	});*/



// THUMBCLICKER
	function thumbclick (itemno) {
		// for the thumb clicks
		
		//$('#th-'+previousitemno).removeClass('pagethumbdpsover');
		//$('#th-'+previousitemno).addClass('pagethumbdps'); 
		stepcarousel.stepTo('mygallery', itemno);
		//$('#th-'+itemno).removeClass('pagethumbdps');
		//$('#th-'+itemno).addClass('pagethumbdpsover');
		previousitemno = itemno;
	
	}
	
	// Previous next arrow events
	function prevnextthumb(goto) {
		// for the previous next
		//alert("previous: "+previousitemno+" : "+totalitems+" : "+goto);
		/*
		$('#th-'+previousitemno).removeClass('pagethumbdpsover');
		$('#th-'+previousitemno).addClass('pagethumbdps');
		*/
		
		if (previousitemno == totalitems-1 && goto == 1) {//at end
			previousitemno = 0;
		} else if (previousitemno == 1 && goto == -1) {//at begining
			previousitemno = totalitems;
		}
		
		
		 
		stepcarousel.stepBy('mygallery', goto);
		//$('#th-'+(previousitemno+goto)).removeClass('pagethumbdps');
		//$('#th-'+(previousitemno+goto)).addClass('pagethumbdpsover');
		previousitemno = previousitemno + goto;
	}



// VERTICAL SCROLLER */
	function makeScrollable(wrapper, scrollable){
		// Get jQuery elements
		var wrapper = $(wrapper), scrollable = $(scrollable);
		
		// Hide images until they are not loaded
		scrollable.hide();
		var loading = $('<div class="loading">Loading...</div>').appendTo(wrapper);
		
		// Set function that will check if all images are loaded
		var interval = setInterval(function(){
			var images = scrollable.find('img');
			var completed = 0;
			
			// Counts number of images that are succesfully loaded
			images.each(function(){
				if (this.complete) completed++;	
			});
			
			if (completed == images.length){
				clearInterval(interval);
				// Timeout added to fix problem with Chrome
				setTimeout(function(){
					
					loading.hide();
					// Remove scrollbars	
					wrapper.css({overflow: 'hidden'});						
					
					scrollable.slideDown('slow', function(){
						enable();	
					});					
				}, 1000);	
			}
		}, 100);
		
		function enable(){
			// height of area at the top at bottom, that don't respond to mousemove
			var inactiveMargin = 99;					
			// Cache for performance
			var wrapperWidth = wrapper.width();
			var wrapperHeight = wrapper.height();
			// Using outer height to include padding too
			var scrollableHeight = scrollable.outerHeight() + 2*inactiveMargin;
			// Do not cache wrapperOffset, because it can change when user resizes window
			// We could use onresize event, but it's just not worth doing that 
			// var wrapperOffset = wrapper.offset();
			
			// Create a invisible tooltip
			var tooltip = $('<div class="sc_menu_tooltip"></div>')
				.css('opacity', 0)
				.appendTo(wrapper);
		
			// Save menu titles
			scrollable.find('a').each(function(){				
				$(this).data('tooltipText', this.title);				
			});
			
			// Remove default tooltip
			scrollable.find('a').removeAttr('title');		
			// Remove default tooltip in IE
			scrollable.find('img').removeAttr('alt');	
			
			var lastTarget;
			//When user move mouse over menu			
			wrapper.mousemove(function(e){
				// Save target
				lastTarget = e.target;
	
				var wrapperOffset = wrapper.offset();
			
				var tooltipLeft = e.pageX - wrapperOffset.left;
				// Do not let tooltip to move out of menu.
				// Because overflow is set to hidden, we will not be able too see it 
				tooltipLeft = Math.min(tooltipLeft, wrapperWidth - 75); //tooltip.outerWidth());
				
				var tooltipTop = e.pageY - wrapperOffset.top + wrapper.scrollTop() - 40;
				// Move tooltip under the mouse when we are in the higher part of the menu
				if (e.pageY - wrapperOffset.top < wrapperHeight/2){
					tooltipTop += 80;
				}				
				tooltip.css({top: tooltipTop, left: tooltipLeft});				
				
				// Scroll menu
				var top = (e.pageY -  wrapperOffset.top) * (scrollableHeight - wrapperHeight) / wrapperHeight - inactiveMargin;
				if (top < 0){
					top = 0;
				}			
				wrapper.scrollTop(top);
			});
			
			// Setting interval helps solving perfomance problems in IE
			var interval = setInterval(function(){
				if (!lastTarget) return;	
											
				var currentText = tooltip.text();
				
				if (lastTarget.nodeName == 'IMG'){					
					// We've attached data to a link, not image
					var newText = $(lastTarget).parent().data('tooltipText');
	
					// Show tooltip with the new text
					if (currentText != newText) {
						tooltip
							.stop(true)
							.css('opacity', 0)	
							.text(newText)
							.animate({opacity: 1}, 1000);
					}					
				}
			}, 200);
			
			// Hide tooltip when leaving menu
			wrapper.mouseleave(function(){
				lastTarget = false;
				tooltip.stop(true).css('opacity', 0).text('');
			});			
			
			/*
			//Usage of hover event resulted in performance problems
			scrollable.find('a').hover(function(){
				tooltip
					.stop()
					.css('opacity', 0)
					.text($(this).data('tooltipText'))
					.animate({opacity: 1}, 1000);
		
			}, function(){
				tooltip
					.stop()
					.animate({opacity: 0}, 300);
			});
			*/			
		}
	}
		
	$(function(){	
		makeScrollable("div.sc_menu_wrapper", "div.sc_menu");
	});



// STEP CAROUSEL
	stepcarousel.setup({
		galleryid: 'mygallery', //id of carousel DIV
		beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
		panelclass: 'panel', //class of panel DIVs each holding content
		autostep: {enable:false, moveby:1, pause:3000},
		panelbehavior: {speed:500, wraparound:true, persist:false},
		defaultbuttons: {enable: false, moveby: 1, leftnav: ['http://i34.tinypic.com/317e0s5.gif', -5, 80], rightnav: ['http://i38.tinypic.com/33o7di8.gif', -20, 80]},
		statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
		contenttype: ['inline'] //content setting ['inline'] or ['ajax', 'path_to_external_file']
	})
	
	
	/*galleryid: 'mygallery', //id of carousel DIV
		beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
		panelclass: 'panel', //class of panel DIVs each holding content
		autostep: {enable:false, moveby:1, pause:3000},
		panelbehavior: {speed:500, wraparound:true, persist:false},
		defaultbuttons: {enable: false, moveby: 1, leftnav: ['http://i34.tinypic.com/317e0s5.gif', -5, 80], rightnav: ['http://i38.tinypic.com/33o7di8.gif', -20, 80]},
		statusvars: ['statusA', 'statusB', 'statusC'], //register 3 variables that contain current panel (start), current panel (last), and total panels
		contenttype: ['inline'] //content setting ['inline'] or ['external', 'path_to_external_file']
	*/
