ExecuteOrDelayUntilBodyLoaded(A365Global);  


function A365Global() {

$('#s4-bodyContainer').before("<div id='UCCheader'><span>UCC Settingspage</span></div>");

	  
	/* ------ Begin Manual & Info ------ */
	  
	   $(".ms-breadcrumb-box").prepend('<div class="manual"><a class="divLink" href="http://golive.workstreampeople.com" title="That&apos;s what this widget is"></a></div>');
	   $(".ms-breadcrumb-box").prepend('<div class="info"><a class="divLink" href="http://www.workstreampeople.com/"></a></div>');
	   
	   $(".manual").click(function(){
			/* window.location = $(this).find("a:first").attr("href"); */
			window.open($(this).find("a:first").attr("href"));
			return false;
		});
		
	   $(".info").click(function(){
			window.open($(this).find("a:first").attr("href"));
			return false;
		});
		
		// Show URL on Mouse Hover
		$(".manual").hover(function () {
			window.status = $(this).find("a:first").attr("href");
		}, function () {
			window.status = "wolek";
		});
		
		$(".info").hover(function () {
			window.status = $(this).find("a:first").attr("href");
		}, function () {
			window.status = "salam";
		});
		
	 /* ------ End Manual & Info ------ */
	  
	
    function checkMediaQuery(x)
    {
        var serverRelativeUrl = _spPageContextInfo.siteServerRelativeUrl;
        if (serverRelativeUrl == "/")
        {
            serverRelativeUrl = "";
        }

        if (x.matches)
        { 
		    var $logo = $("#ctl00_onetidHeadbnnr2");
		    $("#ctl00_onetidHeadbnnr2").attr("src", "" + serverRelativeUrl + "/Style Library/Anywhere365/Images/Logo2x.png");
		    $( "#UCCheader" ).append( $( "#DeltaPlaceHolderSearchArea" ) );
        }
        else
        {
			$("#ctl00_onetidHeadbnnr2").attr("src", "" + serverRelativeUrl + "/Style Library/Anywhere365/Images/Logo2x.png");
			$( "#DeltaPlaceHolderGroupActionsArea" ).append( $( "#DeltaPlaceHolderSearchArea" ) );
		}
	}

	var x = window.matchMedia("(max-width: 479px)")
	checkMediaQuery(x) // Call listener function at run time
	x.addListener(checkMediaQuery) // Attach listener function on state changes
	  
	  //----------------------- Begin breadcrumb -----------------------------

SP.SOD.executeFunc('SP.js', 'SP.ClientContext', LoadSiteBreadcrumb);

    function LoadSiteBreadcrumb() {
        var breadCrumbNode;
        var clientcontext = SP.ClientContext.get_current();
        var site = clientcontext.get_site();
        var currentWeb = clientcontext.get_web();
        clientcontext.load(currentWeb, 'ServerRelativeUrl', 'Title', 'ParentWeb', 'Url');
        clientcontext.load(site, 'ServerRelativeUrl');
        clientcontext.executeQueryAsync(
		function () {
		    var breadcrumbWrapper = document.createElement('div');
		    breadcrumbWrapper.className = "ms-Breadcrumb";
		    breadcrumbWrapper.innerHTML = '<div class="ms-FocusZone"><div id="breadcrumbIcon"></div><ul id="breadcrumbSite" class="ms-Breadcrumb-list"></ul></div>';

		    //for Seattle Master
		    if (document.getElementById('contentRow') !== null) {
		    	try {
			    	//old Seattle master
			        document.getElementById('DeltaPlaceHolderMain').insertBefore(breadcrumbWrapper, document.getElementById('mainContent'));
		        }catch(err){
		        	document.getElementById('contentBox').insertBefore(breadcrumbWrapper, document.getElementById('DeltaPlaceHolderMain'));
		        }
		    } else {
		        document.getElementById('contentBox').insertBefore(breadcrumbWrapper, document.getElementById('DeltaPlaceHolderMain'));
		    }

		    var breadCrumbNode = document.getElementById('breadcrumbSite');
		    var Custombreadcrumb = document.getElementById('DeltaPlaceHolderMain');
		    var breadCrumbNode = document.getElementById('breadcrumbSite');
		    if (document.location.pathname.indexOf('SitePages') != -1 || document.location.pathname.indexOf('Pages') != -1) {
		        var li = document.createElement('li');
		        li.className = "ms-Breadcrumb-listItem";
		        if (document.title.split('-').length > 1) {
		            li.innerHTML = '<span class="ms-Breadcrumb-itemLink">' + document.title.split('-')[1].trim() + '</span><i class="ms-Breadcrumb-chevron ms-Icon ms-Icon--ChevronRight"></i>'
		        } else {
		            li.innerHTML = '<span class="ms-Breadcrumb-itemLink">' + document.title.trim() + '</span><i class="ms-Breadcrumb-chevron ms-Icon ms-Icon--ChevronRight"></i>'
		        }
		        breadCrumbNode.insertBefore(li, breadCrumbNode.childNodes[0]);
		    }
		    else if (document.location.pathname.indexOf('_layouts/15/') != -1) {
		        var li = document.createElement('li');
		        li.className = "ms-Breadcrumb-listItem";
		        li.innerHTML = '<span class="ms-Breadcrumb-itemLink">' + document.title + '</span><i class="ms-Breadcrumb-chevron ms-Icon ms-Icon--ChevronRight"></i>'
		        breadCrumbNode.insertBefore(li, breadCrumbNode.childNodes[0]);
		    }

		    var li = document.createElement('li');
		    li.className = "ms-Breadcrumb-listItem";
		    li.innerHTML = '<a class="ms-Breadcrumb-itemLink" href="' + currentWeb.get_url() + '">' + currentWeb.get_title() + '</a><i class="ms-Breadcrumb-chevron ms-Icon ms-Icon--ChevronRight"></i>'
		    if (Custombreadcrumb != null) {
		        breadCrumbNode.insertBefore(li, breadCrumbNode.childNodes[0]);
		    }
		    if (site.get_serverRelativeUrl() !== currentWeb.get_serverRelativeUrl()) {
		        RecursiveWeb(currentWeb.get_parentWeb().get_serverRelativeUrl())
		    }
		}, fail);
    }


    function RecursiveWeb(siteUrl) {
        var Custombreadcrumb = document.getElementById('contentBox');
        var breadCrumbNode = document.getElementById('breadcrumbSite');
        var clientcontext = new SP.ClientContext(siteUrl);
        var site = clientcontext.get_site();
        var currentWeb = clientcontext.get_web();
        clientcontext.load(currentWeb, 'ServerRelativeUrl', 'Title', 'ParentWeb', 'Url');
        clientcontext.load(site, 'ServerRelativeUrl');
        clientcontext.executeQueryAsync(function () {
            if (site.get_serverRelativeUrl() !== currentWeb.get_serverRelativeUrl()) {
                var li = document.createElement('li');
                li.className = "ms-Breadcrumb-listItem";
                li.innerHTML = '<a class="ms-Breadcrumb-itemLink" href="' + currentWeb.get_url() + '">' + currentWeb.get_title() + '</a><i class="ms-Breadcrumb-chevron ms-Icon ms-Icon--ChevronRight"></i>'
                var Custombreadcrumb = document.getElementById('contentBox');
                breadCrumbNode.insertBefore(li, breadCrumbNode.childNodes[0]);
                RecursiveWeb(currentWeb.get_parentWeb().get_serverRelativeUrl())
            } else {
                var li = document.createElement('li');
                li.className = "ms-Breadcrumb-listItem";
                li.innerHTML = '<a class="ms-Breadcrumb-itemLink" href="' + currentWeb.get_url() + '">' + currentWeb.get_title() + '</a><i class="ms-Breadcrumb-chevron ms-Icon ms-Icon--ChevronRight"></i>'
                breadCrumbNode.insertBefore(li, breadCrumbNode.childNodes[0]);
            }
        }, fail);
    }

    function fail() {
        console.log('Unable to load SharePoint BreadCrumb');
    }


    //Feature Images
    findImagesByRegexp();
    function findImagesByRegexp() {
        var images = Array.prototype.slice.call((document.getElementById('DeltaPlaceHolderMain') || document).getElementsByTagName('img'));
        var length = images.length;
        for (var i = 0; i < length; ++i) {
            if (images[i].src.indexOf('breadcrumb.png') != -1) {
                images[i].src = pathBreadCrumb + '/Style Library/Anywhere365/Images/portal/breadcrumb.png';
            }
        }
    }	

    var serverRelativeUrl = _spPageContextInfo.siteServerRelativeUrl;
    if (serverRelativeUrl == "/")
    {
        serverRelativeUrl = "";
    }
	$('#breadcrumbSite').css('background-image', 'url("'+ serverRelativeUrl +'/Style Library/Anywhere365/Images/portal/SVG/header_ucc.svg")');	
	// $("#breadcrumbSite").attr("src", "" + serverRelativeUrl + "/Style Library/Anywhere365/Images/portal/SVG/header_ucc.svg");
    //-------------------------End Breadcrumb ----------------------------
   
}
