jQuery(document).ready(function(){

jQuery("#commentlist > li:even").addClass("alt");

// jQuery('.wpsc_buy_button').click(function(){
// 	if (jQuery("#sidebar ul li #widgetshoppingcart").parent('li').parent('ul').is(":visible")) {
// 		return true;
// 	}else{
// 		jQuery("#sidebar dd").slideUp("fast");
// 		jQuery("#sidebar dd #widgetshoppingcart").parent('li').parent('ul').slideDown("fast");
// 	}	
// })

jQuery('.nav li a').each(function(){
	if (jQuery(this).attr('href').slice(-4).match(window.location.href.slice(-4))){
			jQuery(this).parent('li').addClass("current");
			jQuery(this).parent('li').parent('ul').parent('li').addClass("current");
		}
});

jQuery(".products #sidebar ul li #widgetshoppingcart").parent('li').parent('ul').slideDown("fast");

// jQuery("#sidebar ul:not(.wp-polls ul), #sidebar .textwidget, #sidebar .wp-polls").hide();
// jQuery("#sidebar h3").click(function(){
// 	if (jQuery(this).next("ul, .textwidget, .wp-polls").is(":visible")) {
// 		jQuery(this).next("ul, .textwidget, .wp-polls").slideUp("fast")
// 	}
// 	else {
// 		jQuery("#sidebar ul, #sidebar .textwidget, #sidebar .wp-polls").slideUp("fast");
// 		jQuery(this).next("ul, .textwidget, .wp-polls, .wp-polls *").slideDown("fast");
// 	}
// 		jQuery('.wp-polls *').show();
// });

// jQuery('#sidebar a:contains(Private: )').each(function() {
// 	var str = jQuery(this).text().slice(9)
// 	jQuery(this).text(str).wrap('<em></em>');
// })

jQuery("#sidebar dd").hide();
jQuery("#sidebar dd").prepend("<span class=\"close\">x</span>")
jQuery("#sidebar dd .close").click(function() {
	jQuery(this).parent("dd").fadeOut("fast").prev("dt").removeClass("active")
})
jQuery("#sidebar h3, #sidebar dt").click(function(){
	var offset = jQuery(this).offset();
	disp = (jQuery("#sidebar").height() + jQuery("#footer").height() + 8);
	// alert(disp)
	jQuery("#sidebar dd").css({bottom: disp+"px"})

	jQuery(this).next("dd").css({left: offset.left, right: 'auto', bottom: disp})
	if (jQuery(this).next("dd").is(":visible")) {
		jQuery(this).next("dd").fadeOut("fast");
		jQuery("#sidebar dt").removeClass("active");
	}
	else {
		jQuery("#sidebar dd").fadeOut("fast");
		jQuery(this).next("dd").fadeIn("fast");
		jQuery("#sidebar dt").removeClass("active");
		jQuery(this).addClass("active");
	}
	return false;
});


jQuery('.democracy').show();
	

jQuery('ul.photos li a').lightBox();
	
	
	
			
		//jQuery(window).resize(function(){
		//	alert("Stop it!");
		//});
		function getFormValues(form) {
			var str = jQuery(form).serialize();
			return str;
		}


		jQuery('#sidebar form.ajax input:submit').click(function() {
			var functionName = "#"+jQuery(this).parent("form").attr("id");
			datastr = getFormValues(functionName);
			// alert(datastr)
			jQuery.ajax({
				type: "POST",
				url: "../functions.php",
				data: datastr,
				success: function(msg){
					// alert(msg)
					// alert(functionName);
					if (msg.match("Updated Twitter")) {
						jQuery(functionName).prev().replaceWith(msg);
						jQuery(functionName).fadeOut().remove();
						jQuery(functionName).prev().fadeOut("fast").fadeIn("fast")
					} else {
						jQuery(functionName).prev().replaceWith(msg);
						jQuery(functionName).prev().fadeOut("fast").fadeIn("fast")
					}
				afterAjax();	
				}
			})
				return false;
		});	


	
})

function afterAjax() {
	
	var cURL = window.location.pathname;

	jQuery('a').click(function() {  // reloads, otherwise with the ajax plugin the target might not have loaded yet

		var nURL = jQuery(this).attr("href");

		if (nURL.match("#comment") && (nURL.slice(0,-12)).match(cURL)) {  //if the link is a comment link, and it's the same page that we're on...
			var match = 0;
			jQuery("#commentlist li").each(function() {
				if (nURL.match(jQuery(this).attr("id"))) { match++; } 
				return match;
			})
			if (match == 0) {  // no comments by this link id matching, so reload the page
				// location.reload(true);
				// alert(nURL)
				parent.location.href = nURL; 
				location.reload(true);
			}
		} // else it's not the same page
		return true;
	});
	
}
