$(document).ready(function(){

	// setup categories

	//$("#morecategories").attr("href", "javascript:void(0);")
	$("#morecategories, #catclose").click(function(){
		if ($("#categories").is(":hidden")) {
			$("#morecategories").html("Less");
			$("#categories").show("normal");
			pageTracker._trackEvent('Categories', 'Open', location.pathname);
		} else {
			$("#morecategories").html("More");
			$("#categories").hide("normal");
			pageTracker._trackEvent('Categories', 'Close', location.pathname);
		}
		return false;
	});

	// prepare share this

	$("a.socialbookmark").attr("href", "javascript:void(0);");
	$("a.socialbookmark").bind("click", function(e) {
		var id = $(this).attr("child");
		var sharethisid = "share-" + id;
		var sharethisdiv = $("#" + sharethisid);

		if ($(sharethisdiv).is(":hidden")) {
			if ($("#" + sharethisid + " > .drop").children().size() == 0) { // load only once
				$("#drop" + id).load(stylesheetdir + "/scripts/ajaxsharethis.php", {id: id}, function(e) {
					//hide and show new content
					$("#loading" + id).slideUp("normal");
					$("#drop" + id).slideDown("normal");
					pageTracker._trackPageview('/ajax/sharethis/' + id);
					
					$("#closelink" + id).click(function(){
						$(document).click();
					});
					
					$("#" + sharethisid + " a[href^=http]").click(function(){
						location.href = $(this).attr("href");
					});
					
					$("#" + sharethisid + " a[title]").hover(
						function () { $("#dropinfo" + id).html($(this).attr("title")); }, 
						function () { $("#dropinfo" + id).html("");	}
					);
					
					$("#more" + id).click(function(){
						if ($("#extrabookmark" + id).is(":hidden")) {
							$("#extrabookmark" + id).slideDown("normal");
							$(this).html("&laquo; Less").attr("title", "Less Bookmarks");
							//$("#evenmore" + id).show();
							pageTracker._trackEvent('Share', 'Open More', sharethisid);
						} else {
							$("#extrabookmark" + id).slideUp("normal");
							$(this).html("More &raquo;").attr("title", "More Bookmarks");
							//$("#evenmore" + id).hide();
							pageTracker._trackEvent('Share', 'Close More', sharethisid);
						}
					});									   
					
					$("#" + sharethisid).bind("click", function(e) {
						// eat the event like candy :)
						return false;
					});
				});
			}
			$(sharethisdiv).show("normal");	
			// simulate event on document, then stop bubbling
			$(document).click();
			e.stopPropagation();
			// setup event handler to close popup
			$(document).one("click", function(f) {
				$(sharethisdiv).hide("normal");
			});
			
			pageTracker._trackEvent('Share', 'Open Drop', sharethisid);
			
		} else {
			$(sharethisdiv).hide("normal");
			// if document listener active, would bubble up, close, and unbind itself anyways
			pageTracker._trackEvent('Share', 'Close Drop', sharethisid);
		}
	});
	
	// set timeless designs ajax handlers
	
	$(".reloadtimeless").attr("href", "javascript:void(0);").click(function(e){
		$("#" + $(this).attr("elementcontainer")).html("<img src=\"" + stylesheetdir + "/images/ajax-loader.gif\" width=\"220\" height=\"19\" alt=\"Loading\" />").load(stylesheetdir + "/scripts/ajaxrandomdesigns.php", {type: "timeless"}, function(i) { pageTracker._trackPageview('/ajax/timeless/'); });
	});

	// set random designs ajax handlers

	$("#reloadrandom").attr("href", "javascript:void(0);").click(function(e){
		$("#randomdesigns").html("<img src=\"" + stylesheetdir + "/images/ajax-loader.gif\" width=\"220\" height=\"19\" alt=\"Loading\" />").load(stylesheetdir + "/scripts/ajaxrandomdesigns.php", {type: "random"}, function(i) { pageTracker._trackPageview('/ajax/random/'); });
	});
	
	// initilize form elements
	
	$("form input[autoclear]").each(function(i) {
		if (this.value == $(this).attr("autoclear")) {
			$(this).addClass("empty");
		}
	}).focus(function() {
		if ($(this).data("val") == undefined) { // first call
			if ($(this).attr("autoclear")) {
				// store the given original
				$(this).data("val", $(this).attr("autoclear"));
			} else {
				// use whats there as original
				$(this).data("val", this.value);
			}
		}
		if (this.value == $(this).data("val")) { // clear field
			this.value = '';
			$(this).removeClass("empty");
		}
	}).blur( function() {
		if (this.value == '') {
			$(this).addClass("empty");
			this.value = $(this).data("val");
		}
	});
	
	$("form input[verify=true]").click(function(e) {
		$formid = $(this).parents("form:first").attr("id");
		
		if ($("#" + $formid + " input[required=true]").hasClass("empty")) {
			alert("Please fill in required fields denoted by *.");
			return false;
		}
		
		$("#" + $formid + " input[autoclear]").each(function(i) {
			if ($(this).hasClass("empty")) {
				this.value = "";
			}
		});
	});

	$(".paginationrandom").click(function(){
		pageTracker._trackEvent('Pagination', 'Random', $(this).attr("href") );
	});

	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var $targetOffset = $target.offset().top;
				if ($targetOffset == 0) {
					// found nothing or refering to top of page, ignore
					return false;
				}
				$('html,body').animate({scrollTop: $targetOffset - 10}, 1000);
				
				pageTracker._trackEvent('Anchor', 'Scroll', location.pathname, this.hash);
				return false;
			}
		}
	});

	$("#cfa_store_links a").click(function(){
		pageTracker._trackPageview('/cfa_store_link/' + $(this).attr("href"));
	});


	$('#spreadshop .spreadrow a').click(function() {
		
		// display frame
		var $shopframe = $('#shopframe');		
		$('#shopiframecontainer').html('<iframe height="665" width="728" src="'+ $(this).attr("href") +'" id="shopiframe" frameborder="0"></iframe>');
		$shopframe.show();
		
		// scroll to frame
		var $targetOffset = $shopframe.offset().top;
		if ($targetOffset == 0) {
			// found nothing or refering to top of page, ignore
			return false;
		}
		$('html,body').animate({scrollTop: $targetOffset - 10}, 1000);
		
		// kill browser click event
		return false;
	});
	
	$('#shopframeclose').click(function() {
		var $shopframe = $('#shopframe');
		$shopframe.slideUp();
		
		//return false;
	});
	
	
	
	/*
	// auto scroller
	$(".index").each(function(e) {
		$height = $(this).outerHeight();
		$div = $("<div>");
		$($div).addClass("scroller");
		$($div).css({ height: $height+"px" });
		$(this).prepend($($div));
		
		$($div).click(function(i) {
			$('html,body').animate({scrollTop: (i.pageY - i.clientY + $height)}, 100);
		});
		
	});
	*/
	
	// lightbox initialization
	$(function() {
	$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
	//$('.gallery a').lightBox(); // Select all links in object with gallery ID
	//$('a.lightbox').lightBox(); // Select all links with lightbox class
	//$('a').lightBox(); // Select all links in the page
	});
	
	// preload the hover image
	image = new Image();
	image.src = stylesheetdir + "/images/postimagedotlinehover.gif";

}); 