// JavaScript Document

// Initialization

var $selected;
var $ajaxContainer = $('<div id="ajaxContainer"></div>');

function init() {
	
	// set selected state for navigation
	setSelectedNavigation();
	setLeftColumnHeight();
	setScrollToTop();
	setContentHeight();
	

	
	// initialize site map animation
	
	$('#theMap > a').click(function() {
		loadSiteMap();	
		return false;
	});
	
	$('a#maplink').click(function() {
		loadSiteMap();	
		return false;
	});
}

// initialize window resize behavior
function initWidths() {
	$(window).resize(function() {
		//if ($(window).width() > 1024) {
  			setStudioWidths();
			//setContentHeight();
		//}
	});
}

// adjust studio icon areas to fill screen width
function setStudioWidths() {
	var overallWidth = $(window).width();
	var fixedWidth = $('div.headerBox.hdLogo.interior').width();
	var calculatedWidth = Math.round((overallWidth - fixedWidth) / 3);
	var contentWidth = Math.round(overallWidth - fixedWidth);
	
	if ($(window).width() > 1024) {
		$('div#rightColumn').css('width',contentWidth-20);
		$('div.headerBox.hdArch').css('width', calculatedWidth-10);
		$('div.headerBox.hdGraph').css('width', calculatedWidth-10);
		$('div.headerBox.hdInt').css('width', calculatedWidth-10);
	} else {
		$('div#rightColumn').css('width',805);
		$('div.headerBox.hdArch').css('width', 265);
		$('div.headerBox.hdGraph').css('width', 265);
		$('div.headerBox.hdInt').css('width', 265);	
	}
}

// Navigation Functions 

function setSelectedNavigation() {
	
	//if ($('li').hasClass('active') && $('li').hasClass('level2')) {
	if ($('li').hasClass('here')) {

		// capture selection
		$selected = $('li.here');
		var top = $('li.here').offset().top + 1;
		
		//add over element
		var $over = $('body').append('<div class="navSelected"></div>');
		
		$(".navSelected").css('top',top);
		$('li.here > a').css("color","#ffffff");
		$selected.css("list-style-image","url(/assets/templates/studiotrope/images/bullet_small_white.gif)");
		$selected.blur();
		$(".navSelected").animate({
			//top:top,
			opacity:1
		},1500,'easeOutQuad', function() {
			// set link color to over
		});
	}
}

function setHeader(parent) {
	switch(parent) {
		case 2:
			$('div.headerBox.hdLogo').addClass('yellowBackground');
			$('div.headerBox.hdLogo img').attr('src',"assets/templates/studiotrope/images/logo_topBar_white.gif");
			break;
		case 3:
			$('div.headerBox.hdArch').addClass('yellowBackground');
			$('div.headerBox.hdArch img').attr('src',"assets/templates/studiotrope/images/icon_arch_white.png");
			break;
		case 4:
			$('div.headerBox.hdGraph').addClass('yellowBackground');
			$('div.headerBox.hdGraph img').attr('src',"assets/templates/studiotrope/images/icon_graphics_white.png");
			break;
		case 5:
			$('div.headerBox.hdInt').addClass('yellowBackground');
			$('div.headerBox.hdInt img').attr('src',"assets/templates/studiotrope/images/icon_interiors_white.png");
			break;
		default:
			//
			break;
	}
}

function setScrollToTop() {
	$(".scroll").click(function(event){
		//alert("scrolling");
		//prevent the default action for the click event
		event.preventDefault();

		//get the full url - like mysitecom/index.htm#home
		var full_url = this.href;

		//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
		var parts = full_url.split("#");
		var trgt = parts[1];

		//get the top offset of the target anchor
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;

		//goto that anchor by setting the body scroll top to anchor top
		$('html, body').animate({scrollTop:target_top}, 1000, 'easeInOutQuad');
	});

}

function scrollUpMap() {
	//get the top offset of the target anchor
	var target_offset = $("#top").offset();
	var target_top = target_offset.top;

	//goto that anchor by setting the body scroll top to anchor top
	$('html, body').animate({scrollTop:target_top}, 1000, 'easeInOutQuad');
}

// sets height of #leftColumn to provide border for #navColumn
function setLeftColumnHeight() {
	var height = $('#navColumn').height() + 100;
	$('#leftColumn').height(height);
}

// sets height of #content to height of overall page
function setContentHeight() {
	var pheight = $('#page').height();
	if (pheight < $(window).height()) {
		var height = $(window).height();
		$('#page').height(height-20);
	}
}

// Sitemap Functions //

function loadSiteMap() {
	var toLoad = "sitemap.html";
	
	$.ajax ({
		type: 'GET',
		url: toLoad,
		dataType: 'html',
		success: function(html) {
			toggleSiteMap(html);
		}
	});
}

function toggleSiteMap(html) {
	scrollUpMap();
	clearAjax();
	
	$('#sitemap').append($ajaxContainer);
	$('#ajaxContainer').append(html);
	// toggle window
	var height = $(document).height();
	$('#sitemap').css('height',height);
	$('#sitemap').slideToggle("slow", function() {
    	$('#theMap > a').blur();
		$('#header').css('background-color','#f9f2ed');
	});
}

function clearAjax() {
	$('#ajaxContainer').empty();
	$('#ajaxContainer').remove();
}
