// Cookie functions inspired form kaosweaver but  these functions really better
// by Ferruh Mavituna
function writeCookie(name, value) {
    //Fix Days
    var days = 1;
    if(days==null || days=="")days=365;
    
    // Set Date
    var d=new Date();
    d.setTime(d.getTime()+(days*24*60*60*1000));
    var expires="; expires="+d.toGMTString();
    // Write Cookie
    document.cookie = name+"="+value+expires+"; path=/"; 
}

function readCookie(name){ // by .fm
    var c=document.cookie ; 
    if (c.indexOf(name)!=-1) { 
        pos1=c.indexOf("=", c.indexOf(name))+1; 
        pos2=c.indexOf(";",pos1);  
            // If last cookie
            if(pos2==-1)    pos2=c.length;;
        
        data=c.substring(pos1,pos2); 
        return data;
    }
}

/**
 ** Click content view check box
 **/
function checkContent(obj) {
	if (obj.id == "chkPhoto") {
		if (obj.checked == true) {
			map.addControl(new PhotoOffControl());
			photoViewMap('true');
		} else {
			map.addControl(new PhotoOnControl());
			photoViewMap('false');
		}
	} else if (obj.id == "chkVideo") {
		if (obj.checked == true) {
			map.addControl(new VideoOffControl());
		} else {
			map.addControl(new VideoOnControl());
		}
	} else {
	}
}

/**
 ** when mouse over on search rollover star on main page
 **/
function minimum_rating_mouseover() {
	$j('#minimum_current_rating').css('width', '0px');
}

/**
 ** when mouse out on search rollover star on main page
 **/
function minimum_rating_mouseout() {
	if (getCookie("filter_rating") == null || getCookie("filter_rating") == '')
		setCookie("filter_rating", 3, 0);
	$j('#minimum_current_rating').css('width', (parseInt(getCookie("filter_rating")) * 18) + 'px');
}

/**
 ** search rollover star on main page
 **/
function checkrating(rating) {
	setCookie("filter_rating", rating, 0);
	$j('#minimum_current_rating').css('width', (18 * parseInt(rating)) + 'px');
	HTML_AJAX.replace('mapredraw','index.php?q=ajx_reloadallmap&rating='+rating+'&state=true');
}

/**
 ** click add/remove button in article writer and show location select dialog
 **/
function showSelectLocation() {
	var left = $j('#viewsellocation').offset().left;
	var	top = $j('#viewsellocation').offset().top;
	var oDiv = document.getElementById('divsellocation');

	$j('#divsellocation').css('left', (left - parseInt(oDiv.style.width) + 90) + 'px');
	$j('#divsellocation').css('top', (top + 12) + 'px');
	$j('#divsellocation').fadeIn('slow');
}

/**
 ** initialize current selected list
 **/
function initializeSelect() {
	selected_list = '';
	selected_number = 0;
	$j("#multiple_warning1").html("0 Photo Selected");
	$j("#multiple_warning2").html("0 Photo Selected");
	document.getElementById("selected_number").innerHTML = selected_number;

	$j("#photo_title").val('');
	$j("#photo_description").val('');
	$j("#photo_tags").val('');
}

/**
 ** close location select dialog in article writer
 **/
function hideSelectLocation() {
	$j('#divsellocation').fadeOut('slow');
}

/**
 ** Select photo in Photo browser
 **/
function select_photo(obj, photo_request, type) {
	if (obj.alt.indexOf("selected") != -1) {
		selected_number--;
		obj.alt = 'unselect';
		obj.style.border = "";
		obj.style.marginTop = "2px";
		obj.style.marginLeft = "2.5px";
		obj.style.marginRight = "2.5px";
		obj.style.marginBottom = "1px";

		if (selected_list.indexOf(photo_request) != -1) {
			if (selected_list[selected_list.indexOf(photo_request) + photo_request.length] == '*') {
				selected_list = selected_list.replace(photo_request + '*', '');
			} else {
				selected_list = selected_list.replace(photo_request, '');
			}
		}
	} else {
		selected_number++;
		
		$j('#selected_photo').val(photo_request);
		$j('#selected_type').val(type);

		obj.alt = 'selected';
		obj.style.border = "solid 2px red";
		obj.style.marginTop = "0px";
		obj.style.marginLeft = "0.5px";
		obj.style.marginRight = "0.5px";
		obj.style.marginBottom = "0px";
		
		if (selected_list == '') {
			selected_list += photo_request;
		} else {
			selected_list += '*' + photo_request;
			selected_list = selected_list.replace('**', '*');
		}

		$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
		$j("#waiting_obj").fadeIn("slow");
		$j.ajax({
			type: "POST",
			url: "actions/photoprocess.php",
			data: "photoSelect=true&photo_request=" + photo_request + "&type=" + type,
			success: function(data){
				$j("#photo_title").val("");
				$j("#photo_description").val("");
				$j("#photo_tags").val("");

				data_arr = data.split("**");

				$j("#photo_title").val(data_arr[0]);
				$j("#photo_description").val(data_arr[1]);
				$j("#photo_tags").val(data_arr[2]);
				$j("#waiting_obj").html("");
			}
		});
	}
	if (selected_number > 1) {
		$j("#multiple_warning1").html("Warning : Multiple Photos Selected");
		$j("#multiple_warning2").html("Warning : Multiple Photos Selected");
	} else {
		$j("#multiple_warning1").html(selected_number + " Photo Selected");
		$j("#multiple_warning2").html(selected_number + " Photo Selected");
	}
	document.getElementById("selected_number").innerHTML = selected_number;
}

/**
 ** when mouse over on photo search rollover star on photo page
 **/
function photo_search_rating_mouseover() {
	$j('#photo_current_rating').css('width', '0px');
}

/**
 ** when mouse out on photo search rollover star on photo page
 **/
function photo_search_rating_mouseout() {
	if (getCookie("photo_filter_rating") == null || getCookie("photo_filter_rating") == '')
		setCookie("photo_filter_rating", 3, 0);
	$j('#photo_current_rating').css('width', (parseInt(getCookie("photo_filter_rating")) * 14) + 'px');
}

/**
 ** search photos when click rollover star on photo page
 **/
function photo_search_rating(rating) {
	setCookie("photo_filter_rating", rating, 0);
	$j('#photo_rating').val(rating);
	$j('#photo_current_rating').css('width', (14 * parseInt(rating)) + 'px');
}

/**
 ** Adding selected Flickr photos to Mappica
 **/
function addFlickr2Mappica() {
	if (selected_list == '') {
		$j("#waiting_obj").fadeOut('fast');
		$j("#waiting_obj").html('!! There is no selected photo. !!');
		$j("#waiting_obj").fadeIn('slow');
		return;
	}
	
	var assignCatFlag = false;
	var assignCat = '';
	for(var i = 0; i < $j('*:checkbox.assignCat').length; i++) {
		if ($j('*:checkbox.assignCat')[i].checked == true) {
			assignCatFlag = true;
			if (assignCat == '') {
				assignCat += $j('*:checkbox.assignCat')[i].value;
			} else {
				assignCat += '*' + $j('*:checkbox.assignCat')[i].value;
			}
		}
	}

	if (assignCatFlag == false) {
		$j("#waiting_obj").fadeOut('fast');
		$j("#waiting_obj").html('!! You must assign at least one category. !!');
		$j("#waiting_obj").fadeIn('slow');
		return;
	}
	
	$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
	$j("#waiting_obj").fadeIn('slow');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "add_flickr_mapica=true&selected_list=" + selected_list + "&assignCat=" + assignCat,
		success: function(data){
			for(var i = 0; i < $j('*:checkbox.assignCat').length; i++) {
				$j('*:checkbox')[i].checked = false;
			}
			$j("#waiting_obj").fadeOut('fast');
			if (data == "error") {
				$j("#waiting_obj").html('Failure to add selected photos to Mappica.');
			} else {
				if (data.indexOf('already') != -1) {
					$j("#waiting_obj").html('These photos are already in Mappica.');
				} else {
					$j("#waiting_obj").html('Success to add selected photos to Mappica.');
				}
				added_list = data.split("***");
				for (var j = 0; j < added_list.length; j++) {
					if (!isNaN(added_list[j])) {
						doted_photo = "<img alt='photo in mappica' src='images/green_dot.gif' style='position:absolute; margin:5px 5px 0px 60px'/>";
						$j('#' + added_list[j]).html(doted_photo + $j('#' + added_list[j]).html());
					}
				}
			}
			$j("#waiting_obj").fadeIn('slow');
		}
	});
}

/**
 ** remove selected flickr photos associated with mappica from mappica
 **/
function removeFlickrPhotos() {
	if (selected_list == '') {
		$j("#waiting_obj").fadeOut('fast');
		$j("#waiting_obj").html('!! There is no selected photo. !!');
		$j("#waiting_obj").fadeIn('slow');
		return;
	}
	var search_option = $j("#photo_search_option").val();

	$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
	$j("#waiting_obj").fadeIn('slow');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "removeFlickrPhotos=true&selected_list=" + selected_list,
		success: function(data){
			if (search_option != 'flickr' && search_option != 'onlyflickr' && search_option != '') {
				initializeSelect();
			}

			$j("#waiting_obj").fadeOut('slow');
			if (data.indexOf('not') != -1) {
				$j("#waiting_obj").html('This photo is not associated with mappica.');
			} else {
				$j("#waiting_obj").html('Success to remove selected photos from Mappica.');
			}
			remove_list = data.split("***");
			for (var j = 0; j < remove_list.length; j++) {
				if (!isNaN(remove_list[j]) && remove_list[j] != '') {
					var oTD = $j('#' + remove_list[j]);
					if (oTD) {
						var original_html = oTD.html();
						var new_html = original_html.substr(original_html.indexOf(">") + 1);
						$j('#' + remove_list[j]).html(new_html);
					}
				}
			}
			$j("#waiting_obj").fadeIn('slow');
		}
	});
}

/**
 ** photo page navigation
 **/
function pageViewPhoto(obj, page) {
	var fuser = $j("#fuser").val();
	var search_option = $j("#photo_search_option").val();
	
	var filter = '';
	var keyword = $j("#photo_search_key").val();
	var rating = $j("#photo_rating").val();

	if ($j('#photo_filter').val() == 'on') {
		filter = "&keyword=" + keyword + "&rating=" + rating + "&ratingCompare=" + $j('#photo_rating_operator').val();
	}

	initializeSelect();

	if (page == current_photo_page)
		return;
	if (obj != null)
		obj.style.color = '#FF0000';
	current_photo_page = page;
	var first_photo_page = 0;
	var last_photo_page = 0;
	first_photo_page = parseInt(page) - 2;
	
	if (first_photo_page < 1)
		first_photo_page = 1;

	last_photo_page = parseInt(page) + 2;
	if (last_photo_page > photo_page_count)	{
		last_photo_page = photo_page_count;
		first_photo_page = photo_page_count - 4;
		if (first_photo_page < 1)
			first_photo_page = 1;
	}

	if (first_photo_page == 1) {
		if (photo_page_count >= 5) {
			last_photo_page = 5;
		} else {
			last_photo_page = photo_page_count;
		}
	}
	
	pagination_html = "";
	for(var i = first_photo_page; i <= last_photo_page; i++) {
		if (i == page)
			pagination_html += "<a href='javascript://' onclick='pageViewPhoto(this, " + i + ")' class='cur_page_number'>" + i + "</a>&nbsp;&nbsp;";
		else
			pagination_html += "<a href='javascript://' onclick='pageViewPhoto(this, " + i + ")' class='page_number'>" + i + "</a>&nbsp;&nbsp;";
	}
	
	next_page_html = "<a href='javascript://' onclick='pageViewPhoto(null, current_photo_page + 1)' class='next_page'>Next Page</a>";
	prev_page_html = "<a href='javascript://' onclick='pageViewPhoto(null, current_photo_page - 1)' class='prev_page'>Prev Page</a>&nbsp;";

	$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
	$j("#waiting_obj").fadeIn('slow');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "photo_pagination=true&page=" + page + "&fuser=" + fuser + "&search_option=" + search_option + filter,
		success: function(data){
			if (page == photo_page_count)
				$j("#next_page").html('');
			else
				$j("#next_page").html(next_page_html);

			if (page == 1)
				$j("#prev_page").html('');
			else
				$j("#prev_page").html(prev_page_html);

			$j("#photo_pagination").html(pagination_html);
			$j("#photo_list").html(data);
			$j("#waiting_obj").fadeOut('fast');
			$j("#waiting_obj").html('');
		}
	});
}

/**
 ** flickr photo page navigation
 **/
function pageViewFlickrPhoto(obj, page) {
	var fuser = $j("#fuser").val();

	initializeSelect();

	if (page == current_photo_page)
		return;
	if (obj != null)
		obj.style.color = '#FF0000';
	current_photo_page = page;
	
	var search_option = $j("#photo_search_option").val();
	
	var keyword = '';
	if ($j('#photo_filter').val() == 'on') {
		keyword = $j("#photo_search_key").val();
	}

	$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
	$j("#waiting_obj").fadeIn('slow');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "flickr_photo_pagination=true&page=" + page + "&fuser=" + fuser + "&search_option=" + search_option + "&keyword=" + keyword,
		success: function(data){
			$j("#photo_board").html(data);
			$j("#waiting_obj").fadeOut('fast');
			$j("#waiting_obj").html('');
		}
	});
}

/**
 ** opening collection dialog in photo view window
 **/
function openCollection() {
	closeUploadMappica();
	closeInformUser();
	closeAssignCat();
	$j("span.openCollectionResult").html("<img src='images/ajax/small_waiting.gif' alt='' />");
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "openCollection=true",
		success: function(data){
			$j("span.openCollectionResult").html('');
			$j('#collection').html(data);
			$j('#collection').fadeIn('slow');
		}
	});
}

/**
 ** close collection dialog in photo view window
 **/
function closeCollection() {
	$j('#collection').fadeOut('slow');
}

/**
 ** adding new collection in photo window
 **/
function addNewCollection() {
	$j("div#collectionResult").fadeIn('slow');
	if ($j("#collection_name").val() == '')	{
		$j("div#collectionResult").html("Collection name is required.");
		return;
	}
	$j("div#collectionResult").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "addNewCollection=true&collection=" + $j("#collection_name").val(),
		success: function(data){
			$j.ajax({
				type: "POST",
				url: "actions/photoprocess.php",
				data: "addNewCollectionList=true&selected=" + $j("#photo_search_option").val(),
				success: function(datalist){
					$j("div#collectionResult").html("");
					$j('#photo_search_option').html(datalist);
					$j("div#collectionResult").fadeIn('slow');
				}
			});
			$j("div#collectionResult").html("");
			$j('#collection').html(data);
			$j("div#collectionResult").fadeIn('slow');
		}
	});
}

/**
 ** add photos to selected collection
 **/
function addToCollection() {
	$j("div#collectionResult").fadeIn('slow');
	
	if (selected_list == '') {
		$j("div#collectionResult").html("Please select photos from photo list.");
		return;
	}
	if ($j("#collections").val() == '')	{
		$j("div#collectionResult").html("Please select collection from dropdown list.");
		return;
	}

	$j("div#collectionResult").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "addToCollection=true&collection=" + $j("#collections").val() + "&selected_list=" + selected_list,
		success: function(data){
			$j("div#collectionResult").html(data);
		}
	});
}

/**
 ** opening dialog to upload non-flickr photos to mappica
 **/
function openUploadMappica() {
	closeCollection();
	closeInformUser();
	closeAssignCat();
	$j('#photo_upload_div').fadeIn('slow');
}

/**
 ** close upload dialog to upload non-flickr photos to mappica
 **/
function closeUploadMappica() {
	$j('#photo_upload_div').fadeOut('slow');
	$j("#title").val("");
	$j("#tags").val("");
	$j("#description").val("");
	$j("#photo_file").val("");
	$j('#uploadResult').html("");
}

/**
 ** adding geotagged photos on map
 **/
function photoViewMap(state) {
	if (state == 'true')
		HTML_AJAX.replace('mapredraw','index.php?q=ajx_reloadmap&photo=true&state=true');
	else
		HTML_AJAX.replace('mapredraw','index.php?q=ajx_reloadmap&photo=true&state=false');
}

/**
 ** opening dialog to assign category into photo
 **/
function openAssignCat(obj) {
	closeUploadMappica();
	closeCollection();
	closeInformUser();

	var left = $j('#'+obj.id).offset().left;
	var	top = $j('#'+obj.id).offset().top + 15;
	$j('#assignCategory').css('left', left);
	$j('#assignCategory').css('top', top);
	$j('#assignCategory').fadeIn('slow');
}

/**
 ** closing dialog to assign category into photo
 **/
function closeAssignCat() {
	$j('#assignCategory').fadeOut('fast');
}

/**
 ** photo search filter on
 **/
function photoFilterOn() {
	$j('a.photo-filter-control').css('color', '#000000');
	$j('#filterOn').css('color', '#0054BF');
	$j('#filterOn').css('text-decoration', 'none');
	$j('#filterOn').css('cursor', 'default');
	$j('#filterOff').css('text-decoration', 'underline');
	$j('#filterOff').css('cursor', 'pointer');
	$j('#photo_filter').val('on');
}

/**
 ** photo search filter off
 **/
function photoFilterOff() {
	$j('a.photo-filter-control').css('color', '#000000');
	$j('#filterOff').css('color', '#0054BF');
	$j('#filterOff').css('text-decoration', 'none');
	$j('#filterOff').css('cursor', 'default');
	$j('#filterOn').css('text-decoration', 'underline');
	$j('#filterOn').css('cursor', 'pointer');
	$j('#photo_filter').val('off');
}

/**
 ** search photo with enter tags/keyword
 **/
function photoEnterSearch(e) {
	var keyCode	= window.event ? window.event.keyCode : e.which;
	if (keyCode == 13) {
		if ($j("#photo_search_key").val() != '') {
			photoFilterOn();
			searchPhoto();
		}
		/*if ($j('#photo_filter').val() != 'on') {
			$j("#waiting_obj").fadeOut('fast');
			$j("#waiting_obj").html('!!Photo filter is off now.!!');
			$j("#waiting_obj").fadeIn('slow');
			return;
		}*/
	}
}

/**
 ** search photo in photo window
 **/
function searchPhoto() {
	current_photo_page = 1;
	var option = $j("#photo_search_option").val();

	var keyword = '';
	var rating = '';
	if ($j('#photo_filter').val() == 'on') {
		keyword = $j("#photo_search_key").val();
		rating = $j("#photo_rating").val();
	}
	var fuser = $j("#fuser").val();

	if (option == '') {
		$j("#waiting_obj").fadeOut('fast');
		$j("#waiting_obj").html('You must select photo search option.');
		$j("#waiting_obj").fadeIn('slow');
		return;
	}

	initializeSelect();

	$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
	$j("#waiting_obj").fadeIn('slow');
	
	var filter = '';
	if ($j('#photo_filter').val() == 'on') {
		filter = "&keyword=" + keyword + "&rating=" + rating + "&ratingCompare=" + $j('#photo_rating_operator').val();
	}

	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "photoSearch=true&option=" + option + "&fuser=" + fuser + filter,
		success: function(data){
			$j('#photo_board').html(data);
			$j("#waiting_obj").fadeOut('fast');
			$j("#waiting_obj").html('');
		}
	});
}

/**
 ** search photo in photo window with rating compare
 **/
function searchRatingPhoto(compare) {
	if ($j('#photo_filter').val() != 'on') {
		$j("#waiting_obj").fadeOut('fast');
		$j("#waiting_obj").html('!!Photo filter is off now.!!');
		$j("#waiting_obj").fadeIn('slow');
		return;
	}

	$j('a.photo-rating-filter').css('color', '#000000');
	$j('a#photo_' + compare + 'Rating').css('color', '#0054BF');
	$j('#photo_rating_operator').val(compare);

	current_photo_page = 1;

	initializeSelect();

	var option = $j("#photo_search_option").val();
	var keyword = $j("#photo_search_key").val();
	var rating = $j("#photo_rating").val();
	var fuser = $j("#fuser").val();

	$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
	$j("#waiting_obj").fadeIn('slow');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "photoSearch=true&option=" + option + "&fuser=" + fuser + "&keyword=" + keyword + "&rating=" + rating + "&ratingCompare=" + compare,
		success: function(data){
			$j('#photo_board').html(data);
			$j("#waiting_obj").fadeOut('fast');
			$j("#waiting_obj").html('');
		}
	});
}

/**
 ** upload photo to mappica
 **/
function uploadPhotoMappica() {
	$j('#uploadResult').hide();
	if ($j("#title").val() == "") {
		$j('#uploadResult').fadeIn('slow');
		$j('#uploadResult').html("Photo title is required.");
		return;
	} else if ($j("#tags").val() == "") {
		$j('#uploadResult').fadeIn('slow');
		$j('#uploadResult').html("Photo tags is required.");
		return;
	} else if ($j("#description").val() == "") {
		$j('#uploadResult').fadeIn('slow');
		$j('#uploadResult').html("Photo description is required.");
		return;
	} else if ($j("#photo_file").val() == "") {
		$j('#uploadResult').fadeIn('slow');
		$j('#uploadResult').html("Photo file is required.");
		return;
	} else {
		var uploadHandler = {
			upload: function(o) {
				var r = eval('(' + o.responseText + ')');
				if(r.hasError){
					$j('#uploadResult').html("Failure to upload photo to mappica.");
				} else {
					$j('#uploadResult').html("Success to upload photo to mappica. Size : " + r.imageSize);
				}
			}
		};
		$j('#uploadResult').html('Uploading... <img src="images/ajax/waiting_photo_bar.gif" alt="" />');
		$j('#uploadResult').fadeIn('slow');
		//the second argument of setForm is crucial,
		//which tells Connection Manager this is an file upload form
		YAHOO.util.Connect.setForm('uploadForm', true);
		YAHOO.util.Connect.asyncRequest('POST', 'actions/photoupload.act.php', uploadHandler);
	}

};

/**
 ** add photo into article editor
 **/
function addPhotoToEditor(id) {
	$j('#search_photo_result').html('<img src="images/ajax/waiting.gif">');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "addPhotoToEditor=true&id=" + id,
		success: function(data){
			//tinyMCE.get('body').setContent(tinyMCE.get('body').getContent() + data);
			tinyMCE.execInstanceCommand("body", "mceInsertContent", false, data);
			$j('#search_photo_result').html('');
		}
	});
}	

/**
 ** search photo in article editor
 **/
function photoSort(sortby, type) {
	$j('#photo_sortby').val(sortby);
	$j('#search_photo_result').html('<img src="images/ajax/waiting.gif">');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "photoSearchEditor=true&keyword=" + $j("#photo_search").val() + "&sortby=" + sortby + "&type=" + type + "&locid=" + $j("#locid").val(),
		success: function(data){
			$j('#photo_result').html(data);
			$j('#search_photo_result').html('');
		}
	});
}

function photoSearch(type) {
	$j('#search_photo_result').html('<img src="images/ajax/waiting.gif">');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "photoSearchEditor=true&keyword=" + $j("#photo_search").val() + "&sortby=" + $j('#photo_sortby').val() + "&type=" + type + "&locid=" + $j("#locid").val(),
		success: function(data){
			$j('#photo_result').html(data);
			$j('#search_photo_result').html('');
		}
	});
}

function photoEnterSearchInEditor(e, type) {
	var keyCode	= window.event ? window.event.keyCode : e.which;
	if (keyCode == 13) {
		photoSearch(type);
	}
}

/**
 ** add photo to location
 **/
function addPhotoToLoc(id) {
	$j('#map_photo_list_result').html('<img src="images/ajax/waiting_photo_bar.gif">');
	$j('#map_photo_list_result').fadeIn('slow');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "addPhotoToLoc=true&id=" + id + "&locid=" + $j('#locid').val(),
		success: function(data){
			$j('#map_photo_list_result').fadeOut('slow');
			$j('#map_photo_list_result').html('');
			$j('#map_photo_list').html(data);
		}
	});
}	

/**
 ** add photo to location
 **/
function removePhotoToLoc(id) {
	$j('#map_photo_list_result').html('<img src="images/ajax/waiting_photo_bar.gif">');
	$j('#map_photo_list_result').fadeIn('slow');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "removePhotoToLoc=true&id=" + id + "&locid=" + $j('#locid').val(),
		success: function(data){
			$j('#map_photo_list_result').fadeOut('slow');
			$j('#map_photo_list_result').html('');
			$j('#map_photo_list').html(data);
		}
	});
}	

/**
 ** when click marker on the map, show photo list of this location
 **/
function viewLocPhotoList(id) {
	$j('#locid').val(id);
	$j('#map_photo_list_result').html('<img src="images/ajax/waiting_photo_bar.gif">');
	$j('#map_photo_list_result').fadeIn('slow');
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "viewLocPhotoList=true&locid=" + id,
		success: function(data){
			$j('#map_photo_list_result').fadeOut('slow');
			$j('#map_photo_list_result').html('');
			$j('#photo_viewer').html(data);
		}
	});
}

/**
 ** open dialog to inform flickr user to delete all his photos
 **/
function openInformUser() {
	if ($j('#fuser').val() == 'not linked') {
		$j("#waiting_obj").fadeOut('fast');
		$j("#waiting_obj").html("Your account is not associated with Flickr.");
		$j("#waiting_obj").fadeIn('slow');
		return;
	}

	$j('div#authFlickrResult').fadeOut('fast');
	$j('#fAuthUser').val('');
	$j('#fAuthPassword').val('');
	closeUploadMappica();
	closeCollection();
	closeAssignCat();
	$j('div#informUser').fadeIn('slow');
}

/**
 ** close dialog to inform flickr user to delete all his photos
 **/
function closeInformUser() {
	$j('div#informUser').fadeOut('slow');
}

/**
 ** authorize to Flickr account
 **/
function authorizeFlickr() {
	$j("div#authFlickrResult").fadeOut('fast');
	$j('div#authFlickrResult').html('<img src="images/ajax/waiting_photo_bar.gif">');
	$j("div#authFlickrResult").fadeIn('slow');

	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "checkAuthFlickr=true&fuser=" + $j("#fuser").val(),
		success: function(data){
			$j("div#authFlickrResult").fadeOut('fast');
			if (data == "true")	{
				$j("div#authFlickrResult").html("You are already authorized with Flickr.");
				$j("div#authFlickrResult").fadeIn('slow');
			} else {
				document.location.href = "?q=getimages&authFlickr=true";
			}
		}
	});
}

/**
 ** write Flickr setting to Mappica
 **/
function writeFlickr2Mappica() {
	if ($j('#selected_photo').val() == '' || $j('#selected_type') == '') {
		$j("#waiting_obj").fadeOut('slow');
		$j("#waiting_obj").html("There is no selected photo.");
		$j("#waiting_obj").fadeIn('slow');
	} else {
		if ($j('#selected_type').val() != 'flickr') {
			$j("#waiting_obj").fadeOut('slow');
			$j("#waiting_obj").html("You must select your flickr photo.");
			$j("#waiting_obj").fadeIn('slow');
		} else {
			$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
			$j("#waiting_obj").fadeIn('slow');
			$j.ajax({
				type: "POST",
				url: "actions/photoprocess.php",
				data: "writeFlickr2Mappica=true&flickr_id=" + $j('#selected_photo').val() + 
					"&title=" + $j("#photo_title").val() + 
					"&description=" + $j("#photo_description").val() + 
					"&tags=" + $j("#photo_tags").val(),
				success: function(data){
					$j("#waiting_obj").fadeOut('slow');
					$j("#waiting_obj").html(data);
					$j("#waiting_obj").fadeIn('slow');
				}
			});
		}
	}
}

/**
 ** write Mappica settings to Flickr
 **/
function writeMappica2Flickr() {
	var fuser = $j("#fuser").val();

	if ($j('#selected_photo').val() == '' || $j('#selected_type') == '') {
		$j("#waiting_obj").fadeOut('slow');
		$j("#waiting_obj").html("There is no selected photo.");
		$j("#waiting_obj").fadeIn('slow');
	} else {
		if ($j('#selected_type').val() != 'flickr') {
			$j("#waiting_obj").fadeOut('slow');
			$j("#waiting_obj").html("You must select your flickr photo.");
			$j("#waiting_obj").fadeIn('slow');
		} else {
			$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
			$j("#waiting_obj").fadeIn('slow');
			$j.ajax({
				type: "POST",
				url: "actions/photoprocess.php",
				data: "writeMappica2Flickr=true&flickr_id=" + $j('#selected_photo').val() + 
					"&title=" + $j("#photo_title").val() + 
					"&description=" + $j("#photo_description").val() + 
					"&tags=" + $j("#photo_tags").val() +
					"&fuser=" + fuser,
				success: function(data){
					$j("#waiting_obj").fadeOut('slow');
					$j("#waiting_obj").html(data);
					$j("#waiting_obj").fadeIn('slow');
				}
			});
		}
	}
}

/**
 ** show photo with lightbox
 **/
function view_photo(imageLink, id) {
	imageType = 'normal';
	$j("#photoLightboxResult").html("<img src='images/ajax/waiting.gif' alt='' />");
	$j("#expandResult").html("<img src='images/ajax/waiting.gif' alt='' />");
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "getPhotoDetail=true&id=" + id + "&imageType=" + imageType,
		success: function(data){
			$j("#photoLightboxResult").html("");
			$j("#expandResult").html("");
			dataArr = data.split("***");
			$j.fn.lightbox.ajaxStart(id, imageLink, dataArr);
		}
	});
}

/**
 ** set photo rate in lightbox
 **/
function setPhotoRate(id, rating) {
	$j("#photo_rating_field").html("<img src='images/ajax/waiting.gif' alt='' />");
	$j.ajax({
		type: "POST",
		url: "actions/photoprocess.php",
		data: "setPhotoRate=true&id=" + id + "&rating=" + rating,
		success: function(data){
			$j("#photo_rating_field").html(data);
		}
	});
}

/**
 ** set my photo detail
 **/
function setMyPhotoDetail() {
	if ($j('#selected_photo').val() == '' || $j('#selected_type') == '') {
		$j("#waiting_obj").fadeOut('slow');
		$j("#waiting_obj").html("There is no selected photo.");
		$j("#waiting_obj").fadeIn('slow');
	} else {
		if ($j('#selected_type').val() != 'mappica') {
			$j("#waiting_obj").fadeOut('slow');
			$j("#waiting_obj").html("You must select your mappica photo.");
			$j("#waiting_obj").fadeIn('slow');
		} else {
			$j("#waiting_obj").html("<img src='images/ajax/waiting_photo_bar.gif' alt='' />");
			$j("#waiting_obj").fadeIn('slow');
			$j.ajax({
				type: "POST",
				url: "actions/photoprocess.php",
				data: "setMyPhotoDetail=true&id=" + $j('#selected_photo').val() + 
					"&title=" + $j("#photo_title").val() + 
					"&description=" + $j("#photo_description").val() + 
					"&tags=" + $j("#photo_tags").val(),
				success: function(data){
					$j("#waiting_obj").fadeOut('slow');
					$j("#waiting_obj").html(data);
					$j("#waiting_obj").fadeIn('slow');
				}
			});
		}
	}
}
