//redirect if original.html

/*var url = String(document.location);
var jpg = String(url.indexOf('.jpg'));
	
if (jpg==-1) {
	var html = url.indexOf('.html');
	var backslash = url.lastIndexOf('/');
	var gallery = url.substring(backslash+1,html);
	document.location.replace("#" + gallery + "/01.jpg");
}
*/

//dynamically pass image data to cart
function AddToCartHelper(data) {
	document.order.NAME.value = current.attr('title');
	document.order.ID_NUM.value = current.attr('src');
	AddToCart(data);
	//status();
	MM_swapImage('Add','','cart_add.gif',1);
	location.href = "managecart.html";
}

//status notifier instead of redirect to cart page
function status() {
	$('div.added').fadeOut(400,function(){ 
	//$('div.added').hide('fast'); 
	document.getElementById("added").style.display="none";
	document.getElementById("added2").style.display="none";
	//document.getElementById("added").style.fontSize="11px";
	document.getElementById("added").style.fontStyle="italic";
	document.getElementById("added").style.color="#FF0000";
	document.getElementById("added").innerHTML="Image added to shopping cart."
	//document.getElementById("added").style.display="none";
	//document.getElementById("added2").style.display="none";
	$(document.getElementById("added")).fadeIn(400,function(){$(document.getElementById("added")).fadeTo(400,1,function(){$(document.getElementById("added")).fadeOut(400,function(){fade()})})});
	//document.getElementById("added").style.display="block";
	//$('div.added').show('fast');
	});
}

function statusHelper() {
	alert("hello");
	//document.getElementById("added").style.display="none";
	//document.getElementById("added2").style.display="none";
	//document.getElementById("added").style.font="10px";
	//document.getElementById("added").style.fontStyle="italic";
	//document.getElementById("added").style.color="#FF0000";
	//document.getElementById("added").innerHTML="Image added to shopping cart."
	//document.getElementById("added").style.display="block";
	//$('div.added').show('fast');
	
}


//shopping cart rollover helper
function test() {
	if (document.getElementById("options").style.display=="none") {
		MM_swapImgRestore();
	}
}	

//add to cart options toggler
function fade() {
	if (document.getElementById("options").style.display=="none") {
		$('div.options').show('fast');
	}
	else {
		$('div.options').hide('fast');
	}
}

//arrow key functionality

document.onkeydown = nav;

function nav(e) {
	
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code==37) {
		$.galleria.prev();
		return false;
	}
	if (code==39) {
		$.galleria.next();
		return false;
	}
}

//correct tab display

var current;

function load() {
	
	var url = String(current.attr('src'));
	var backslash = url.lastIndexOf('/');
	var number = (current.attr('src').substr(backslash+1,2))*1;
	if(number<13) {
		TabbedPanels1.showPanel(0);
	}
	else if(number<25) {
		TabbedPanels1.showPanel(1);
	}
	else if(number<37) {
		TabbedPanels1.showPanel(2);
	}
	else if(number<49) {
		TabbedPanels1.showPanel(3);
	}
	else if(number<61) {
		TabbedPanels1.showPanel(4);
	}
	else if(number<73) {
		TabbedPanels1.showPanel(5);
	}
}

jQuery(function($) {
		
		$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
		
		$('ul.gallery_demo').galleria({
			history   : true, // activates the history object for bookmarking, back-button etc.
			clickNext : true, // helper for making the image clickable
			insert    : '#main_image', // the containing selector for our main image
			onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
				
				// fade in the image & caption
				//if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				current = thumb;
				image.css('display','none').fadeIn(400);
				//}
				if (!$.browser.msie) {
					caption.css('display','none').fadeIn(400);
				}
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(400,0.6);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title',thumb.attr('title'));
				
				//activate correct tab
				load();
				var addy = String(current.attr('src'));
				var backsl = addy.lastIndexOf('/');
				var numb = (addy.substr(backsl+1,2))*1;
				if (forSale(numb)) {
					if (document.getElementById('options').style.display=="none") {
						document.getElementById('options').style.display="none";
						document.getElementById('forsale').style.display="block";
						MM_swapImage('Add','','cart_add.gif',1);
					}
					
					
					//update sizes select box
					var currentGallery = String(current.attr('src'));
					var backslash = currentGallery.lastIndexOf('/');
					var galleryName = currentGallery.substr(0,backslash);
					var newfuntion = galleryName + "_updateSizes";
					window[newfuntion]();
				}
				else {
					document.getElementById('forsale').style.display="none";
					document.getElementById('options').style.display="none";
				}

			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.6';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(400);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.6); } // don't fade out if the parent is active
				)
			}
		});
});
