// Gallery View
/*
#	$.address.change()
#	This event is called (using the jquery.address plugin) when any link with rel="remote:..." is clicked.
#	Event is also called when photo page is accessed by direct (deep-linked) URL
*/

var real_path;

if($('ul.gallery').length){
	$.address.change(function(event) {
		// First check to see if there's a photo in the url
		if(event.value.length>1){
			// Grab photo ID from URL and call gotoPhoto()
			var photo_id = event.value.substring(1);
			//console.log(photo_id);
			gotoPhoto(photo_id);
			// Update Page Title
			var address_parts = $.address.title().split(' #');
			$.address.title(address_parts[0] + ' #' + photo_id + address_parts[1].substring(photo_id.length));
		}else{
			// No photo in the URL, Show orgininal (landing) photo (has class 'o')
			$('li.f').removeClass('f').fadeOut("fast",function(){  $('ul.gallery li.o').addClass('f').show(); });	
		}
	});
}

if($('form.pform').length){
	$('#city').change(function(){
		show_areas($(this).val());
	});
	$('#pform_areas').change(function(){
		show_locations($('#city').val(),$(this).val());
	});
	$('#pcity').change(function(){
		show_albums($('#pcity').val());
	});
}

function loadNextPhotos(startAt){
	// every 10
	var a = startAt % 7;
	if(a==0){
		//alert('trying to load');
		// 10th photo, load 10 more
		$("ul.gallery li img[src='']").each(function(i,e){
			if(i<10){ 
				$(e).attr('src',$(e).attr('lang'));
			}else{
				return true;
			}
		});
	}
}

/*
#	gotoPhoto(photo:mixed)
#	Swaps main (large) photo and replaces css as needed, then calls helper functions
#	inputs: photo (mixed, ID of li element)
#	returns: none 
*/
function gotoPhoto(photo){
	if(photo) $('li.f').removeClass('f').fadeOut("fast",function(){  $('#p'+photo).addClass('f').show(); });
	countPhoto(photo);
	swapThumbs(photo);
}

/*
#	swapThumbs(photo:mixed)
#	Removes css from currently displayed photos and adds css to photos that 'should' be displayed
#	inputs: photo (mixed, ID of li element)
#	returns: none 
*/
function swapThumbs(photo){
	$('li.l').removeClass('l');
	$('li.r').removeClass('r');
	$('#t'+photo).prev().addClass('l');
	$('#t'+photo).next().addClass('r');
	updateCurrentLabel(photo);
}

/*
#	updateCurrentLabel()
#	Updates any element with the specified class with the current series-number of the displayed photograph
#	inputs: none
#	returns: none 
*/
function updateCurrentLabel(photo){
	var current = $('li.f').attr('rel');
	loadNextPhotos(current);
	$('.current_photo').html(current);
	updatePaginationLinks(photo);
}

/*
#	navPhoto(direction=mixed)
#	Manual call (usually via pagination link) to go to next/prev photo
#	inputs: direction(mixed, either 'next' or 'prev')
#	returns: none 
*/
function updatePaginationLinks(photo){
	$('a.pprev').attr({rel:$('#t'+photo).prev().children('a').attr('rel')});
	$('a.pnext').attr({rel:$('#t'+photo).next().children('a').attr('rel')});
	// Update vote link
	$('li.vote').removeClass('disabled voted_up voted_down');
	showComments(photo);
}

/*
#	showComments(photo=mixed)
#	Show li's with class of 'photo', hide all others
#	inputs: photo(mixed, ID of current photo)
#	returns: none 
*/
function showComments(photo){
	$('ul.comments li').hide();
	$('ul.comments li.'+photo).show();
	// update comments count
	$('b.comment_count').html($('li.f').attr('lang'));
	//$('a.flagit').attr({rel:photo});	
}

/* Record a Photo Vote */
$('li.vote a').click(function(){
	if(!$('li.vote').hasClass('disabled')){
		var photo_id = $('li.f').attr('id').substring(1);
		var vote_value = ($(this).hasClass('up')) ? 1 : 0;
		var new_class = ($(this).hasClass('up')) ? 'voted_up' : 'voted_down';
		$.ajax({
	   		url: "/ajax/photos/vote/"+photo_id+"/"+vote_value,
			success: function(msg){
	     		$('li.vote').addClass(new_class + ' disabled');
	   		}
	   	});
	};
});

function countPhoto(photo){
	$.post("/photos/vote/"+photo,false);
}

/* Add a Photo Comment */
/* This is different from other comment forms as the photo id has to be grabbed in real-time */
$("#photo_review_form").submit(function(){ 
	//alert($('li.f').attr('id').substring(1));
	var photo_id = $('li.f').attr('id').substring(1);
	$.post("/ajax/comment/photo", {
		user_id: $('#user_id').val(), item: photo_id, type: $('#type').val(), 
		comment: $('#comment').val(), username: $('#name').val(), email: $('#email').val(), fbuser: $('#fbuser').val()
	}, function(response){
		$('#photo_review_form').html('<p class="response">Thank you. Your photo comment has been submitted!</p>');
		$.get('/photos/photo_comments/'+photo_id,'',function(data){
			$('ul.comments').prepend(data);
			FB.XFBML.Host.parseDomTree();
		});
	
	});
	return false;
});

$("#photo_flag_form").submit(function(){
		if(!$('#pf_name').val() && !$('#pf_email').val()){
			$.post("/ajax/validate/photo_comment", {
				type: 'photo_flag', comment: $('#pf_comment').val(), username: $('#pf_name').val(), email: $('#pf_email').val()
			}, function(response){
				$('div.error_msg').hide();
				$('#photo_flag_form').prepend(response);
				//$('#review_form input, #review_form textarea').css({background:'#fcc'});
			});
		}else{
			$.post("/ajax/flag/photo", {
				vars: $('li.f').attr('id').substring(1), comment: $('#pf_comment').val(), username: $('#pf_name').val(), email: $('#pf_email').val()
			}, function(response){
				$('#photo_flag_form').html('<p class="response">Thank you. This photo as been reported.</p>');
			});
	};
	return false;
});

/*

$(document).ready(function() {
	$('ul.gallery li img').each(function(i,e){
		if(i>9){
			$(e).attr('dir',$(e).attr('src')).attr('src','');
		}
	});
});
*/

// Administrative Helpers
// These functions control the form helpers and AJAX tools used to add/remove/modify albums and album attributes (cover photo, etc.)

// Build photo removal ajax
if($('a.cf').length>0)
{	
	$("a.cf").click(function()
	{	//console.log("/modules/photos/remove/"+$(this).attr('rel')+"/"+$('ul.photos').attr('rel')+"/"+$(this).attr('id'));
		$.get("/modules/photos/remove/"+$(this).attr('rel')+"/"+$('ul.photos').attr('rel')+"/"+$(this).attr('id'),function(response)
		{
			if(response)
			{
			
			}										 
		});
		$(this).parent().parent().children('img').fadeTo('fast',.20);
		$(this).parent().html('Deleted');
	}).confirm({
  		msg:'Sure?',
  		timeout:5000,
  		buttons: {
    			cancel:'',
    			separator:''
  		}
	});

	if($('li.f').length<1){
		// First page load, presumably after adding photos to a new album
		// Since albums won't display until a cover is chosen, let's default to the first photo
		setAsCover($('a.cs:first'));
	};

	$("a.cs").click(function(){
		setAsCover($(this));
		return false;
	});

	$("#album_title, a.change_title").click(function(){
		$('#album_title').hide();
		$('#album_title_form').show();
		
	});
};

/* Share on Facebook */
$('a.fb_share_photo').live("click",function(){
	var link_url = $('li.f a img').attr('rel');;
	var image_url = $('li.f a img').attr('src');
	var name = $('h1:first').html();
	var description = $('p.meta_description_clean').html();
	var attachment = {'name':name,'href':link_url,'description':description,'media': [{'type':'image',
                             
                             'src': image_url,
                             'href': link_url
    }]};
	FB.Connect.streamPublish('', attachment);
	return false;
});

/* Share on Twitter */
$('a.twitter_share_photo').live("click",function(){
	var message = $('p.meta_description').html();
	var permalink = $('li.f a img').attr('rel');

	$.post("/modules/broadcast/get_tiny_url/0/0", { url: permalink },
	   function(data){
			window.open("http://twitter.com/home?status="+message+" "+ data,"width=800,height=450,status=yes,toolbar=yes,menubar=yes")
			return true;
	});
	return false;
});

$('a.fu').click(function(){
	$(this).html('Uploading...');
});

function setAsCover(obj){
	$.get("/modules/photos/cover/"+$(obj).attr('rel')+"/"+$(obj).attr('id'));
	$('li.f').removeClass('f');
	$(obj).parent().parent().addClass('f');
	return false;
}

function show_areas(city_ID){
	$('#area_row').show();
	$('#pform_areas').load("/photos/getselects/areas/"+city_ID,false);
	show_locations(city_ID,false);
	return false;
};

function show_locations(city_ID,area_ID){
	$('#locations_row').show();
	$('#pform_locations').load("/photos/getselects/locations/"+city_ID+"/"+area_ID);
	return false;
};

function show_albums(city_ID){
	$('#albums_row').show();
	$('#pform_albums').load("/photos/getselects/albums/"+city_ID);
	return false;
};



