// JavaScript Document

/* 
	SECTION CONTACT INFORMATION 
	---------------------------
*/

var contactInfoToggler;

		
function ContactInfoToggler(speed)
{
	this.POSTER_HEIGHT 	= 314;
	this.TOGGLE_SPEED 	= 0.3;
	this.visible				// Visibility of ContactInfo
	this.contactInfo;			// DOM object
	this.contactInfoHeight;	// Height of contactInfoContent
	this.contactInfoBaseY;	// Default y-position of ContactInfo
	this.tween;					// Tweener
	this.visibleHeight = 0;	// Visible height of ContactInfo - animated
	
	if (speed) {
		this.TOGGLE_SPEED = speed;
	}
	
	this.init();
}

ContactInfoToggler.prototype.init = function()
{
	var posterY = $("#SectionPoster").offset().top;

	// Position buttons
	var buttonContainer = $("#PosterButtons .buttonContainer");
	buttonContainer.css("position", "absolute");
	buttonContainerHeight = buttonContainer.innerHeight();
	buttonContainer.css("top", posterY + this.POSTER_HEIGHT - buttonContainerHeight);
	
	this.contactInfo = $("#ContactInfo");
	this.contactInfo.css("position", "absolute");
	this.contactInfoHeight = this.contactInfo.innerHeight();

	this.contactInfoBaseY = posterY + this.POSTER_HEIGHT - buttonContainerHeight;
	this.repositionContactInfo();
	
	this.contactInfo.css("display", "block");
}

ContactInfoToggler.prototype.toggle = function()
{
	if (!this.visible) {
		$(".buttonContainer .button.contact").addClass("open")
		this.setTween(this.contactInfoHeight);
	} else {
		this.setTween(0);
	}
	this.visible = !this.visible;
}

ContactInfoToggler.prototype.repositionContactInfo = function ()
{
	 this.contactInfo.css("top", this.contactInfoBaseY - this.visibleHeight);
	 this.contactInfo.css("clip", "rect(0px 235px " + this.visibleHeight +"px 0px)");
}

ContactInfoToggler.prototype.motionFinished = function()
{
	this.repositionContactInfo();
	if (!this.visible) {
		$(".buttonContainer .button.contact").removeClass("open")
	}
}

ContactInfoToggler.prototype.setTween = function(height) 
{
	if (this.tween) {
		this.tween.continueTo(height, this.TOGGLE_SPEED);
	} else {
		this.tween = new Tween(this, "visibleHeight", Tween.regularEaseOut, this.visibleHeight, height, this.TOGGLE_SPEED, "");
		this.tween.onMotionChanged = Delegate.create(this, this.repositionContactInfo);
		this.tween.onMotionFinished = Delegate.create(this, this.motionFinished);
		this.tween.start();
	}
}

function applyAlternateRowColors(item, inverse) {
	var pos = inverse ? 1 : 0;
	// List items
	$(item).find("ul li").each(function() {
		if (pos % 2 == 1) {
			$(this).addClass("alternate");
		}
		pos++;
	});

	// table items
	pos = inverse ? 1 : 0;
	$(item).find("tr").each(function() {
		if (pos % 2 == 1) {
			$(this).addClass("alternate");
		}
		pos++;
	});
}

function verticalAlignBanner(banner) {
	var title = $(banner).children("span");
	if (title && title.height() < 30) {
		$(banner).addClass("singleLine");
	}
}

/*
	Search GUI
*/
var DEFAULT_SEARCH_TEXT = "Search";
var searchPageURL = null; // Is set from episerver masterpage
var searchTextField = null;
function initSearch() {
	searchTextField = $("#Search .text");
	searchTextField.focus( function (evt){
		updateSearchInput(evt);
	});
	searchTextField.blur( function (evt){
		updateSearchInput(evt);
	});

	$(".searchResultList li").live("click", function() {
		window.location = $(this).find("a").attr("href");
	});
	
}

function updateSearchInput(evt) 
{
	if (evt) {
		var value = evt.target.value;
		if (evt.type == "focus") {
			if (value == DEFAULT_SEARCH_TEXT) {
				evt.target.value = "";
			}
		} else {
			if (value == "") {
				evt.target.value = DEFAULT_SEARCH_TEXT;
			}
		}
	}
}
function filterSearchKeyPress(input, e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	}
	else if (e) {
		keycode = e.which;
	}
	else {
		return true;
	}

	if (keycode == 13) {
		if (searchPageURL && input) {
			var url = searchPageURL + "?query=" + input.value;
			window.location = url;
			return false;
		} else {
			alert("SearchPageURL not set");
			return false;
		}
	}
	return true;
}

/* Cufon */
function activateCufon()
{
	Cufon.replace("h1");
	Cufon.replace(".titleTextBox span.title");
}


$(document).ready(function() {
	$("body").addClass("cufon");

	// Remove link on button
	$(".buttonContainer .button.contact").attr("href", "#");
	
	contactInfoToggler = new ContactInfoToggler();
	$(".buttonContainer .button.contact").click(function() {
		contactInfoToggler.toggle();
	});
	
	/* Alternate row color on tables and lists */
	$(".alternate").each( function() {
		applyAlternateRowColors(this);
	});

	/* Alternate row color on episerver table*/
	$(".grayHeaderAlternate").each( function() {
		applyAlternateRowColors(this, true);
	});

	/* Vertical align title banners */
	$(".banner.titleBox").each( function() {
		verticalAlignBanner(this);
	});
	
	
	$("body").removeClass("alternative");
	
	initSearch();
});



function fv(str) 
{
	return str.replace("'", "\'");
}