﻿var SELECTED_USERID = null;
var ENABLE_AUTORESIZE = true;
var USER_NAME = '';
var IS_ADMIN = false;

var BUTTON_EDIT_TITLE = '';
var BUTTON_REMOVE_TITLE = '';
var BUTTON_REMOVE_QUESTION = '';
var BUTTON_ACTIVATE_TITLE = '';


$(document).ready(function() {
	$('#thmbList').prettyPhoto({
		//prettyPhoto options
		animationSpeed: 'fast',
		allowresize: false,
		modal: false
	});
});

function loadCountryTripsList(countryID) {
	if (countryID != null)
		$('#btnShowAllTrips').parents('.contentRightMenu').show();
	var rndDate = new Date();
	$.getJSON(
		'/ProfileData.ashx?rnd=' + rndDate.getTime() + '&u=' + SELECTED_USERID + '&t=rt' + (countryID != null ? '&iso3=' + countryID : ''),
		function(trips) {
			if (trips != null) {
				$('#profileTrips').empty();

				$.each(trips, function(i, trip) {
					var el;
					if (IS_ADMIN && trip.thumburl.indexOf("thumbNoPhoto.jpg") == -1)
						el = $('<div class="trip"/>')
							.append($('<a/>').attr({ href: trip.url })
								.append($('<img class="thumbn" id="thumbn_' + trip.id + '" src="' + trip.thumburl + '" alt=""/>')))
							.append($('<a id="chngBtn" tid="' + trip.id + '" href="#">' + CHNG_TRIP_THBN + '</a>'))
							.append($('<div class="info"/>')
								.append($('<a/>').attr({ href: trip.url })
									.append($('<span class="title"/>').text(trip.title)))
								.append($('<span class="label"/>').html(trip.desc))
								.append($('<span class="time"/>').text(trip.info))
								.append($('<span class="cleaner"/>'))
							);
					else
						el = $('<div class="trip"/>')
							.append($('<a/>').attr({ href: trip.url })
								.append($('<img class="thumbn" src="' + trip.thumburl + '" alt=""/>')))
							.append($('<div class="info"/>')
								.append($('<a/>').attr({ href: trip.url })
									.append($('<span class="title"/>').text(trip.title)))
								.append($('<span class="label"/>').html(trip.desc))
								.append($('<span class="time"/>').text(trip.info))
								.append($('<span class="cleaner"/>'))
							);



					if (trip.active) {
						el.addClass('on').find('div.info').prepend($('<h2/>').text(USER_NAME + ' ' + USR_ON_TRIP));
					}

					if (IS_ADMIN) {
						el.append($('<span class="adminMenu"/>' + TEXT_TRIP_MENU + ":")
							.append($('<input type="button" value="' + BUTTON_ADD_CONTENT + '" onclick="_gaq.push([\'_trackEvent\', \'USER-Profile\', \'Click\',\'CreatePost\']);return __doCustomPostBack(\'createPost\', \'' + trip.id + '\');"/>'))
						//.append($('<input type="button" value="' + BUTTON_EDIT_TITLE + '" onclick="return __doCustomPostBack(\'editTrip\', \'' + trip.id + '\');"/>'))
						//.append($('<input type="button" value="' + BUTTON_ITINERAY_TITLE + '" onclick="return __doCustomPostBack(\'editItinerary\', \'' + trip.id + '\');"/>'))
						);

						if (!trip.active) {
							el.find('span.adminMenu')
								.append($('<input type="button" value="' + BUTTON_ACTIVATE_TITLE + '" onclick="_gaq.push([\'_trackEvent\', \'USER-Profile\', \'Click\',\'ActivateTrip\']);return __doCustomPostBack(\'activateTrip\', \'' + trip.id + '\');"/>'));
							//.append($('<input type="button" value="' + BUTTON_REMOVE_TITLE + '" onclick="return __doCustomPostBack(\'removeTrip\', \''+ trip.id + '\', true, \'' + BUTTON_REMOVE_QUESTION + '\');"/>'));
						}
						/*
						el.find("#chngBtn").click(function() {
						$.getJSON('/ProfileData.ashx?t=img&u=' + SELECTED_USERID + '&fid=' + trip.id,
						function(data) {
						fillImages(data);
						});
							
						});
						*/
						el.find("#chngBtn").click(function() {
							showDialog(trip.id);
						});



					}

					el.appendTo('#profileTrips');
				});
			}
		}
	);
}

function showDialog(tripId) {
	$('#thmbList').data('tripid', tripId);
	$.getJSON('/ProfileData.ashx?t=img&u=' + SELECTED_USERID + '&fid=' + tripId,
				function(data) {
					fillImages(data);
					$.prettyPhoto.open('#thmbList', '', '');
				});
	
/*
	var $dialog = $('#thmbList').dialog({
		autoOpen: true,
		modal: true,
		resizable: false,
		draggable: false,
		closeOnEscape: true,
		minHeight: 100,
		width: 420,
		height: 245,
		open: function(event, ui) {
			var tripId = $('#thmbList').data('tripid');
			$.getJSON('/ProfileData.ashx?t=img&u=' + SELECTED_USERID + '&fid=' + tripId,
										function(data) {
											fillImages(data);
										});

		},
		close: function(event, ui) {
			$(this).dialog('destroy');
		}
	});
*/
}
function fillImages(data, dialog) {
	$('#thmbList').empty();
	$('#thmbList').append('<div id="thmbImages" style="width:420px;overflow:scroll;overflow-x:hidden;height:310px;" ></div>');
	
	var image ;
	for (var i = 0; i < data.length; i++)
	{ 
		image = data[i];
		var img = $('<img class="thmbImg" id="' + image.imgGuid + '" src="' + image.imgUrl + '">')
						.click(function() {
							var selImg = $(this);
							var tripId = $('#thmbList').data('tripid');
							//alert(selImg.attr('id'));
							$.getJSON('/ProfileData.ashx?t=setimg&u=' + SELECTED_USERID + '&fid=' + tripId + '&img=' + selImg.attr('id'),
								function(data) {
									if (data != null && data.length > 0) {
										//set thumbnail
										var tripId = $('#thmbList').data('tripid');
										$('#thumbn_' + tripId).attr('src', data);
									}
								});
								$('#thmbList').empty();
								$.prettyPhoto.close();
						});
		$('#thmbImages').append(img);
	};
	if ($('#thmbList').height() > ($(window).height()) / 2) {
		$('#thmbList').height(($(window).height()) / 2);
	}
}

function profileMap_focusAllTrips() {
	loadCountryTripsList(null);
	swfobject.getObjectById("ProfileMap").focusAllTrips();
}

function page_SetDimensions() {
	if (ENABLE_AUTORESIZE) {
		page_SetHeight();
	}
}

function page_SetHeight() {
	var windowHeight = $(window).height();

	if (windowHeight < 500)
		windowHeight = 500;

	var sizeDif = windowHeight - $('#page').outerHeight(true);

	var contentLeftHeight = $('div.contentLeftWindow').height() + sizeDif;
	var contentRightHeight = $('div.contentRightWindow').height() + sizeDif;
	var dividerHeight = $('a.contentDivider').height() + sizeDif;

	$('div.contentLeftWindow').height(contentLeftHeight);
	$('div.contentRightWindow').height(contentRightHeight);
}