$(document).ready(function(){


	$("a[rel=example_group]").fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});
			
	var version;
	var browser;
	$.each(jQuery.browser, function(i, val) {
		if(i=='version')
			version = val;
		if(i=='msie' && val==true)
			browser = i;
	});
	var current;
	$('.hover').hover(function()
	{
		current = $(this);
		var padding = 40;
		var p = $(this);
		//var pos = p.offset();
		//var p_width = p.width()-padding;

		//var top = pos.top+p.height();
		//var left = pos.left-padding;
		var top = p.height();
		var left = $(this).offset()-1;
		left++;

		if(id = this.id)
			var sub_id = '#ul_'+id;
		
		//$(sub_id).css({'margin-left':left+'px','margin-top':top+'px'});
		$(sub_id).show();
		current.addClass('mock_hover');
	},function()
	{
		$('.sub_nav').hide();
		current.removeClass('mock_hover');		
	});	
	
	$('.sub_nav').hover(function()
	{

		current.addClass('mock_hover');
	});

	if($('.gallery_demo_unstyled').length)
	{
	$('.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
				image.css('display','none').fadeIn(1000);
				caption.css('display','none').fadeIn(1000);
				
				var img_height = image.height();
				if(img_height < 438)
				{
					var top_mar = (438-img_height)/2;
					image.css('margin-top',top_mar);
				}
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500,0.3);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title','Next image >>');
			},
			onThumb : function(thumb) { // thumbnail effects goes here
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				thumb.parents('li').css({overflow:'hidden'});
				
				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
				)
			}
		});	
	}	
	if ($("#slider1").length){
		featuredcontentslider.init({
					id: "slider1",  //id of main slider DIV
					contentsource: ["inline", ""],  //Valid values: ["inline", ""] or ["ajax", "path_to_file"]
					toc: "#increment",  //Valid values: "#increment", "markup", ["label1", "label2", etc]
					nextprev: ["Previous", "Next"],  //labels for "prev" and "next" links. Set to "" to hide.
					revealtype: "click", //Behavior of pagination links to reveal the slides: "click" or "mouseover"
					enablefade: [true, 0.2],  //[true/false, fadedegree]
					autorotate: [true, 4000],  //[true/false, pausetime]
					onChange: function(previndex, curindex){  //event handler fired whenever script changes slide
						//previndex holds index of last slide viewed b4 current (1=1st slide, 2nd=2nd etc)
						//curindex holds index of currently shown slide (1=1st slide, 2nd=2nd etc)
					}
		});
	}


	// var has_cufon=false;
	// if ($("h2").length){
		// Cufon.replace('h2', { fontFamily: 'dbold' });
		// has_cufon=true;
	// }
	// if ($("h1").length){
		// Cufon.replace('h1', { fontFamily: 'fgoth' });
		// has_cufon=true;
	// }
	// if (has_cufon)
		// Cufon.now();
	
	$('.bill').change(function()
	{
		value = $(this).attr('value');
		name = $(this).attr('name');
		ship_name = 'ship_'+name.substring(5);

		if($('#copy').is(':checked'))
		{
			if($('input[name="'+ship_name+'"]').attr('value')=='')
				$('input[name="'+ship_name+'"]').attr('value',value);
		}
	});
	
	$('.numbers_only').keydown(function(e)
	{
		key = e.keyCode;
		
		if(key==9)
		{
		}
		else if(key > 105 || key < 48 || (key > 57 && key < 96) || e.shiftKey)
			return false;
	});
	
	if($('.slideshow'))
	{
		$('.slideshow').cycle({
			fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});
	}
	//$('.img').corner();
	var container = $('div.sliderGallery');
	var ul = $('ul', container);
	
	var itemsWidth = ul.innerWidth() - container.outerWidth();

	$('.slider', container).slider({
		min: 0,
		value: 0,
		max: itemsWidth,
		handle: '.handle',
		stop: function (event, ui) {
			ul.animate({'left' : ui.value * -1}, 500);
		},
		slide: function (event, ui) {
			ul.css('left', ui.value * -1);
		}
	});
	
	$(function() {
		$('.dialog').dialog({
			autoOpen: false,
			show: 'blind',
			hide: 'explode'
		});
	});
	
	// attemp at a slider
	
	$(function() {
		//scrollpane parts
		var scrollPane = $('.scroll-pane');
		var scrollContent = $('.scroll-content');
		
		var sci = $('.scroll-content-item');
		sci.width('150px');
		width = (200)*(sci.length);
		
		scrollContent.width(width);
		//build slider
		var scrollbar = $(".scroll-bar").slider({
			slide:function(e, ui){
				if( scrollContent.width() > scrollPane.width() ){ scrollContent.css('margin-left', Math.round( ui.value / 150 * ( scrollPane.width() - scrollContent.width() )) + 'px'); }
				else { scrollContent.css('margin-left', 0); }
			}
		});
		
		//append icon to handle
		var handleHelper = scrollbar.find('.ui-slider-handle')
		.mousedown(function(){
			scrollbar.width( handleHelper.width() );
		})
		.mouseup(function(){
			scrollbar.width( '100%' );
		})
		.append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
		.wrap('<div class="ui-handle-helper-parent"></div>').parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css('overflow','hidden');
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar(){
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - (proportion * scrollPane.width());
			scrollbar.find('.ui-slider-handle').css({
				width: handleSize,
				'margin-left': -handleSize/2
			});
			handleHelper.width('').width( scrollbar.width() - handleSize);
		}
		
		//reset slider value based on scroll content position
		function resetValue(){
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
			var percentage = Math.round(leftVal / remainder * 150);
			scrollbar.slider("value", percentage);
		}
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent(){
				var showing = scrollContent.width() + parseInt( scrollContent.css('margin-left') );
				var gap = scrollPane.width() - showing;
				if(gap > 0){
					scrollContent.css('margin-left', parseInt( scrollContent.css('margin-left') ) + gap);
				}
		}
		
		//change handle position on window resize
		$(window)
		.resize(function(){
				resetValue();
				sizeScrollbar();
				reflowContent();
		});
		//init scrollbar size
		setTimeout(sizeScrollbar,10);//safari wants a timeout
	});

	$('#copy').click(function()
	{
		if($(this).is(':checked'))
		{
			$('input[name^="bill_"]').each(function()
			{
				var name = $(this).attr('name');
				var val = $(this).val();
				name = name.substring(5);
				$('input[name="ship_'+name+'"]').val(val);
			});
		}
	});
});
	


