$(function () {
	$('.dropdown').each(function () {
		$(this).parent().eq(0).hoverIntent({
			timeout: 100,
			over: function () {
				var current = $('.dropdown:eq(0)', this);
				current.slideDown(100);
				$(this).find('a').addClass('active');
			},
			out: function () {
				var current = $('.dropdown:eq(0)', this);
				current.fadeOut(200);
				$(this).find('a').removeClass('active');
			}
		});
	});
});

$(function () {
	// check for incompatible browsers
	var ver = getInternetExplorerVersion();
	if (ver > -1 && ver < 7.0) 
		$('#browserDetect').show();
});

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
	var rv = -1; // Return value assumes failure.
	if (navigator.appName == 'Microsoft Internet Explorer') {
		var ua = navigator.userAgent;
		var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat(RegExp.$1);
	}
	return rv;
}

/* quicksearch tabs */
$(function() {
	if($("#quicksearchtabs").length) { // only for the quick search tabs on the homepage
		var activetab = $.cookie("qstab");	
		
		$('#quicksearchtabs a').each(function() {
			$(this).click(function(e) {
				e.preventDefault();								   
				var current_tab = $(this).attr('id');
				
				$('#quicksearchtabs a').each(function() {
					if($(this).attr('id') != current_tab) {
						$(this).attr('class', $(this).attr('id'));
					} else { 
						$(this).attr('class', $(this).attr('id') + '-active');
					}
				});
				
				$('.qs-content').hide();
				$('#'+current_tab+'-content').show();
				
				// set cookie 
				$.cookie("qstab", current_tab, { expires: 1, path: "/" });			
			});
		});
		
		// set active tab based on cookie 
		if(activetab && activetab != 'qs-sell') {
			// remove active classes from default tab
			$('#qs-sell-content').hide();
			$('#qs-sell').attr('class','qs-sell');
			
			// add active classes to selected tab
			$('#'+activetab+'-content').show();		
			$('#'+activetab).attr('class', $('#'+activetab).attr('id') + '-active');		
		}
	}
});


/* jcarousel and gallery function */
function mycarousel_itemLoadCallback(carousel, state)
{
	// Check if the requested items already exist
	if (carousel.has(carousel.first, carousel.last)) {
		return;
	}

	jQuery.get(
		'/search/inc-gallery.cfm?xtype=' + $("#xtype").val() + '&' + $('#ltype').val() + '=' + $('#lid').val(),
		{
			first: carousel.first,
			last: carousel.last
		},
		function(xml) {
			mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
		},
		'xml'
	);
};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
	// Set the size of the carousel
	carousel.size(parseInt(jQuery('total', xml).text()));
	jQuery('image', xml).each(function(i) {
		carousel.add(first + i, mycarousel_getItemHTML(jQuery(this).text(),i)); // pass i to create a unique ID per image
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({
		opacity: 0.60,
		show_title: false,
		theme: 'light_square',
		overlay_gallery: false
	});			
};

function mycarousel_getItemHTML(url,index)
{
	var fixedSrc = url.replace('custom/','custom/t_');
	fixedSrc = fixedSrc.replace('img/','img/th/');
	return '<a href="' + url + '" rel="prettyPhoto[gal]" target="_blank"><img id="tmb' + index + '" src="' + fixedSrc + '" width="80" height="60" alt="" onmouseover="loadImage(\'' + url + '\',this.id);" /></a>';
};

function loadImage(url,id) {
	$(".jcarousel-list").children().removeClass('jcarousel-item-selected');
	$("#"+id).parent().addClass('jcarousel-item-selected');
	if($("#gallery_selected").attr("src") != url) {
		$("#gallery_selected").fadeOut('fast',function() {
			$("#gallery_selected").attr("src",url);
			$("#gallery_selected").fadeIn('fast');
		});
	}
};

// homepage image rotator
function rotateImages() {				
	$.ajax({
	  type: "POST",
	  url: "/_cfc/utility.cfc?method=rotateImages&returnformat=json&currentIDList=" + _currentIDList,
	  success: function (res) {
		$('#galimgbox0').css('background-image','url("/images/gallery/'+res.DATA[0][2]+'")');
		$('#galimgbox1').css('background-image','url("/images/gallery/'+res.DATA[1][2]+'")');	
		$('#galimgbox2').css('background-image','url("/images/gallery/'+res.DATA[2][2]+'")');	

		$.each(res.DATA, function(i,item) {
				setTimeout(function() {
				$('#title'+i).slideToggle(400, function() {
					$('#galimgbox'+i+' div').text(item[1]);									   
				});
				$('#galimg'+i).fadeOut(600, function() {
					$('#galimgbox'+i+' img').attr('src','/images/gallery/'+item[2]).show();
				});
				$('#title'+i).slideToggle(400);		
			},i*3000);
		}); 
		/*
		$('#title0').slideToggle(400, function() {
			$('#galimgbox0 div').text(res.DATA[0][1]);									   
		});
		$('#galimg0').fadeOut(600, function() {
			$('#galimgbox0 img').attr('src','/images/gallery/'+res.DATA[0][2]).fadeIn(100);
		});
		$('#title0').slideToggle(400);
		
		$('#title1').slideToggle(400, function() {
			$('#galimgbox1 div').text(res.DATA[1][1]);							   
		});
		$('#galimg1').fadeOut(600, function() {
			$('#galimgbox1 img').attr('src','/images/gallery/'+res.DATA[1][2]).fadeIn(100);
			
		});
		$('#title1').slideToggle(400);
		
		$('#title2').slideToggle(400, function() {
			$('#galimgbox2 div').text(res.DATA[2][1]);								   
		});
		$('#galimg2').fadeOut(600, function() {
			$('#galimgbox2 img').attr('src','/images/gallery/'+res.DATA[2][2]).fadeIn(100);
			
		});
		$('#title2').slideToggle(400);	
		*/

		/*
		$('#galimg1').fadeOut(600, function() {
			$('#galimgbox1').html('<img id="galimg1" src="/images/gallery/'+data.DATA[1][2]+'" alt="" /><div class="title"><span class="galtitle">'+data.DATA[1][1]+'</span></div>').fadeIn(800);
		});
		
		$('#galimg2').fadeOut(600, function() {
			$('#galimgbox2').html('<img id="galimg2" src="/images/gallery/'+data.DATA[2][2]+'" alt="" /><div class="title"><span class="galtitle">'+data.DATA[2][1]+'</span></div>').fadeIn(800);
		});
		
		$('.title').slideToggle(600);*/
		_currentIDList = res.DATA[0][0] + "," + res.DATA[1][0] + "," + res.DATA[2][0];
	  },
	  dataType: "json",
	  cache: false
	});	
}	

function closeWindow(el) {
	$('#'+el).hide();	
}

(function($) {
	/**
	 * attaches a character counter to each textarea element in the jQuery object
	 * usage: $("#myTextArea").charCounter(max, settings);
	 */
	
	$.fn.charCounter = function (max, settings) {
		max = max || 100;
		settings = $.extend({
			container: "<span></span>",
			classname: "charcounter",
			format: "(%1 characters remaining)",
			pulse: true,
			delay: 0
		}, settings);
		var p, timeout;
		
		function count(el, container) {
			el = $(el);
			if (el.val().length > max) {
			    el.val(el.val().substring(0, max));
			    if (settings.pulse && !p) {
			    	pulse(container, true);
			    };
			};
			if (settings.delay > 0) {
				if (timeout) {
					window.clearTimeout(timeout);
				}
				timeout = window.setTimeout(function () {
					container.html(settings.format.replace(/%1/, (max - el.val().length)));
				}, settings.delay);
			} else {
				container.html(settings.format.replace(/%1/, (max - el.val().length)));
			}
		};
		
		function pulse(el, again) {
			if (p) {
				window.clearTimeout(p);
				p = null;
			};
			el.animate({ opacity: 0.1 }, 100, function () {
				$(this).animate({ opacity: 1.0 }, 100);
			});
			if (again) {
				p = window.setTimeout(function () { pulse(el) }, 200);
			};
		};
		
		return this.each(function () {
			var container = (!settings.container.match(/^<.+>$/)) 
				? $(settings.container) 
				: $(settings.container)
					.insertAfter(this)
					.addClass(settings.classname);
			$(this)
				.bind("keydown", function () { count(this, container); })
				.bind("keypress", function () { count(this, container); })
				.bind("keyup", function () { count(this, container); })
				.bind("focus", function () { count(this, container); })
				.bind("mouseover", function () { count(this, container); })
				.bind("mouseout", function () { count(this, container); })
				.bind("paste", function () { 
					var me = this;
					setTimeout(function () { count(me, container); }, 10);
				});
			if (this.addEventListener) {
				this.addEventListener('input', function () { count(this, container); }, false);
			};
			count(this, container);
		});
	};

})(jQuery);
