/** 
* main.js
*
* Bone Creative Inc.
*
* Francis Daigle 
* Version	0.1 
**/

// Parse URL
var aUrl = document.location.href.split(/\//);
var sUrlParam = aUrl[aUrl.length-1];
var sUrlFlag = aUrl[aUrl.length-2];

// Onload
$(document).ready(function() {
	/* Preload media */
	$.preloadCssImages();
	
	/* Initial display properties */
	$('#who-we-are #textarea1 div').css('display', 'none');
	$('#who-we-are #textarea1 #brad').css('display', 'block');
	$('#ethics .statement').css('display', 'none');
	// Set initial opacity
	$('#undermainnav').css('opacity', 0.50); 
// comment out vbird //	$('#pointer').css('opacity', 0.50); 
	$('#hover-left').css('opacity',0.50);
	$('#hover-right').css('opacity',0.50);
	/* Animate tabs */ 
	var aTabs = new Array('home','who-we-are', 'our-process', 'portfolio', 'contact-us','ethics','client-login', 'blog'); //, 'dothis', 'client-list');
	
	// Parse URL
	var aUrl = document.location.href.split(/\//);
	var sUrlParam = aUrl[aUrl.length-1];
	var sUrlFlag = aUrl[aUrl.length-2];
	
	// Index of tab
	var nSelectedIndex = 0;
	if (sUrlFlag == 'portfolio') {
		nSelectedIndex = 2;
	} else {
		var nSelectedIndex = aTabs.indexOf(sUrlParam);
	}
	if (nSelectedIndex == -1 || nSelectedIndex == undefined) {
		nSelectedIndex = 0;
	}
	// Hilghlight menu tab // 
	 $('#undermainnav ul li a').eq(nSelectedIndex).css({
	//	'background-color' : '#0099FF', 
		'color' : '#ffffff'
	});
	$('#mainnav ul li a').eq(nSelectedIndex).css({
		'color' : '#ffffff'
	});
	// List item anchor offset
		var nPointerPos = $('#undermainnav ul li a').eq(nSelectedIndex).offset().left;

	// Parent padding 
	var nElementPadding = $('#undermainnav ul li a').css("padding-left");
	nElementPadding = nElementPadding.replace(/px/, "") * 2;
	// Element width
	var nElementWidth = $('#undermainnav ul li a').eq(nSelectedIndex).width() + nElementPadding;
	
	/* Animate pointer */
	// Pointer width
	// comment out vbird //nPointerWidth = $('#pointer').width();
	//Pointer position
	// comment out vbird //if (nSelectedIndex < 4) {
	// comment out vbird //	$('#pointer').css('left', nPointerPos + nElementWidth - nPointerWidth);
	// comment out vbird //} else {
	// comment out vbird //	$('#pointer').css('left', nPointerPos);
	// comment out vbird //}

	// Pointer animation
// comment out vbird //	$('#pointer').animate({'height': '11px'}, {queue:false, duration:500});
	// Pointer background
// comment out vbird //	if (sUrlParam == 'contact-us' || sUrlParam == 'ethics' || sUrlParam == 'client-login' || sUrlParam == 'blog') {
// comment out vbird //		$('#pointer-bg').animate({'height': '11px'}, {queue:false, duration:500});
// comment out vbird //		$('#pointer-bg').css({
// comment out vbird //			'display' : 'block',
// comment out vbird //			'left' : nPointerPos
// comment out vbird //		});
// comment out vbird //	}

	//alert (sUrlFlag);
// comment out vbird //	if (sUrlParam == 'portfolio' || sUrlFlag == 'portfolio' || sUrlParam == 'our-process') {
// comment out vbird //		$('#pointer-bg').animate({'height': '11px'}, {queue:false, duration:500});
// comment out vbird //		$('#pointer-bg').css({
// comment out vbird //			'display' : 'block',
// comment out vbird //			'left' : nPointerPos + nElementWidth - nPointerWidth
// comment out vbird //		});
// comment out vbird //	}
	
	/* Menu hover */
	// Change opacity on #menu hover
	$('#mainnav').hover(function () {	
		//$('#mainnav').fadeTo('normal', 0.90);
		$('#undermainnav').fadeTo('normal', 0.90);
		$('#mainnav ul li a').eq(nSelectedIndex).css({
			'color' : '#FFFFFF'
		});
		}, function () {
			//$('#mainnav').fadeTo('normal', 0.50);
			$('#undermainnav').fadeTo('normal', 0.50);
			$('#mainnav ul li a').eq(nSelectedIndex).css({
			'color' : '#FFFFFF'
		});
		}
	);
	// Change opacty on #li hover
	$('#mainnav ul li').hover(function () {	
		var nMenuIndex = $('#mainnav ul li').index(this);
		if (nMenuIndex != nSelectedIndex) {
			// Hover on list item background color
			$('#undermainnav ul li').eq(nMenuIndex).css({
				'background-color' : '#CCCCCC'
			});
			// Hover on list item anchor color
			$('#undermainnav ul li a').eq(nMenuIndex).css({
				'color' : '#CCCCCC'
			});
		}
		}, function () {
			var nMenuIndex = $('#mainnav ul li').index(this);
			if (nMenuIndex != nSelectedIndex) {
				// Hover out list item background color
				$('#undermainnav ul li').eq(nMenuIndex).css({
					'background-color' : '#0099FF'
				});
				// Hover out list item anchor color
				$('#undermainnav ul li a').eq(nMenuIndex).css({
					'color' : '#0099FF'
				});

			}
		}
	);
	
	/* Nav Hover */
	$('#nav li').hover(function () {	
		$(this).addClass('hover');
	}, function () {
		$(this).removeClass('hover');
	});

	
	/* Animate who-we-are media */
	var amedia = new Array('brad','tarek','john','shon','rod','hugh','jay');
	var bActive = true;
	var bLeft;
	var bRight;
	var nMove = 0;
	var nCounter = 1;
	var sDirection;
	var nSpeed = 1000;
	var nWidth = $('#who-we-are').width();
	// Bind hover and mouseout events
	$('#hover-left').hover(function () {
		bLeft = true;
		HoverHandler();
	});
	$('#hover-right').hover(function () {
		bRight = true;
		HoverHandler();
	});
	$('#hover-left').mouseout(function () {
		bLeft = false;
	});
	$('#hover-right').mouseout(function () {
		bRight = false;
	});
	// Easing method
	jQuery.easing.def = 'easeInOutSine';
	// Hover event handler
	function HoverHandler() {
		//alert(nCounter);
		if (sDirection == null) {
			if (bLeft == true) {
				nCounter = amedia.length - 1;
			}
			$('#who-we-are').css('backgroundImage', "url('/media/photos/" + amedia[nCounter] + ".jpg')");
			//$('#mediaspace1').css('backgroundImage', "url('/media/photos/" + amedia[nCounter] + ".jpg')");
		}
		if (bLeft == true) {
			if (sDirection == 'right') {
				nCounter -= 2;
				if (nCounter == -1) {
					nCounter = amedia.length - 1;
				}
				$('#who-we-are').css('backgroundImage', "url('/media/photos/" + amedia[nCounter] + ".jpg')");
			}
			sDirection = 'left';
			$('#who-we-are').css('backgroundPosition', '-' + nWidth + 'px 25px');
			if (bActive == true) {
				nMove += nWidth;
				$('#who-we-are').stop().animate({'backgroundPosition': (nWidth - nMove) + 'px 25px'}, nSpeed);
				$('#mediaspace1').stop().animate({'backgroundPosition': nMove + 'px 0'}, nSpeed, function(){ChangeImage('left')});
				bActive = false;
			}
		}
		if (bRight == true) {
			if (sDirection == 'left') {
				nCounter += 2;
				if (nCounter == amedia.length) {
					nCounter = 0;
				}
				$('#who-we-are').css('backgroundImage', "url('/media/photos/" + amedia[nCounter] + ".jpg')");
			}
			sDirection = 'right';
			$('#who-we-are').css('backgroundPosition', '' + nWidth + 'px 25px');
			if (bActive == true) {
				nMove -= nWidth;
				$('#who-we-are').stop().animate({'backgroundPosition': (nWidth + nMove) + 'px 25px'}, nSpeed);
				$('#mediaspace1').stop().animate({'backgroundPosition': nMove + 'px 0'}, nSpeed, function(){ChangeImage('right')});
				bActive = false;
			}
		}
	}
	// Swap media
	function ChangeImage(bearing) {
		if (bearing == 'left') {
			nMove = 0;
			if (nCounter < 0) {
				nCounter = amedia.length - 1;
			}
			var nNext = nCounter - 1;
			var nCurrent = nCounter;
			if (nCurrent == 0){
				nNext = amedia.length - 1;
			}
		}
		if (bearing == 'right') {
			nMove = 0;
			if (nCounter == amedia.length) {
				nCounter = 0;
			}
			var nNext = nCounter + 1;
			var nCurrent = nCounter;
			if (nCurrent == amedia.length - 1){
				nNext = 0;
			}
		}
		$('#mediaspace1').css('backgroundPosition', '0 0');
		$('#mediaspace1').css('backgroundImage', "url('/media/photos/" + amedia[nCurrent] + ".jpg')");
		$('#who-we-are').css('backgroundImage', "url('/media/photos/" + amedia[nNext] + ".jpg')");
		$('#who-we-are').css('backgroundPosition', '' + nWidth + 'px 25px');

		$('#who-we-are #textarea1 div').css('display', 'none');
		// IE7 Cleartype fix
		$('#who-we-are #textarea1 #' + amedia[nCounter] + '').fadeIn(function() {
			if (! $.support.opacity) {
			this.style.removeAttribute('filter');
			}
		});
		$('#who-we-are #textarea1 #' + amedia[nCounter] + '').fadeIn("fast", function(){HoverHandler()});
		if (bearing == 'left') {
			nCounter -= 1;
		} else {
			nCounter += 1;
		}
		bActive = true;
	}
	
	/* client-list*/
	if (sUrlParam == 'client-list') {
		$('#clients div').each(function (i) {
			var nParentWidth = $(this).width();
			var nChildWidth = $('span', this).width();
			var nSpace = nParentWidth - nChildWidth;
			nNumOfChildren = $('span', this).children().size();
			sPadding = Math.floor(nSpace / (nNumOfChildren - 1)) + 'px';
			$('h4', this).slice(1).css('padding-left', sPadding);
		 });
		$('#client-list #clients h4:nth-child(odd)').addClass('odd');
		$('#client-list #clients h4:nth-child(even)').addClass('even');
	}
	
	/* ethics */
	if (sUrlParam == 'ethics') {
		$('#ethics #sidebar h3').eq(0).css('color', '#0099FF');
		$('#ethics #textarea1 #statements .statement').eq(0).css('display', 'block');
		var aSubject = new Array('responsibility','design-can-change','sustainability','qanda');
		$('#sidebar h3').hover(function () {
				nSubjectIndex = aSubject.indexOf($(this).attr('id'));
				$.each(aSubject, function(i, val) {
					$('#ethics #sidebar h3').eq(i).css('color', '#999999');
					//alert("i:" + i + "ind: " + nSubjectIndex);
					if (i !== nSubjectIndex) {
						$('#ethics #textarea1 #statements .statement').eq(i).stop(true, true);
						$('#ethics #textarea1 #statements .statement').eq(i).css('display', 'none');
					}
				});
				$('#ethics #textarea1 #statements .statement').eq(nSubjectIndex).fadeIn(function() {
					if (! $.support.opacity) {
						this.style.removeAttribute('filter');
					}
				});
				$(this).css('color', '#0099FF');
				$('#ethics #textarea1 #statements .statement').eq(nSubjectIndex).fadeIn(100);
			}
		);
	}
	
	/* portfolio */
	$('#nav li').click(function () {
		if ($('#carousel').length > 0) {
			callToActionscript(this.id);
		} else {
			window.location = '/portfolio/' + this.id;
		}
	});
	
	/* contact-us */
	if (sUrlParam == 'contact-us') {
		// Check for browser compatibility
		if (GBrowserIsCompatible()) {
			// Define new control function
			function MapTypeControl() {}
			// Subclass Gcontrol via prototype
			MapTypeControl.prototype = new GControl();
			// Initialize
			MapTypeControl.prototype.initialize = function(MapT) {		
				// Conatiner
				var container = document.createElement('div');
				container.setAttribute('id', 'container');
				// Create buttonA 'Satellite'
				var buttonA = document.createElement('div');
				buttonA.className='control';
				container.appendChild(buttonA);
				buttonA.appendChild(document.createTextNode('Satellite'));
				GEvent.addDomListener(buttonA, "click", function() {
					map.setMapType(G_SATELLITE_MAP);
				});
				// Create buttonB 'Hybrid'
				var buttonB = document.createElement('div');
				buttonB.className='control';
				container.appendChild(buttonB);
				buttonB.appendChild(document.createTextNode('Hybrid')); 
				GEvent.addDomListener(buttonB, "click", function() {
					map.setMapType(G_HYBRID_MAP );
				});
				// Create oButtonC 'Street'
				var buttonC = document.createElement('div');
				buttonC.className='control';
				container.appendChild(buttonC);
				buttonC.appendChild(document.createTextNode('Street'));
				GEvent.addDomListener(buttonC, "click", function() {
					map.setMapType(G_NORMAL_MAP);
				});
				// Append buttons to API 'oContainer'
				map.getContainer().appendChild(container);
				return container;
			}
			var TopRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0,0));
			// Initilize map object
			var map = new GMap2(document.getElementById('map'));
			// Add Controls
			map.addControl(new GLargeMapControl3D());
			map.addControl(new MapTypeControl(), TopRight);
			map.enableScrollWheelZoom();
			// Set map coordinates
			map.setCenter(new GLatLng(48.4269500, -123.3662200), 16);
			var latlng = new GLatLng(48.4269500, -123.3662200);
			// Set marker icon
			var boneIcon = new GIcon(G_DEFAULT_ICON);
			boneIcon.image = './media/map-icon.png';
			boneIcon.iconSize = new GSize(106, 55);
			boneIcon.iconAnchor = new GPoint(40, 51);
			boneIcon.shadow = './media/map-shadow.png';
			boneIcon.shadowSize = new GSize(106, 55);
			markerOptions = { icon:boneIcon };
			// Add marker
			map.addOverlay(new GMarker(latlng, markerOptions));
			//map.addOverlay(new GMarker(latlng));
		}
	}
});

/*
$(window).unload( function () { 
	if ($('#carousel').length > 0) {
		var swf = document.getElementById("carousel");
		swf.unloadAll();
		alert ('unload');
	}
});
*/

// Initialize portfolio
function initPortfolio() {
	if (sUrlFlag == 'portfolio') {
		callToActionscript(sUrlParam);
	}
}

// Highlight selected nav items
function highlightCat(cat) {
	setTimeout(function () {
		$('#nav li').removeClass('selected');
		if ($('#' + cat + '').length > 0 ) {
			$('#' + cat + '').addClass('selected')
		}
		
	}, 0);
}

// IndexOf method - IE fix 
if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
  }
}

/* Retrieve XML via AJFAX */
// Call from onload event
function getCaption(clientId) {
	$.get('/xml/carousel.xml',{},function(xml){
		$('piece',xml).each(function(i) {
			if (i == clientId) {
				var sCaption = $(this).find("caption").text();
				var sTitle = $(this).find("title").text();
				$('#portfolio #textarea1 #description').stop(true, true).css('display', 'none');
				$('#portfolio #textarea1 #description').stop(true, true).html("<h2>" + sTitle + " | </h2><p>" + sCaption + "</p>");
				$('#portfolio #textarea1 #description').stop(true, true).fadeIn("normal");
			}
		});	
	});
}
// Call to AS3
function callToActionscript(category) {
	var swf = document.getElementById("carousel");
	swf.sendToActionscript(category);
}
