﻿if(typeof(WPSC) == "undefined"){
 WPSC = new Object(); 
 WPSC.Init = function(){
 //do nothing
 }
 WPSC.WebPartPage = new Object();
 WPSC.WebPartPage.Parts = new Object();
 WPSC.WebPartPage.Parts.Register = function()
 {
 //do nothing
 }
}


$(document).ready(function()
{
	pageBasics();
	
	
	// calendar
	
	var keyMap = {};
	var json;

	
	if ($("#ctl00_PlaceHolderCalendar_hidEventsJson").length){
		var json = $.parseJSON($("#ctl00_PlaceHolderCalendar_hidEventsJson").val());
		
		if (json.events.length > 0){
			 $('#events-calendar').datepick({
                dateFormat: 'dd MM yyyy',
                minDate: $.datepick.today(),
                maxDate: new Date(2012, 12 - 1, 31),
                firstDay: 1,
                onDate: highlightDays,
                onShow: $.datepick.hoverCallback(showHover)
            });
		}
	}
	
   	
   


		
	
	function highlightDays(date)
	{	
		for (var i = 0; i < json.events.length - 1; i++) {    
			if ($.datepick.formatDate('dd MM yyyy', date) == json.events[i].date) {			
				// add the event to the map of events
				// we can then search this map later (to display the tooltip) without a performance hit
				keyMap[json.events[i].date] = json.events[i].html
			
				return {selectable: true, dateClass: 'eventday'};
       		}
       	}
  		return {selectable: false};
	}
	
	
	function showHover(date, selectable) {
		if (selectable) {
			var timestamp = $.datepick.formatDate('@', date);
			
			$('#hoverEventOutput').html(getEventHtml($.datepick.formatDate('dd MM yyyy', date)));
			
			var pos = $("#events-calendar table a.dp" + timestamp + "000").offset();

			//show the popup event details box
			$("#hoverEventOutput").css({
				"left": pos.left + "px",
				"top": (pos.top + 20) + "px"
			}).show().delay(6000).hide(0);
		}
	}
	
	
	function getEventHtml(tagName) {
		return keyMap[tagName];
	}
	
	
	
	setupPageRatings();
	
	
	var arrayData = ['Facebook', 'Flickr', 'Twitter', 'YouTube', 'Blog']; 	    
    $("#followus a").each(function(i) {
        $(this).simpletip({
            content: arrayData[i],
            showTime: 50,
            hideTime: 50,
            position: 'bottom',
            offset: [0, 3]
        });
        return true;
    });
});



function pageBasics()
{
	// add the asterisk next to google translate
   setTimeout(setAsterisk, 4000);
   
	function setAsterisk(){	
		$("div.goog-te-gadget").append("<span class=\"asterisk\"><a href=\"/website/corporatepages/pages/copyright.aspx\" title=\"Copyright and disclaimer\">*</a></span>");
	}   


	// display the sitemap div if it's not empty
    if ($("#ctl00_sitemapPanel span").text() != ""){

        var crumb = $("#ctl00_sitemapPanel span").text();

        // split the string on a hyphen
        var splitcrumb = crumb.split(" - ");

        if (splitcrumb.length > 0) {
            var possibleParentPage = splitcrumb[0].replace("You are here > ", "").trim();

            // search for the text as a link elsewhere in the page
            if ($("a[title='" + possibleParentPage + "']").length) {
                // if it exists as a link then use the url in the crumbtrail
                var url = $("a[title='" + possibleParentPage + "']").attr('href');
                crumb = crumb.replace(possibleParentPage, "<a href=\"" + url + "\" title=\"" + possibleParentPage + "\">" + possibleParentPage + "</a>");
            }
        }

        crumb = crumb.replace("You are here >", "You are here > <a href=\"/home\" title=\"Home\">Home</a> &gt;");
        
        $("#content").prepend("<div style=\"padding-bottom: 10px\">" + crumb + "</div>");
        
        $("#ctl00_sitemapPanel").remove();        
    }

	
	
	// Remove underline from "bookmark" links
	$("a").not($("a[href]")).addClass('bookmark').wrap("<span style=\"cursor: text !important\"></span>");
	
	$("a#printWebPart").click(function (){
		window.print();
	});
	
	
	// Link logo to homepage
	$("div#logo").click(function() {
		window.location = "/";
	});	
	
	// facebook link
	$("#followus a[href*='facebook']").attr('href', 'http://www.facebook.com/pages/Bolton-at-Home/111875788862894?ref=ts');

	$("#ctl00_PlaceHolderMain_pnlQuickLinks div.title").html("Quick links")
	
	// footer links open in new window	
	$("div.col a").attr("target", "_blank");
	
	
	var footer = $("#bah .container_16 > div").last();
	footer.wrapInner("<div class='clearfix'></div>");
	footer.append("<img src='/style%20library/bah/imgs/footer1.gif' id='footer1' />");
	footer.append("<a href='http://investorsinexcellence.com/' title='Investors in Excellence'><img src='/style%20library/bah/imgs/footer2.gif' id='footer2' /></a>");	

	// remove foi link from footer	
	footer.find("a[href='/website/pages/Freedomofinformation.aspx']").parent().remove();
	
	// change phone number
	footer.find("div.numbers p").first().text("T: 01204 338000");
	
	// charity number
	footer.find(".grid_4").last().find("ul").append("<li>Charity number: 1139370</li><li>TSA 4568</li>");
}



function setupPageRatings()
{
    if (!$(".thankyou-msg-box").length) {
        if ($("div#faces").length) {
            $("#faces .voteGood").click(function () {clickFace(3);return false;});
            $("#faces .voteAverage").click(function () {clickFace(2);return false;});
            $("#faces .voteBad").click(function () {clickFace(1);return false;});
        }
    } else {
        $("a.voteGood, a.voteAverage, a.voteBad").remove();
  		$("span#ratethispage-msg").remove();
    }
}



function clickFace(rating) {
    $("#faces input").val(rating);
    $("#faces a").removeAttr("class");
    $("a#happy").addClass("voteGood");
    $("a#average").addClass("voteAverage");
    
    $("a#bad").addClass("voteBad");
    
    if (rating != 3) {
        $("a.voteGood").addClass("goodUnselected");
    }
    if (rating != 2) {
        $("a.voteAverage").addClass("averageUnselected");
    }
    if (rating != 1) {
        $("a.voteBad").addClass("badUnselected");
    }
    $("#popupVote").css("display", "block");
}


String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
