//  blindmicemedia.com Basic behaviors
//  February 2008
//  
//  Makes use of Prototype 1.6.0 (http://prototypejs.org)
//  and Scriptaculous 1.8.1 (http://script.aculo.us).
//

var defaultDuration = 0.3;
var baseURL = 'http://www.blindmicemedia.com/';
var locations = ["losangeles", "toronto"];
var locationDetails, locationListItems;
var currentLocation;
var notesURL = baseURL+"xml/notes.xml";

function blindmicemediaInit(){
	locationDetails = $$('#footer .contact .details');
	locationListItems = $$('#footer .contact .locations li');
	// set default location to the first details div in the html file
	// by hiding all the other divs
	for (i=1; i < locationDetails.length; i++){
		locationDetails[i].toggle();
	}
 	currentLocation = locations[0];

	// Find any collapser divs and hide them
	var collapsers = $$('div.collapser');
	for (i = 0; i < collapsers.length; i++){
		collapsers[i].toggle();
	}
	updateNote();
}

function showContact(location){
	if (currentLocation != location){	// Only do something if the user clicks on a new location
		var newLocationNum = getLocationNum(location);
		var oldLocationNum = getLocationNum(currentLocation);
		
		// Hide and show location details
		Effect.Fade(locationDetails[oldLocationNum], {duration: defaultDuration, queue: {position:'end', scope: 'contactScope', limit:4}});
		window.setTimeout(function(){
			Effect.Appear(locationDetails[newLocationNum], {duration: defaultDuration, queue: {position:'end', scope: 'contactScope', limit:4}});
		}, defaultDuration*1000);
		
		// Update links on left
		locationListItems[oldLocationNum].removeClassName('active');
		locationListItems[newLocationNum].addClassName('active');
		
		// Remember the current location
		currentLocation = location;
	}
}

function getLocationNum(location){
	var locationNum = -1;
	for (i=0; i < locations.length; i++){
		if (locations[i].toLowerCase() == location.toLowerCase()) locationNum = i;
	}
	return locationNum;
}

function toggleCollapser(jobID){
	// by default, a is the list of ancestors of the a tag that was clicked
	//aAncestors[0].next('div.collapser').toggle();
	var aAncestors = $(jobID).ancestors();
	var collapser = aAncestors[0].next('div.collapser');
	Effect.toggle(collapser, 'blind', {duration: 0.3});
}

// Sticky and Note information
function updateNote(){
	// Brower Detect
	var browser_type = navigator.appName;
	var browser_version = parseInt(navigator.appVersion);
	var browserFix = 1;
	var browserFix2 = 0;	
	if (browser_type == "Microsoft Internet Explorer" && browser_version >= 4) {
		browserFix = 1;
		browserFix2 = 0;
	} else {
		browserFix = 2;
		browserFix2 = 1;
	}
	
	var today = new Date();
	var currentDay = today.getDate();
	var currentMonth = today.getMonth() + 1;
	
	// declare variables
	var newLabel = '';
	var newMsg = '';
	var hasDateNote = 0;
	
	var noteLabel = $$('#footer .note .label').pop();
	var noteMsg = $$('#footer .note .message').pop();
	//alert(browserFix);
	
	new Ajax.Request(notesURL, {
		method: 'get',
		onSuccess: function(transport){		
			var xmlDoc = transport.responseXML;
			var dateArray = xmlDoc.getElementsByTagName("date_note");
			
			// Display Date Note, if date is specified
			// Note that the childNode number is specified as all white space is recorded as a child
			for( var j=0; j < dateArray.length; j++) {
				var day = xmlDoc.getElementsByTagName("day")[j].firstChild.nodeValue;
				if (day == currentDay) {
					/*newLabel = xmlDoc.getElementsByTagName("date_note")[j].childNodes[5].childNodes[0].nodeValue;
					newMsg = xmlDoc.getElementsByTagName("date_note")[j].childNodes[7].childNodes[0].nodeValue;*/
					newLabel = xmlDoc.getElementsByTagName("date_note")[j].childNodes[2*browserFix+1*browserFix2].childNodes[0].nodeValue;
					newMsg = xmlDoc.getElementsByTagName("date_note")[j].childNodes[3*browserFix+1*browserFix2].childNodes[0].nodeValue;
					noteLabel.update(newLabel);
					noteMsg.update(newMsg);
					hasDateNote = 1;
				}
			}
			
			// Display Random Note
			// Note that the childNode number is specified as all white space is recorded as a child
			if (hasDateNote == 0) {
				var normalArray = xmlDoc.getElementsByTagName("note");
				var num = Math.floor(Math.random()*normalArray.length);

				/*newLabel = xmlDoc.getElementsByTagName("note")[num].childNodes[1].childNodes[0].nodeValue;
				newMsg = xmlDoc.getElementsByTagName("note")[num].childNodes[3].childNodes[0].nodeValue;*/
				newLabel = xmlDoc.getElementsByTagName("note")[num].childNodes[0*browserFix+1*browserFix2].childNodes[0].nodeValue;
				newMsg = xmlDoc.getElementsByTagName("note")[num].childNodes[1*browserFix+1*browserFix2].childNodes[0].nodeValue;
				noteLabel.update(newLabel);
				noteMsg.update(newMsg);
			}
			
		},
		onFailure: function(transport){
			// 2008 03 18 - RY - deal with failure case here...
			alert('onFailure');
		}
	});
	
}