jQuery(document).ready(function(){
	//Back to top slider
    jQuery('a[href=#totop]').click(function(){
        jQuery('html, body').animate({scrollTop:0}, 600);
        return false;
    });
	// Product Slider
    jQuery('#featured-products').jcarousel();

	// FancyBox jQuery
	jQuery("a.group").fancybox({ 'zoomSpeedIn': 300, 'zoomSpeedOut': 300, 'overlayShow': true });

	// Slider Homepage
	jQuery("#slider").easySlider({
		auto: true,
		continuous: true,
		numeric: true,
		pause: 5000//default to 2000,
	});

	//make our Lure Insert and Skirt popups
	//this is for the lure
	jQuery("<a href='#' class='overlay-link overlay-swatch' rel='.color-swatch-option-wrapper'><span>Open</span></a>")
		.appendTo(jQuery(".color-swatch-option-wrapper").parent().prev());

	//this is for the skirt
	//remove imageoptions with no images
	removeNonValidDivs();

	jQuery("<a href='#' class='overlay-link overlay-imageoptions' rel='.imageoptions'><span>Open</span></a>")
		.appendTo(jQuery(".imageoptions").siblings("dt").first());

	//enable the overlay
	jQuery(".overlay-link").overlay({closeOnClick: true});

	//setup auto close features
	jQuery(".imageoptions").click(function() {
		jQuery(".overlay-imageoptions").data('overlay').close();
	});

	jQuery(".color-swatch-option-wrapper").click(function() {
		jQuery(".overlay-swatch").data('overlay').close();
	});

	//dealing with youtube overlays
	jQuery("div.youtube-video a[rel]").overlay({
		onBeforeLoad:function(e) {
			var t = e.originalTarget || e.srcElement;
			var youTubeURL = jQuery(t).parent().attr('href');
			//jQuery(".youtube-overlay").html('<iframe title="YouTube video player" width="640" height="390" src="'"+youTubeURL+"'" frameborder="0" allowfullscreen></iframe>');
			//jQuery(".youtube-overlay").html('Testing: '+youTubeURL);
			jQuery("#youtubeFrame").attr("src",youTubeURL);

			e.stopImmediatePropagation();
		}
	});

	jQuery(document).click(function() {
		jQuery("div.youtube-video a[rel]").each(function() {
			jQuery(this).overlay().close();
		});
	});
});

function removeNonValidDivs() {
	console.debug("removeNonValidDivs");
	jQuery(".imageoptions").each(function(i) {
		var io = this;
		var remove = true;
		console.debug(this);
		jQuery(io).find("img").each(function(x) {
			console.debug(jQuery(this).attr("src"));
			if(jQuery(this).attr("src")!=null)
				remove = false;
		});

		console.debug("remove: "+remove);

		if(remove)
			jQuery(io).remove();
	});
}

(function($) {
    var uid = 0;
    $.getUID = function() {
        uid++;
        return 'jQ-uid-'+uid;
    };
    $.fn.getUID = function() {
        if(!this.length) {
            return 0;
        }
        var fst = this.first(), id = fst.attr('id');
        if(!id) {
            id = $.getUID();
            fst.attr('id', id);
        }
        return id;
    }
})(jQuery);

