$(function()
{
	$('.tooltip').hover( function(){
	     $(this).children('span').css('display', 'block');
	},
	function(){
	     $(this).children('span').css('display', 'none');
	});
	
	$('#show_hide_instructions').click(function()
	{
		$('#instructions').toggle();
		return false;
	});

	$('#mark_all :checkbox').click(function()
	{
		$('td.status :checkbox').attr('checked',($(this)[0].checked ? 'checked' : ''));
	});
	
	$('#mark_read, #mark_unread, #mark_delete').click(function()
	{
		var ids = '?ids=';
		$('td.status :checkbox').each(function()
		{
			ids += ($(this)[0].checked ? $(this).attr('id') + ',' : '');
		});
		//$(this).attr('href', $(this).attr('href') + '/' +ids);
		location.href = $(this).attr('href') + '/' +ids;
		return false;
	});

	$('.flickr-selector li').click(function()
	{
		$('#id_logo_upload_button').parents('div.buttons').show();
	});

	$('#id_skip').click(function(){ $(this).parents('form').attr('target', ''); });
	$('#idea_create').submit(function()
	{	
		if(!$('#id_action').is(':checked'))
		{	
			$('#id_action_title').val('.');
			$('#id_action_desc').val('.');
		}
	});

	$('#id_people_needed').focus(function (){ $('#id_people_needed_toggle').attr('checked', ''); });
	$('#id_people_needed').blur(function ()
	{
		if ( $(this).attr('value') == '' )
		{
			$('#id_people_needed_toggle').attr('checked', 'checked');
		}
	});
	
	$('#id_people_needed_toggle').bind('load change', function()
	{
		if ($(this).is(':checked'))
		{
			$('#id_people_needed').attr('disabled', 'disabled');
		}
		else
		{
			$('#id_people_needed').removeAttr('disabled');
		}
	}).load();
	
	$('#id_action').change(function()
	{
		if ($('#add_action_div')[0])
		{
			if ($(this).is(':checked'))
			{
				$('#id_action_title').val($('#id_title').val());
				$('#id_action_desc').val($('#id_content').val());
			}
			else
			{
				$('#id_action_title').val('.');
				$('#id_action_desc').val('.');
			}
		}
	});
	
	loader_image = '<img id="uploader-loading" src="/public/styles/images/loader.gif" style="float: left; margin-top: 4px;" />';

	// $("input[type=file]").addClass('field').filestyle({ 
	//     image: "/public/styles/images/browse.jpg",
	//     imageheight : 23,
	//     imagewidth : 82,
	//     width : 200
	// });
	
	// $('textarea').autogrow({ lineHeight: 16 });
	
	flickrSelector($('ul.flickr-selector'));
	
	$(".print").click(function(){ window.print(); return false; });
	
	var fp_ideas_i = 0;
	var fp_ideas_time = 8; // seconds
	var $line = $('#ideas_holder #list');
	
	var fp_ideas = $('#ideas_holder .pages a.page').click(function()
	{
		var width = 740;
		var page = $(this).text()-1;
		
		$line.animate({ left: -width * page }, 500 );
		
		$('#ideas_holder .pages a').removeClass('active');
		$(this).addClass('active');
		
		fp_ideas_i = $(this).text();
		clearTimeout(window.fp_ideas_timeout);
		
		return false;
	});
	
	if(fp_ideas.length)
	{
		$line.width(fp_ideas.length * 740);
		
		function fp_ideas_timer()
		{
			fp_ideas.eq((fp_ideas_i % fp_ideas.length)).click();
			window.fp_ideas_timeout = setTimeout(fp_ideas_timer, fp_ideas_time * 1000);
		};
		fp_ideas_timer()
		
		$('#ideas_holder #list').hover
		(
			function(){ clearTimeout(window.fp_ideas_timeout); }, 
			function(){ window.fp_ideas_timeout = setTimeout(fp_ideas_timer, fp_ideas_time * 1000); }
		);
	}

	if($('.datepicker').length > 0)
	{
		function customRange(input){
			// return and '{' MUST be on one line 
			return {
				minDate: (input.id == "id_valid_to" 	? $("#id_valid_from")	.datepicker("getDate") : null), 
				maxDate: (input.id == "id_valid_from" 	? $("#id_valid_to")		.datepicker("getDate") : null)
			}; 
		}

		$(".datepicker").datepicker({ speed: "", beforeShow: customRange });
	}
	
	$('#id_my_area').change(function()
	{
		$(this).is(':checked')
			? $('#id_area').attr('disabled', 'disabled')
			: $('#id_area').removeAttr('disabled');
	});

	$('#invite_now').click(function()
	{
		var ids = '';
		$('input.invite').each(function()
		{
			if (this.checked)
			{
				ids += $(this).attr('id').substr(10) + ',';
			}
		});
		var idea_id		= $(this).attr('rel');
		var idea_slug	= $(this).attr('rev');
		if (ids)
		{
			$.post('/users/', {invite : 1, ids : ids, idea_id : idea_id}, function()
			{
				//window.humanMsg.displayMsg( 'Selected users was invited.' );
				location.href = '/members/invite/' + idea_id + '/' + idea_slug + '/report'; 
			});
		}
		return false;
	});
	$('a#accept_invite').click(function()
	{
		window.humanMsg.displayMsg( 'You accept the invitation for co-owner.' );
	})

	$('#list.ideas .show_actions').click(function()
	{
		var $button = $(this);
		var $actions = $button.parents('dd').find('.inline_actions');
		var $dd = $button.parents('dd').addClass('active_actions');
		
		if(!$actions.length)
		{ 
			$('<div class="inline_actions"></div>').appendTo($(this).parents('dd')).load(this.href + ' #list.actions dl', function()
			{
				$button.text('Hide Help Wanted');
				$('dd dd .idea_line, dd dd .user_line').remove();
				$(this).slideDown();
			});
		}
		else
		{
			if($actions.is(':visible'))
			{
				$actions.slideUp(500, function()
				{
					$dd.removeClass('active_actions');
					$button.text('View Help Wanted');
				});
			}
			else
			{
				$button.text('Hide Help Wanted');					
				$actions.slideDown(500);
			}
			
		}
		
		return false;
	});
	
	$('#list.actions .show_idea').click(function()
	{
		var $button = $(this);
		var $actions = $button.parents('dd').find('.inline_actions');
		var $dd = $button.parents('dd');
		
		if(!$actions.length)
		{ 
			$('<div class="inline_actions"></div>').appendTo($(this).parents('dd')).load(this.href + ' #list.ideas dl', function()
			{
				$dd.addClass('active_actions');
				$button.text('Hide idea summary');
				$(this).slideDown();
			});
		}
		else
		{
			if( $actions.is(':visible') )
			{
				$actions.slideUp(500, function()
				{
					$dd.removeClass('active_actions');
					$button.text('View idea summary');
				});
			}
			else
			{
				$dd.addClass('active_actions');
				$button.text('Hide idea summary');					
				$actions.slideDown(500);
			}
			
		}
		
		return false;
	});
	
	var $icropper = $('.image_cropper'); if($icropper.length){ ImageCropper($icropper, crop_width, crop_height); }
	
	// Organization show/hide checkbox
	if ($('#id_is_organization').is(':checked'))
	{
		is_org();
		$('#organization_div').show();
		$('#not_organization').hide();
	}
	else
	{
		$('#organization_div').hide();
		$('#not_organization').show();
	}  
	$('#id_is_organization').click(function()
	{
		if (this.checked)
		{
			is_org();
			$('#organization_div').show()
			$('#not_organization').hide();
		}
		else
		{
			$('#organization_div').hide();
			$('#not_organization').show();
		}
	});
	function is_org() {
		$("#id_birthdate_day").val("01");
		$("#id_birthdate_month").val("01");
		$("#id_birthdate_year").val("1920");
		$("#id_employment").val("8");
	}
	
	
	$('#registration_form #id_first_name, #registration_form #id_last_name').change(function(){ $('#id_display_name').val($('#id_first_name').val() + ' ' + $('#id_last_name').val()); });
	
	$('form').submit(function()
	{
		if($('#id_org_postcode').length) $('#id_org_postcode').val($('#id_org_postcode').val().toUpperCase());
		if($('#id_postcode').length && !$(this).is('.ajax_edit_action_property') ) $('#id_postcode').val($('#id_postcode').val().toUpperCase());
	});
	
	$('form#registration_form').submit(function()
	{
		if ($('#id_is_organization')[0].checked)
		{
			$('#id_display_name').val($('span#org_title input').val());
			$('#id_first_name').val($('#id_org_first_name').val());
			$('#id_summary').val($('span#org_description textarea').val());
			$('#id_last_name').val($('#id_org_last_name').val());
			$('#id_postcode').val($('#id_org_postcode').val().toUpperCase());
		}
		else
		{	
			$('#organization_div').remove();
		}
	});
	
	$('.ajax_checkbox :checkbox').click(function()
	{
		var what = $(this).parents('ul').attr('id').substr(3);
		var url = $(this).parents('form').attr('action');
		var checked = this.checked ? 1 : 0;
		var title = $(this).next().text();
		
		$.post(url, { what: what, property_id: $(this).val(), value: checked }, function ()
		{

			window.humanMsg.displayMsg( title + (checked ? ' added' : ' removed') ); 
		});
	});

	// Uniersity show/hide checkbox
	$('#id_university').is(':checked') ? $('#university_div').show() : $('#university_div').hide(); 
	$('#id_university').click(function() { this.checked ? $('#university_div').show() : $('#university_div').hide(); });

	// Funding show/hide checkbox	
	$('#id_funding').is(':checked') ? $('#funding_div').show() : $('#funding_div').hide();
	$('#id_funding').click(function() { this.checked ? $('#funding_div').show() : $('#funding_div').hide(); });	
	
	// Help show/hide checkbox	
	$('#id_help').is(':checked') ? $('#help_div').show() : $('#help_div').hide();
	$('#id_help').click(function() { this.checked ? $('#help_div').show() : $('#help_div').hide(); });	

	// Action add show/hide checkbox	
	$('#id_action').is(':checked') ? $('#add_action_div').show() : $('#add_action_div').hide();
	$('#id_action').click(function() { this.checked ? $('#add_action_div').show() : $('#add_action_div').hide(); });	

	
	// Action status show/hide checkbox
	$('#id_status').change(function(){ $('#id_status option:selected').val() == 1 ? $('#action_people_div').show() : $('#action_people_div').hide(); });

	$('.button.save').each(function(){	$(this).parent().addClass('save'); });
	
	$('#edit_summary').click(function(){ $('#summary_form').show(); $('#summary_view').hide(); return false; });
	
	$('.delete, #report_abused').click(function(){ return confirm('Are you sure?'); });
	$(".actionDrop").click(function(){ $(this).blur(); return confirm(__labels.__globals.CONFIRM.action_drop); });
	$(".actionTake").click(function(){ $(this).blur(); return confirm(__labels.__globals.CONFIRM.action_take); });
	
	$('#listing-filter .apply.button').click(function()
	{
		fields = $('input, select', '#listing-filter, .listing_filter').serialize();
		
		fields_names = [];
		t_fields = fields.split('&');
		for(f in t_fields){ fields_names[f] = t_fields[f].split('=')[0]; }
		fields_names = fields_names.join('|');
		
		eval( 'reg = /(?:[A-Za-z0-9_-]+=|page=[0-9]+|(?:(?:^|&)'+fields_names+')=[A-Za-z0-9_%-]+)(?:&|$)/g' )
		url = location.search.substr(1).replace(reg, '');
		
		fields = fields.replace(/[A-Za-z0-9_-]+=\+*(&|$)/g, '').replace(/&$/, '');
		
		url = url + (url && fields ? '&' : '') + fields;
		
		if(this.href.indexOf('?') > 0){ this.href = this.href.substr(0, this.href.indexOf('?')); }
		this.href = this.href.replace('#') || '';
		link = this.href ? this.href + '?' : '';
		
		if(url){ location.href = link + url; }
		
		return false;
	});
	
	$autocompleter_tags = $('.filters .search input[type=text][rel=tag]');
	if($autocompleter_tags.length){ $autocompleter_tags.suggest(location.href); }
	
	$('#ajax_edit_summary_form').submit(function()
	{
		var url = location.href;
		var val = $('textarea[name=summary]', this).val();
		
		$.post ( url, { summary: val, save_summary: 1 }, function(response)
		{
			if (response){ $('#summary_view').html(val.nl2br()).removeClass('default'); tb_remove(); }
		});
		
		return false;
	});
	
	$('#ajax_edit_content_form').submit(function()
	{
		var url = location.href;
		var val = $('textarea[name=content]', this).val();
		
		$.post( url, { content: val, save_content: 1 }, function(response)
		{
			if(response == 1){ $('#content_view').html(val.nl2br()).removeClass('default'); tb_remove(); }
		});
		
		return false;
	});
	
	$('#ajax_edit_funding_content_form').submit(function()
	{
		var url = location.href;
		var val = $('textarea[name=funding_content]', this).val();
		
		$.post( url, { funding_content: val, save_funding_content: 1 }, function(response)
		{
			if(response == 1){ $('#funding_content_view').html(val.nl2br()).removeClass('default'); tb_remove(); }
		});
		
		return false;
	});
	
	$('#ajax_edit_help_content_form').submit(function()
	{
		var url = location.href;
		var val = $('textarea[name=help_content]', this).val();
		
		$.post( url, { help_content: val, save_help_content: 1 }, function(response)
		{
			if(response == 1){ $('#help_content_view').html(val.nl2br()).removeClass('default'); tb_remove(); }
		});
		
		return false;
	});
	
	$('.ajax_edit_action_property').submit(function()
	{
	
		var url 		= location.href;
		var valid_from 	= '';
		var valid_to 	= '';
		var field 		= $('input[type=submit]', this).attr('name').substr(5);
		var val 		= $('input[name='+field+']', this).val();
		
		if (!val){	val = $('select[name='+field+']', this).val(); }
		if (field == 'idea_status_id')
		{
			val = $('#id_idea_status_id').val();
		}
		/*
		else if (!val && field != 'people_needed')
		{
			var year 	= $('#id_'+field+'_year').val();
			var month 	= $('#id_'+field+'_month').val();
			var day 	= $('#id_'+field+'_day').val();
			val = year + '-' + month + '-' + day + ' 00:00:00';
		}
		*/
		
		// if (field == 'postcode' && $('#id_postcode').val()) { val = val.toUpperCase(); $('#id_postcode').val(val); }
		if (field == 'valid')
		{
			valid_from 		= $('#id_valid_from').val();
			valid_to 		= $('#id_valid_to').val();
			val = '';
		}
		
		$.post( url, { field: field, value: val, valid_from: valid_from, valid_to: valid_to, save_property: 1 }, function(response)
		{
			if(response)
			{
				if (field == 'valid')									{ $('#'+field).html(valid_from + (valid_to ? ' to ' + valid_to : (valid_from ? '' : 'Any time'))); }
				else if (field == 'closing_date')						{ $('#'+field).html(!val ? 'Any time' : val); }
				else if (field == 'time_needed')						{ $('#'+field).html(__labels.__globals.FORMS_FIELDS.time_needed[val]); }
				else if (field == 'postcode')							{ $('#'+field).html(response); }
				else if (field == 'status')								{ $('#'+field).html(__labels.__globals.FORMS_FIELDS.action_status[val]); }
				else if (field == 'people_needed')						{ $('#'+field).html((!val || val == 0 ? 'Unlimited' : val-$('#id_joined_users').val() + ' of ' + val)); }
				else if (field == 'actions_difficulty_id')				{ $('#'+field).html(__difficulties[val]); }
				else if (field == 'idea_status_id')						{ $('#'+field).html(__statusses[val]); }
				else													{ $('#'+field).html(val); }
		
				tb_remove();
			}
			else if(field == 'postcode')
			{
				humanMsg.displayMsg(__labels.__globals.ERRORS.postcode);
				$('#'+field).html('');
				tb_remove();
			}
		});
		
		return false;
	});
	
/*	$('#ajax_edit_picture_form').submit(function()
	{
		var url 			= location.href;//$(this).attr('action');
		var val 			= $('.flickr_image_cropper:visible .crop_position, .image_cropper:visible .crop_position').val();
		var path			= '/public/files/';
		var path_small		= '/public/files/';
		var prefix			= '';
		var pic_id			= $('#id_pic_id').val();
		var d				= new Date();
		var idea			= false;
		var uni				= false;
		
		if(val)
		{
			if ($('input[type=submit][name=save_logo]', this).length)
			{
				path += 'ideas/';
				path_small += 'ideas/listing_';
				prefix = 'listing_';
				idea = true;
			}
			else if ($('input[type=submit][name=save_uni]', this).length)
			{
				path += 'universities/big/';
				uni = true;
			}
			else
			{
				path += 'users/profile/';
				path_small += 'users/listing/';
			}
			
			$.post( url, { crop_position: val, save: 1 }, function(response)
			{
				if(response)
				{
					if (!idea && !uni) { $('.inner .image img').attr('src', '/public/files/users/comment/'+pic_id+'.'+response+'?'+d.getTime()); }
					$('#image_view').attr('src', path+prefix+pic_id+'.'+response+'?'+d.getTime());
					$('.input.ic img').attr('src', path_small+pic_id+'.'+response+'?'+d.getTime());
					$('div.image_cropper').hide();
					tb_remove();
				}
			});
		
			return false;
		}
	});*/
	
	// Do not delete
	$('form').each(function(){ new Validator($(this)); });
	$('#id_cancel').click(function(){ $(this).parents('form:eq(0)').unbind('submit'); });
	
/*	// ajax upload image
	$('#id_upload').click(function()
	{
		if($('#id_picture, #id_logo').val().length <= 0){ return false; }
		 
		$('.image_cropper').hide();
		$('#id_logo').css('visibility', 'hidden');
		$('#uploader-loading').remove();
		$(this).val('Loading ...').attr('disabled', 'disabled').parents('form:eq(0)').attr('target', 'upload_frame')
			.end().parent().after(loader_image);
			
		$(this).parents('form:eq(0)').submit(); // IE shit
		return false;
	});
	$('#id_finish, #id_next, #id_logo_button, #id_logo_search_button').click(function(){ $(this).parents('form:eq(0)').removeAttr('target'); });
	
	if ( $.browser.safari ) {
		window.uploadedImage = function(src)
		{
			$('.flickr_image_cropper').hide().parents('.box:eq(0)').slideDown(300);
			$('.upload-search-forms, #photos.flickr-hide').slideUp(300);
			$('.image_cropper img').attr('src', src).removeAttr('style').load(function()
			{
				$icropper.show();
				$(".crop_image_text").show();
				$('#id_logo').css('visibility', 'visible');
				$('#id_upload').removeAttr('disabled').val('Upload');
				$('#uploader-loading').remove();
			});
			ImageCropper($icropper, crop_width, crop_height);
		}
	}
	else
	{
		top.uploadedImage = function(src)
		{
			$('.flickr_image_cropper').hide().parents('.box:eq(0)').slideDown(300);
			$('.upload-search-forms, #photos.flickr-hide').slideUp(300);
			$('.image_cropper img').attr('src', src).removeAttr('style').load(function()
			{
				$icropper.show();
				$(".crop_image_text").show();
				$('#id_logo').css('visibility', 'visible');
				$('#id_upload').removeAttr('disabled').val('Upload');
				$('#uploader-loading').remove();
			});
			ImageCropper($icropper, crop_width, crop_height);
		}
	}
	$('.ic-back').click(function()
	{
		if($(this).is('.flickr'))
		{
			$('#photos.flickr-hide').slideDown(300);
			$('.icroppers').parents('.box:eq(0)').slideUp(300);
		}
		else
		{
			
			$('.upload-search-forms').slideDown(300, function(){ $(this).parents('.box:eq(0)').addClass('or'); });
			$('.icroppers').slideUp(300);
		}
		return false;
	});
	*/
	
	//Input clear on focus
	$('.filters input[type=text], #login input[type=text], .search input[type=text]').focus(function() //#login input[type=text], #login input[type=password],
	{
		if ((($.inArray($(this).val(), __labels.__globals.FORMS_FIELDS.search_titles)) >= 0) || $(this).attr('type') == 'password')
		{ 
			$(this).attr('value', '');
		} 
	});

	// Cancel button functionality in search fields (x)
	$('.search .cancel').click(function(){ $('id_search').val(''); });
	
	//ajax filter form submit
	$('.filters select').change(function(){ $('.filters form').submit(); });
	$('.filters :checkbox').change(function(){ $('.filters form').submit(); });
	
	$('#check_all input:checkbox').change(function()
	{
		var status = $(this).attr('checked');
		$('input.list_to_check').each(function(){ this.checked = status; });
	});

	//grab and drop action when watching idea
	$('.actionTake.ajax').click(function()
	{
		var elem = $(this);
		$.get (this.href, {}, function()
		{
			elem.hide();
			elem.prev('.drop').show();
		});
		
		return false;
	});
	
	$('.actionDrop.ajax').click(function()
	{
		var elem = $(this);
		$.get (this.href, {}, function()
		{
			elem.hide();
			elem.next('.grab').show();
		});
		
		return false;
	});

});

String.prototype.nl2br = function(){ return this.replace(/\n/g, '<br />'); }

function ImageCropper($icropper, crop_width, crop_height)
{
	$icropper.hide();
	
	var crop 		= { width: crop_width, height: crop_height };
	
	var $container 	= $('body');
	var $img 		= $('img', $icropper);
	var $input 		= $('input.crop_position', $icropper);
	
	$('.croped_area, .croped_area_top, .croped_area_bot', $icropper).remove();
	var $area 		= $('<div class="croped_area"></div>')		.prependTo($icropper);
	var $area_top 	= $('<div class="croped_area_top"></div>')	.prependTo($icropper);
	var $area_bot 	= $('<div class="croped_area_bot"></div>')	.prependTo($icropper);
	
	$img.css({opacity: '.0', filter: 'alpha(opacity=0)'});
	$img.load(function()
	{
		$icropper.show().parents('.icroppers').slideDown().parents('.box.or:eq(0)').removeClass('or');
		
		// console.info($icropper);
		var K = $img.width() / $img.height();
		
		$area.css({	position: 'absolute', 'z-index': '1000', cursor: 'move', background: '#FFF', opacity: '.01', filter: 'alpha(opacity=1)' });
		var area_css = { position: 'absolute', 'z-index': '1000', background: '#000', opacity: '.6', filter: 'alpha(opacity=60)' };
		$area_top.css(area_css);
		$area_bot.css(area_css);
		
		if($img.width()/crop.width < $img.height()/crop.height)
		{
			var short_side = $img.width(crop.width).width();
			var long_side = $img.height(crop.width / K).height();
			var offset_side = 'top';
			var event_side = 'pageY';
			var short_side_label = 'width';
			var long_side_label = 'height';
			var input_value = '+0+%d';
		}
		else
		{
			var short_side = $img.height(crop.height).height();
			var long_side = $img.width(crop.height * K).width();
			var offset_side = 'left';
			var event_side = 'pageX';
			var short_side_label = 'height';
			var long_side_label = 'width';
			var input_value = '+%d+0';
		}
		
		var max_move = long_side - crop[long_side_label];
		var move = max_move / 2;
		
		var area_offset = $area.offset()[offset_side];
		var img_offset_min = $img.offset()[offset_side];
		var img_offset_max = img_offset_min + max_move;
		
		$input.val(input_value.replace('%d', move));
		
		$area_top.css(short_side_label, crop[short_side_label]).css(long_side_label, move);
		$area_bot.css(short_side_label, crop[short_side_label]).css(long_side_label, move)
			.css(offset_side, move + crop[long_side_label]);
		
		$area.width(crop.width).height(crop.height).css(offset_side, move)
			.mousedown(function(event)
			{
				var offset = $img.offset()[offset_side] + (event[event_side] - $(this).offset()[offset_side]);
				
				$('#id_search').val(long_side_label);
				
				$container.bind('mousemove.cropper', function(event)
				{
					move = event[event_side] - offset;
					move = move <= 0 ? 0 : move >= max_move ? max_move : move;
					
					$area_top.css(long_side_label, move);
					$area_bot.css(long_side_label, max_move - move).css(offset_side, move + crop[long_side_label]);
					$area.css(offset_side, move);
				});
				
				return false;
			});
			
		$container.mouseup(function(){ $(this).unbind('mousemove.cropper'); $input.val(input_value.replace('%d', move)); });
		
		$img.show().css({opacity: '1', filter: 'alpha(opacity=100)'});
		
	});
}

function flickrSelector($ul)
{
	if(!$ul.length || $ul.is('.nojs')){ return false; }
	
	var $loading = $('#id_flickr_loading');
	var $hidden = $('#id_flickr_logo');
	
	$lis = $ul.find('li').hide();
	$ul.find('img:last').load(function(){ $lis.fadeIn(500); $loading.remove(); });
	
	setTimeout(function()
	{ 
		$lis.fadeIn(500); $loading.remove();
		/*
		var scroll_from = $(window).scrollTop();
		var scroll_to = $ul.offset().top - 70
		
		var scroller = setInterval(function()
		{
			scroll_from += (scroll_to - scroll_from)/20 + 5;
			if(scroll_from >= scroll_to){ clearInterval(scroller); $(window).scrollTop(scroll_to); return; }
			$(window).scrollTop(scroll_from);
		}, 1);
		*/
	}, 3000);
	
	
	$ul.find('li').click(function()
	{
		$lis.removeClass('selected');
		$(this).addClass('selected').animate({opacity: 1}, 300);
		$lis.not('.selected').animate({opacity: 0.3}, 300);
		
		$hidden.val($(this).attr('rel').substr('http://farm'.length));
//		$('#id_logo_upload_button').show()
	});
	
	$('#id_logo_upload_button').click(function()
	{
		if($hidden.val().length <= 0){ return false; } 
		$('#uploader-loading').remove();
		$(this).val('Loading ...').addClass('disabled').parents('form:eq(0)').attr('target', 'upload_frame')
			.end().parent().after(loader_image);
			
		$(this).parents('form:eq(0)').submit(); // IE Shit
		return false;
	});
//	.hide();
	if ( $.browser.safari ) {
		window.flickrUploadedImage = function(src)
		{
			$('.image_cropper').hide().parents('.box:eq(0)').slideDown(300);
			$('.upload-search-forms, #photos.flickr-hide').slideUp(300);
			$('.flickr_image_cropper img').attr('src', src).removeAttr('style').load(function()
			{
				$('.flickr_image_cropper').show();
				$(".crop_image_text").show();
				$('#id_logo_upload_button').removeClass('disabled').val('Upload');
				$('#uploader-loading').remove();
			});
			ImageCropper($('.flickr_image_cropper'), crop_width, crop_height);
		}
	}
	else
	{
		top.flickrUploadedImage = function(src)
		{
			$('.image_cropper').hide().parents('.box:eq(0)').slideDown(300);
			$('.upload-search-forms, #photos.flickr-hide').slideUp(300);
			$('.flickr_image_cropper img').attr('src', src).removeAttr('style').load(function()
			{
				$('.flickr_image_cropper').show();
				$(".crop_image_text").show();
				$('#id_logo_upload_button').removeClass('disabled').val('Upload');
				$('#uploader-loading').remove();

			});
			ImageCropper($('.flickr_image_cropper'), crop_width, crop_height);
		}
	}

}

