$(function(){
	$('.jsshow').show();
	
	$.blockUI.defaults = {
	    // fadeOut time in millis; set to 0 to disable fadeout on unblock 
	    fadeOut:0, 
	    // suppresses the use of overlay styles on FF/Linux (due to significant performance issues with opacity) 
	    applyPlatformOpacityRules: true 
	}; 
	
	
	// Show labels over form fields on login form
	$('label.overlabel').overlabel();
	
	// Hide the comment form and show it when the link is clicked
	$('#commentForm').hide();
	$('#commentAdd').click( function() {
		$('#commentForm:hidden').show();
		$(this).parent().hide();
		$('#commentTextarea').focus();
		return false;
	});
	
	
	// Set up toolbox
	var _toolbox = $('#toolbox');
	
	if (_toolbox.length) {
		$('h2',_toolbox).addClass('down').click(function(){
			toggleToolsMenu($(this).next());
		});
		
		// Check cookies - if toolbox has been hidden, hide it!
		var cookies = document.cookie;
		var cookiename = 'showtoolbox';
		if (cookies.indexOf(cookiename) != -1){
			var startpos = cookies.indexOf(cookiename)+cookiename.length+1;
			var endpos = cookies.indexOf(";",startpos);
			if (endpos == -1) endpos = cookies.length;
			if (unescape(cookies.substring(startpos,endpos)) == '0') {
				$('div',_toolbox).hide().prev().removeClass('down').addClass('up');
			}
		}
		
		// Take any select fields and turn them into list of links
		var _select = $('select',_toolbox);
		_select.each(function(){
			var _this = $(this);
			// Create top link which drops down the list
			var _toplink = $('<a href="">'+_this.children().eq(0).text()+'</a>')
				.attr('class',_this.attr('class'))
				.click(function(){
					$(this).not('.btnDisabled').next().toggle();
					return false;
				});
			// Make a hidden field to contain the selected value
			var _hidden = $('<input type="hidden" name="'+_this.attr('name')+'" value="" />');
			// Create list of option links
			var _links = $('<ul></ul>').hide();
			for (var i = 1;i<_this.children().length;i++) {
				var _opt = _this.children().eq(i);
				$('<a href="">'+_opt.text()+'</a>')
					.attr('class',_this.attr('class'))
					.data('value',_opt.attr('value'))
					.click(function(){
						$(this).not('.btnDisabled').parent().parent().next().val($(this).data('value')).parents('form').submit();
						return false;
					})
					.appendTo(_links)
					.wrap('<li></li>');
			}
			// Replace select field with new links
			_this.after(_hidden).after(_links).after(_toplink).remove();
		});
	}
	
	function toggleToolsMenu(menuObject) {
		var date = new Date();
		date.setTime(date.getTime()+(7*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		
		if (menuObject.is(':visible')) {
			menuObject.slideUp(100).prev().removeClass('down').addClass('up');
			document.cookie = 'showtoolbox=0;'+expires+'; path=/';
		} else {
			menuObject.slideDown(100).prev().removeClass('up').addClass('down');
			document.cookie = 'showtoolbox=1;'+expires+'; path=/';
		}
	}
	
	
	// Set up toolbox actions
	
	// Hide the Add to Album button and make it submit automatically
	$('.addToAlbumBtn',_toolbox).hide();
	$('#addToAlbum',_toolbox).change( function() {
		$(this).parents('form').submit();
	});
	
	$('a.deleteBtn',_toolbox).each(function(){
		$(this).data('deleteMessage',$(this).attr('title')).removeAttr('title');
	});
		
	// Convert toolbox buttons to <a> links
	$('input.formLink',_toolbox).each( function() {
		var _input = $(this);
		var _link = $('<a></a>')
			.attr('href',_input.parents('form').attr('action'))
			.attr('class',_input.attr('class'))
			.addClass('wasButton')
			.text(_input.attr('value'));
		if (_input.hasClass('deleteBtn')) _link.data('deleteMessage',_input.attr('title'));
		_input.after(_link).remove();
	});
	
	
	// Set up action for <a> buttons
	$('a.formLink',_toolbox).click( function(event) {
		var _button = $(this);
		if (_button.hasClass('btnDisabled')) {
			return false;
		} else {
			var _formAction = _button.text();
			if (_button.hasClass('deleteBtn')) {
				event.preventDefault();
				var _dialog = $('<div id="deleteDialog">'+_button.data('deleteMessage').replace('  ','<br/><br/>')+'<br/><br/></div>');
				$('<input type="submit" value="Yes, continue" />').click(function(){
					if (_button.hasClass('wasButton')) {
						_button.after('<input type="hidden" name="Action" value="' + _formAction + '" />')
							.parents('form').submit();
					} else {
						location.href = _button.attr('href');
					}
				}).appendTo(_dialog);
				$('<input type="submit" value="Cancel" class="cancel" />').click(function(){
					$.unblockUI();
				}).appendTo(_dialog);
				
				$.blockUI({
					message: _dialog,
					baseZ: 1000
				});
				//var doit = confirm( _button.data('deleteMessage').replace('  ','\n\n') );
				//if (!doit) return false;
			} else {
				_button.after('<input type="hidden" name="Action" value="' + _formAction + '" />')
					.parents('form').submit();
			}
		}
	});
	
	
	// Set up smooth scrolling to comments
	$('.animScroll').click(function(event) {
		event.preventDefault();
		$( $(this).attr("href") ).scrollTo();
	});
	
	
	
	
	// Hide advanced search options and add link to show them
	$('#advancedSearch.hideme').before('<p><a href="#" id="advancedSearchLink">More search options...</a></p>').hide();
	$('#advancedSearchLink').click(function(event) {
		event.preventDefault();
		$('#advancedSearch').show();
		$(this).parent().hide();
	});
	
	$('#productPopup').click(function(event) {
		event.preventDefault();
		var href = $(this).attr('href');
		new_window = window.open(href,'popup','width=700,height=500,toolbar=no,scrollbars=no,menubar=no');
	});
	$('.productEdit').click(function(event) {
		event.preventDefault();
		var href = $(this).attr('href');
		new_window = window.open(href,'popup','width=700,height=500,toolbar=no,scrollbars=no,menubar=no');
	});
	// If doing file upload, show a waiting dialog
	$('.uploadForm').submit(function() {
		var files = 0;
		$(':file').each(function() {
			files += $(this).val().length;
		});
		
		if ( files > 0 ) {
			$.blockUI({
				message: '<p>Please wait while photos are uploaded...</p>',
				baseZ: 1000
			});
		}
	});

/*
	// Call Google Analytics scripts
	$.getScript("/j/urchin.js", function(){
		setTimeout('_uacct = "UA-259674-4";urchinTracker();',3000);
	});
	//$.gaTracker('UA-259674-4');
*/
	
	
	if ($.browser.msie) {
		$('a').focus(function() {
			this.hideFocus = true; // hide dotted outline from links in IE
		});
	}
	
});

jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    return this.each(function() {
      var targetOffset = $(this).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});

function img404(e,size) {
	e.src='/i/f/no_user_pic_'+size+'.gif';
}

