$.ui.dialog.defaults.bgiframe = true;

var popup = new popupProxy();
var projIdx = 1;
var scrollObj = null;
var winHeight = 333;
var windowScrollPosition = 0;

function initializeMap() {
	var id = $('#map_canvas').attr('locationID');
	var lat = popup.getLatitude(id);
	var lng = popup.getLongitude(id);
	var addy = popup.getAddress(id);
	var name = popup.getLocationName(id);
	
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(lat, lng), 13);
		var point = new GLatLng(lat,lng);
		var marker = new GMarker(point);
		GEvent.addListener(marker, 'click', function() {  
		marker.openInfoWindowHtml(sname + '<br>' + addy);	});  
		map.addOverlay(marker);
		marker.openInfoWindowHtml(sname + '<br>' + addy);   
	}
	
	scrollToPopup();
	return false;
}

function initPopup() {
	$('#upscroller').hover(
		function() {	$(this).addClass('ui-state-hover');	},
		function() {	$(this).removeClass('ui-state-hover');	}
	).mousedown(function() {
		$(this).addClass('ui-state-active');
		scrollStart('.popupVertScrollingContent', 'up');
	}).bind("mouseleave mouseup", function() {
		$(this).removeClass('ui-state-active');
		scrollStop();
	});

	$('#downscroller').hover(
		function() {	$(this).addClass('ui-state-hover');	},
		function() {	$(this).removeClass('ui-state-hover');	}
	).mousedown(function() {
		$(this).addClass('ui-state-active');
		scrollStart('.popupVertScrollingContent', 'down');
	}).bind("mouseleave mouseup", function() {
		$(this).removeClass('ui-state-active');
		scrollStop();
	});
	
	$('#windescription.tip').height($('#winLcolumn').height()-33);
	$('#windescription.project').height($('#winLcolumn').height()-33);
	$('.scrollSpacer').height($('#windescription').height()-40);
	
	scrollToPopup();
	return false;
}

function scrollStart(element, direction) {
    var scrollStep = 11; //pixels to scroll
    var scrollSpeed = 44; //MS between scroll steps, decrease for faster, increase for slower
    
    scrollObj = setInterval(function() {
		switch (direction) {
			case 'down':
				$(element).scrollTop($(element).scrollTop()+scrollStep);
				break;
			case 'up':
				$(element).scrollTop($(element).scrollTop()-scrollStep);
				break;
		}
    },
	scrollSpeed);
}

function scrollStop() {
    if (scrollObj) clearInterval(scrollObj);
}

function scrollToPopup() {
	$(window).scrollTop(windowScrollPosition);
}

function showPopup(type, id) {
	var winPage = '';
	var winTitle = '';
	var winWidth = 333;

	windowScrollPosition = $(window).scrollTop();
	
	$('#popupDiv').dialog('destroy');
	
	switch (type) {
		case 'singleTestimonial':
			winHeight = 291;
			winPage = '/tags/singleTestWindow.cfm?contactID='+id;
			winTitle = popup.getTestimonialTitle(id);
			winWidth = 444;
			break;
		case 'allTestimonies':
			window.location = 'viewAllTestimonials.cfm';
			return false;
			
			winHeight = 555;
			winPage = '/tags/multiTestWindow.cfm';
			winTitle = 'What our customers had to say:';
			winWidth = 444;
			break;
		case 'service':
			winHeight = popup.getServiceHeight(id);
			winPage = '/tags/serviceWindow.cfm?serviceID='+id;
			winTitle = popup.getServiceTitle(id);
			winWidth = popup.getServiceWidth(id);
			break;
		case 'project':
			var thisHeight = popup.getProjectHeight(id);
			winHeight = thisHeight;
			winPage = '/tags/projectWindow.cfm?projectID='+id;
			winTitle = popup.getProjectName(id);
			winWidth = 777;
			break;
		case 'tip':
			var thisHeight = popup.getTipHeight(id);
			winHeight = thisHeight;
			winPage = '/tags/tipWindow.cfm?tipID='+id;
			winTitle = popup.getTipTitle(id);
			winWidth = 777;
			break;
		case 'map':
			winHeight = 500;
			winPage = '/tags/gmap.cfm?locationID='+id;
			winTitle = popup.getMapTitle(id);
			winWidth = 700;
	}

	$("#popupDiv").dialog({
		autoOpen	:	false,
		close		:	function(event, ui) {
			$('#popupDiv').dialog('destroy');
		},
		height		:	winHeight,
		modal		:	true,
		open		:	function(event, ui) {
			ColdFusion.navigate(winPage, 'popupDiv');
			return false;
		},
		position	:	'center',
		resizable	:	false,
		title		:	winTitle,
		width		:	winWidth
	});
	
	$('#popupDiv').dialog('open');
	return false;
}

