// Global vars
var fadeSpeed = 500;
var getAllSources = new Array();
var allSourceLoc;
var stateSelection;
var categorySelection;
var visibleCount;

$(document).ready(function() {

	loadGallery();
	loadUploader();

	// Open images
	$('.thumb').live('click',function(){
		popSources();
		var detailSrc = $(this).attr('alt');
		var detailLink = $(this).children('a').attr('href');
		allSourceLoc = $(this).attr('rel');
		$("#gallery_form").hide();
		populateDetail(detailSrc, detailLink, allSourceLoc);
		return false;
	});

	// Close images
	$('#close_detail').live('click',function(){
		$('#detail_container').fadeOut(fadeSpeed);
		$("#gallery_form").show();
	//	setTimeout(function(){ $('#img_lg').attr("src","images/clear.gif"); },fadeSpeed);
		return false;
	});

	$("#link_to_flickr").live('click',function(){
		$('#close_detail').click();
	});

	// Show Flickr upload
	$('#open_flickr_upload').live('click',function(){
		$('#close_detail').click();
		$("#gallery_form").hide();
		$('#flickr_container').fadeIn(0);
		return false;
	});

	// Hide Flickr upload
	$('#close_flickr').live('click',function(){
		$('#flickr_container').fadeOut(0);
		$("#gallery_form").show();
		return false;
	});

	// Filter gallery
	$("#gallery_form").change(function () {
		stateSelection = $("#state option:selected").attr("value");
		categorySelection = $("#category option:selected").attr("value");
		filterTags(stateSelection,categorySelection);
	});

	// Reset Filter
	$("#reset_form").live('click',function(){
		stateSelection = "false";
		categorySelection = "false";
		filterTags(stateSelection,categorySelection);
	});

	// Flickr rollover
	$("#link_to_flickr").hover(function () {
	//		$("#img_lg_flickr").fadeIn(200);
		},function () {
			$("#img_lg_flickr").fadeOut(200);
	});

	// Detail traversing
	$("#arrow_right").click(function(){

		if ( parseInt(allSourceLoc) < (parseInt(visibleCount) - 1) ){

			allSourceLoc++;
			var rightTEMP = getAllSources[parseInt(allSourceLoc)];
			populateDetail(rightTEMP, "#", allSourceLoc);

		} else {

			allSourceLoc = 0;
			var rightTEMP = getAllSources[parseInt(allSourceLoc)];
			populateDetail(rightTEMP, "#", allSourceLoc);
		}
	});

	$("#arrow_left").click(function(){

		if ( parseInt(allSourceLoc) == 0 ){

			allSourceLoc = (parseInt(visibleCount) - 1);
			var leftTEMP = getAllSources[parseInt(allSourceLoc)];
			populateDetail(leftTEMP, "#", allSourceLoc);

		} else {

			allSourceLoc--;
			var leftTEMP = getAllSources[parseInt(allSourceLoc)];
			populateDetail(leftTEMP, "#", allSourceLoc);
		}
	});

}); // End (document).ready

/////////////////////////////////////////////////////////////////

function loadGallery(){

	var appendToImages = "";

	$.getJSON("http://pipes.yahoo.com/pipes/pipe.run?_id=d1019b6981db715136f477cce03b3554&_render=json&content_type=7&extras=tags%2Curl_sq%2Curl_t%2Curl_s%2Curl_m%2Curl_o&per_page=100&sort=date-posted-desc&user_id=47164259%40N06&_callback=?", function(data){

		$.each(data.value.items, function(i,item){
		
			var newItem = $('<div class="thumb visible '+item.tags+'" style="background: url('+item.url_s+') no-repeat; background-position: center;" alt="'+item.url_s+'" rel="'+i+'"><a href="http://www.flickr.com/photos/47164259@N06/'+item.id+'/"/><img src="images/clear.gif" height="125" width="175" border="0" /></a></div>').hide().fadeTo(0,0).appendTo("#images").delay(Math.ceil(Math.random()*1000)+1000).fadeTo(800,1, function(){
				visibleCount = $('.visible').size();
			});
		});
	});
}

/////////////////////////////////////////////////////////////////

function populateDetail(photoID, flickrLink, sourceLoc){

	var newSource = photoID.substr(0, photoID.length - 6 ).concat(".jpg");

	$('#img_lg').attr("src",newSource);
//	$('#link_to_flickr').attr("href",flickrLink);
	$('#detail_container').fadeIn(fadeSpeed);
}

/////////////////////////////////////////////////////////////////

function filterTags(state, type){

	state = state.toLowerCase();

	$("#no_results").removeClass("visible").addClass("invisible");
	
	$(".thumb").each(function(i) {

		if ( state == "false" && type == "false" ){

			$(".thumb").removeClass("invisible").addClass("visible");
			
			$('#reset_form').fadeOut(200);
	
		} else if ( state != "false" && type == "false" ){

			$(this).removeClass("invisible").addClass("visible");

			if ( $(this).hasClass(state) ){

				 $(this).removeClass("invisible").addClass("visible");
			
			} else {
			
				 $(this).removeClass("visible").addClass("invisible");
			}
	
			$('#reset_form').fadeIn(400);

		} else if ( state == "false" && type != "false" ){

			$(this).removeClass("invisible").addClass("visible");

			if ( $(this).hasClass(type) ){

				 $(this).removeClass("invisible").addClass("visible");
			
			} else {
			
				 $(this).removeClass("visible").addClass("invisible");
			}

			$('#reset_form').fadeIn(400);

		} else if ( state != "false" && type != "false" ){

			$(this).removeClass("invisible").addClass("visible");

			if ( $(this).hasClass(state) && $(this).hasClass(type) ){

				 $(this).removeClass("invisible").addClass("visible");
			
			} else {
			
				 $(this).removeClass("visible").addClass("invisible");
			}

			$('#reset_form').fadeIn(400);
		}

	});

	visibleCount = $('.visible').size();

	if ( visibleCount == 0){
		$("#no_results").removeClass("invisible").addClass("visible");
	}
}

/////////////////////////////////////////////////////////////////

function popSources(){

	$(".visible").each(function(i) {

		getAllSources[i] = $(this).attr("alt");
		$(this).attr("rel",i);
	
	});
}
/////////////////////////////////////////////////////////////////

function loadUploader(){

	var params = {	menu: "false",
					bgcolor: "#FFFFFF",
					allowFullScreen: "false"
				 };

	var flashvars = {
					};			
	
	 swfobject.embedSWF("flash/rally_flickr_upload.swf", "flash_flickr_upload", "785", "500", "9.0.0", "expressInstall.swf", flashvars, params);
}