// JavaScript Document

var canHideMenu = false; // to prevent the showMenu from being called repeatedly
var menuEnabled = true; // to prevent the showMenu from being called whilst the user is scrolling across the page

$(document).ready(function(){
	
	imageCount = $(".BackgroundPane .c_content img").size();
	// shows the background navigation if any background images exist
	if(imageCount > 0) setupBackground();
	setupBackgroundPaneControls();
	
	// preload images
	$(".BackgroundPane .c_content img").each(function(){
		var path = $(this).attr("src");
		jQuery.preLoadImages(path);
	});
	
	// FLOATING NAVIGATION
	$("#floating-nav").click(function(){
	    $.scrollTo(0,600); // returns to the top of the page
	    hideMenu();
	    return false;
	});

    $(".MenuPane").hover(function(){
    },
    function(){
        hideMenu();
    });
	
	//console.log($(window).mousemove());
	 $("html").mousemove(function(e){
	    //console.log(e.pageX +', '+ e.pageY);
	    var offset = $("#floating-nav").offset();
	    var menuPaneOffset = $(".MenuPane").offset();
	    var menuPanePos = menuPaneOffset.top + $(".MenuPane").height()+30;
	    //console.log("e.pageX " + e.pageX + " offset.left " + offset.left );
	    if( (e.pageX < (offset.left -200) && canHideMenu) || (e.pageY > menuPanePos && canHideMenu) || (e.pageX > (offset.left + 40) && canHideMenu)) {
	        hideMenu();
	        canHideMenu = false;
	    } 
	}); 
	
	$("#floating-nav").hover(function(){
	    // only show the menu if the nav is deep enough
	    var winheight = $(window).height()
	    var height = $(this).css("top");
	    h= height.replace("px","");
	    //console.log("height: "+ parseInt(height) + " winheight: " + parseInt(winheight));
	    if(parseInt(h) > parseInt(winheight))
	    {
	        $(".MenuPane").css("top",height);
	        if(menuEnabled) showMenu();
	    }
	},
	function(){
	});

	// SCROLLING LOGIC
	var t, l = (new Date()).getTime();
    $(window).scroll(function(){
        menuEnabled = false;
        var now = (new Date()).getTime();

        if(now - l > 400){
            //console.log("scrollStart");
            $(this).trigger('scrollStart');
            l = now;
            hideMenu();
        }

        clearTimeout(t);
        t = setTimeout(function(){
            //console.log("scrollEnd");
            $(window).trigger('scrollEnd');
            var newpos = $("#ScrollTop").attr("value");
            $("#floating-nav").animate({ top:newpos },900, function(){ menuEnabled = true;} );
        }, 500);
    });
	
	
	// SETUP TAG CONTENT
	//console.log(window.location.pathname);
	var path = window.location.pathname;
	var partPath = path.slice(1,5);
	var tag = path.slice(6);
	//console.log(partPath + " | " + tag);
	if(partPath == "news")
	{
	    //console.log(partPath + " | " + tag);
	    $(".ContentPane").prepend("<h1>Our work with the tag <b>'" + tag + "'</b></h1>");
	    
	    // change menu css - has to be inline as DNN already has css classes that can't be used due to the way their JS works
	    $(".mainMenu span span.txt").each(function(){
	        if($(this).html() == "Home") $(this).parent().css("background","none");
	        if($(this).html() == "Work") $(this).parent().css("background-color","#F05784"); 
	    });
	}
	
	// LeftPane expandable content content
	$("#LINK-ABOUT-US").click(function(){ toggleElementDisplay($("#LIST-ABOUT-US")); });
	$("#LINK-SECTOR-EXPERIENCE").click(function(){ toggleElementDisplay($("#LIST-SECTOR-EXPERIENCE")); });
	$("#LINK-AGENCY-BACKGROUND").click(function(){ toggleElementDisplay($("#LIST-AGENCY-BACKGROUND")); });
	$("#LINK-OUR-MOTIVE").click(function(){ toggleElementDisplay($("#LIST-OUR-MOTIVE")); });
	$("#LINK-WHY-DESIGN-MOTIVE").click(function(){ toggleElementDisplay($("#LIST-WHY-DESIGN-MOTIVE")); });
	$("#LINK-CLIENTS").click(function(){ toggleElementDisplay($("#LIST-CLIENTS")); });
	$("#LINK-QUOTES").click(function(){ toggleElementDisplay($("#LIST-QUOTES")); });
	$("#LINK-WEBSITE").click(function(){ toggleElementDisplay($("#LIST-WEBSITE")); });
	
	$(".dm-list-link").click(function(){
	    // toggling the position of a graphic to reveal either a plus or minus image    
	    if($(this).css("background-position") == "100% 0px") {
	        $(this).css("background-position","right -15px");
	        // scroll to position of the page - minus 180 so it sits below the header on the site
	        var offset = $(this).offset();
	        $.scrollTo( ({top:offset.top-180+'px',left:'0px'}), 800 );
	    }
	    else $(this).css("background-position","right 0px");
	    
	    return false;
	})
	
	// ******************************* SITE ADMIN CHANGES *******************************
	// TELERIK EDITOR CHANGES
	if($("#dnn_ControlPanel .ControlPanel").size() > 0)
	{
		// show module titles
		$(".c_title").css("display","block");
	}
	else{
	    // ******************************* NON ADMIN CHANGES *******************************
	    
	}
	
	// ******************************* CHANGES FOR ALL *******************************
	
	// fix the height for the #menu-wrap for longer pages
	if($(".ContentPane").height() < $(".LeftPane").height()) $(".ContentPane").height($(".LeftPane").height());
	
	// Amazon S3 Cloudfront streaming
    $f("a.rtmp", "http://dmstreaming.s3.amazonaws.com/website/flowplayer-3.2.5.swf", { 
        clip: { 
	        provider: 'rtmp'
        }, 
        plugins: {  
	        rtmp: {
	            url: 'http://dmstreaming.s3.amazonaws.com/website/flowplayer.rtmp-3.2.3.swf',
	            netConnectionUrl: 'rtmp://s25v415gdupbod.cloudfront.net/cfx/st'
	            //netConnectionUrl: 'rtmp://streaming.designmotive.co.uk/cfx/st'
            } 
        }	     
    });	
});

function toggleElementDisplay(element)
{
    if($(element).css("display") == "block") $(element).hide();
    else $(element).show({speed:500, effect:"clip"});
    //else $(element).show(600);
	// some browsers needs this recalculated
    //if($("#menu-wrap").height() < $(document).height()) $("#menu-wrap").height($(document).height());
    if($(".ContentPane").height() < $(".LeftPane").height()) $(".ContentPane").height($(".LeftPane").height());
}

(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 setupBackground()
{
	$.each($(".BackgroundPane .c_content img"), function(count, item){
		$(this).addClass("BackgroundImage");
	});	
	
	// This will activate the full screen background!
	// give each bg image an id
	var bgID = 1;
	$(".BackgroundPane .c_content img.BackgroundImage").each(function(){
		var theid = "bg" + bgID;
		$(this).attr("id",theid);
		
		// using the new ID add the full screen functionality
		var FullscreenrOptions = {  width: 1024, height: 768, bgID: $("#"+theid) };
		$.fn.fullscreenr(FullscreenrOptions);
		bgID++;
	});	
}

function setupBackgroundPaneControls() {
	// Due to the CSS styling for the background pane, the edit content button is duplicated and placed somewhere visible
	if($(".BackgroundPane .c_footer a.CommandButton").size() != 0)
	{
		
		EditContentLink = $(".BackgroundPane .c_footer a.CommandButton").attr("href");		
    	$(".ContentPane").prepend($("<a href="+EditContentLink+" class='CommandButton background-edit-button'>Edit Page Background</a> "));	
		
		$(".BackgroundPane .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($(".BackgroundPane"));
			}
			// hide standard buttons
			$(this).hide();
			$(".BackgroundPane .c_footer a.CommandButton").hide();
		});
		//console.log(EditContentLink);
	}
}

function showMenu() {
	if(canHideMenu == false) $(".MenuPane").show({speed:600, effect:"clip"});
	canHideMenu = true;
}
function hideMenu()  {
 	if(canHideMenu) $(".MenuPane").hide({speed:600, effect:"slide"});
	canHideMenu = false;
}

// Telerik editor contros
if(typeof Telerik === 'undefined'){
    // ignore
}
else {
    Telerik.Web.UI.Editor.CommandList["Custom1"] = function(commandName, editor, args) {
      alert("Just testing please ignore!");
    };
}

