/* Author: Chris How, Primesolid 
 * 
 */

var screenshotCarousel;
var descriptionCarousel;
var carouselIndex;
var map;

$(document).ready(function() {
//	$('#left_col p').hyphenate();
	 $('#left_col p').addClass('hyphenate');
	Hyphenator.run();
	initCarouselIndex();
	if ($('#project_carousel').length > 0 ) {
		screenshotCarousel = $('#project_carousel').jCarouselLite({
			btnPrev: '#carousel_prev_button',
			btnNext: '#carousel_next_button',
			speed: 600,
			easing: "easeInOutExpo"	    
		});
		descriptionCarousel = $('#project_desc_carousel').jCarouselLite({
			btnPrev: '#carousel_prev_button',
			btnNext: '#carousel_next_button',
			speed: 600,
			easing: "easeInOutExpo"	    
		});
	}

	if (typeof(getLocation) != 'undefined' && getLocation) {
		getLoc();
		initGtalkStatus();		
	}

	if (typeof(checkHash) != 'undefined' && checkHash) {
		moveToExample();
	}


	JSTarget.addEvent(window, 'load', function(){JSTarget.init("rel","external","");});
});

function initCarouselIndex() {
	carouselIndex = new Array();
	var examples = $('#project_desc_carousel > ul > li');
	for(var i = 0; i < examples.length; i++) {
		var currentId = $(examples[i]).attr('id').substr(8);
		carouselIndex[i] = currentId;
	}
}

function moveToExample() {
	if (window.location.hash) {
		var hash = window.location.hash.substr(1);
		// check to see if it exists. If not, stay here.
		for(var i = 0; i < carouselIndex.length; i++) {
			if (carouselIndex[i] == hash) {
				// scroll to this one...
				screenshotCarousel.go(i);
				descriptionCarousel.go(i);
				break;
			} 
		}		
	}
}


function initMap() {
	initialMapLocation = new google.maps.LatLng(lat,lng);

	var myOptions = {
			zoom: 12,
			center: initialMapLocation,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false,
			navigationControl: false,
			streetViewControl: false
	};

	setTimeout(function() {	
		map = new google.maps.Map(document.getElementById("map"), myOptions);		
		map.setCenter(locationLatLng);
		if (navigator.userAgent.toLowerCase().indexOf("safari") == -1) {
			styleMap();
		}

	}, 500);
}

function styleMap() {
	var style = mapStyler();
	var styledMapOptions = {
			map: map,
			name: "Primesolid Location Map Type"
	};

	var primesolidMapType =  new google.maps.StyledMapType(style, styledMapOptions);

	map.mapTypes.set('Primesolid Location Map Type', primesolidMapType);
	map.setMapTypeId('Primesolid Location Map Type');

}

function mapStyler() {
	if (day) {
		return ([ { featureType: "all", elementType: "all", stylers: [ { hue: "#FFF600" } ] } ]);		
	} else {
		return ([ { featureType: "all", elementType: "all", stylers: [ { hue: "#FFF600" }, { invert_lightness: true } ] } ]);		
	}
}

function refreshTweet() {
	var url ='/refresh_tweet';
	$.ajax({
		url: url,
		cache: false,
		dataType : 'json',
		success: function(data) {},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});	
}

function getLoc() {
	var url ='/latitude_location';
	$.ajax({
		url: url,
		cache: false,
		dataType : 'json',
		success: function(data) {
			$('#current_city').text(data['location'] + ' (updated ' + data['humanized_timestamp'] + ')');
			day = data['is_day'];
			locationLatLng = new google.maps.LatLng(data['lat'], data['lng']);
			lat = data['lat'];
			lng = data['lng'];
			var locationString = data['location'] + ' (updated ' + data['humanized_timestamp'] + ')';
			$('#location_string').text(locationString);
			initMap();			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			log('getLoc error: ', errorThrown);
		}
	});
}


function initGtalkStatus() {
	var container = $('.chat_link');
	if (container.length > 0) {
		$.ajax({
			url: '/gtalk_status',
			cache: false,
			dataType : 'text',
			success: function(data) {
				updateGtalkStatus(data);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				log('updateGtalkStatus error: ', errorThrown);
			}
		});
	}
}

var GTalkWindow = null;

function showChatWindow() {
	var url = 'http://www.google.com/talk/service/badge/Start?tk=z01q6amlqonckkr415uj33kpvtednq721loaf7973hmp6m60n1dkjepg93o03dfcriujapu660i4vm8lkhg1sip9fkhvajqrbc9a9mshvchce9kb9kkk8806e2avkua330do1l8ag186ef5fbvvjl1icc7r3a7lsvtl8al1q6';
	GTalkWindow = window.open(url, 'LiveChat', 'status=0,toolbar=0,location=0,menubar=0,directories=0,scrollbars=auto,height=450,width=350'); 
}

function updateGtalkStatus(status) {
//	log('updateGtalkStatus: ', data);
	var message;

	var icon_html = '<img id="gtalk" height="9" width="9" src="http://www.google.com/talk/service/badge/Show?tk=z01q6amlqj72vc6f399kqnaubjiqfnroqmba8b3cc5lokt466q5mn9ilved1o7m9bhr0nifoqj7uujjifjn8hulgks8t1gmdj0b9j6g15sphaqs7b6dvlkums8696dbk1u7n3v00fcbgpjkg9li7vv8hfmerpogu39bevrcus&amp;w=9&amp;h=9" alt="">&nbsp;';
	var link_html = '<a href="#" onclick="showChatWindow();return false;">';
		switch(status) {
		case 'idle':
		case 'online':
			message = link_html + 'Online</a>';		
			break;
		case 'busy':
			break;
			message =  'Busy';		
		case 'offline':
		default:
			message = 'Offline';
		break;
		}
	$('.chat_link').html(message);
}




















