// JavaScript Document

var imageFade = 0;
var moving_news = false;
var scrollUp = false;
var scrollDown = false;
var scroller = "";
var scrollContainer = "";
var currentArticle = "";
var articleLoaded = 1;

function articleSelect(object, imgsrc) {
	if (articleLoaded == 1) {
		if (($(object).offset().top + $(object).height()) > ($("#articleContainer").height() + 128)) {
			var adjustment = ($(object).offset().top + $(object).height()) - ($("#articleContainer").height() + 128);
			$("#articleScroller").css("top", "-" + (adjustment + 11) + "px");
		}
		articleLoaded++;
	}
	$(currentArticle).css("background", "");
	$(".articleSelected").removeClass("articleSelected").addClass("article");
	$(object).removeClass("article").addClass("articleSelected");
	$("#sideImage").attr("src",imgsrc);		
	$("a.view").hide();
	$(object).find("a.view:first").show();
	currentArticle = object;
}

function articleClose() {
	$("#articleOverlay").animate({"left": "-984px"}, "slow");
	$("#articleSlider").animate({"left": "-984px"}, "slow");	
}

function changeMap() {	
	$("body").append("<div id='mapBackground'>&nbsp;</div>");
	$("#mapBackground").height($(document).height()).css("opacity", "0.7");		
	$("body").append('<div id="mapOverlay"><iframe width="900" height="500" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=Little+Priory+Court,+Fore+St,+TOTNES,+Devon+TQ9+5NJ&amp;sll=50.431281,-3.685377&amp;sspn=0.011605,0.027874&amp;ie=UTF8&amp;hq=&amp;hnear=Little+Priory+Court,+Fore+St,+Totnes,+Devon+TQ9+5NJ,+United+Kingdom&amp;t=h&amp;layer=c&amp;cbll=50.431374,-3.686657&amp;panoid=q2o40ae0WrmuFUmCCZpD7A&amp;cbp=13,330.1,,0,2.39&amp;ll=50.431309,-3.685484&amp;spn=0.027337,0.077248&amp;z=14&amp;output=svembed"></iframe><div id="mapClose"><a href="#" onclick="closeMap();"><img src="/images/close.gif" alt="Close"></a></div>');	
	$("#mapOverlay").centerInClient({ container: window });
	return false;
}


function closeMap() {
	$("#mapOverlay").remove();
	$("#mapBackground").remove();
}

function fadeImages() {
	if ((imageFade + 1) == $("div.backgroundImageHolder").size()) {
		$("div.backgroundImageHolder").eq(imageFade).fadeOut("fast", function (){
			$("div.backgroundImageHolder").eq(0).fadeIn("medium");
			imageFade = 0;
		});	
	} else {
		$("div.backgroundImageHolder").eq(imageFade).fadeOut("fast", function (){
			$("div.backgroundImageHolder").eq(imageFade + 1).fadeIn("medium");
			imageFade++;
		});	
	}
}

function menuBar(linkID, lineID) {
	$('#' + lineID).fadeIn(0, function() {
		$('#' + lineID).animate({left: ($("#" + linkID).position().left), width: $("#" + linkID).width()}, "slow");
	});
}

function pageSetup() {
	
	if ($("#textScroller").height() < $("#textContainer").height()) {
		$('#textScroller').css({'top' : 'auto', 'bottom' : '0px'});	
	}
	
	$('.midContentRightHomeLayer1').css('opacity', 0.4).show(0);	
	$('.midContentRightHomeLayer2').css('opacity', 0.6).show(0);
	$(".textScroller").fadeIn("slow");
	
	$('#menuRightLine').css({left:($("#home_link").position().left), width: $("#home_link").width()});
	
	
	if ($("div.backgroundImageHolder").size() > 0) {
		interval = window.setInterval(fadeImages, 4000);
	}
	
	$('#flash').flash({src: 'harrison.swf', width: 637, height: 422, wmode: 'transparent'});
	
	$("#articleOverlay").css("opacity", 0.95);
	
	$(".menuWorkContainer").slideDown(1000,"easeOutBounce");
	
	scrollingInt = window.setInterval(scrollSettings, 5);
	
}

function projectThumbSelect(object, imgsrc) {
	$(".thumbSelected").removeClass("thumbSelected");
	$(object).addClass("thumbSelected");
	$("#sideImage").attr("src", "/images/project_large/" + imgsrc);		
}


function scrollSettings() {
	if (scrollContainer != "") {
		if (scrollUp == true) {
			var top = $("#" + scroller).position().top;
			maxTop = 0;
			var nextTop = top + 2;
			if (nextTop > maxTop) {		
				nextTop = maxTop;	
			}
			if (top < 0) {
			$("#" + scroller).css("top", nextTop);	
			}
		}
		if (scrollDown == true) {
			var top = $("#" + scroller).position().top;	
			var height = $("#" + scroller).height();	
			var containerHeight = $("#" + scrollContainer).height();
			var maxTop = height - containerHeight;
			maxTop = maxTop - (maxTop*2);
			var nextTop = top - 2;
			if (nextTop < maxTop) {			
				nextTop = maxTop;	
			}
			$("#" + scroller).css("top", nextTop);
		}
	}
}

function slideArticle(articleID) {
	$.post("/includes/what_content.php", { articleID: articleID }, function(data){
		$("#articleSlider").html(data);
		$('body').supersleight();
		$("#textScroller").fadeIn();
		$("#articleOverlay").animate({"left": "0px"}, "slow");
		$("#articleSlider").animate({"left": "0px"}, "slow");
	});
}

$(document).ready(function(){
	
	//Sets items to certain opacity and position
	pageSetup();
	
	// IE6 PNG FIX
	$('body').supersleight();
	
	/*$('#textScroller').mousewheel(function(event, data) {
		if (data > 0) {
			scrollUp();
		} else if (data < 0) {
			scrollDown();
		}
	});*/
	
	$("#textContainer").live('mouseleave', function(e) {
		scrollDown = false;
		scrollUp = false;
	});
	
	$("#textContainer").live('mouseenter', function(e) {
		if ($("#textScroller").height() > $("#textContainer").height()	) {						   
			scroller = "textScroller";
			scrollContainer = "textContainer";
			var relativeY = e.pageY - $(this).offset().top;
			if (relativeY < ($(this).height() / 2)) {
				scrollUp = true;	
			} else {
				scrollDown = true;	
			}
		}
	});
	
	
	$("#articleArrowUp").hover(function(e){
		if ($("#articleScroller").height() > $("#articleContainer").height()	) {										
			scroller = "articleScroller";
			scrollContainer = "articleContainer";
			scrollUp = true;
		}
	},function(){
		scrollUp = false;
	});
	
	$("#articleArrowDown").hover(function(e){	
		if ($("#articleScroller").height() > $("#articleContainer").height()	) {										  
			scroller = "articleScroller";
			scrollContainer = "articleContainer";
			scrollDown = true;
		}
	},function(){
		scrollDown = false;
	});
	
	
	$("#textContainer").live('mousemove', function(e){		   
		var relativeY = e.pageY - $(this).offset().top;
		if (relativeY < ($(this).height() / 2)) {
			scrollDown = false;
			scrollUp = true;	
		} else {
			scrollUp = false;
			scrollDown = true;	
		}
    });
	
	
	$("#projectTypes a.type").click(function () { 
		menuBar($(this).attr("id"), "menuWorkLine");
		var nextElement = $(this).next();
		if((nextElement.is('ul')) && (!nextElement.is(':visible'))) {
			$('#projectTypes ul:visible').slideUp('slow', function() {
				$(nextElement).slideDown('slow');
				$(nextElement).find("a:first").trigger('click');
			});
		}			
		return false;
    });
	
	
	$("#projectTypes li ul li a").hover(function(e){										 
		$("#projectName").html($(this).attr("rel"));
	},function(){
		$("#projectName").html("");
	});
	
	$("#projectTypes li ul li a").click(function () { 	
		var projectID = $(this).attr("id").replace("project_", "");
		$(".workSelected").removeClass("workSelected");
		$(this).addClass("workSelected");
		$("#projectLeft").fadeOut("slow");
		$("#projectRight").fadeOut("slow", function() {													
			$("#projectContent").empty();
			$.post("/includes/project_content.php", { projectID: projectID }, function(data){
				$("#projectContent").html(data);
				$('body').supersleight();
				$("#projectRight").fadeIn();
				$("#projectLeft").fadeIn();
			});
		});
		$("#projectName").html("");
		return false;
	});
	
	$('.articleSelected').live('mouseleave', function() {
		$(this).css("background", "url(/images/article_background.gif) repeat-x top left");
	});
	
	$('.article').live('mouseenter', function() {
		$(this).css("background", "url(/images/article_background.gif) repeat-x top left");
	});
	
	$('.article').live('mouseleave', function() {
		$(this).css("background", "");
	});
	
});


$(window).resize(function() {
  	$("#mapBackground").height($(document).height());	
	$("#mapOverlay").centerInClient({ container: window });
});




