var fadeSpeed = 1500;
var imageSpeed = 1000;

var EditContentLink;
var SettingsLink;
var SettingsImg;


$(document).ready(function(){
	// preload all TopPane BG images
	$(".TopPane img.BackgroundImage").each(function(){
	    var path = $(this).attr("src");
		jQuery.preLoadImages(path);
	})
	
	// bring in the background
	$(".TopPane img.BackgroundImage:eq(0)").load(function(){
	    $(this).fadeIn(fadeSpeed);
	});
	$(".TopPane img.BackgroundImage:eq(0)").fadeIn(fadeSpeed);
	
	// Fade images in
	$(".TwoPaneContainer img").each(function(){
	    $(this).load().fadeIn(imageSpeed);
	});
	
	setupTopPaneControls();
	forceGap();
	$(".BottomPane, .ContentPane, .LeftPane, .RightPane").fadeIn(200);
	$("#BottomShadow, #RightShadow").fadeIn(10);
	fixActiveItemClassDisplay();
	
	// orizonti news editor fix
	if($("#dnn_ctr1618_editNews_pnlEditNews").size())
	{
	    $("#dnn_ctr1618_editNews_pnlEditNews").hideOrizontiElements();
	}
});

(function($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)
// jQuery.preLoadImages("image1.gif", "/path/to/image2.png");

function forceGap()
{
	// Number of empty string checks to force a gap on the paragraph
	$("p").each(function(){
		if($(this).html() == "" || $(this).html() == " ")
		{
			$(this).addClass("forceGap");
		}
	});
	$("p span").each(function(){
		if($(this).html() == "" || $(this).html() == " ")
		{
			$(this).addClass("forceGap");	
		}
	});
	$("p a").each(function(){
		if($(this).html() == "" || $(this).html() == " ")
		{
			$(this).addClass("forceGap");	
		}
	});
}

function setupTopPaneControls()
{
	// DNN TopPane Editor controls - Because of the z-index levels being a negative value serve this sites backgrounds
	// The TopPane editor controls are being replicated somewhere outside of the negative z-index
	if($(".TopPane .c_footer a.CommandButton").size() != 0)
	{
		
		EditContentLink = $(".TopPane .c_footer a.CommandButton").attr("href");
		$("<a href="+EditContentLink+" class='CommandButton'>Edit Page Background</a> ").insertBefore($(".TopPane"));
		
		$(".TopPane .c_footer input").each(function(){
			if($(this).attr("alt") == "Settings")
			{
				SettingsLink = $(this).attr("name");
				SettingsImg = $(this).attr("src");
				//console.log(SettingsLink);
				$("<input name="+SettingsLink+" type='image' src='"+SettingsImg+"'/>").insertBefore($(".TopPane"));
			}
			// hide standard buttons
			$(this).hide();
			$(".TopPane .c_footer a.CommandButton").hide();
		});
		
		//$(this).hide();
		//console.log(EditContentLink);
			
	}
}

// About us > Exclusive dining
function initExclusiveDining()
{	
	// table version
	$(".ImageAndCaptionTable td:nth-child(3n)").css("padding-right","0px");
}

// CISS Side Menu
function fixActiveItemClassDisplay()
{
	// Checks if there are multiple ActiveItem classes injected in the menu and if so remove them all excluding the last one
	if($(".SideNav .ActiveItem").size() > 1) $(".SideNav .ActiveItem:not(:last)").removeClass("ActiveItem");
}

jQuery.fn.hideOrizontiElements = function(sender, options) {
	myoptions = jQuery.extend({
    	
    },
	options);
	// simply hide certain tables	
	$("table:first tr:eq(15)", this).hide();
	$("table:first tr:eq(28)", this).hide();
}