// Re-initialize cluetips on page updates
function initCluetips() {
	// Cluetip for image captions
	$('a.thumb').cluetip({cluetipClass: 'jtip', width: 200, splitTitle: '|', arrows: true});
	$('a.cluetip').cluetip({cluetipClass: 'jtip', width: 225, local: true, arrows: true});
}

$(document).ready(function() { 
	
	// Add hover state to lightboxes
	$('#lightbox li').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); } );
	
	// Add : to labels
	$('fieldset ol li label').append(' : ');
	$('fieldset ol li:last-child').css({border:0});
	
	// Autocomplete searches
	$("#query").autocomplete("/scripts/search_autocomplete.php", {
		delay: 50,
		minChars: 2,
		cacheLength: 500,
		onItemSelect: function(){ $('#search_form form').submit(); },
		maxItemsToShow: 10
	});

	// Send focus to search box when clicking search navigation item
	$('#search a').click(function(){
		$('#query').focus();
		return false;
	});
		
	// Add hover state to thumbnails
	$('li.thumb').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); } );
	
	// Contact form AJAX options
	var contactOptions = {
		target : '#output',
		beforeSubmit : function(data){
			$('#contactSend').attr('disabled','true');
			$('img.loading').show();
		},
		success: function(data) {
			$('#output').fadeIn();
			$('img.loading').hide();
			$('#contactSend').removeAttr('disabled');
		}
	};
		
	// set up form validation and AJAX call
	$("form#contactform").validate({
		rules: {
			contactName: "required",
			contactEmail: {
				required: true,
				email: true
			}
		},
		messages: {
			contactName: "Please enter your name.",
			contactEmail: "Please enter a valid email."
		},
		submitHandler: function(form) {
			$('form#contactform').ajaxSubmit(contactOptions);
		}
	});

	// Per Page prefrences
	$('.per_page_pref').live('click', function(){
		var $this = $(this);
		var href = $this.attr('href');	
		$.ajax({
			type: 'POST',
			url: '/action/perpage/'+$this.attr('rel'),
			data: 'ajax=1',
			success: function(msg) {
				$('#content').load(href+' #content');
			}
		});
		return false;
	});
	
	// Cycle home images
	$('#homephotos').cycle({
		delay: 4000,
		speed: 2000,
		pause: true
	});
	
	// Add pngFix 
	$('img[src$=png]').pngFix();
		
	// Lightbox modifications
	$('.lbxAction').live('click',function(){
		var $this = $(this);
		var href = $this.attr('href');
		$.ajax({
			type: 'POST',
			url: href,
			data: 'ajax=1',
			success: function(msg){
				$('.lbx_count').text(msg);
				$('#lbx_cart').animate({backgroundColor: '#AA0000'}, 1250).animate({backgroundColor: '#ffffff'}, 750);
				
				// Adding from search
				if ( $this.is('.albx') ) {
					$this.removeClass('albx').addClass('rlbx')
							.attr('rel','#remLightboxInfo')
							.attr('title','Remove image from cart')
							.html('<img src="/images/icons/minus.png" alt="Remove from cart" />')
							.attr('href', href.replace('albx','rlbx'));
				
				// Removing from search
				} else if ( $this.is('.rlbx') ){
					$this.removeClass('rlbx').addClass('albx')
							.html('<img src="/images/icons/plus.png" alt="Add to cart" />')
							.attr('title','Add this image to your cart')
							.attr('rel','#addLightboxInfo')
							.attr('href', href.replace('rlbx','albx'));				
				
				// Removing from lightbox
				} else if ( $this.is('.rlbxuser') ){
					$this.parent().parent().parent().remove();

				// Adding from photo
				} else if ( $this.is('.addLightbox') ){
					$this.removeClass('addLightbox').addClass('remLightbox')
							.attr('rel','#remLightboxInfo')
							.attr('title','Remove from cart')
							.html('Remove image from cart')
							.attr('href', href.replace('albx','rlbx'));
				
				// Removing from photo
				} else if ( $this.is('.remLightbox') ){
					$this.removeClass('remLightbox').addClass('addLightbox')
							.html('Add this image to your cart')
							.attr('title','Add this image to your cart')
							.attr('rel','#addLightboxInfo')
							.attr('href', href.replace('rlbx','albx'));								
				}
				initCluetips();		
			}
		});
		return false;
	});
	
	initCluetips();
	
	// Google Analytics Tracking
	$('a').click(function () {
		var thisel = $(this);
		try	{
			_gaq.push(['_trackEvent', thisel.parents('[id!=""]:first').get(0).id, 'clicked', (thisel.text() || thisel.children('img:first').attr('alt'))]);
		} catch (err) {}
	});
	
	
});