﻿var header_block;
var body_block;
var print_block;
var masker;
var site_id;
var last_load = "welcome";
var colours = ['#CC0000', '#318238', '#3551A0', '#666666'];
var names = ['Planning Perspectives', 'Environmental Perspectives', 'Strategic Perspectives', 'The Perspectives Group'];
var urls = ['planper.com', 'environper.com', 'strategicper.com', 'thepergroup.com'];
var phones = ['020 7493 6008', '020 7529 1530', '020 7529 1527 ', '020 7493 6008'];
var faxes = ['020 7491 9654', '020 7491 9654', '020 7491 9654', '020 7491 9654'];
var pdf_available = false;
var seen_pdf_tip = false;

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function doHide(hide) {
    if(!getFlashMovie("flash_menu").tempShowHide){ return; }
	getFlashMovie("flash_menu").tempShowHide(hide);
}

function setMenuURL(url) {
    if(!getFlashMovie("flash_menu").setMenuURL){ return; }
	getFlashMovie("flash_menu").setMenuURL(url);
}

function siteColour() {
    return colours[site_id];
}

function siteName() {
    return names[site_id];
}

function siteURL() {
    return urls[site_id];
}

function flashRequestURL(url){
    $.historyLoad(url);
}

function resizeWindow() {
	if ($(window).width() < 1100) {
		$('.page_holder').css("padding-left", "65px");
		$('.sitePicker_holder').css("margin-left", "-7px");
	} else {
		$('.page_holder').css("padding-left", "90px");
		$('.sitePicker_holder').css("margin-left", "17px");
	}
}

function doMouseWheelScroll(event, delta){
    getFlashMovie("scroll_bar").doWheel(delta);
}

var _anim_int;
var _cur_margin = 0;
function setScrollBar(height){
    $('.bottom .holder').css('marginTop','0px');
    clearInterval(_anim_int);
    _cur_margin = 0;
    $('.bottom .holder').unbind('mousewheel');
    $('.bottom .holder').bind('mousewheel', function(event, delta){ doMouseWheelScroll(event, delta);});
    if(!getFlashMovie("scroll_bar").setContentHeight){ return; }
	getFlashMovie("scroll_bar").setContentHeight(height);
}

function fadeIn() {
	$('object', body_block).css("background-color", "White");
	header_block.animate({ opacity: 0 }, 500, "linear", function() {
		header_block.animate({ opacity: 1 }, 150, "linear");
		body_block.animate({ opacity: 1 }, 150, "linear");
		setScrollBar(body_block.height());
	});
}

function makeInfoPair(title, value) {
	return "<div class='info_pair'><h2 class='tint'>" + title + ":</h2><h2>" + value + "</h2><div style='clear:both'></div></div>";
}

function makeProjectPage(data) {
	header = "<h1>" + data.name + "</h1>";
	if (data.consultants != ""){ header += makeInfoPair("Consultants", data.consultants); }
	if (data.clients != ""){ header += makeInfoPair("Client", data.clients); }
	if (data.LPAs != ""){ header += makeInfoPair("LPA", data.LPAs); }
	if (data.architect != ""){ header += makeInfoPair("Architect", data.architect); }
	header_block.html(header);
	body_block.html(data.summary + data.body);
	print_block.html("<div class='header'>" + header + "</div>" + data.summary + data.body);
}

function makeTextPage(title, body, subtitle) {
	header = "<h1>" + title + "</h1>";
	if (subtitle) { header += "<h2>" + subtitle + "</h2>"; }
	header_block.html(header);
	body_block.html(body);
	print_block.html("<div class='header'>" + header + "</div>" + body);
}

function submitContactForm() {
    valid = true;
	name = $('#contact_name', body_block).val();
	email = $('#contact_email', body_block).val();
	phone = $('#contact_phone', body_block).val();
	message = $('#contact_message', body_block).val();
	
	if(name == ""){$("#contact_name").next().show(0); valid = false;}else{$("#contact_name").next().hide(0);}
	if(email == ""){$("#contact_email").next().show(0); valid = false;}else{$("#contact_email").next().hide(0);}
	if(phone == ""){$("#contact_phone").next().show(0); valid = false;}else{$("#contact_phone").next().hide(0);}
	if(message == ""){$("#msg_err img").show(0); valid = false;}else{$("#msg_err img").hide(0);}

    if(!valid){ return; }
	$('.sending').show();
	$.getJSON(rel + "sendEmail.ashx?name=" + name + "&email=" + email + "&phone=" + phone + "&message=" + message, function(data) {
		$('.sending').animate({ opacity: 0 }, 100, "linear", function() {
			$('.sending').html("<img src='" + rel + "Images/email.png'/>Thank you");
			$('#contact_name', body_block).val("");
			$('#contact_email', body_block).val("");
			$('#contact_phone', body_block).val("");
			$('#contact_message', body_block).val("");
			$('.sending').animate({ opacity: 1 }, 100, function() { $('.sending'); });
		});
	});

}

function showError(text) {
	error_div = $('.error');
	if (error_div.css('display') == 'none') {
		error_div.html("<img src='Images/exclamation.png'/>" + text);
		error_div.css('display','inline-block');
	} else {
		error_div.animate({ opacity: 0 }, 200, "linear", function() {
			error_div.html("<img src='Images/exclamation.png'/>" + text);
			error_div.animate({ opacity: 1 }, 300, "linear");
		});
	}
}

function hideError() {
	$('.error').slideUp(200, "linear", function() { $('.error').text(""); });
}

function makeContactForm() {
    error_img = "<img src='" + rel + "Images/exclamation.png'/>";
	return "<table class='contact_table'><tr><td>Name</td><td colspan='2'><input type='text' id='contact_name'/>" + error_img + "</td></tr><tr><td>Email</td><td colspan='2'><input type='text' id='contact_email'/>" + error_img + "</td></tr><tr><td>Phone</td><td colspan='2'><input type='text' id='contact_phone'/>" + error_img + "</td></tr><tr><td valign='top'>Message</td><td><textarea id='contact_message'/></td><td id='msg_err' valign='top'>" + error_img + "</td></tr><tr><td></td><td colspan='2'><div class='button'>Send</div><div class='sending' style='display:none'><img src='" + rel + "Images/email_open.png'/>Sending...</div><div style='clear:both'></div></td></tr></table><br/>";
}

function makeContactPage() {
	txt = "<h1>Contact us</h1>";
	txt += makeInfoPair("Phone", phones[site_id]);
	txt += makeInfoPair("Email&nbsp;", "info@" + siteURL());
	txt += makeInfoPair("Fax", faxes[site_id]);
	header_block.html(txt);
	body = "<p>Our offices are at 24 Bruton Place, just off Berkeley Square, and within easy walking distance of Bond Street, Oxford Circus and Green Park tube stations. Please use the form below to get in touch.</p>" + makeContactForm();
	body_block.html(body);
}

function doPrintElements(){
    $('img#print_logo').attr('src', rel + '/Images/' + siteURL() + '.jpg');
    $('span#print_phone').text(phones[site_id]);
    $('span#print_fax').text(faxes[site_id]);
    $('span#print_url').text('www.' + siteURL());
}

function setScrollPos(position){
    clearInterval(_anim_int);
    $('.bottom .holder').stop().animate({'marginTop':position});
    _cur_margin = position;
}

function setScrollPosHard(position){
    clearInterval(_anim_int);
     $('.bottom .holder').stop().animate({'marginTop':position}, 200, 'linear');
    _cur_margin = position;
}

function setScrollPosHold(position){
       clearInterval(_anim_int);
     $('.bottom .holder').stop().css({'marginTop':position});
    _cur_margin = position;
}

function doScrollTween(position){
    var diff = (position - _cur_margin) * 0.4;
    _cur_margin += diff;
    if(Math.abs(position - _cur_margin) < 2){
        _cur_margin = position;
        clearInterval(_anim_int);
    }
    $('.bottom .holder').css('marginTop', _cur_margin);
}

function doScrollDrag(position){
    clearInterval(_anim_int);
    doScrollTween(position);
}

function doScrollDragStop(position){
    _anim_int = setInterval('doScrollTween(' + position + ')', 10);
}

function getContent(id_string) {
    if(id_string == ''){ id_string = 'welcome'; }
    if(id_string == last_load){ return; }
    last_load = id_string;
	content_type = id_string.split("/")[0];
	id = id_string.split("/")[1];
    setMenuURL(id_string);
    pageTracker._trackPageview(id_string);
    
    pdf_available = content_type == "project" || content_type == "staff";   
    var overlay_opacity = 1;
    var overlay_cursor = 'pointer';
    var pdf_link = rel + 'PDF.ashx?m=' + content_type + '&s=' + site_id + '&id=' + id;
    
    if(!pdf_available){
        overlay_opacity = 0;
        overlay_cursor = 'default';
        $('#downloadPDFTip').stop().animate({'opacity':0});
    }else{
        if(!seen_pdf_tip){
            seen_pdf_tip = true;
            $('#downloadPDFTip').animate({'opacity':1}).animate({'opacity':1},10000).animate({'opacity':0});
        }
    }
    
    $('#pdf_link img.overlay').animate({'opacity':overlay_opacity});
    $('#pdf_link').css('cursor', overlay_cursor);
    $('#pdf_link').attr('href', pdf_link);

	if (content_type == "contact") {
		body_block.stop().animate({ opacity: 1 }, 1500, "linear", function() {
			body_block.stop().animate({ opacity: 0 }, 300, "linear");
			header_block.stop().animate({ opacity: 0 }, 300, "linear", function() {
			    body_block.css('padding-bottom', '0px');
				makeContactPage();
				first_el = $($('.bottom .holder').children()[0]);
				if (first_el.is(':p')){ first_el.css('margin-top', '0px'); }
				$('div.button').click(function(){submitContactForm();});
				print_block.html('<img style="margin-bottom:20px" src="' + rel + 'Images/' + site_id + 'Map.png"/>');
				fadeIn();
			});
		});
	} else {
	$.getJSON(rel + "getData.ashx?site_id=" + site_id + "&mode=" + content_type + "&id=" + id, function(data) {
		if (data.success == 'true') {
		    body_block.css('padding-bottom', '20px');
			body_block.stop().animate({ opacity: 0 }, 300, "linear");
			header_block.stop().animate({ opacity: 0 }, 300, "linear", function() {
				switch (data.type) {
					case "welcome": //Deliberate fall-through
					case "text":
						makeTextPage(data.data.title, data.data.body, data.data.subtitle);
						break;
					case "news":
						makeTextPage(data.data.title, data.data.body, data.data.date);
						break;
					case "service":
						makeTextPage(data.data.title, data.data.body);
						break;
					case "project":
						makeProjectPage(data.data);
						break;
					case "staff":
						summary = (data.data.summary == "<br />" || data.data.summary == "") ? "" : "<h2>" + data.data.summary + "</h2>";
						makeTextPage(data.data.name, summary + data.data.body, data.data.position);
						break;
					case "career":
						makeTextPage(data.data.title, data.data.body);
						break;
				}
				//window.location.hash = id_string;
				document.title = siteName();
				first_el = $($('.bottom .holder').children()[0]);
				if (first_el.is(':p')){ first_el.css('margin-top', '0px'); }
				fadeIn();
			});
		} else {
			if (data.data) {
				makeTextPage(data.data.title, data.data.body);
				fadeIn();
			}
			return true;
		}
	});
	}
}

function jumpTo(id_string){
    getContent(id_string);
}

$(document).ready(function() {
  $('#pdf_link').click(function(){return pdf_available;});
	header_block = $('#content .top .holder .top_content');
	body_block = $('#content .bottom .holder');
	print_block = $('#print_content div#inner_content');
	doPrintElements();
	document.title = siteName();
	if (site_id != 3) {
		$('#content .top').css('border-bottom', 'solid 8px ' + siteColour());
	}
	hash = window.location.hash.substr(1);
	swfobject.embedSWF(rel + "Flash/menu.swf", "flash_menu", "466", "480", "9.0.0", "", { "rel": rel, "site_id": site_id, _init_selection: hash }, { "wmode": "transparent" });
	swfobject.embedSWF(rel + "Flash/scroller.swf", "scroll_bar", "17", "316", "9.0.0", "", {}, { "wmode": "transparent" });
    $('#flash_menu').mouseout(function() { doHide(true); });
	$('#flash_menu').mouseover(function() { doHide(false); });
	$('#print_icon').click(function() { goToPrint(); });
	$('div#current_site').css({ 'background': '#F8F8F8 url(' + rel + 'Images/dropdownArrow-' + site_id + '.jpg) no-repeat scroll 180px 6px'});

    $.historyInit(getContent);
	resizeWindow();
	var resizeTimer = null;
	$(window).bind('resize', function() {
		if (resizeTimer){ clearTimeout(resizeTimer); }
		resizeTimer = setTimeout(resizeWindow, 100);
	});

	$('div#current_site').click(
		function(event) {
			$('div#other_sites').slideDown();
			event.stopPropagation();
		}
	);

	$('body').click(
		function() {
			$('div#other_sites').slideUp();
		}
	);
	
	$('#print_icons a img.overlay').css('opacity', 0);
});

