// preload images

var menu_living = new Image(); menu_living.src = template_dir+"/images/menu_living_over.gif";
var menu_family = new Image(); menu_family.src = template_dir+"/images/menu_family_over.gif";
var menu_travel = new Image(); menu_travel.src = template_dir+"/images/menu_travel_over.gif";
var menu_media = new Image();  menu_media.src  = template_dir+"/images/menu_media_over.gif";
var menu_clips = new Image();  menu_clips.src  = template_dir+"/images/menu_clips_over.gif";


jQuery(document).ready(function($) {
	
	// init js css changes & corners
	$(".css_hide").css("display", "block");
	$(".sidebar_expresso_ads").corner("12px"); 
	$(".widget").corner("20px");
	$(".sidebar_adverts").corner("20px");
	$(".sidebar_extra_page1").corner("20px");
	$(".post_the_tags, .post_the_categories").css({"position":"absolute","top":"1.5em"});
	if(!ie7 && !ie6){
		$(".post_the_tags, .post_the_categories").corner("bottom 15px");
	}
	$(".postmetadata").corner("round 16px").parent().css('padding', '3px').corner("round 20px");
	
	
	$(".comment_body_text").corner("15px");
	$('.comment, .pingback').corner("20px");
	$('.outer_comment').corner("25px");
	if(ie8){
		$('.commentlist li:not(li li)').corner("30px").css('paddingTop', '5px');
	} else {
		$('.commentlist li:not(li li)').corner("30px");
	}
	$('#respondInnerContainer').corner("30px");

	
	$("#tabbed_pop_posts, #tabbed_recent_posts, #tabbed_recent_comments").corner("bottom 20px");
	$("#tabbed_widgets h3").css("display", "none");
	$("#tabbed_widgets").tabs({ fx: { opacity: 'toggle' } }).hide().removeClass("js_hide").show(400);
	$(".search_form").corner("tl 20px");

	$("a.rss_entries").bind("mouseenter focus", function(e){
		$("a.rss_comments").parent("li").children("ul").fadeOut(100, function(e){
			$("a.rss_entries").parent("li").children("ul").fadeIn(200);
		});
	});
	$("a.rss_entries").parent("li").children("ul").children("li:last").children("a").bind("blur", function(e){
		$("a.rss_entries").parent("li").children("ul").fadeOut(100);
	});
	
	$("a.rss_comments").bind("mouseenter focus", function(e){
		$("a.rss_entries").parent("li").children("ul").fadeOut(100, function(e){
			$("a.rss_comments").parent("li").children("ul").fadeIn(200);
		});
	});
	$("a.rss_comments").parent("li").children("ul").children("li:last").children("a").bind("blur", function(e){
		$("a.rss_comments").parent("li").children("ul").fadeOut(100);
	});
	
	$("div#sidebar_rss_subscribe").bind("mouseleave", function(e){
		$("a.rss_entries").parent("li").children("ul").fadeOut(100);
		$("a.rss_comments").parent("li").children("ul").fadeOut(100);
	});

	
	$(".post_the_categories_container").bind("mouseenter", function(){
		hold_PDM_cat_menu = true;
		$(this).children("div").slideDown("fast");
	}).bind("mouseleave blur", function(){
		hold_PDM_cat_menu = false;
		PDM_cat_menu = $(this);
		setTimeout('if(!hold_PDM_cat_menu){PDM_cat_menu.children("div").fadeOut("normal");}',200);
	}).each(function(i){
		$(this).children("a").bind("focus", function(){
			$(this).parent("div").children("div").slideDown("fast");
			$(this).parent("div").children("div").children("ul").children("li").children("a:last").bind("blur", function(){
				$(this).parent("li").parent("ul").parent("div").fadeOut("normal");
			})
		}).bind("click", function(){return false;}).bind("mouseenter", function(){hold_PDM_cat_menu = true;}).bind("mouseleave", function(){hold_PDM_cat_menu = false;})
	});
	
	
	$(".post_the_tags_container").bind("mouseenter", function(){
		hold_PDM_tag_menu = true;
		$(this).children("div").slideDown("fast");
	}).bind("mouseleave blur", function(){
		hold_PDM_tag_menu = false;
		PDM_tag_menu = $(this);
		setTimeout('if(!hold_PDM_tag_menu){PDM_tag_menu.children("div").fadeOut("normal");}',200);
	}).each(function(i){
		$(this).children("a").bind("focus", function(){
			$(this).parent("div").children("div").slideDown("fast");
			$(this).parent("div").children("div").children("ul").children("li").children("a:last").bind("blur", function(){
				$(this).parent("li").parent("ul").parent("div").fadeOut("normal");
			})
		}).bind("click", function(){return false;}).bind("mouseenter", function(){hold_PDM_tag_menu = true;}).bind("mouseleave", function(){hold_PDM_tag_menu = false;})
	});
	
	
	$(".cls_addthis").bind("focus", function(){
		$(".post_the_tags").fadeOut("normal");
	});
	$(".post_the_tags_container a").bind("focus", function(){
		$(".post_the_categories").fadeOut("normal");
	});
	
	
	
	
	

	$("#menu a").bind("mouseenter focus", function(){
		if($(this).children("img").attr("src").indexOf('_over.gif')==-1){
			$(this).children("img").attr("src", $(this).children("img").attr("src").substring(0, $(this).children("img").attr("src").indexOf('.gif'))+"_over.gif");
		}
	}).bind("mouseleave blur", function(){
		if($(this).children("img").attr("src").indexOf('_over.gif')!=-1) {
			$(this).children("img").attr("src", $(this).children("img").attr("src").substring(0, $(this).children("img").attr("src").indexOf('_over.gif'))+".gif");
		}
	});
	
	$(".contact_popup").bind("click", function(){
		var scroll_top = $(window).scrollTop();
		var window_height = $(window).height();
		var popup_height = $("#popupContainer").height();
		var popup_top = scroll_top + 15;
		var diff = Math.round((window_height-popup_height)/2);
		if(window_height>popup_height){popup_top = scroll_top + diff}
		else{}
		//alert("popup_height: "+popup_height+" ; scroll_top: "+scroll_top+" ; window_height: "+window_height+"\n"+"popup_top("+popup_top+") = scroll_top("+scroll_top+") + Math.round((window_height("+window_height+") - popup_height("+popup_height+"))/2))("+diff+")");
		$("#popupContainer").css({top: popup_top + "px"}).fadeIn("normal");
		return false;
	})
	
	$("#popup_close a").bind("click", function(){
		$("#popupContainer").fadeOut("normal");
		return false;
	})
	
	
/* ### fill sidebar by cloning sidebar_adverts ###

	// get the sidebar_adverts object
	var sidebar_adverts = $(".sidebar_adverts");
	
	// this is a bit silly but replace the sidebar_adverts object with itself
	// this will help fix images that haven't loaded properly
	sidebar_adverts.clone().replaceAll(".sidebar_adverts");
	// reset the sidebar object
	sidebar_adverts = $(".sidebar_adverts");
	
	// get sidebar_adverts outerHeight, offset, and footer offset
	var sidebar_adverts_outerHeight = sidebar_adverts.outerHeight();
	var sidebar_adverts_offset = sidebar_adverts.offset();
	var footer_offset = $("#footer").offset();
	// calculate number of times to repeat
	var times_repeat = ((Math.round(footer_offset.top) - Math.round(sidebar_adverts_offset.top) - sidebar_adverts_outerHeight)/sidebar_adverts_outerHeight)|0;
	
	// show you work >>> alert( "( " +Math.round(footer_offset.top) + " - " + Math.round(sidebar_adverts_offset.top) + " - " + sidebar_adverts_outerHeight + " ) / " + sidebar_adverts_outerHeight + " = " + times_repeat);
	
	// now do the cloning
	var i;
	for (i=0;i<times_repeat;i++)
	{
		sidebar_adverts.clone(true).insertAfter(sidebar_adverts);
	}
	
*/


/* this should be last */
	
	// here's a wierd fix. Wanna make sure your images are displaying?
	// Replace each with a clone of itself.
	$("img").each(function(){$(this).clone().replaceAll(this)});
	
});

var hold_PDM_cat_menu, hold_PDM_tag_menu;
var PDM_cat_menu, PDM_tag_menu;